diff --git a/src/main/kotlin/net/shadowfacts/cacao/util/EnumHelper.kt b/src/main/kotlin/net/shadowfacts/cacao/util/EnumHelper.kt new file mode 100644 index 0000000..3ed19eb --- /dev/null +++ b/src/main/kotlin/net/shadowfacts/cacao/util/EnumHelper.kt @@ -0,0 +1,20 @@ +package net.shadowfacts.cacao.util + +/** + * @author shadowfacts + */ +object EnumHelper { + + fun > next(value: E): E { + val constants = value.declaringClass.enumConstants + val index = constants.indexOf(value) + 1 + return if (index < constants.size) constants[index] else constants.first() + } + + fun > previous(value: E): E { + val constants = value.declaringClass.enumConstants + val index = constants.indexOf(value) - 1 + return if (index >= 0) constants[index] else constants.last() + } + +} \ No newline at end of file diff --git a/src/test/kotlin/net/shadowfacts/cacao/util/EnumHelperTests.kt b/src/test/kotlin/net/shadowfacts/cacao/util/EnumHelperTests.kt new file mode 100644 index 0000000..b24207d --- /dev/null +++ b/src/test/kotlin/net/shadowfacts/cacao/util/EnumHelperTests.kt @@ -0,0 +1,27 @@ +package net.shadowfacts.cacao.util + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +/** + * @author shadowfacts + */ +class EnumHelperTests { + + enum class MyEnum { + ONE, TWO + } + + @Test + fun testNext() { + assertEquals(MyEnum.TWO, EnumHelper.next(MyEnum.ONE)) + assertEquals(MyEnum.ONE, EnumHelper.next(MyEnum.TWO)) + } + + @Test + fun testPrev() { + assertEquals(MyEnum.ONE, EnumHelper.previous(MyEnum.TWO)) + assertEquals(MyEnum.TWO, EnumHelper.previous(MyEnum.ONE)) + } + +} \ No newline at end of file