Simplified software development with Java enums

Tutorial:

Top ways that Java enums can simplify the software development process

By Randall Nagy

TheServerSide.com

While not originally part of the Java specification, the addition of the Enum type in Java 5 was much anticipated, adding a new language feature that reflected a new level of maturity. Unfortunately though, not every developer is using the enum construct to its fullest, and that means time is being lost to unconstructive development. Here we will look at a variety of interesting and unique ways that software developers are simplifying the development of code through the use of the Java enum.

Taking a closer look at enums

On the surface, enums are fairly self explanatory and easy to understand. The following code snippet is a prime example:

enum codes {
one, two, three;
}
enum cards {
hearts, diamonds, clubs, spades;
}

Understanding object members

What is interesting to note is that unlike its C++ brethren, any enum in either Java or .NET is not merely about mnemonic substitution. Rather, the enum keyword in both technologies creates a fully-qualified class. This allows the addition of behavior to the enum through methods:

   public String get(codes which) {
return vals.get(which.ordinal());
}
public String set(codes which, String sWho) {
return vals.set(which.ordinal(), sWho);
}

While many developers are familiar with enum methods such as .ordinal(), few actually know how easy it is to add custom ones:

public enum foonum {
one, two, three;

public static void SayHello(foonum val) {
System.out.println(Hello + val);
}
public void greeting() {
System.out.println(Greetings + this);
}
public static void main(String ... args) {
SayHello(foonum.two);
foonum.two.greeting();
}
}

Furthermore, having a built-in ability to translate between an enum's name() to editable text, XML, or property is also a great way to get the drop on a human-machine interface:

      if(foonum.valueOf(two) == foonum.two) {
System.out.println(Yes. We can translate, too.);
}

The default members of an enum allow us to translate between a set of zero-based integers, as well as any enumerated token. Keeping with the spirit of the original C++ enumeration construct, a modern Java enum can be associated with many values. This ability to associate with other values, for example the JfileChooser pre-enum constants like FILES_ONLY, as seen below, can be very handy. When mapping these other values, each enumerated token can use a constructor without calling new:

   public enum ShowNode {

       BOTH(JFileChooser.FILES_AND_DIRECTORIES),
FOLDERS(JFileChooser.DIRECTORIES_ONLY),
FILES(JFileChooser.FILES_ONLY);
private int code; ShowNode(int code) {
this.code = code;
}
public int getCode() {
return code;
}
public String getDescription() {
switch (this) {
case BOTH:
return Select Files and Folders;
case FOLDERS:
return Select Folders;
case FILES:
return Select FIles;
default:
return Select Node;
}
}
}
Translating enums to zero-based arrays

But here is another fun idea: since by default the enum uses a zero-based ordinal, the use of any enumerated set of tokens readily transfers over to a zero-based array. The application of an enum's default numbering series is that any set of arbitrary enumerators can be used to quickly create a class, with just as many value storage opportunities.
 

class ClientSettings {
ArrayList<String> vals = new ArrayList<String>();
public enum codes {
// We could also use enums like name, address, phone (etc)
one, two, three
}
public ClientSettings() {
codes[] set = codes.values();
for (codes code : set) {
vals.add(code.name());
}
}
public String get(codes which) {
return vals.get(which.ordinal());
}
public String set(codes which, String sWho) {
return vals.set(which.ordinal(), sWho);
}
public static void main(String[] args) {
ClientSettings ref = new ClientSettings();
if (ref.get(codes.two).equals(codes.two.name()) == false) {
System.out.println(Error - Conceptual failure #1.);
}
String sTest = Enumerated arrays are so easy, they are scary.;
ref.set(codes.two, sTest);
if (ref.get(codes.two).equals(sTest) == false) {
System.out.println(Error - Conceptual failure #2.);
}
}

Enums were one of the most eagerly anticipated language features ever to hit the language, and by using them effectively, developers can save time as they develop more robust and more maintainable code.

20 Jul 2013

Related Content

Related Resources