0

In our code there are 10's of enum classes with same template code pasted each and every time when ever a new enum class needs to be created.

Need a way to generic way to avoid duplication of this code.

Below are couple of same enums classes

Class-1

 public enum AccountStatus {

ACTIVE("ACTIVE", 1), 
INACTIVE("INACTIVE", 2);

private final int value;

private final String key;

AccountStatus(String key, int value) {
    this.value = value;
    this.key = key;
}

public int getValue() {
    return this.value;
}

public String getKey() {
    return this.key;
}

@Override
public String toString() {
    return String.valueOf(this.value);
}

@JsonCreator
public static AccountStatus create(String key) {
    if (key == null) {
        throw new IllegalArgumentException();
    }
    for (AccountStatus v : values()) {
        if (v.getKey().equalsIgnoreCase(key)) {
            return v;
        }
    }
    throw new IllegalArgumentException();
}

public static AccountStatus fromValue(Integer value) {

    for (AccountStatus type : AccountStatus.values()) {
        if (value == type.getValue()) {
            return type;
        }
    }
    throw new IllegalArgumentException("Invalid enum type supplied");
}

public static AccountStatus fromValue(String key) {

    for (AccountStatus type : AccountStatus.values()) {
        if (type.getKey().equalsIgnoreCase(key)) {
            return type;
        }
    }
    throw new IllegalArgumentException("Invalid enum type supplied");
}
 }

Class-2

      public enum Gender {

MALE("MALE", 1), 
FEMALE("FEMALE", 2);

private final int value;

private final String key;

Gender(String key, int value) {
    this.value = value;
    this.key = key;
}

public int getValue() {
    return this.value;
}

public String getKey() {
    return this.key;
}

@Override
public String toString() {
    return String.valueOf(this.value);
}

@JsonCreator
public static Gender create(String key) {
    if (key == null) {
        throw new IllegalArgumentException();
    }
    for (Gender v : values()) {
        if (v.getKey().equalsIgnoreCase(key)) {
            return v;
        }
    }
    throw new IllegalArgumentException();
}

public static Gender fromValue(Integer value) {

    for (Gender type : Gender.values()) {
        if (value == type.getValue()) {
            return type;
        }
    }
    throw new IllegalArgumentException("Invalid enum type supplied");
}

public static Gender fromValue(String gender) {

    for (Gender type : Gender.values()) {
        if (type.getKey().equalsIgnoreCase(gender)) {
            return type;
        }
    }
    throw new IllegalArgumentException("Invalid enum type supplied");
}

}

Need a generic a solution which handles all the methods in class.

3
  • 1
    i mean key is just name() and value is ordinal() + 1 so eh Commented May 3, 2017 at 20:29
  • stackoverflow.com/questions/15450897/… Commented May 3, 2017 at 20:53
  • Although, I don't know if this is recommended. Commented May 3, 2017 at 20:53

1 Answer 1

3

Two options that I can see, and both involve interfaces. In either case though, you will still need to make the constructor for your enum and declare the fields.

  1. Implement an interface on your enum for the getValue and getKey methods, and then move the create and fromValue methods to a utility class that takes the enum class as a parameter. e.g.

EnumUtility.create(AccountStatus.class, "ACTIVE");

  1. Use JDK8 interfaces to declare default method implementations that will be inherited into your enum.
Sign up to request clarification or add additional context in comments.

2 Comments

@Shoaib627 The answer here has an example that is nearish to some of what you want to do: stackoverflow.com/a/22102121/1074041
Can u have a look at his : stackoverflow.com/questions/43895415/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.