David Zhao
David Zhao

Reputation: 4504

How to use enum with values in Java

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

Upvotes: 7

Views: 2728

Answers (3)

Jeff Goldberg
Jeff Goldberg

Reputation: 961

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:
         // ...
}

Upvotes: 2

Kevin Welker
Kevin Welker

Reputation: 7937

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)) {
...
}

Upvotes: 4

Paul
Paul

Reputation: 20061

I like to use a static import in these cases.

package mypackage;

public enum DataValueModifier
{
  //your enum code
}

then...

import static mypackage.DataValueModifier.*;

public MyClass
{

  // code blah blah blah

  public void doIt()
  {
    // more code blah blah
    if (dataValue.contains(EQUAL.getValue())) 
    {
      //...
    }
  }
}

It's a little nicer.

Upvotes: 2

Related Questions