Created
August 27, 2023 20:23
-
-
Save farneser/93450c10624612017e6315905e7999ab to your computer and use it in GitHub Desktop.
cycled enum with any type of value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum CycledEnum { | |
Zero(0), One(1), Two(2), Thre(3), Four(4), Five(5); | |
public CycledEnum next() { | |
if (this == Five) { | |
return Zero; | |
} else { | |
return CycledEnum.values()[this.ordinal() + 1]; | |
} | |
} | |
public final int value; | |
CycledEnum(int value) { | |
this.value = value; | |
} | |
public static void main(String[] args) { | |
var cycledEnum = CycledEnum.Zero; | |
for (var i = 0; i < 15; i++) { | |
System.out.println(cycledEnum.value); | |
cycledEnum = cycledEnum.next(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment