Pass Enum value per method parameter

Asked

Viewed 95 times

0

I have a value that stores the status of an element. For example: 1 - sending 2 - Cancelled 3 - Error.

The value that will be saved in the database is the numeric value. I have a method that sets the status, but its parameter is of the numeric type.

The fact is that I didn’t want to have to pass the value on itself, but I wanted to pass using enum.

setStatus (Status.CANCELADA);

public enum Status {
     A_ENVIAR(1L), CANCELADA(2L), ERRO(3L);
    
     private Long numVal;
    
     Status(Long numVal) {
         this.numVal = numVal;
     }
    
     public Long getNumVal() {
         return numVal;
     }
}

But to take the numerical value that each enum represent, I’m having to do Status.CANCELADA.getNumVal().

Is there any way I can get the value "2" without calling the method getNumVal()? I want the value 2 calling only Status.CANCELADA.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

4

Why don’t you use the method ordinal() that all enum has? Stay like this:

class Program {
    public static void main (String[] args) {
        Status status = Status.Cancelada;
        System.out.println(status.ordinal());
    }
}

enum Status {
    None, AEnviar, Cancelada, Erro;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I gave an improved on this code and without using this nonsense that they taught to write everything in Caps (can do it your way too, but it is very ugly, need to create the constructor, then either use ready method or own).

  • 1

    The ordinal is a good solution, but what if he needs each Enum value to actually have a internal value (do not know the terminology of this in Java) predefined, following criteria external to the code? And regarding caps, I agree it’s ugly, but it’s the convention that Oracle itself seems to follow, so it would not be better to follow him?

  • 2

    It doesn’t work there, but the question doesn’t talk about it, it shows a sequential pattern. I think something as ugly as this should not follow, there is no gain in it.

  • @Maniero What has no gain is this imposition of the C crop# about the marginalized Javeira community :D Look at the bullshit rs. Not to prolong the discussion I will just leave the link of the arguments that were centered in this question of the Soen dealing with the subject, who wants to vote there: https://stackoverflow.com/q/3069743/2241463

  • 1

    There is no gain for people to do things without knowing the reason, including the creators of Java. If I used CAPS in C because it was macro not because it was constant, someone from Sun did not understand this and decided that it was for constant and decided that it would import this ugly thing to Java. Luckily the people who take care of Java are now much better and don’t make those mistakes anymore, so people can start avoiding the initial mistake. The only argument for that is that "Sun told us to do it".

  • 1

    @Maniero I spoke more in jest to leave the link, I did not need to give this reversed :D But I will reflect.

1

There’s no way to do that. Because, if it were possible, you could do something like:

Status.CANCELADA == 2L; // ???

And that should be evaluated for what? true? false? Like the Runtime would be able to distinguish when should return the Long (representation of values in the example) or Status (the type of the enumeration in question)?

The previous expression is not even executed - an error is thrown on account of the incompatible types:

Main.java:XX: error: bad operand types for binary operator '=='
    x == 1L;
      ^
  first type:  Status
  second type: long

Therefore, it is ideal to create a way to expose the value of the property. In this case, you are already doing this by the method getNumVal.

  • 1

    Only one correction/clarification, enums were made to compare with enums, using the ==, because they are "singletons" by design. So in case AP is wanting to compare a higher level status of an element with the value Status.CANCELADA can do meuElemento.getStatus() == Status.CANCELADA or as appropriate until creating an API meuElemento.isCancelada() that checks this internally, but has to arrange the conversion from the lowest level brought from the bank (number, Long) to the highest level that is the Enum instance, in the DAO or ORM you are using, for example.

  • Yes, @Piovezan, precisely why I mentioned the part about the mistake of Runtime while trying to compare the Status direct with the internal value (2L, for example). It doesn’t really make sense to compare a enum with something that is not part of the enumeration itself.

  • Ah, I think now I understand what the AP wanted. He wants a setStatus(enum) high level. There within the implementation of this setStatus() is that you should invoke the getNumVal() (or ordinal()) of the Enum instance that has been provided.

0


galley!

I just had to think for a while, I was thinking of an unnecessary solution.

Define in the class itself the constants with their respective values in a public and static way.

public static final int A_ENVIAR = 1;

So I just called

setStatus(ClasseEmQuestao.A_ENVIAR);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.