eliminating duplicate Enum code

Posted by Don on Stack Overflow See other posts from Stack Overflow or by Don
Published on 2008-09-16T21:15:52Z Indexed on 2010/03/18 17:11 UTC
Read the original article Hit count: 184

Filed under:
|
|

Hi,

I have a large number of Enums that implement this interface:

/**
 * Interface for an enumeration, each element of which can be uniquely identified by it's code
 */
public interface CodableEnum {

    /**
     * Get the element with a particular code
     * @param code
     * @return
     */
    public CodableEnum getByCode(String code);

    /**
     * Get the code that identifies an element of the enum
     * @return
     */
    public String getCode();
}

A typical example is:

public enum IMType implements CodableEnum {

    MSN_MESSENGER("msn_messenger"),
    GOOGLE_TALK("google_talk"),
    SKYPE("skype"),
    YAHOO_MESSENGER("yahoo_messenger");

    private final String code;

    IMType (String code) {
    	this.code = code;
    }

    public String getCode() {
    	return code;
    }	

    public IMType getByCode(String code) {
    	for (IMType e : IMType.values()) {
    		if (e.getCode().equalsIgnoreCase(code)) {
    			return e;
    		}
    	}
    }
}

As you can imagine these methods are virtually identical in all implementations of CodableEnum. I would like to eliminate this duplication, but frankly don't know how. I tried using a class such as the following:

public abstract class DefaultCodableEnum implements CodableEnum {

    private final String code;

    DefaultCodableEnum(String code) {
    	this.code = code;
    }

    public String getCode() {
    	return this.code;
    }	

    public abstract CodableEnum getByCode(String code);  
}

But this turns out to be fairly useless because:

  1. An enum cannot extend a class
  2. Elements of an enum (SKYPE, GOOGLE_TALK, etc.) cannot extend a class
  3. I cannot provide a default implementation of getByCode(), because DefaultCodableEnum is not itself an Enum. I tried changing DefaultCodableEnum to extend java.lang.Enum, but this doesn't appear to be allowed.

Any suggestions that do not rely on reflection? Thanks, Don

© Stack Overflow or respective owner

Related posts about java

Related posts about enums