Stopping repetition in Java enums

后端 未结 4 1547
梦谈多话
梦谈多话 2020-12-03 16:26

I have the following enum in a Java class:

public enum Resolution {
    RES_32 (32),
    RES_64 (64);
    private final int asInt;
    private R         


        
相关标签:
4条回答
  • 2020-12-03 16:53

    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 讨论(0)
  • 2020-12-03 17:10

    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);
    }
    
    0 讨论(0)
  • 2020-12-03 17:10

    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;
       }
    };
    
    0 讨论(0)
  • 2020-12-03 17:11

    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.

    0 讨论(0)
提交回复
热议问题