Add EnumHelper
This commit is contained in:
parent
4070baaa63
commit
b48b72d5bb
|
@ -0,0 +1,20 @@
|
|||
package net.shadowfacts.cacao.util
|
||||
|
||||
/**
|
||||
* @author shadowfacts
|
||||
*/
|
||||
object EnumHelper {
|
||||
|
||||
fun <E: Enum<E>> 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 <E: Enum<E>> previous(value: E): E {
|
||||
val constants = value.declaringClass.enumConstants
|
||||
val index = constants.indexOf(value) - 1
|
||||
return if (index >= 0) constants[index] else constants.last()
|
||||
}
|
||||
|
||||
}
|
|
@ -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))
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue