Excelence by Experience
[ start | index | login ]
start > dev > java tips > enum reverse lookup

enum reverse lookup

Created by maz. Last edited by maz, one year and 272 days ago. Viewed 174 times. #7
[diff] [history] [edit] [rdf]
labels
attachments

reverse lookup with enums

The implementation of enumerations is great in java. In this Post I will introduce a very helpful hint for reverse lookup of enums.

What is reverse lookup

Just consider the case that there are some non easy understandable status columns within the database. In the java application it is wise to have enums with comprehensive items, so the code is understood very easy.

For example just the gender column for user is in some databases realized as a varchar(1) or char column with 'M' for male and 'F' for female. (Don't blame me for this database definitions. I came across many of this kind definitions and in a ideal world you would define lookup table in the database and link the value to them. But this is just an example for the use case :) )

So in the first place the enum for gender could look like this:

public enum Gender
{
   MALE, FEMALE;
}

One would then use switch-case-instructions (actually I have seen this on the time of writing many times) to assign the database value and to get the enum from the database.

But the most elegant way is to use the abilities of enums itself. Enums are in java classes by itself and may have constructors, members and functions. So let's use them.

public enum Gender
{
  MALE("M"),
  FEMALE("F");

protected String dbValue;

public Gender(String s) { dbValue=s; }

public String getDbValue() { return dbValue;}

// prepare for reverse lookup public static Map<String, Gender> lookupMap = new HashMap<String, Gender>(); // fill the reverse lookup map, alternatively this could be done within the constructor. static { for(Gender item : EnumSet.allOf(Gender.class)) lookupMap.put(item.getDbValue(), item); }

public Gender findbyDbValue(String val) { return lookupMap.get(val); } }

A generic enum inversion

For easy usage it is rather useful to have an enum reverse finder as a helper utility and use it whereever useful:

public abstract class EnumReverseFinder<K,V extends Enum<V>> 
{
  private Map<K, V> map = new HashMap<K, V>();
  public EnumReverseFinder(Class<V> enumclass)
  {
    for(V v:enumclass.getEnumConstants())
      map.put(getKey(v), v);
  }
  public V get(K k) {return map.get(k);}
  protected abstract K getKey(V value);
}

On the usage one must define the key by implementing the abstract getKey-Method. The Gender-Enum as an example would look like this:

public enum Gender
{
  MALE("M"),
  FEMALE("F");

protected String dbValue;

public Gender(String s) { dbValue=s; }

public String getDbValue() { return dbValue;}

// reverse lookup for dbValue private static final EnumReverseFinder<String, Gender> dbValueFinder = new EnumReverseFinder<String, Gender>(Gender.class) { protected String getKey(Gender v){return v.dbValue;} };

public static Gender findbyDbValue(String val){return dbValueFinder.get(val);} }

More fancy things about enums

no comments | post comment

< February 2012 >
SunMonTueWedThuFriSat
1234
567891011
12131415161718
19202122232425
26272829

Most Viewed



Logged in Users: (0)
… and a Guest.
Help
For hints about formatting text see snipsnap-help.

Powered by SnipSnap 1.0b3-uttoxeter

amazers.net | © 2010 All rights reserved by Maz Rashid