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.

package org.coffeecrew.examples.enumeration;

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?

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).

Like this post? Share on: TwitterFacebookEmail


Jens Frey Avatar Jens Frey is the creator of the datapile blog.

Keep Reading


Published

Category

Programming

Tags

Stay in Touch