Skip to main content
[Edit removed during grace period]
Source Link
toto2
  • 5.4k
  • 16
  • 21
Source Link
toto2
  • 5.4k
  • 16
  • 21

There are 4 boolean state-returning methods, but my solution is probably more relevant when there are more states. Instead of having 4 boolean state members, the Component class would have an EnumSet<State>.

public enum State {
   HAS_ONE_CHANNEL, HAS_VALID_CERTIFICATE, IS_TESTED, IS_FREQUENTLY_USED;
}

public enum Category {
   C24(EnumSet.of(States.HAS_ONE_CHANNEL, ...)),
   C95(EnumSet.of(...)),
   ...;
   
   private EnumSet<State> enumSet;

   private Category(EnumSet<State> enumSet) {
      this.enumSet = enumSet;
   }

   public boolean equals(EnumSet<State> otherEnumSet) {
      return enumSet.equals(otherEnumSet);
   }

   // returns null if not found
   public static Category find(EnumSet<State> otherEnumSet) {
       // TODO instead of looping, it would be more efficient to build a HashMap<EnumSet<State>, Category>> once
       for (Category category : Category.values()) {
          if (category.equals(otherEnumSet)) 
             return category;
       }
       return null;
   }
 }