added a new method getColorByIndex(int) and next() method to the enum 'Color'

This commit is contained in:
Daniel Grigencha
2024-12-01 19:37:40 +01:00
parent fb6cfaa518
commit 3467dd2f04

View File

@@ -26,16 +26,33 @@ public enum Color {
*/
NONE;
/**
* This method will be used to return a Color enumeration depending on the given index parameter.
*
* @param index as the index of the color inside the values as an Integer.
* @return a Color enumeration.
*/
public static Color getColorByIndex(int index) {
if (index < 0 || index >= values().length) {
throw new IllegalArgumentException("");
}
return values()[index];
}
/**
* This method will be used to calculate the next color inside the sequence.
*
* @return color as a Color Enumeration.
*/
public Color next() {
if (this.next() == NONE) {
return AIRFORCE;
Color[] colors = values();
int nextIndex = (this.ordinal() + 1) % colors.length;
if (colors[nextIndex] == NONE) {
nextIndex = (nextIndex + 1) % colors.length;
}
return values()[(ordinal() + 1) % values().length];
return colors[nextIndex];
}
}