消除重复的枚举代码

时间:2008-09-16 21:15:52

标签: java enums enumeration

我有大量实现此界面的枚举:

/**
 * 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();
}

一个典型的例子是:

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;
            }
        }
    }
}

您可以想象这些方法在CodableEnum的所有实现中几乎完全相同。我想消除这种重复,但坦率地说不知道如何。我尝试使用如下的类:

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);  
}

但事实证明这是无用的,因为:

  1. 枚举不能扩展类
  2. 枚举的元素(SKYPE,GOOGLE_TALK等)无法扩展类
  3. 我无法提供getByCode()的默认实现,因为DefaultCodableEnum本身不是Enum。我尝试更改DefaultCodableEnum以扩展java.lang.Enum,但似乎不允许这样做。
  4. 任何不依赖反思的建议? 谢谢, 唐

15 个答案:

答案 0 :(得分:13)

您可以将重复的代码分解为CodeableEnumHelper类:

public class CodeableEnumHelper {
    public static CodeableEnum getByCode(String code, CodeableEnum[] values) {
        for (CodeableEnum e : values) {
            if (e.getCode().equalsIgnoreCase(code)) {
                return e;
            }
        }
        return null;
    }
}

每个CodeableEnum类仍然需要实现getByCode方法,但该方法的实际实现至少已集中到一个地方。

public enum IMType implements CodeableEnum {
    ...
    public IMType getByCode(String code) {
        return (IMType)CodeableEnumHelper.getByCode(code, this.values());
    } 
}

答案 1 :(得分:7)

抽象枚举可能非常有用(目前不允许)。但是如果你想在Sun游说某人添加它,那么就会有一个提案和原型:

http://freddy33.blogspot.com/2007/11/abstract-enum-ricky-carlson-way.html

Sun RFE:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6570766

答案 2 :(得分:5)

整理戴夫的代码:

public class CodeableEnumHelper {
    public static <E extends CodeableEnum> E getByCode(
        String code, E[] values
    ) {
        for (E e : values) {
            if (e.getCode().equalsIgnoreCase(code)) {
                return e;
            }
        }
        return null;
    }
}

public enum IMType implements CodableEnum {
    ...
    public IMType getByCode(String code) {
        return CodeableEnumHelper.getByCode(code, values());
    } 
}

或更有效率:

public class CodeableEnumHelper {
    public static <E extends CodeableEnum> Map<String,E> mapByCode(
        E[] values
    ) {
        Map<String,E> map = new HashMap<String,E>();
        for (E e : values) {
            map.put(e.getCode().toLowerCase(Locale.ROOT), value) {
        }
        return map;
    }
}

public enum IMType implements CodableEnum {
    ...
    private static final Map<String,IMType> byCode =
        CodeableEnumHelper.mapByCode(values());
    public IMType getByCode(String code) {
        return byCode.get(code.toLowerCase(Locale.ROOT));
    } 
}

答案 3 :(得分:2)

我写了一个与本地化组件类似的问题。我的组件旨在访问具有索引到资源包的枚举常量的本地化消息,而不是一个难题。

我发现我正在复制并粘贴相同的“模板”枚举代码。我避免重复的解决方案是一个代码生成器,它接受带有枚举常量名称和构造函数args的XML配置文件。输出是具有“重复”行为的Java源代码。

现在,我维护配置文件和生成器,而不是所有重复的代码。我到处都有枚举源代码,现在有一个XML配置文件。我的构建脚本检测过时的生成文件,并调用代码生成器来创建枚举代码。

您可以看到此组件here。我复制和粘贴的模板已计入an XSLT stylesheetcode generator运行样式表转换。与生成的枚举源代码相比,input file非常简洁。

HTH,
格雷格

答案 4 :(得分:1)

不幸的是,我认为没有办法做到这一点。你最好的选择是完全放弃emums并使用传统的类扩展和静态成员。否则,习惯于复制该代码。遗憾。

答案 5 :(得分:1)

创建一个类型安全的实用程序类,它将按代码加载枚举:

界面归结为:

public interface CodeableEnum {
    String getCode();
}

实用程序类是:

import java.lang.reflect.InvocationTargetException;


public class CodeableEnumUtils {
    @SuppressWarnings("unchecked")
    public static <T extends CodeableEnum>  T getByCode(String code, Class<T> enumClass) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        T[] allValues = (T[]) enumClass.getMethod("values", new Class[0]).invoke(null, new Object[0]);
        for (T value : allValues) {
            if (value.getCode().equals(code)) {
                return value;
            }
        }
        return null;
}

}

演示用法的测试用例:

import junit.framework.TestCase;


public class CodeableEnumUtilsTest extends TestCase {
    public void testWorks() throws Exception {
    assertEquals(A.ONE, CodeableEnumUtils.getByCode("one", A.class));
      assertEquals(null, CodeableEnumUtils.getByCode("blah", A.class));
    }

enum A implements CodeableEnum {
    ONE("one"), TWO("two"), THREE("three");

    private String code;

    private A(String code) {
        this.code = code;
    }

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

现在,您只复制了getCode()方法,并且getByCode()方法位于一个位置。将所有异常包装在单个RuntimeException中可能会很好:)

答案 6 :(得分:1)

我在这里有另一个解决方案:

interface EnumTypeIF {
String getValue();

EnumTypeIF fromValue(final String theValue);

EnumTypeIF[] getValues();

class FromValue {
  private FromValue() {
  }

  public static EnumTypeIF valueOf(final String theValue, EnumTypeIF theEnumClass) {

    for (EnumTypeIF c : theEnumClass.getValues()) {
      if (c.getValue().equals(theValue)) {
        return c;
      }
    }
    throw new IllegalArgumentException(theValue);
  }
}

诀窍在于内部类可用于保存“全局方法”。

对我来说工作得很好。好的,你必须实现3个方法,但那些方法, 只是委托人。

答案 7 :(得分:0)

看起来您实际上正在实现运行时类型信息。 Java将此作为语言功能提供。

我建议你查看RTTI或反思。

答案 8 :(得分:0)

我不认为这是可能的。但是,如果要将枚举值的名称用作代码,则可以使用enum的valueOf(String name)方法。

答案 9 :(得分:0)

静态通用方法怎么样?您可以在枚举的getByCode()方法中重用它,或者直接使用它。我总是为我的枚举使用整数id,所以我的getById()方法只做这样做:return values()[id]。它更快更简单。

答案 10 :(得分:0)

如果你真的想要继承,不要忘记你可以implement the enum pattern yourself,就像糟糕的旧Java 1.4天一样。

答案 11 :(得分:0)

尽可能接近你想要的是在IntelliJ中创建一个“实现”通用代码的模板(使用enum的valueOf(String name))。不完美,但效果很好。

答案 12 :(得分:0)

在您的特定情况下,getCode()/ getByCode(String code)方法似乎对所有枚举提供的toString()/ valueOf(String value)方法的行为非常封闭(委婉地说)。你为什么不想使用它们?

答案 13 :(得分:0)

另一个解决方案是不将任何内容放入枚举本身,只提供双向映射Enum&lt; - &gt;每个枚举的代码。你可以,例如使用Google Collections中的ImmutableBiMap进行此操作。

这样就没有重复的代码了。

示例:

public enum MYENUM{
  VAL1,VAL2,VAL3;
}

/** Map MYENUM to its ID */
public static final ImmutableBiMap<MYENUM, Integer> MYENUM_TO_ID = 
new ImmutableBiMap.Builder<MYENUM, Integer>().
put(MYENUM.VAL1, 1).
put(MYENUM.VAL2, 2).
put(MYENUM.VAL3, 3).
build();

答案 14 :(得分:0)

在我看来,这是最简单的方法,没有反思,也没有为你的枚举添加任何额外的包装。

您创建了枚举实现的接口:

public interface EnumWithId {

    public int getId();

}

然后在帮助器类中,您只需创建一个类似这样的方法:

public <T extends EnumWithId> T getById(Class<T> enumClass, int id) {
    T[] values = enumClass.getEnumConstants();
    if (values != null) {
        for (T enumConst : values) {
            if (enumConst.getId() == id) {
                return enumConst;
            }
        }
    }

    return null;
}

然后可以像这样使用此方法:

MyUtil.getInstance().getById(MyEnum.class, myEnumId);