开发者

Stopping repetition in Java enums

I have the following enum in a Java class:

public enum Resolution {
    RES_32 (32),
    RES_64 (64);
    private final int asInt;
    private Resolution(int asInt) {
        this.asInt = asInt;
    }
};

I have more classes that need a similar kind of enum, with the same asInt property and the same constructor, but with different constants. So, in another class, I need the following enum:

public enum Resolution {
    RES_32 (32),
    RES_64 (64),
    RES_128 (128);
    private final int asInt;
    private Resolution(int asInt) {
        this.asInt = asInt;
    }
};

If this was a class, I could use inheritance to not repeat the code in the constructor (and would likely have made a getter for that asInt property). What can I do in order to stop repeating myself each time I need such a Resolution enum? Ideally, I would like to just specify the constants for each Resolution, and to 开发者_StackOverflowhave the constructor and property kept.


EnumSet may be helpful in this context. Given the following,

public enum Resolution {

    RES_32(32),
    RES_64(64),
    RES_128(128),
    RES_256(256);

    public static Set<Resolution> deluxe = EnumSet.allOf(Resolution.class);
    public static Set<Resolution> typical = EnumSet.range(RES_64, RES_128);
    public static Set<Resolution> ecomomy = EnumSet.of(RES_32);

    private final int asInt;

    private Resolution(int asInt) {
        this.asInt = asInt;
    }
};

Suitably named sets may be used as shown below.

for (Resolution r : Resolution.deluxe) {
    System.out.println(r.asInt);
}


Why not take the enums out of the class and create a stand-alone enum file that only use the second one (the one with RES_128) for all processing?

Edit 1
Your comment:

Because not all classes should have the same constants. Some need to have only 32 and 64, while others need to have 32, 64 and 128

There really is only one Resolution "type" and this suggests that there should be but one Resolution enum, but the problem appears to be that not all classes accept all resolutions. One possible solution is to use one enum to represent all resolutions, but have EnumMap for different classes, with some classes marking a resolution false or meaning not valid for that class.

Edit 2
Or even just have a HashSet of accepted enums.

Edit 3
e.g., using HashSet

class Foo002 {
   public static Set<Resolution> allowedResolution = new HashSet<Resolution>();
   static {
      allowedResolution.add(Resolution.RES_32);
      allowedResolution.add(Resolution.RES_64);
   }
   private Resolution resolution;

   public void setResolution(Resolution resolution) {
      if (!(allowedResolution.contains(resolution))) {
         throw new IllegalArgumentException("Disallowed Resolution: " + resolution);
      }
      this.resolution = resolution;
   }
}

enum Resolution {
   RES_32 (32),
   RES_64 (64),
   RES_128 (128);
   private final int asInt;
   private Resolution(int asInt) {
       this.asInt = asInt;
   }

   public int getIntValue() {
      return asInt;
   }
};


I would add the new value to the original enum and have the new classes just re-use it. Why can't they call the first one you posted? You have bigger issues than merely repeating a constructor if you copy and extend the first one.


The following lets you work with enums on runtime, this is the reference link http://www.theserverside.com/news/thread.tss?thread_id=50190:

Constructor con = Day.class.getDeclaredConstructors()[0]; 
Method[] methods = con.getClass().getDeclaredMethods(); 
for (Method m : methods) 
{ 
    if (m.getName().equals("acquireConstructorAccessor")) 
    { 
        m.setAccessible(true); 
        m.invoke(con, new Object[0]); 
    } 
}

Field[] fields = con.getClass().getDeclaredFields(); 
Object ca = null; 
for (Field f : fields) 
{ 
    if (f.getName().equals("constructorAccessor")) 
    { 
        f.setAccessible(true); 
        ca = f.get(con); 
    } 
}

Method m = ca.getClass().getMethod( "newInstance", new Class[] { Object[].class }); 
m.setAccessible(true); 
Day v = (Day) m.invoke(ca, new Object[] { new Object[] { "VACATION", Integer.MAX_VALUE } }); 
System.out.println(v.getClass() + ":" + v.name() + ":" + v.ordinal());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜