'Allow a method to get either an Enum or its sub Enum
Got the following enumerator:
public enum Movement {
FORWARD;
@AllArgsConstructor
public enum Turn {
RIGHT(90),
LEFT(-90);
@Getter private final int degrees;
}
}
and I got a method that needs to take as parameter an element of type Movement, hence FORWARD, along with its inner enumerator Turn (Turn.RIGHT and Turn.LEFT). Here is the current method that only works with FORWARD:
private Diary.Record buildDiaryRecord(Movement movement) {
return Diary.Record.builder()
.timestamp(Timestamp.from(Instant.now()))
.currentLocation(currentLocation)
.direction(getCompass().getDirection())
.action(movement)
.build();
}
if I try to do this:
public void turn(Movement.Turn turn) {
diary.getRecords().push(buildDiaryRecord(turn));
}
Correctly I got a compilation error.
Hence my question is: how to make this method generic enough to accept Turn as well?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
