[Java] [Kotlin] Generically call valueOf and values of Enum

The methods / member functions valueOf and values that are implicitly declared in the Enum derived class How to call generically.

Java

Suppose the following enum type is defined.

public enum PrimaryColor {
    RED, GREEN, BLUE
}

Call when the concrete type is known

public static void main(String... args) {
    PrimaryColor red = PrimaryColor.valueOf("RED"); // RED
    PrimaryColor[] values = PrimaryColor.values(); // [RED, GREEN, BLUE]
}

Generic call

public static <E extends Enum<E>> E valueOf(Class<E> enumType, String name) {
    return Enum.valueOf(enumType, name);
}

public static <E extends Enum<E>> E[] values(Class<E> enumType) {
    return enumType.getEnumConstants();
}

public static void main(String... args) {
    PrimaryColor red = valueOf(PrimaryColor.class, "RED"); // RED
    PrimaryColor[] values = values(PrimaryColor.class); // [RED, GREEN, BLUE]
}

Kotlin

Suppose the following enum type is defined.

enum class PrimaryColor {
    RED, GREEN, BLUE
}

Call when the concrete type is known

fun main() {
    val red: PrimaryColor = PrimaryColor.valueOf("RED") // RED
    val values: Array<PrimaryColor> = PrimaryColor.values() // [RED, GREEN, BLUE]
}

Generic call

inline fun <reified E : Enum<E>> valueOf(name: String): E =
    enumValueOf<E>(name) // `<E>`Can be omitted (type inference) in this case

inline fun <reified E : Enum<E>> values(): Array<E> =
    enumValues<E>() // `<E>`Can be omitted (type inference) in this case

fun main() {
    val red: PrimaryColor = valueOf("RED")
    val values: Array<PrimaryColor> = values()
}

/that's all

Recommended Posts

[Java] [Kotlin] Generically call valueOf and values of Enum
Management of partitioned values and their names Enum (Java) vs DB
[Java] Dynamic method call by reflection of enum (enum)
Please note the division (division) of java kotlin Int and Int
Java language from the perspective of Kotlin and C #
Write a class in Kotlin and call it in Java
Advantages and disadvantages of Java
I tried to summarize the basics of kotlin and java
[Kotlin] Get Java Constructor / Method from KFunction and call it
Differences between "beginner" Java and Kotlin
About fastqc of Biocontainers and Java
Java arguments, return values and overloads
[Java] Judgment of identity and equivalence
Generics of Kotlin for Java developers
About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
Conversion between Kotlin nullable and Java Optional
Relationship between kotlin and java access modifiers
After 3 months of Java and Spring training
[For beginners] Difference between Java and Kotlin
[Java] Inheritance and structure of HttpServlet class
A Java engineer compared Swift, Kotlin, and Java.
[Java / Swift] Comparison of Java Interface and Swift Protocol
Summary of Java Math.random and import (Calendar)
[Java] Contents of Collection interface and List interface
Basics of java basics ② ~ if statement and switch statement ~
Acquisition of JSON data and rotation of values
Discrimination of Enums in Java 7 and above
[Java] enum
Kotlin functions and lambdas to send to Java developers
[Ruby] Arguments with keywords and default values of arguments
Java switch statement and break, Kotlin when expression ...
[Java] Personal summary of classes and methods (basic)
[Java] The confusing part of String and StringBuilder
I compared the characteristics of Java and .NET
JAVA: Realizes generation and scanning of various barcodes
Basics of threads and Callable in Java [Beginner]
About call timing and arguments of addToBackStack method
[Java] Classification memo of compilation error and run-time error
[Java / Kotlin] Resize considering the orientation of the image
Java enables extraction of PDF text and images
[Java] Convert and import file values with OpenCSV
Find the maximum and minimum values out of the 5 numbers entered in Java (correction ver)