Suppose I have an interface…
public interface Feature
{
public abstract void doSmth();
}
… and multiple enums implementing that interface:
// First enum
public enum FeatureA implements Feature
{
A1
{
@Override public void doSmth() { System.out.println("A ==== A1"); }
},
A2
{
@Override public void doSmth() { System.out.println("A ==== A2"); }
};
@Override public abstract void doSmth();
}
//Second enum
public enum FeatureB implements Feature
{
B1
{
@Override public void doSmth() { System.out.println("B ==== B1"); }
},
B2
{
@Override public void doSmth() { System.out.println("B ==== B2"); }
},
B3
{
@Override public void doSmth() { System.out.println("B ==== B3"); }
};
@Override public abstract void doSmth();
}
// and many other enums implementing Feature
Now suppose I assemble a set of Strings corresponding to the names of enum constants like this (this is necessary for the third party code)…
Set<String> singleEnum = new LinkedHashSet<String>();
singleEnum.add("A2");
singleEnum.add("A1");
… and later I need to convert each String in such a set to the enum constant and then apply the doSmth() method. singleEnum contains names of FeatureA constants only, so I’m able to apply the valueOf(String) method in this case:
for (String s : singleEnum)
{
FeatureA.valueOf(s).doSmth();
}
But what if the Strings in the set correspond to varying enum constants? E.g.:
Set<String> multipleEnums = new LinkedHashSet<String>();
multipleEnums.add("A1");
multipleEnums.add("B2");
multipleEnums.add("A2");
I would like to do something like this:
for (String s : multipleEnums)
{
Feature./* convert s to the correct enum */.doSmth();
}
Is there an easy way to do that? The only method Eclipse allows to apply to Feature is Feature.class, and I don’t really know how to proceed from there.
I’m dealing with huge datasets, so performance is important.
Thank you.
You can create a Map with the name of every enum