Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping repetition in Java enums

Tags:

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 have the constructor and property kept.

like image 348
rid Avatar asked Dec 17 '22 12:12

rid


2 Answers

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);
}
like image 129
trashgod Avatar answered Dec 19 '22 01:12

trashgod


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;
   }
};
like image 42
Hovercraft Full Of Eels Avatar answered Dec 19 '22 01:12

Hovercraft Full Of Eels