rocky
rocky

Reputation: 831

How to return Enum if matches with given String

I have Enum class as given below

public enum AlgorithmEnum {

    SHA512("RSA", "SHA512", 1), SHA1("RSA", "SHA1", 1), SHA384("RSA", "SHA384", 1);

    private String keyAlgorithm;
    private String hashAlgorithm;
    private Integer key;

    private AlgorithmEnum(String keyAlgorithm, String hashAlgorithm, Integer key) {
        this.keyAlgorithm = keyAlgorithm;
        this.hashAlgorithm = hashAlgorithm;
        this.key = key;
    }

    public String getKeyAlgorithm() {
        return keyAlgorithm;
    }

    public void setKeyAlgorithm(String keyAlgorithm) {
        this.keyAlgorithm = keyAlgorithm;
    }

    public String getHashAlgorithm() {
        return hashAlgorithm;
    }

    public void setHashAlgorithm(String hashAlgorithm) {
        this.hashAlgorithm = hashAlgorithm;
    }

    public Integer getKey() {
        return key;
    }

    public void setKey(Integer key) {
        this.key = key;
    }
}

I need to have method something like below which takes input as string and returns Enum

public AlgorithmEnum getAlgorithm(String algorithm){
        //returns AlgorithmEnum object
    }

I would call above method by passing "SHA512withRSA" as input for getAlgorithm method.

I need help in implementing the getAlgorithm method.

Upvotes: 0

Views: 1169

Answers (4)

Andrea Calin
Andrea Calin

Reputation: 338

I'm leaving you an example of how I did on similar cases, you can easily adapt it to your needs:

private static Map<Integer, YourEnum> valuesById = new HashMap<>();
private static Map<String, YourEnum> valuesByCode = new HashMap<>();

    static {
        Arrays.stream(YourEnum.values()).forEach(value -> valuesById.put(value.reasonId, value));
        Arrays.stream(YourEnum.values()).forEach(value -> valuesByCode.put(value.reasonCode, value));
    }


    public static YourEnum getByReasonId(int endReason) {
        return valuesById.get(endReason);
    }

Upvotes: 0

deHaar
deHaar

Reputation: 18578

You can check if the given String contains a value that matches one of the enum attributes with some if statements:

public AlgorithmEnum getAlgorithm(String algorithm) {
    if (algorithm.contains("SHA1")) {
        return SHA1;
    } else if (algorithm.contains("SHA512")) {
        return SHA512;
    } else if (algorithm.contains("SHA384")) {
        return SHA384;
    } else {
        return null;
    }
}

Please note that this will match Strings like "SHA512withoutRSA", too...

Maybe a method like

public AlgorithmEnum getAlgorithm(String keyAlgorithm, String hashAlgorithm)

would be better. However, you would have to provide two parameters then.

Upvotes: 0

Karan Khanna
Karan Khanna

Reputation: 2147

You can have something like:

public static AlgorithmEnum getAlgorithm(final String algorithm)
        throws IllegalArgumentException
    {
        for (final AlgorithmEnum algorithmEnum : AlgorithmEnum.values())
        {
            if (algorithm.equalsIgnoreCase(String.format("%swith%s", algorithmEnum.getHashAlgorithm(), algorithmEnum.getKeyAlgorithm())))
            {
                return algorithmEnum;
            }
        }
        throw new IllegalArgumentException("Unknown algorithm: " + algorithm);
    }

However, I will not suggest to use this approach. Instead use 2 different arguments instead of a single String.

Upvotes: 1

Nicholas K
Nicholas K

Reputation: 15423

Assuming all string values passed to your method getAlgorithm() end with withRSA you could use the following to fetch the enum values :

public AlgorithmEnum getAlgorithm(String algorithm) {
    return AlgorithmEnum.valueOf(algorithm.substring(0, algorithm.indexOf("withRSA")));
}

Upvotes: 1

Related Questions