Is there a way to define a constant Map for the use in a switch statement? All my trials with static Maps from here, here and others were not successful. Why isn't the Map constant?
For the line case (kws.get(KEYWORD_NAME)): I always get constant string expression required error.
public class Demo {
public static final String KEYWORD_NAME = "Name";
public static final String KEYWORD_TYPE = "Type";
private static final Map<String, String> kws = Collections.unmodifiableMap(
new HashMap<String, String>() {{
put(KEYWORD_NAME, KEYWORD_NAME.toLowerCase());
put(KEYWORD_TYPE, KEYWORD_TYPE.toLowerCase());
}});
public static void parse(String kw){
switch(kw){
case (kws.get(KEYWORD_NAME)):
System.out.println("Test");
break;
default:
System.out.println("Unknown");
}
}
}
No, because the case labels in a switch statement must be either constant expressions or enumerators.
The reference to your Map is final, and your map is unmodifiable. But the latter is enforced at run time. The compiler does not treat the object as a constant.
From the Java Language Specification, 14.11: The switch statement:
SwitchLabel:
case ConstantExpression :
case EnumConstantName :
default :
Constant expressions can be composed only of primitive and String literals, certain operators without side-effects, constant variables, and a few other components that can be computed at compile-time. (A constant variable is a final variable of primitive or String type that's been initialized to a constant expression.) More detail is at 15.28: Constant expressions.
I do not know your use case, but instead of using unmodifiable map you can use parametrized enum:
>> when:
public enum Demo {
NAME("Name"),
TYPE("Type"),
NOT_SUPPORTED("");
private String code;
private static final Map<String, Demo> CODE_VALUE_MAP = new LinkedHashMap<>();
// -- static
static {
for (Demo demo : Demo.values()) {
CODE_VALUE_MAP.put(demo.getCode(), demo );
}
}
public static Demo forCode(#NotNull String code) {
if (code!= null) {
return CODE_VALUE_MAP.getOrDefault(code.toUpperCase(), NOT_SUPPORTED);
}
return NOT_SUPPORTED;
}
}
// -- constructor
Demo(String code) {
this.code= code;
}
// -- public methods
public String getCode() {
return code;
}
}
>>Then:
...
public static void parse(String kw){
Demo demo = Demo.forCode(kw);
switch(demo){
case NAME:
System.out.println("Test");
break;
default:
System.out.println("Unknown");
}
...
Related
I have got an Enum with different values and want to switch a string variable. Now I hit a wall trying to convert the Enum values to Strings, that I can use as case constant.
My best try was to convert the Enum to a String array, but the switch doesn't seem to accept array values as a case constant. (IntelliJ says: "constant expression required")
Enum myEnum = {FOO, BAR}
String input = "foo"
final String[] constant = Arrays.stream(myEnum.values()).map(Enum::name).toArray(String[]::new);
//converts Enum to String[]; made it final, so it is "constant"
switch (input) {
case constant[0]:
System.out.println("foo");
break;
case constant[1]:
System.out.println("bar");
break;
}
Is there an elegant way to make this switch depend on the Enum?
You shouldn't convert it because it isn't needed. Also, your code won't even compile because case is a reserved keyword in Java.
You should take a look at the valueOf method of an Enum.
Your code can look like that:
public enum MyEnum {FOO, BAR}
//method
String input = "foo";
MyEnum niceFound = MyEnum.valueOf(input.toUpperCase());
That will return FOO but can throw an IllegalArgumentException when the given value isn't present as type.
You can do this :
public enum MyEnum {
FOO("foo"),
BAR("bar");
private String value;
public String getValue() {
return value;
}
public static MyEnum getState(String value) {
switch (value) {
case "foo":
return FOO;
case "bar":
return BAR;
}
return null;
}
private MyEnum(String value) {
this.value = value;
}
}
Now, in your class, you can :
MyEnum myEnum = getState("foo"); // returns MyEnum.FOO
Also make sure you handle the case when getState() returns null
A solution with Java 8+ streams would be to create a method inside your enum :
public static Optional<MyEnum> getByValue(final String value) {
return Arrays.stream(MyEnum.values())
.filter(myEnum -> myEnum.value.equals(value))
.findFirst();
}
This returns optional in case there is no enum value for your String parameter. But you can change it according to your needs.
Use MyEnum.valueOf(value.toUpperCase())
public enum MyEnum {
FOO, BAR;
}
public static void process(String value) {
try {
switch (MyEnum.valueOf(value.toUpperCase())) {
case FOO :
System.out.println("FOO");
break;
case BAR :
System.out.println("BAR");
break;
default :
break;
}
}
catch (IllegalArgumentException e) {
// TODO: handle exception
}
public static void main(String[] a){
process("foo");
}
Java prohibits access of a final static field from an initializer. For example:
public enum Example {
ValueA("valueAA", "valueAB"),
ValueB("valueBA", "valueBB");
final static Map<String, Example> exampleByPropertyA = new HashMap<>();
final String propertyA;
final String propertyB;
Example(String propertyA, String propertyB) {
this.propertyA = propertyA;
this.propertyB = propertyB;
Example.exampleByPropertyA.put(propertyA, this); // <- Not permitted
}
}
However, if the update to the static Map is performed in a separate method that is called by the initializer, this is fine. For example:
public enum Example {
ValueA("valueAA", "valueAB"),
ValueB("valueBA", "valueBB");
final static Map<String, Example> exampleByPropertyA = new HashMap<>();
final String propertyA;
final String propertyB;
Example(String propertyA, String propertyB) {
this.propertyA = propertyA;
this.propertyB = propertyB;
addExample(this);
}
private addExample(Example example) {
Example.exampleByPropertyA.put(example.propertyA, example); // <- Permitted
}
}
Given this context, my question is: Does a call to a member method constitute a "freeze action" or is it indicative to the JVM that the object is, for all intents and purposes, "initialized"? Curious why this makes a difference.
I've done some searching, but haven't found anything that articulates this well.
Thank you in advance!
Does a call to a member method constitute a "freeze action" or is it indicative to the JVM that the object is, for all intents and purposes, "initialized"? Curious why this makes a difference.
The problem is that your class is initialised top to bottom. This means your static fields have not been initialised yet i.e. your Map is null.
Another approach is to add a static initialisation block to be called after everything has been initialised.
static {
for (Example e: values()) {
addExample(e);
}
}
private static addExample(Example example) {
Example prev = exampleByPropertyA.put(example.propertyA, example);
assert prev == null;
}
NOTE: You can see a final variable before it is initialised. This means final can have a before and after value even without using reflection.
public class A {
final String text = getText();
private String getText() {
System.out.println("text= " + text);
return "is set";
}
public static void main(String... args) {
new A().getText();
}
}
prints
text= null
text= is set
Using reflection you can alter final fields even after initialisation though you should avoid doing this unless there is no other option.
The correct way to do what you're trying to do, is to write a static initializer, which runs after all the enums have been created.
Defensive programming: You should also add a simple check to guard against programming errors.
public enum Example {
ValueA("valueAA", "valueAB"),
ValueB("valueBA", "valueBB");
final static Map<String, Example> exampleByPropertyA = new HashMap<>();
static {
for (Example ex : values())
if (exampleByPropertyA.put(ex.propertyA, ex) != null)
throw new IllegalStateException("Duplicate propertyA: " + ex.propertyA);
}
final String propertyA;
final String propertyB;
Example(String propertyA, String propertyB) {
this.propertyA = propertyA;
this.propertyB = propertyB;
}
}
I can't access my enum variables in my switch-case statement:
public enum Country {
FRANCE(0, "France"), SPAIN(1, "Spain");
private final int code;
private final String name;
Country(int code, String name) {
// TODO Auto-generated constructor stub
this.code = code;
this.name = name;
}
public int getCode() {
return code;
}
public String getName() {
return name;
}
}
In another class there is this code :
public Drawable getFlag(){
Drawable d = null;
switch(country_id){
case Country.FRANCE.getCode():
break;
}
return d;
}
But the problem is that when i type Country, there is only class or this.
The expressions in the case statements must be constant values.
One (commonly used) way to approach your problem is by creating a function that gets the enum from the numeric code:
public enum Country {
...
public static Country getCountry(int countryCode) {
for(Country country : Country.values()) {
if(country.code == countryCode) {
return country;
}
}
throw new IllegalArgumentException();
}
Then you'll be able to do the switch on the enum:
switch(Country.getCountry(country_id)){
case Country.FRANCE:
break;
...
}
case labels in a switch statement need be constants
The case expressions must be compile time constant expressions. Your enum instance's variables are constants, but not compile time constants.
We call a variable, of primitive type or type String, that is final
and initialized with a compile-time constant expression (§15.28) a
constant variable. Whether a variable is a constant variable or not
may have implications with respect to class initialization (§12.4.1),
binary compatibility (§13.1, §13.4.9) and definite assignment (§16).
I have an interface where I have defined constants used across application. I have a scenario where I need to initialize constants based on condition.
for eg , something like,
if(condition){
public static final test = "some value";
}
Is this possible.
Interfaces are to be implemented. They should not be used as carriers for constants. If you need such a thing you might consider a final class with a private constructor.
What you seem to want is a global variable or singleton, which are rather problematic designs, or something like a c preprocessor directive, dynamically evaluated at compile time.
So consider if it is really a constant you need - something which is defined at compile (or class loading) time.
Interface contains no code.
Split your interface in many specific interfaces declaring and initializing their own constants.
This will follow the Interface Segregation Principle where a class doesn't have to be bored by some useless constants or methods.
Of course, Java let classes implement several interfaces at once. So if you have specific interfaces to mix up for one concrete class, this would be pretty easy.
You can set static final variable with condition in following way:
public class Test {
public static final String test;
static {
String tmp = null;
if (condition) {
tmp = "ss";
}
test = tmp;
}
}
You can do it in one line, also in interface with:
public static final String test = condition ? "value" : "other value";
This can be another reason why Interface constants are bad. You can simply use enums Like below.
public enum Const {
SAMPLE_1(10), SAMPLE_2(10, 20);
private int value1, value2;
private Const(int value1, int value2) {
this.value1 = value1;
this.value2 = value2;
}
private Const(int value1) {
this.value1 = value1;
}
//Value based on condition
public int getValue(boolean condition) {
return condition == true ? value2 : value1;
}
//value which is not based on conditions
public int getValue() {
return value1;
}
}
public interface InitializeInInterface {
public static final String test = Initializer.init();
static class Initializer {
public static String init() {
String result = "default value";
InputStream is = InitializeInInterface.class.getClassLoader().getResourceAsStream("config.properties");
Properties properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
}
if ("bar".equals(properties.getProperty("foo"))) {
result = "some value";
}
return result;
}
}
}
I need to use an Enum with a combobox (values shown below).
YES (shown as YES on UI, stored in DB as Y)
NO (shown as NO on UI, stored in DB as N)
DEFAULT (shown as "" on UI, stored in DB as null)
The Enum has methods to perform the following -
toString() - to provide the custom String for UI. (showing the combo options)
OptionToDB (static) - Convert a selected option to db value (on save / update)
DBToOption (static)- Convert a DB value to selcted option (while loading the screen)
static enum EnumOption{
YES,NO,DEFAULT;
....
public static EnumOption DBToOption(String val){
if("Y".equals(val)){
return YES;
} else if("N".equals(val)){
return NO;
}else {
return DEFAULT;
}
}
....
}
It works pretty well, but the issue with above methods is that it uses if/else comparison to deduce which option / db value to be returned.
I thought of storing the dbValue as a field in enum but I was not able to reduce the if/else from DBToOption.
Can this if/else be avoided in any way using a better design??
If you store the dbValue as a field in the enum, you can remove the if/else and replace it with a for-loop, although I don't see anything wrong with those if/elses for this particular case:
static enum EnumOption {
YES("Y"),
NO("N"),
DEFAULT("");
private final String value;
private EnumOption(String value) {
this.value = value;
}
public static EnumOption DBToOption(String val) {
for (EnumOption opt : EnumOption.values()) {
if (opt.value.equals(val)) {
return opt;
}
}
return DEFAULT;
}
}
public enum EnumOption {
YES("Y"), NO("N"), DEFAULT("");
private final String value;
private final static Map<String, EnumOption> options;
static {
options = new HashMap<String, EnumOption>();
for (EnumOption opt : EnumOption.values()) {
options.put(opt.value, opt);
}
}
private EnumOption(String value) {
this.value = value;
}
public static EnumOption DBToOption(String val) {
return options.get(val) != null ? options.get(val) : DEFAULT;
}
}
And here is the test that proves it works.
public void testDBToOption() {
assertEquals(EnumOption.NO, EnumOption.DBToOption("N"));
assertEquals(EnumOption.YES, EnumOption.DBToOption("Y"));
assertEquals(EnumOption.DEFAULT, EnumOption.DBToOption(""));
assertEquals(EnumOption.DEFAULT, EnumOption.DBToOption(null));
assertEquals(EnumOption.DEFAULT, EnumOption.DBToOption("R"));
}
So you want to get rid of the remaining if/else ...Are you doing Object Calisthenics?
You could do the following, if you do not have compatibility issues:
public enum EnumOption {
Y("Y", "YES"),
N("N", "NO"),
D("D", "");
private final String dbValue;
private final String uiValue;
private EnumOption(String dbValue, String uiValue) {
this.dbValue = dbValue;
this.uiValue = uiValue;
}
public String getDbValue() {
return this.dbValue;
}
public String uiValue() {
return this.uiValue;
}
public static EnumOption getFromDb(String dbValue) {
return EnumOption.valueOf(dbValue);
}
}
Since each enum value can only occur once, this has at least the same performance as all the other implementations.
For details about the automatically generated valueOf(String) method in enum types, and James DW's solution, you can read up in Josh Bloch's Effective Java Item 30 (Use enums instead of int constants), page 154.