Search code examples
javaenumsextendjava-8

Is it possible to extend enum in Java 8?


Just playing and came up with a sweet way to add functionality to enums in Java Enum toString() method with this.

Some further tinkering allowed me to nearly also add a tidy (i.e. not throwing an exception) reverse look-up but there's a problem. It's reporting:

error: valueOf(String) in X cannot implement valueOf(String) in HasValue
public enum X implements PoliteEnum, ReverseLookup {
overriding method is static

Is there a way?

The aim here is to silently add (via an interface implementation with a default method like I added politeName in the linked answer) a lookup method that does the valueOf function without throwing an exception. Is it possible? It is clearly now possible to extend enum - one of my major problems with Java until now.

Here's my failed attempt:

public interface HasName {

    public String name();
}

public interface PoliteEnum extends HasName {

    default String politeName() {
        return name().replace("_", " ");
    }
}

public interface Lookup<P, Q> {

    public Q lookup(P p);
}

public interface HasValue {
    HasValue valueOf(String name);
}

public interface ReverseLookup extends HasValue, Lookup<String, HasValue> {

    @Override
    default HasValue lookup(String from) {
        try {
            return valueOf(from);
        } catch (IllegalArgumentException e) {
            return null;
        }
    }

}

public enum X implements PoliteEnum/* NOT ALLOWED :( , ReverseLookup*/ {

    A_For_Ism, B_For_Mutton, C_Forth_Highlanders;
}

public void test() {
    // Test the politeName
    for (X x : X.values()) {
        System.out.println(x.politeName());
    }
    // ToDo: Test lookup
}

Solution

  • You are over-complicating your design. If you are willing to accept that you can invoke a default method on an instance only, there entire code may look like this:

    interface ReverseLookupSupport<E extends Enum<E>> {
        Class<E> getDeclaringClass();
        default E lookup(String name) {
            try {
                return Enum.valueOf(getDeclaringClass(), name);
            } catch(IllegalArgumentException ex) { return null; }
        }
    }
    enum Test implements ReverseLookupSupport<Test> {
        FOO, BAR
    }
    

    You can test it with:

    Test foo=Test.FOO;
    Test bar=foo.lookup("BAR"), baz=foo.lookup("BAZ");
    System.out.println(bar+"  "+baz);
    

    An non-throwing/catching alternative would be:

    interface ReverseLookupSupport<E extends Enum<E>> {
        Class<E> getDeclaringClass();
        default Optional<E> lookup(String name) {
            return Stream.of(getDeclaringClass().getEnumConstants())
              .filter(e->e.name().equals(name)).findFirst();
    }
    

    to use like:

    Test foo=Test.FOO;
    Test bar=foo.lookup("BAR").orElse(null), baz=foo.lookup("BAZ").orElse(null);
    System.out.println(bar+"  "+baz);