7

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

5
  • you should compare it like DataValueModifier.EQUAL.getValue().equals(dataValue) and the Enums are better for something like days of week (`Day.MONDAY, Day.TUESDAY and on) and doing a switch comparison. Commented Feb 17, 2012 at 19:17
  • looks ok to me - I'd set it up like Paul says below but otherwise it's clear and doesn't look all that silly... Commented Feb 17, 2012 at 19:21
  • I assume dataValue is a String? If so, you should be able to override DataValueModifier.toString() and just do this: dataValue.contains(DataValueModifier.EQUAL) If you also do static imports like Paul suggested, it will turn into dataValue.contains(EQUAL) Commented Feb 17, 2012 at 19:26
  • If your enum is inside another class you then it is implicitly static, see the Java Language Specification 8.9. Commented Feb 17, 2012 at 20:37
  • thank you all for your great answers, all are very useful, I chose Jeff's since it's a complete answer. Commented Feb 18, 2012 at 0:40

3 Answers 3

4

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)) {
...
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

I am parsing strings in this case, but isModifier method is not checking "contains" in your code
2

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.

1 Comment

You'll have to use the enum name then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.