Let's say I have 2 different sets of enums: fruits and vegetables.
public static enum Fruits{
APPLE ("Apple"),
PEAR ("Pear");
//constructor
//getName()
...
}
public static enum Vegetables{
CARROT ("Carrot"),
LETTUCE ("Lettuce");
//constructor
//getName()
...
}
I display all this in a JComboBox. After someone selects something, I want to use a getter method to get back the Enum.
For a single enum, I would do something like:
public static Fruits getEnum(String name) {
for(Fruits fruit: Fruits.values()) {
if(name.equals(fruit.getName())) {
return fruit;
}
}
return null;
}
Any ideas what the return type is? I tried using Enum instead of Fruits. When I do that, I don't seem to have access to the getName() methods.
Here is another demonstration of what you're looking for. The difference between this and previous solutions is that this one is more generic and reusable pattern. This in fact goes beyond the original problem, to show some other benefits of this approach. So you might just comment the bits you don't need. I also attach a unit test to demonstrate the behaviour.
So basically to look for name Apple
or APPLE
in one of these enums just write:
FruitVeg<?> fvg = getEnum("APPLE", Fruits.class, Vegetables.class);
FruitVeg<>
is an interface, which allows to also tap inside of Enum, this interface allows to do some very interesting things with enums below. Here are just some of the things you could do with that:
Enum.valueOf(fvg.getDeclaringClass(), fvg.name())
: returns enum Value e.g. APPLE
fvg.getRaw()
: returns enum Value e.g. APPLE
fvg.name()
: returns enum's String Name e.g. APPLE
fvg.getFriendlyName()
: e.g. Apple
fvg.getDeclaringClass()
: returns Class<Enum<?>>
e.g. class ox.dummy.dummyTest$Fruits
fvg.getClass()
: class ox.dummy.dummyTest$Fruits returns Class<?>
EnumSet.allOf(fvg.getDeclaringClass()))
: e.g. [APPLE, PEAR]
Here is code
@Test
public void doSimpleTest() throws Exception {
FruitVeg<?> fvg = getEnum("APPLE", Fruits.class, Vegetables.class);
log.info("{} : {} : {} : {} : {}", fvg.name(), fvg.getFriendlyName(), fvg.getClass(), fvg.getDeclaringClass(), EnumSet.allOf(fvg.getDeclaringClass()));
log.info("get enum: {} ", Enum.valueOf(fvg.getDeclaringClass(), fvg.name()));
}
public interface FruitVeg<T extends Enum<T>> {
String name();
String getFriendlyName();
Class<T> getDeclaringClass();
T getRaw();
}
enum Fruits implements FruitVeg<Fruits> {
APPLE("Apple"),
PEAR("Pear");
Fruits(String friendlyName) {
this.friendlyName = friendlyName;
}
private final String friendlyName;
@Override
public String getFriendlyName() {
return friendlyName;
}
@Override
public Fruits getRaw() {
return this;
}
}
enum Vegetables implements FruitVeg<Vegetables> {
CARROT("Carrot"),
LETTUCE("Lettuce");
Vegetables(String friendlyName) {
this.friendlyName = friendlyName;
}
private final String friendlyName;
@Override
public String getFriendlyName() {
return friendlyName;
}
@Override
public Vegetables getRaw() {
return this;
}
}
public static FruitVeg<?> getEnum(String name, Class<? extends FruitVeg<?>>... fvgClasses) {
for (Class<? extends FruitVeg<?>> fruitVegCLass : Arrays.asList(fvgClasses)) {
for (FruitVeg<?> fvg : fruitVegCLass.getEnumConstants()) {
if (name.equals(fvg.name()) || name.equals(fvg.getFriendlyName())) {
return fvg;
}
}
}
return null;
}
Option 1.
Create one method that returns Enum
public static Enum getEnum(String name) {
Enum selectedEnum = null;
for (Fruits fruit : Fruits.values()) {
if (name.equals(fruit.getName())) {
selectedEnum = fruit;
}
}
for (Vegetables vegetables : Vegetables.values()) {
if (name.equals(vegetables.getName())) {
selectedEnum = vegetables;
}
}
return selectedEnum;
}
and to get the name of enum you can use this method
public static String getName(final Enum value) {
String name = null;
if (value instanceof Fruits) {
name = ((Fruits) value).getName();
} else if (value instanceof Vegetables) {
name = ((Vegetables) value).getName();
}
return name;
}
Option 2.
You can combine 2 enum as
public static enum FruitsAndVegitables{
APPLE ("Apple" , true),
PEAR ("Pear", true),
CARROT ("Carrot", false),
LETTUCE ("Lettuce", false);
private String name;
private boolean isFurit;
//constructor
//getName()
...
}
Pass in the Enums themselves as values. Then use getSelectedItem
to retrieve the selected object, and do a test to see what type the object is.
Make the return type of your method an Object
, not an enum of a specific type. This would fix your problem.
However, I think your approach is wrong. If I wanted fruits and vegetables displayed in a list and broken into categories, I'd create an object to do so, and an enum to represent type of food like so:
public enum FoodType{FRUIT, VEGETABLE}
public class Food{
FoodType type;
String name;
public Food(FoodType type, String name){
this.type = type;
this.name = name;
}
public String toString(){return this.name;}
}
//and then when you want to use it...
Food lettuce = new Food(FoodType.VEGETABLE, "Lettuce");
Food berry = new Food(FoodType.FRUIT, "Berry");
comboBox.add(lettuces);
comboBox.add(berry);
And only add Food
items to your JComboBox
. Return Food
items when a selection is made, and test for food type using the FoodType
enum.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With