Archive

Archive for May 24, 2011

Creating a successful Enum with Java

Enum is basically a dictionary to work with, that gives the programmer a better representation of data through out the application, while maintaining the consistency of the data through different modules.

So what is a successful Enum… the best way to explain is to give a little demonstration on how to upgrade your basic Enum to something more useful.
Application <—> Database is a powerful and common situation to work with.

First of all, an Enum is a class like any other, thus having contructors, members and functions like any other class, and we can use them to our advantage.

Lets begin with a simple week days Enum

enum weekDays
{
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday;
}

The first problem with this Enum is that its inconsistent with the week days (ha ha why?), because Sunday is not day 0, but day 1. (with out the philosophical debate, Sunday is the First day of the week, and if you want it to be Monday thats OK too, same rule still applies).
So we want to change the Enum elements values?. In order to do that we need to have a constructor, because when the class loader first loads the Enum it creates an instance for each of the Enums elements, in the code example above the default constructor is called for each of the element and consecutive numbers are given to each element starting with 0.
If we want different values than we will create another constructor.

Now, our Enum should look like this:

enum weekDays
{
Sunday(1) , Monday(2), Tuesday(3), Wednesday(4), Thursday(5), Friday(6), Saturday(7);
private weekDays (int value) { }
}

OK!, this looks more like it… but that too doesn’t help us so much. We have ourselves a small dictionary of the weekdays and we set they’r values to what ever we want them to be but the only problem is that we don’t really have any way of getting that value. if we talked about a calendar database and we have a new calendar entry with weekDays member, we need to have some way of getting that value that corresponds to that Enum element (we might want to insert a new record to the database).
Here is where we make this Enum allot more helpful… if we take into consideration the fact that the class loader instantiates each of the Enum values when the class is read for the first time, we can do the following, and expect to have 7 different immutable’s in the JVM immutable pull, each with a different value according to the values we gave the Enum elements.

enum weekDays
{
Sunday(1) , Monday(2), Tuesday(3), Wednesday(4), Thursday(5), Friday(6), Saturday(7);
private int value;
private weekDays (int value)
{
this.value = value;
}
public int toInt()
{
return this.value;
}
}

So now we can translate from the Enum element to its numeric representation (if you were following this far you probably understand that it could be any representation you want it to be).
if we wanted to insert a new record into the database, getting the numerical representation would be as simple as someWeekDay.toInt();

but what about the other way around… what if we wanted to create an application entities from the data stored in the database… well no problem there.

enum weekDays
{
Sunday(1) , Monday(2), Tuesday(3), Wednesday(4), Thursday(5), Friday(6), Saturday(7);
private int value;

private weekDays (int value)
{
this.value = value;
}

public int toInt()
{
return this.value;
}
public static weekDays fromInt(int value)
{
weekDays retVal = null;
switch (value)
{
case 1: retVal = weekDays.Sunday; break;
case 2: retVal = weekDays.Monday; break;
case 3: retVal = weekDays.Tuesday; break;
case 4: retVal = weekDays.Wednesday; break;
case 5: retVal = weekDays.Thursday; break;
case 6: retVal = weekDays.Friday; break;
case 7: retVal = weekDays.Saturday; break;
}
return retVal;
}
}

By now i think you got the point of making Enums work for you. the point is making the code more understandable and consistent traversing databases application network or any other module or medium that you desire.

if you found this helpful in any way or have something to say about what you have just read. please leave a comment.
Happy coding.

Categories: Java Tags: , , ,