14

I need to make an Enum containing some strings with spaces and their values in int like:

public enum status{
Active(1),
Inactive(2);
}

because I am using it with hibernate and also will convert it to JSON for alpaca js forms.

like:

[{"text": "Inactive", "value":"2"},{"text": "Active", "value":"1"}]

I'm stuck in making enum. how to make such type of enum?

Mohammed Idris
  • 768
  • 2
  • 9
  • 26
Saqib Ahmed
  • 1,240
  • 1
  • 15
  • 25
  • it is not duplicate because i'm using hibernate annotated bean in which it is used so I need key and value with spaces, link you shared is making enum having value as string but its key is not integer it is also string – Saqib Ahmed Nov 11 '16 at 12:05
  • Are you sure that enum is the only way to solve this issue? – Murat Karagöz Nov 11 '16 at 12:25
  • The English word ‘inactive’ is spelled with no space in it. Are you quite positive you cannot live with using the correct spelling? :-) – Ole V.V. Nov 11 '16 at 12:34
  • Thanks but its client requirement and further in future we can face such kind of requirement with such different values. – Saqib Ahmed Nov 11 '16 at 12:36
  • 1
    This really has nothing to do with Hibernate. You **cannot** have spaces in an enum value, that is forbidden by the Java language itself. You will have to find another solution to the use case you didn't describe. – Tunaki Nov 11 '16 at 23:35
  • 2
    You can totally avoid the problem by properly spelling *inactive* – Ryan Ward Valverde Jun 08 '18 at 14:01

3 Answers3

39

You can not put space between strings. Instead of the you can use underscore as follows:

In_Active

You can use this way:

enum Status {

    ACTIVE("Active", 1), IN_ACTIVE("In Active", 2);

    private final String key;
    private final Integer value;

    Status(String key, Integer value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }
    public Integer getValue() {
        return value;
    }
}
Chandana Kumara
  • 2,485
  • 1
  • 22
  • 25
11

You can hold multiple values in one enum and even have getters to handle them. Here is an example I used once (I try to adapt it to your problem):

public enum Status{

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

    private final Integer value;
    private final String text;

    /**
     * A mapping between the integer code and its corresponding text to facilitate lookup by code.
     */
    private static Map<Integer, Status> valueToTextMapping;

    private Status(Integer value, String text){
        this.value = value;
        this.text = text;
    }

    public static Status getStatus(Integer i){
        if(valueToTextMapping == null){
            initMapping();
        }
        return valueToTextMapping.get(i);
    }

    private static void initMapping(){
        valueToTextMapping = new HashMap<>();
        for(Status s : values()){
            valueToTextMapping.put(s.value, s);
        }
    }

    public Integer getValue(){
        return value;
    }

    public String getText(){
        return text;
    }

    @Override
    public String toString(){
        final StringBuilder sb = new StringBuilder();
        sb.append("Status");
        sb.append("{value=").append(value);
        sb.append(", text='").append(text).append('\'')
        sb.append('}');
        return sb.toString();
    }
}

So in your code you can simply use Status.ACTIVE and it will represent an instance of your Enum, that holds value and text the way you want it

GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • Sorry, I am a little too late, didn't see Chandana Kumara's answer – GameDroids Nov 11 '16 at 12:28
  • Great but can you help me out why hibernate auto generated bean having its name as string not its text – Saqib Ahmed Nov 11 '16 at 12:34
  • I am not sure to what sort of auto-generation you are referring to, but there is a difference between `Status.ACTIVE.name()` and `Status.ACTIVE.toString()` and with this implementation even `Status.ACTIVE.getText()` It is possible that the auto-generated code calls the `.name()` method. In this case you should change that and call `.getText()` or change your `toString()` (even better) – GameDroids Nov 11 '16 at 12:43
  • in hibernate we use list function which automatically run the select query and make list of bean class fill with those values. – Saqib Ahmed Nov 11 '16 at 12:45
  • Sorry, that I can't help you any further from here. It may be best if you try it out with this "new" enum implementation and check what Strings you get. It is very likely that the `.toString()` method is used to convert the enum into String - so you might want to override that method. – GameDroids Nov 11 '16 at 12:51
1

You can't put a space in the middle of an identifier.

Check out this link Is it possible to assign numeric value to an enum in Java? for assigning the value to an enum in java.

Community
  • 1
  • 1
Someone
  • 444
  • 3
  • 12