3

I have looked this link : Convert from enum ordinal to enum type

and tried to get the enum value. But is not working. My enum class is :

public enum OrderStatus {

    OPEN(0),
    DELIVERED(1),
    CANCELLED(3),
    PARTIALLY(4)
}

I will pass the values 0,1,3,4 where 2 is missing , so it has no such order. How to get enum by passing 0,1,3 or 4 in groovy or java.

Community
  • 1
  • 1
SudeepShakya
  • 571
  • 3
  • 14
  • 34

3 Answers3

2

Add a field to the enum, and a constructor:

public enum OrderStatus {
    private Integer codice;

    public Integer getCodice() {
        return codice;
    }

    private OrderStatus(Integer codice) {
        this.codice = codice;
    }

    OPEN(0),
    DELIVERED(1),
    CANCELLED(3),
    PARTIALLY(4)
}

and then you can define a method like this:

public static OrderStatus getByCodice(int codice) {
    for (OrderStatus tipo : values()) {
        if (tipo.codice == codice) {
            return tipo;
        }
    }
    throw new IllegalArgumentException("Invalid codice: " + codice);
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Shine
  • 3,788
  • 1
  • 36
  • 59
1

Record the value in the enum and build a Map to convert.

public enum OrderStatus {

    OPEN(0),
    DELIVERED(1),
    CANCELLED(3),
    PARTIALLY(4);
    final int ordinal;

    private OrderStatus(int ordinal) {
        this.ordinal = ordinal;
    }

    static Map<Integer, OrderStatus> lookup = null;

    public static OrderStatus lookup(int ordinal) {
        // Could just run through the array of values but I will us a Map.
        if (lookup == null) {
            // Late construction - not thread-safe.
            lookup = Arrays.stream(OrderStatus.values())
                    .collect(Collectors.toMap(s -> s.ordinal, s -> s));
        }
        return lookup.get(ordinal);
    }
}

public void test() {
    for (int i = 0; i < 5; i++) {
        System.out.println(i + " -> " + OrderStatus.lookup(i));
    }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
  • 1
    Would you recommend using Map for an enum with 5 constants ? – SacJn Sep 24 '15 at 10:46
  • 1
    If you need to look up very often then yes! The difference between `O(1)` and `O(n)` can be very significant, even for small `n` if the algorithm is used often. Remember that new statuses can be added later too. – OldCurmudgeon Sep 24 '15 at 10:57
  • The `O(1)` and `O(n)` only tells you how the performance *scales* with large *n*s, i.e. what will happen if it changes from one million to two millions. For a single concrete number, it doesn’t say anything, especially for numbers as small as `5`. In fact, an `O(n)` algorithm can turn out to be way faster than the `O(1)` alternative. But here, a much simpler solution is possible anyway. Just create an array or list containing the values at their appropriate position (leaving `null` at index `2`) and use the lookup `int` as index… – Holger Sep 25 '15 at 14:09
  • 1
    @OldCurmudgeon: We can also use `Function.identity` in place of `s -> s` as that is more clear IMO. – akhil_mittal Oct 21 '15 at 05:35
0

Just declare a field inside enum as you do in class. And provide a getter method for the field:

public enum OrderStatus 
{
    OPEN(0),
    DELIVERED(1), /*pass value*/
    CANCELLED(3),
    PARTIALLY(4);

    private int value; /*Add a field*/

    OrderStatus ( int value )
    {
        this.value = value;
    }

    /*Access with getter*/      
    int getValue ( )
    {
        return value;
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
SacJn
  • 777
  • 1
  • 6
  • 16