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
?
An enum can, just like a class, have attributes and methods. The only difference is that enum constants are public, static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces). Why And When To Use Enums?
The enum type has a values () method, which returns an array of all enum constants. This method is useful when you want to loop through the constants of an enum: An enum can, just like a class, have attributes and methods.
values() method can be used to return all values present inside enum. Order is important in enums.By using ordinal() method , each enum constant index can be found, just like array index. valueOf() method returns the enum constant of the specified string value, if exists.
However, we may want to look up an enum value by our label field as well. To do that, we can add a static method: The static valueOfLabel () method iterates the Element values until it finds a match. It returns null if no match is found. Conversely, an exception could be thrown instead of returning null.
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;
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With