Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

Enum’s provide valueOf(String) to get an enum via it’s String value of the value of the Enum itself.  This is good, but doesn’t help if we need to get an enum from one of it’s other values.  I added the following method to an enum.

An example is starting with the following enum

package com.javaninja.example;

/**
 * Transaction operation enum.
 * @author norris.shelton
 */
public enum OperationEnum {
    CARD_DEPOSIT("cad", "card deposit", false),
    CARD_WITHDRAW("caw", "card withdraw", true);

    private String  key;
    private String  value;
    /**
     * Is money going into or out of the players account, e.g. negative transaction.
     */
    private boolean negativeTransaction;

    OperationEnum(String key, String value, boolean negativeTransaction) {
        this.key = key;
        this.value = value;
        this.negativeTransaction = negativeTransaction;
    }

    public String key() {
        return key;
    }

    public String value() {
        return value;
    }

    public boolean negativeTransaction() {
        return negativeTransaction;
    }

To get an enum from one of it’s values, create a method similar to:

public static OperationEnum get(String anyProperty) {

    OperationEnum returnValue = null;
    for (OperationEnum operationEnum : values()) {
        if (operationEnum.key.equals(anyProperty) ||
            operationEnum.description.equals(anyProperty)) {
            returnValue = operationEnum;
            break;
        }
    }
    if (returnValue == null) {
        returnValue = valueOf(anyProperty);
    }
    return returnValue;
}

The Enum that I have has a key and a description property. If it doesn’t find a value, I have it use the standard valueOf() implementation as a fallback.

I tested it with the following:

package com.cdi.igs.models.enums;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;

/**
 * Tests the operation enum class.
 * @author norris.shelton
 */
public class TestOperationEnum {
    @Rule
    public ExpectedException expectedEx = ExpectedException.none();

    /**
     * Verify that an illegal argument exception is thrown if an unknown value is provided.
     */
    @Test(expected = IllegalArgumentException.class)
    public void testIllegalArgumentException() {
        OperationEnum.get("junk");
    }

    /**
     * Verify that the enum can be found via it's value.
     */
    @Test
    public void testValue() {
        OperationEnum operationEnum = OperationEnum.get("LEAVE_TABLE");
        assertEquals(OperationEnum.LEAVE_TABLE, operationEnum);
    }

    /**
     * Verify that the enum can be found via it's key property.
     */
    @Test
    public void testKey() {
        OperationEnum operationEnum = OperationEnum.get("102");
        assertEquals(OperationEnum.CHIPS_TO_TABLE, operationEnum);
    }

    /**
     * Verify that the enum can be found via it's description property.
     */
    @Test
    public void testDescription() {
        OperationEnum operationEnum = OperationEnum.get("leave table");
        assertEquals(OperationEnum.LEAVE_TABLE, operationEnum);
    }
}

October 1st, 2014

Posted In: Java, java ninja, Javaninja

Tags: , ,

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com