Java: new enum that is a subset of the old enum

In Java, is there a way to define a new enum from an existing enum? I want the following features.

public enum A{1, 2, 3, 4, 5, 6, 7, 8, 9};
public enum B{1, 2, 3, 4};
public enum C{3, 4, 5, 6, 7};

      

1, 2, 3 and all must be the same, i.e.

A.1 == B.1 = true
B.4 == C.4 = true

      

A real example would be DAY, WEEK, WEEKENDDAYS, LECTUREDAYS, PARTYDAYS, etc.

+3


source to share


3 answers


You cannot expand enums, but judging by your week / weekend example, it seems you might need to think about it a bit. If we have enum Days{ MON, TUE, WED, THU, FRI, SAT, SUN }

, then think about the days of the week / days like this:

EnumSet<Days> weekDays    = EnumSet.range(Days.MON, Days.FRI);
EnumSet<Days> weekendDays = EnumSet.range(Days.SAT, Days.SUN);
EnumSet<Days> partyDays   = EnumSet.of( Days.TUE, Days.THU );

      

Natural questions like "are there any days of the week and parties?" can be expressed as you would expect:



!Collections.disjoint(weekDays, partyDays)

      

Hope it helps.

+5


source


I think your best bet is to do something like:

public class EnumTest
{
  public enum A {
    X1(1), X2(2), X3(3), X4(4), X5(5), X6(6), X7(7), X8(8), X9(9);

    private final int value;
    public int getValue() { return this.value; }
    A(int value) {
      this.value = value;
    }
  }
  public enum B {
    X1(1), X2(2), X3(3), X4(4);
    private final int value;
    public int getValue() { return this.value; }
    B(int value) {
      this.value = value;
    }
  }
  public enum C {
    X3(3), X4(4), X5(5), X6(6), X7(7);

    private final int value;
    public int getValue() { return this.value; }
    C(int value) {
      this.value = value;
    }
  }

  public static void main()
  {
    A a;
    a = A.X1;
    B b;
    b = B.X1;
    if (b.getValue()==a.getValue()) {
      // do something
    }
  }
}

      



But if you do, you have to be careful that you are actually using Enums type safety, otherwise why not just store all the values โ€‹โ€‹as integers? Something like this, where each Enum value has some other meaning, can be useful if you sometimes want to compare between Enums, but usually want to use type safety. In the DAYS, WEEKDADA, WEEKENDDAYS, LECTUREDAYS examples, I would make sure that the internal value is something strongly typed, like Java8 java.time.DayOfWeek

.

+1


source


Enumerated types in Java provide the ability to define additional properties and methods.

public enum DAY_INDICATOR {
  SUNDAY(1,true), MONDAY(2,false), TUESDAY(3,false), WEDNESDAY(4,false), THURSDAY(5,false), FRIDAY(6,false), SATURDAY(7,true)
  private final int dayNumber;
  private final bool weekendDay;
  public DAY_INDICATOR(int dn, bool wd) {
    dayNumber = dn;
    weekendDay = wd;
  }
  public bool isWeekendDay() {
    return weekendDay;
  }
  ... more accoutrements of an enumerated type
}

      

You can now extend the base type with whatever predicates you need to differentiate the members of the enumerated value set according to your business rules.

+1


source







All Articles