Reputation: 45
I want to implement a method who decide from what java enum to get a specific value. For example:
public enum Credentials1 {
USERNAME("user"),
PASSWORD("pass");
}
public enum Credentials2 {
USERNAME("user"),
PASSWORD("pass");
}
and then, in another class I want a generic method to decide, based on another param, what enum should I use to get the correct user. Something like:
public String decide(here I should pass something?) {
switch (myParam) {
case "1":
return Credentials1.USERNAME;
case "2":
return Credentials2.USERNAME;
}
return "";
}
Can this be achievable, because I didn't succeed? And think that besides USERNAME
and PASSWORD
I will have a lot of constants in those enums.
Upvotes: 3
Views: 422
Reputation: 6028
First create a method that returns the desired enumeration class:
public Class<? extends Enum<?>> getEnum(int param)
{
// Determine enumeration class based on specified parameter
switch (param)
{
case 1:
return (Enum1.class);
case 2:
return (Enum2.class);
default:
return (null);
}
} // getEnum
Then create a method that returns the desired instance:
public Object getEnumInstance(Class<? extends Enum<?>> clazz,
String name)
throws Exception
{
// Determine the 'valueOf()' method that creates an instance from a string
// Every enumeration class has a static valueOf() method that expects a String as a parameter
Method value_of_method;
value_of_method = clazz.getDeclaredMethod("valueOf", String.class);
// Get and return instance
return (value_of_method.invoke(null, name));
} // getEnumInstance
Next, create a method that returns the value of the specified enum:
public String getEnumValue(Object enum_instance)
throws Exception
{
// Determine the 'getValue()' method of this enumeration instance
// At least, I suppose the method is called like that in your enumeration classes
// It doesn't want parameters and returns a String
Method get_value_method;
get_value_method = enum_instance.getClass().getMethod("getValue");
// Return value of the enumeration instance
return ((String)get_value_method.invoke(enum_instance));
} // getEnumValue
Finally, create a method that combines the whole stuff:
public String decide(int param,
String enum_instance_name)
throws Exception
{
// Get correct enumeration class
Class<? extends Enum<?>> enum_class;
enum_class = getEnum(param);
// Get correct instance
Object enum_instance;
enum_instance = getEnumInstance(enum_class, enum_instance_name);
// Return its value
return (getEnumValue(enum_instance));
} // decide
Now we can finally do what we have to do:
public void run()
throws Exception
{
// Get password of 2nd enum
String result;
result = decide(2, "PASSWORD");
System.out.println(result);
// Or username of 1st enum
result = decide(1, "USERNAME");
System.out.println(result);
} // run
Upvotes: 0
Reputation: 44456
Create an interface Credentials
with the following method:
public interace Credentials {
public String value();
}
Make these enum
s to implement an interface with a common method returning the value. Below is Credentials1
. The Credentials2
is implemented analogically.
public enum Credentials1 implements Credentials {
USERNAME("user"),
PASSWORD("pass");
private String value;
Credentials1(String value) {
this.value= value;
}
@Override
public String value() {
return this.str;
}
}
Here you pass the correct enum
for the further processing:
public String decide(Credentials credentials) {
return credentials.value();
}
A sample usage is:
String string = this.decide(Credentials1.USERNAME);
Upvotes: 1