Disclaimer: not very strong with generics.
Lets say I have Enum like this:
public enum FirstEnum implements BaseEnum<FirstEnum> {
SOMETHING_1(Month.FEBRUARY, MZ_AUGUST, MZ_NOVEMBER),
SOMETHING_2(Month.JANUARY, MZ_APRIL, MZ_NOVEMBER);
private final Month month;
private final List<OtherEnum> otherEnumList;
FirstEnum(final Month month, final OtherEnum... otherEnumList) {
this.month = month;
this.otherEnumList = Arrays.asList(otherEnumList);
}
public static List<OtherEnum> getByMonth(final Month month) {
for (final FirstEnum firstEnum : FirstEnum.values()) {
if (month.equals(firstEnum.getMonth())) {
return firstEnum.getOtherEnumList();
}
}
return Collections.emptyList();
}
}
There is also a SecondEnum like this but it is not relevant right now. My problem is how to implement BaseEnum (I think it would be an Interface), where a getByMonth() method will be implemented using generics.
I am trying something like this (but it's not working at all):
public interface BaseEnum<T extends BaseEnum<T>> {
static List<OtherEnum> getByMonth(final Month month) {
for (final T firstEnum : T.values()) {
if (month.equals(firstEnum.getMonth())) {
return Arrays.asList(firstEnum.getOtherEnumList());
}
}
return Collections.emptyList();
}
}
Any help is appreciated!