Get Enum by reverse lookup from the contents

I will introduce what kind of method is available when you want to get the value by reverse lookup from the contents of Enum.

◆ Method 1 --Create a method for acquisition in Enum class

__Enum class __ As an example, create an Enum class with a code value and a name in the member variable.

SolarSystem.java


import java.util.Arrays;


public enum SolarSystem {

    MERCURY("S01", "Mercury"),
    VENUS("S02", "Venus"),
    EARTH("S03", "Earth"),
    MARS("S04", "Mars"),
    JUPITER("S05", "Jupiter"),
    SATURN("S06", "Saturn"),
    URANUS("S07", "Uranus"),
    NEPTUNE("S08", "Neptune");

    /**Code value*/
    private String code;

    /**Japanese name*/
    private String jpName;


    SolarSystem(String code, String jpName) {
        this.code = code;
        this.jpName = jpName;
    }


    /**
     * @return code value
     */
    public String getCode() {
        return code;
    }


    /**
     * @return Japanese name
     */
    public String getJpName() {
        return jpName;
    }


    /**
     *Get Enum from Japanese name (use for statement)
     *
     * @param code
     * @return
     */
    public static SolarSystem getByCode(String code) {
        for (SolarSystem solorSystem : SolarSystem.values()) {
            if (solorSystem.getCode().equals(code)) {
                return solorSystem;
            }
        }
        return null;
    }


    /**
     *Get Enum from Japanese name (using stream)
     *
     * @param code
     * @return
     */
    public static SolarSystem getByCode2(String code) {
        return Arrays.stream(SolarSystem.values())
                .filter(data -> data.getCode().equals(code))
                .findFirst()
                .orElse(null);
    }
}

The getByCode method is the process for performing reverse lookup. (The same process is performed just by rewriting the getByCode2 method with stream)

Example of use

public static void main(String args[]) {

    SolarSystem object = SolarSystem.getByCode("S03");     // object: "EARTH"
    SolarSystem object2 = SolarSystem.getByCode2("S05");   // object2: "JUPITER"
}

◆ Method 2-Create a reverse lookup utility.

You can achieve what you want with the above implementation, but it is not realistic to implement it one by one when there are multiple Enum classes that you want to reverse lookup. Therefore, instead of implementing it individually, we will make it possible to obtain it in the form of a utility.

__ Interface creation __ First, create an interface that makes the target Enum class implementmets.

CodeInterface.java


public interface CodeInterface {

    public String getCode();

    public String getJpName();

}

__Enum class __ Create an Enum that implements the above interface

SolarSystem.java


public enum SolarSystem implements CodeInterface {

    MERCURY("S01", "Mercury"),
    VENUS("S02", "Venus"),
    EARTH("S03", "Earth"),
    MARS("S04", "Mars"),
    JUPITER("S05", "Jupiter"),
    SATURN("S06", "Saturn"),
    URANUS("S07", "Uranus"),
    NEPTUNE("S08", "Neptune");


    /**Code value*/
    private String code;

    /**Japanese name*/
    private String jpName;


    SolarSystem(String code, String jpName) {
        this.code = code;
        this.jpName = jpName;
    }


    /**
     * @return code value
     */
    @Override
    public String getCode() {
        return code;
    }


    /**
     * @return Japanese name
     */
    @Override
    public String getJpName() {
        return jpName;
    }
}

__ Utility class methods __ Create a static method that executes reverse lookup processing.

EnumUtils.java


import java.util.Arrays;

public class EnumUtils {


    /**
     *From the Enum specified in the first argument, the one that matches the code value of the second argument is acquired.
     *
     * @param target Enum class you want to get
     * @param code Code value to search
     * @param <E>Enum class that implements CodeInterface
     * @return
     */
    public static <E extends Enum & CodeInterface> E valueOf(Class<E> target, String code) {

        return Arrays.stream(target.getEnumConstants())
                .filter(data -> data.getCode().equals(code))
                .findFirst()
                .orElse(null);
    }
}

Example of use

python


public static void main(String args[]) {

    SolarSystem object = EnumUtils.valueOf(SolarSystem.class, "S02");   // object: "VENUS"
}

Recommended Posts

Get Enum by reverse lookup from the contents
Enum misconfiguration. For some reason, I can't get data from the column specified by Enum ...
The contents of the data saved by CarrierWave.
Get "2-4, 7, 9" from [4, 7, 9, 2, 3]
Get the value of enum saved in DB by Rails with attribute_before_type_cast
[Java] Get MimeType from the contents of the file with Apathce Tika [Kotlin]
[Reverse lookup] Spring Security (updated from time to time)
◆ Get API created by Spring Boot from React
[Kotlin] Get the argument name of the constructor by reflection
Get the anime name for this term by scraping
Communicate from the outside to the container launched by docker-compose
Reverse Enum constants from strings and values in Java
[Rails] How to get the contents of strong parameters