Reputation: 6440
With an enum like this one where each key has several values
ABBR1("long text 1", "another description 1", "yet another one 1"),
ABBR2("long text 2", "another description 2", "yet another one 2"),
//and so on...
how can I reverse lookup an abbreviation (constant) by calling a method like getAbbreviation(descriptionText)
?
I'm basically looking for an implementation as described here, I think, but with the difference that each ENUM key (constant) has several values coming with it, and I want it to work with getAbbreviation("long text 1")
as well as with getAbbreviation("yet another one 2")
...
Is there an easy way to loop over each ENUM's (i.e. ABBRn
's) value fields, to populate a giant map, or is there maybe even a better solution?
Upvotes: 1
Views: 2637
Reputation: 5081
I think the solution from: c.P.u1 is on the right track. However there is a more direct way to populate the HashMap as you go using the solution from this question.
How to facilitate Netbeans Designer to load JPanel-s that use an enum reverse-lookup using hashmap?
private static final Map<String, Abbreviation> abbreviationMap;
private Abbreviation(String... longForms) {
this.longForms = longForms; // optional
mapAbbreviations( this, longForms )
}
private static void mapAbbreviations( final Status state, String... longForms ){
if( null == abbreviationMap ){
abbreviationMap = new HashMap( 20 );
}
for( String abbrev : longForms ){
abbreviationMap.put( abbrev, state );
}
}
Also, you don't really need the private longForms
string array for the toString() function, since all the values are saved with the Map anyway.
Upvotes: 1
Reputation: 17094
This relies on the fact that the enum member constructors are run before the static initializer. The initializer then caches the members and their long forms.
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public enum Abbreviation {
ABBR1("long text 1", "another description 1", "yet another one 1"),
ABBR2("long text 2", "another description 2", "yet another one 2");
private static final Map<String, Abbreviation> ABBREVIATIONS = new HashMap<>();
private String[] longForms;
private Abbreviation(String... longForms) {
this.longForms = longForms;
}
public String toString () {
return Arrays.toString(longForms);
}
static {
for(Abbreviation abbr : values()) {
for(String longForm : abbr.longForms) {
ABBREVIATIONS.put(longForm, abbr);
}
}
}
public static Abbreviation of(String longForm) {
Abbreviation abbreviation = ABBREVIATIONS.get(longForm);
if(abbreviation == null) throw new IllegalArgumentException(longForm + " cannot be abbreviated");
return abbreviation;
}
public static void main(String[] args) {
Abbreviation a = Abbreviation.of("yet another one 2");
System.out.println(a == Abbreviation.ABBR2); //true
}
}
Upvotes: 2
Reputation: 2188
Each enum has a method values() so You can do like that:
for(YourEnum type : values()){
if(/*is Equal to Your descriptionText*/){
return type;
}
}
throw new IllegalArgumentException("No such BailiffPaymentStatus:"+dbRepresentation);
Upvotes: 2