Skip to content

Instantly share code, notes, and snippets.

@farneser
Created August 27, 2023 20:23
Show Gist options
  • Save farneser/93450c10624612017e6315905e7999ab to your computer and use it in GitHub Desktop.
Save farneser/93450c10624612017e6315905e7999ab to your computer and use it in GitHub Desktop.
cycled enum with any type of value
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