Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use enum with values in Java

Tags:

java

enums

When I try to use enum to store: "=", ">", "<", etc, I have:

    public static enum DataValueModifier {
    EQUAL("="),
    GREATER_THAN(">"),
    GREATER_EUQAL(">="),
    LESS_THAN("<"),
    LESS_EQUAL("<="),
    APPRROXIMATE("~"),
    NOT_DETERMINED("ND");
    private String value;
    private DataValueModifier(String value) {
        this.value = value;
    }
    public String getValue() {
        return value;
    }
}

How do I use it when I try to compare a string to see if it contains a "=" sign, should I do:

if (dataValue.contains(DataValueModifier.EQUAL.getValue())) {
...
}

I understand using enum is the better practice here, but this just looks silly... Thanks,

David

like image 688
David Zhao Avatar asked Feb 17 '12 19:02

David Zhao


People also ask

How do you add a value to an enum in Java?

You cannot create an object of an enum explicitly so, you need to add a parameterized constructor to initialize the value(s). The initialization should be done only once. Therefore, the constructor must be declared private or default. To returns the values of the constants using an instance method(getter).

Can we assign values to enum?

Enum ValuesYou can assign different values to enum member. A change in the default value of an enum member will automatically assign incremental values to the other members sequentially.

How is enum used in Java?

Enums are used when we know all possible values at compile time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time. A Java enumeration is a class type.

Can enum have multiple values?

The Enum constructor can accept multiple values.


2 Answers

If you defined a method boolean containedIn(String str) in your enum and imported your enum values of interest (in this case EQUAL), usage would look like:

if (EQUAL.containedIn(dataValue)) {
...
}
like image 172
Kevin Welker Avatar answered Oct 04 '22 09:10

Kevin Welker


First of all, I'd move the "contains" method (or the equivalent of it) to the enum itself by defining an isModifier method.

public static enum DataValueModifier {

    ...

    public boolean isModifier( String modifierString ) 
    {
       return modifierString != null && value.equals(modifierString);
    }
}

Then, your code looks like this instead:

if (DataValueModifier.EQUAL.isModifier(dataValue)) 
{
  //...
}

But, more importantly, why are you using dataValue instead of the enum in the first place? If you are getting command line input or something or parsing a string equation and then need to figure out the expression I guess I understand. But if you have control of the code then you should just start with the enum and you'll be able to say

if ( dataValueEnum == DataValueModifier.EQUAL ) {
{
  //...
}

I'd also consider adding a static method to the enum that converts a given string to the correct enum value. It's not quite as efficient, perhaps, but unless you really care about efficiency it will make your code much cleaner. So add this method to your enum:

public static DataValueModifier toDataValueModifier( String dataValue ) {
    if( EQUAL.isModifier( dataValue ) {
       return EQUAL;
    } else if( GREATER_THAN.isModifier( dataValue ) {
       return GREATER_THAN;
    } else if...
       // Do this for all possible values
    } else {
       return UNKNOWN;
       // Also, add an UNKNOWN to your list of enum values.
    }
}

The isModifier and the toDataValueModifier methods might add a bit of ugly code to your DataValueModifier enum, but all your other code will look great. You can now do something like this:

DataValueModifier dataValueEnum = DataValueModifier.toDataValueModifier(dataValue);
if (dataValueEnum == DataValueModifier.EQUAL) {
   ...
}

or even

switch( DataValueModifier.toDataValueModifier(dataValue) ) {
    case EQUAL:
        // ...
        break;
    case GREATER_THAN:
        // ...
        break;
    case GREATER_EQUAL:
        // ...
        break;
    // ... define all the cases you want
    case UNKNOWN:
    default:
         // ...
}
like image 41
Jeff Goldberg Avatar answered Oct 04 '22 07:10

Jeff Goldberg