Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java enum getter setter

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?

like image 935
GAMA Avatar asked Dec 11 '22 00:12

GAMA


1 Answers

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..

like image 182
Akash Chavda Avatar answered Dec 28 '22 07:12

Akash Chavda