Handling Enum values ​​from different enumerations that implement a specific interface in a switch / case

I have an empty Units interface that I use to label different enums , so I know that they contain different units (for example, I have enum TemperatureUnits implements Units{} , where Fahrenheit, Celsius, etc. are stored, then I have a DistanceUnits that contains meters, feet, etc.

I want to do different things in the switch / case, based on which the enum value is passed to the method. I want it to accept a value from any enum that implements Units . The problem is that since enums are actually different classes, I have to use a qualified value name in the switch / case. My code looks something like this:

  public static void foo(Units units){ //units has to be an enum that implements Units switch (units){ case TemperatureUnits.FAHRENHEIT: //I can't say just FAHRENHEIT, because Units itself doesn't //have a FAHRENHEIT value, but it won't let me say TemperatureUnits.FAHRENHEIT because it won't let use // qualified enum values as cases. //Do stuff break; case DistanceUnits.METERS: //Do other stuff break; //...etc 

How do I get a switch / case statement that can use values ​​from different enums ?

Edit: My Units interface:

 public interface Units{ String getLabel(); } 
+5
source share
3 answers

This is not allowed by the Java Language specification.

11/14. The Switch Statement states that

The type of the expression must be char, byte, short, int, Character, Byte, Short, Integer, String or enum type ( Β§8.9 ), or a compile-time error occurs.

Here, expression is the value that you include ( switch(expression) ). In your case, the type of expression is Units , which is an interface, not an enum .

Next to this

Each case label has a case constant, which is either a constant expression or the name of an enum constant.

The enum constants do not match the type of enumeration, so you cannot mix different variables of the enumeration type in the switch.

A possible solution for you would be to implement it as follows:

 interface Unit { void doSomething(); } enum A implements Unit { A_ONE, A_TWO, A_THREE { void doSomething() { // implement it in a way specific for A_THREE } }; void doSomething() { // implement it in a generic way for A } } 

Thus, each enumeration that Unit implements can and should contain processing logic encapsulated inside it, and if you have any special constant constants, they can implement special logic themselves. Naturally, sometimes each enumeration constant can have a certain implementation, and at the level of the enumeration class there will not be a single generic one.

+3
source

This may suit your use case:

 /** Defines all supported unit types */ public enum UnitType { Distance, Temperature, Volume }; /** Defines all units (of all supported unit types) */ public enum Units { FEET(UnitType.Distance), METERS(UnitType.Distance), FAHRENHEIT(UnitType.Temperature), CELSIUS(UnitType.Temperature), GALLONS(UnitType.Volume), LITERS(UnitType.Volume); private final UnitType type; Units(UnitType type) { this.type = type; }; public UnitType getType() { return type; } } // In some class ... public static void foo(Units units) { switch (units) { case FAHRENHEIT: // ... break; case METERS: // ... break; case GALLONS: // ... break; // ... } } 
+1
source

You can use the instanceof operator to determine which enum was passed, and then switch based on the corresponding enum :

 public static void foo(Units units) { if(units instanceof DistanceUnits) { DistanceUnits d = (DistanceUnits) units; // METERS, INCHES... switch (d) { case METERS: // code break; case INCHES: // code break; } } else if(units instanceof TemperatureUnits) { TemperatureUnits t = (TemperatureUnits) units; // FAHRENHEIT, CELSIUS switch (t) { case FAHRENHEIT: // code break; case CELSIUS: // code break; } } } 

Considering this:

 enum DistanceUnits implements Units { METERS, INCHES } enum TemperatureUnits implements Units { FAHRENHEIT, CELSIUS } public interface Units { // some code } 
0
source

Source: https://habr.com/ru/post/1239303/


All Articles