java enum getter setter
Solution 1
You can create enum like this
public enum EventStatus {
PENDING(1), OPEN(2), DISPATCHED(3), APPENDED(4), CLOSED(5), REQUESTED_TO_CLOSE(
6), ACTION_REQUESTED_FROM_POLICE_STATION(7), ACTION_REQUESTED_FROMD_ISPATCHER(
8), ACTION_REQUESTED_FROM_SUPERVISOR(9);
private int value;
private EventStatus(int value) {
this.value = value;
}
public int getValue() {
return value;
}
//Just for testing from some SO answers, but no use
public void setValue(int value) {
this.value = value;
}
public static EventStatus getEventStatusById(int id) {
EventStatus event = null;
switch (id) {
case 1:
event = PENDING;
break;
case 2:
event = OPEN;
break;
case 3:
event = DISPATCHED;
break;
case 4:
event = APPENDED;
break;
case 5:
event = CLOSED;
break;
case 6:
event = REQUESTED_TO_CLOSE;
break;
case 7:
event = ACTION_REQUESTED_FROM_POLICE_STATION;
break;
case 8:
event = ACTION_REQUESTED_FROMD_ISPATCHER;
break;
case 9:
event = ACTION_REQUESTED_FROM_SUPERVISOR;
break;
default:
break;
}
return event;
}
}
after you can try below line to set EventStatus
event.setEventStatus(EventStatus.getEventStatusById(getAttributeValueInt(linkedEventElement, "status")));
I think its useful..
Solution 2
Create a mapping in enum itself from value to enum constant. And then a static
method, which takes an int
type and returns the enum
type.
public enum EventStatus {
// constants
;
private final static Map<Integer, EventStatus> REVERSE_MAP = new HashMap<>();
static {
for (EventStatus status: values()) {
REVERSE_MAP.put(status.value, status);
}
}
public static EventStatus forValue(int value) {
return REVERSE_MAP.get(value);
}
}
Solution 3
An enum is a class. It's not a simple alias to an int as it is in C for example. 1 is an integer, not an instance of EventStatus, and your method expects an instance of EventStatus, so that doesn't compile.
Just provide a factory method in the enum to transform an int value to the enum:
public static EventStatus fromIntValue(int value) {
// iterate through the enum constants, returned by EventStatus.value(),
// and find the one with the given value
}
You could also store the enum constants in a Map<Integer, EventStatus>
to make the lookup O(1).
Solution 4
Quick solution, since your ordinals (Enum indexes) match your values (with a difference of 1), modify your Enum EventStatus
to include new array of EventStatus
:-
public enum EventStatus {
PENDING(1), OPEN(2), DISPATCHED(3), APPENDED(4), CLOSED(5), REQUESTED_TO_CLOSE(
6), ACTION_REQUESTED_FROM_POLICE_STATION(7), ACTION_REQUESTED_FROMD_ISPATCHER(
8), ACTION_REQUESTED_FROM_SUPERVISOR(9);
private int value;
public static final EventStatus values[] = values();
private EventStatus(int value) {
this.value = value;
}
public int getValue() {
return value;
}
//Just for testing from some SO answers, but no use
public void setValue(int value) {
this.value = value;
}
}
Now instead of this:-
event.setEventStatus(getAttributeValueInt(linkedEventElement, "status"));
Use this:-
event.setEventStatus(EventStatus.values[(getAttributeValueInt(linkedEventElement,"status"))-1)]);
This will return Enum value for corresponding ordinal value, For example:-
EventStatus.values[1-1]; //this will return EnumStatus.PENDING
-1
is because you have provided your ordinal values starting from 1.
Comments
-
GAMA almost 2 years
I'm consuming data from web service, will be storing that data into data holder classes and then get that data at some other place.
For a particular field, most suitable data type is enum and hence I've created following enum:
public enum EventStatus { PENDING(1), OPEN(2), DISPATCHED(3), APPENDED(4), CLOSED(5), REQUESTED_TO_CLOSE( 6), ACTION_REQUESTED_FROM_POLICE_STATION(7), ACTION_REQUESTED_FROMD_ISPATCHER( 8), ACTION_REQUESTED_FROM_SUPERVISOR(9); private int value; private EventStatus(int value) { this.value = value; } public int getValue() { return value; } //Just for testing from some SO answers, but no use public void setValue(int value) { this.value = value; } }
This enum is used within another class as follows:
public EventStatus getEventStatus() { return eventStatus; } public void setEventStatus(EventStatus eventStatus) { this.eventStatus = eventStatus; }
Now when I try to set value like following:
event.setEventStatus(getAttributeValueInt(linkedEventElement, "status"));
which equivalents to
event.setEventStatus(1);
I'm getting compilation error that method is not applicable for arguments(int)
One way I can do this is by something like this:
switch(getAttributeValueInt(linkedEventElement, "status")){ case 1:eventLinkedEvent.setEventStatus(EventStatus.PENDING); //and so on... }
But this ruins the sole purpose of enum.
Also how to get event status value in integer form?
Can anyone please guide me how to go ahead?