So I have several Enums defined like the following. I have a method that uses the Enum to find the value.
public enum MyEnum1 { STRING1("My String1"), STRING2("My String2"), STRING3("My String3"), STRINGLESS("String not found"); private String s; private MyEnum1(String s) { this.s = s; } public String getValue() { return s; } } public enum MyEnum2 { STRING1("My String1"), STRING2("My String2"), STRING3("My String3"), STRINGLESS("String not found"); private String s; private MyEnum2(String s) { this.s = s; } public String getValue() { return s; } } public class MyClass1 { public static String getPlacement(String myString) { for (MyEnum1 a: MyEnum1.values()) if (myString.contains(a.toString())) return a.getValue(); return MyEnum1.STRINGLESS.getValue(); } } public class MyClass2 { public static String getPlacement(String myString) { for (MyEnum2 a: MyEnum2.values()) if (myString.contains(a.toString())) return a.getValue(); return MyEnum2.STRINGLESS.getValue(); } } Right now I have 5 Enums defined that all get processed by the same getPlacement method but I had to create 5 different MyClass classes (MyClass2, MyClass3...) with MyEnum2, MyEnum3... hard coded in the For Loop to make it happen.
I've tried...
public static String getPlacement(Enum e, String myString) {} and
public static String getPlacement(Enum<?> e, String myString) {} but neither works.
I would like to pass an Enum to the getPlacement method as a parameter thus allowing me to only have one MyClass class that could process the 5 different Enums. Is this possible?