Java
public enum Fruit {
APPLE(0),
BANANA(1),
PEACH(2),
ORANGE(3);
private final int value;
Fruit(int value) {
this.value = value;
}
@NonNull
public static Fruit find(int value) {
for (Fruit fruit : values()) {
if (fruit.value == value) {
return fruit;
}
}
return APPLE;
}
}
Kotlin 変換直後
enum class Fruit(private val value: Int) {
APPLE(0),
BANANA(1),
PEACH(2),
ORANGE(3);
companion object {
@JvmStatic
fun find(value: Int): Fruit {
for (fruit in values()) {
if (fruit.value == value) {
return fruit
}
}
return APPLE
}
}
}
Kotlin with firstOrNull()
enum class Fruit(private val value: Int) {
APPLE(0),
BANANA(1),
PEACH(2),
ORANGE(3);
companion object {
@JvmStatic
fun find(value: Int): Fruit {
return values().firstOrNull { it.value == value } ?: APPLE
}
}
}
0 件のコメント:
コメントを投稿