Skip to content

announce/enum-or-null

Repository files navigation

enum-or-null

Build Status

Why enum-or-null?

In Kotlin, the valueOf() method throws an IllegalArgumentException if the specified name does not match any of the enum constants defined in the class. This library provides a collection of handy functions to access the constant value safely.

Install

The package is available via maven.pkg.github.com.

With Maven:

<dependency>
  <groupId>io.github.announce</groupId>
  <artifactId>enum-or-null-kt</artifactId>
  <version>1.3.0</version>
</dependency>

With Gradle:

implementation 'io.github.announce:enum-or-null-kt:1.x.x'

Usage

The code typically looks like the following:

class Example {
  enum class Direction(val az: Int) {
    NORTH(0),
    EAST(90),
    SOUTH(180),
    WEST(240)
  }

  fun printAz01(name: String = "EAST") {
    val direction = enumValueOrNull<Direction>(name) ?: Direction.EAST
    println("az01=${direction.az}")
  }

  fun printAz02(name: String = "EAST") {
    val direction = name.toEnumOrNull<Direction>() ?: Direction.EAST
    println("az02=${direction.az}")
  }

  fun printName01(az: Int = 0) {
    val direction = enumValueOrNull<Direction> {
      it.az == az
    } ?: Direction.NORTH
    println("name03=${direction.name}")
  }

  fun printName02(ordinal: Int = 0) {
    val direction = enumValueOrNull<Direction> {
      it.ordinal == ordinal
    } ?: Direction.NORTH
    println("name03=${direction.name}")
  }
}

Pros and Cons

  • Pros: the easy and safe access to the specified value of Enum with less human errors
  • Cons: the computational cost to access the specified value is O(N) where the length of the target Enum constant values is N

Development

Here's how to release the library:

# consider using direnv
./gradlew clean build publish