Data Pile

Against Digital Amnesia

Using Java Enumerations

This article shows a basic implementation of a Java enumeration type that you can use to do a switch on Strings for example.

Introduction

Now for the moment let’s assume that we want to write a program that is able tell us if a programming language is more hardware adjacent than another. Since we already know the adjacency of the programming language, we can group that into the enumeration.

So, we’re basically enriching the Enumeration constant with some additional information that we later can easily gather from the object. To distinguish the various adjacency levels we are needed to supply some additional information to the enum so we can later ask it if a programming language is more hardware adjacent than another. In addition to that, we want to refer to the programming language with it’s long, real name. So instead of just having print ASM we want to be able to extract that information as Assembler from the enum type.

The c0de

We’ll now have a look at the code and how it’s actually implemented. As you can see, you cannot rely on the default implementation of the toString() method when you do want to print the enums name. Exactly for this the name() method was introduced. For internal handling of the data structure Java holds an ordinal value too, so the enums can be located correctly.

ProgrammingLanguage.java

ProgramminLanguage Enumeration Type (ProgrammingLanguage.java) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package org.coffeecrew.examples.enumeration;

/**
 * This enumeration describes the hardware adjacency
 * of the specified programming languages.
 * @author j
 */
public enum ProgrammingLanguage {

    /* ASM is hardware ;) */
    ASM("Assembler", -10),
    C("C", 0),
    CPP("C ++", 10),
    Java("Java", 20);

    public enum Adjacence {
        UNSET,
        LESS,
        EQUAL,
        MORE
    };

    private ProgrammingLanguage(String longName, int hardwareAdjacence) {
        this.longName = longName;
        this.hardwareAdjacence = hardwareAdjacence;
    }
    private final String longName;
    private final int hardwareAdjacence;

    public int hardwareAdjacence() {
        return hardwareAdjacence;
    }

    public String longName() {
        return this.longName;
    }

    public Adjacence isMoreAdjecent(ProgrammingLanguage than) {
        Adjacence toReturn = Adjacence.UNSET;
        if (this.hardwareAdjacence() < than.hardwareAdjacence()) {
            toReturn = Adjacence.LESS;
        } else if (this.hardwareAdjacence() == than.hardwareAdjacence()) {
            toReturn = Adjacence.EQUAL;
        } else if (this.hardwareAdjacence() > than.hardwareAdjacence()) {
            toReturn = Adjacence.MORE;
        }
        return toReturn;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("Name: ");
        sb.append(name())
          .append("\nLong name: ")
          .append(longName)
          .append("\nOrdinal: ")
          .append(ordinal())
          .append("\nHardware adjacency: ")
          .append(hardwareAdjacence)
          .append("\n\n");
        return sb.toString();
    }
}

Next we probably want to have a sample program calling this wonderful enum … so why not develop one?

Main.java

Main Program (Main.java) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package org.coffeecrew.examples.enumeration;

public class Main {
    public static void main(String[] args) {
       for (ProgrammingLanguage pl: ProgrammingLanguage.values()) {
           printProgrammingLanguage(pl);
           printMoreAdjacent(ProgrammingLanguage.C, pl);
       }
    }

   public static void printProgrammingLanguage(ProgrammingLanguage pl) {
       System.out.println(pl);
   }

   public static void printMoreAdjacent(ProgrammingLanguage moreAdjacent,
                                        ProgrammingLanguage than) {
       ProgrammingLanguage.Adjacence adj = moreAdjacent.isMoreAdjecent(than);
       StringBuilder text = new StringBuilder();
       text.append(moreAdjacent.name())
           .append(" (")
           .append(moreAdjacent.longName())
           .append(")")
           .append(" is ");

       switch (adj) {
           case LESS:
               text.append(adj.name());
               break;
           case EQUAL:
               text.append(adj.name());
               break;
           case MORE:
               text.append(adj.name());
               break;
           default:
               text.append(adj.name());
       }

       text.append(" hardware adjacent than ")
           .append(than.name())
           .append(" (")
           .append(than.longName())
           .append(")");
       System.out.println(text.toString());
   }
}

Btw. with this concept you can also do the somehow often wanted switch on strings concept (just switch on the enum and get the String name).