Learning to use ENUM in Java

1

Good afternoon.

I'm learning how to use enum types in Java and experiencing some problems.

First, I created my enum as follows:

public enum enumUpdateAction {
    NEW(0), CHANGE(1), DELETE(2), DELETE_THRU(3), DELETE_FROM(4);

    public int codigo;

    enumUpdateAction(int codigo) {
        this.codigo = codigo;
    }

    public int getCodigo() {
        return this.codigo;
    }
}

Now, I need to use this enum inside a Switch, in another class:

public static LinkedList<LinkedList<Eventos>> Working(enum_type tipo, enum_updateAction updateAction) {

    switch (Eventos.get(0).getUpdateaction()) {

        case enum_updateAction.NEW://enum_updateAction.NEW: // 0 = new
                ArrayEventos.add(InserirOrdenado(ArrayEventos, Ordem), Ordem);
                break;

    }


}

The Eventos.get(0).getUpdateaction() returns a int . I could use numbers 0 to 2 in cases, but since I know each number represents BID , OFFER or TRADE , I'm trying to use those words directly in switch , with the help of enum . However, I get " enum_updateAction.NEW can not be converted to int ". Can anyone tell me how I get the value number of NEW of enum , and not the word NEW itself?

    
asked by anonymous 24.02.2017 / 18:04

2 answers

3

All enum has a method called ordinal() that gives the position in which it was declared within the class, the first element being ordinal() == 0 . Well, that's exactly what your codigo field does. So you do not need to use this field to reinvent the wheel, just use ordinal() .

public enum EnumUpdateAction {
    NEW, CHANGE, DELETE, DELETE_THRU, DELETE_FROM;
}

In addition, all enum has a values() method that returns an array of the same type as enum containing the elements of that enum in the order in which they are defined. You can use this to translate the values from ordinal() to elements.

In switch , you can use the elements of enum s directly, referencing them by their names. You do not need any code int for this:

public static List<? extends List<Eventos>> working(EnumType tipo, EnumUpdateAction updateAction) {

    switch (EnumUpdateAction.values()[eventos.get(0).getUpdateAction()]) {
        case NEW:
            ArrayEventos.add(inserirOrdenado(arrayEventos, ordem), ordem);
            break;
        case CHANGE:
            // ...
            break;
        case DELETE:
            // ...
            break;
    }
}

I also noticed that if eventos.get(0) returned EnumUpdateAction instead of int , its switch would be simpler:

switch (eventos.get(0)) {

In addition, please obey the language conventions .

And I notice that maybe in your code, what you really wanted was switch (updateAction) { ... } .

See an example with enum in switch :

class EnumComSwitch {
    private static enum MeuEnum {
        NEW, CHANGE, DELETE, BLABLA;
    }

    public static void main(String[] args) {
        MeuEnum elemento = MeuEnum.CHANGE;
        switch (elemento) {
            case NEW:
                System.out.println("Este é o NEW");
                break;
            case CHANGE:
                System.out.println("Este é o CHANGE");
                break;
            case DELETE:
                System.out.println("Este é o DELETE");
                break;
            case BLABLA:
                System.out.println("Este é o BLABLA");
                break;
        }
    }
}
    
24.02.2017 / 19:35
1

First of all I will use another name for enum , considering code convption, which classes should start with a capital letter and _ should be avoided.

There are several ways to make the comparison you want, one of which is to transform the int that represents the enum (consisderando that you can put any code in any order).

public enum Type {
  BID(0), OFFER(1), TRADE(2);

  public final int codigo;

  private Type(int codigo) {
    this.codigo = codigo;
  }

  public int getCodigo() {
    return this.codigo;
  }

  public static Type getType(int codigo) {
    for(Type type : Type.values()) {
      if (type.getCodigo() == codigo) {
        return type;
      }
    }

    return null;
  }
}

So your comparison would look like this:

public static LinkedList<LinkedList<Eventos>> working() {
  Type type = Type.getType(Eventos.get(0).getUpdateaction());

  switch(type) {
      case Type.NEW:
        arrayEventos.add(InserirOrdenado(arrayEventos, ordem), ordem);
        break;
  }
}

Note that I used the Type#get method to transform int from Eventos.get(0).getUpdateaction() to get enum for it.

The second form would be, using the implementation of enum previous, to do the verification as follows:

public static LinkedList<LinkedList<Eventos>> working() {
  switch(Eventos.get(0).getUpdateaction()) {
      case Type.NEW.getCodigo():
        ArrayEventos.add(InserirOrdenado(arrayEventos, ordem), ordem);
        break;
  }
}

If you make sure that the order will actually refer to the int you will use a solution in this answer .

Reference:

Answer to a similar question in StackOverflow: How to retrieve Enum name using the id?

    
24.02.2017 / 18:14