3
\$\begingroup\$

I know exactly how I would handle this in PHP, Python, JS, and AS, but I want to confirm that this is the correct approach to Enums in Java.

I have a database view which has a status column. It will only ever have one of four values (in MySQL, I would have made it an enum type column, but the type doesn't really matter in my current context). Each directly maps to one of the values defined below.

public enum StatusDefinitions {
    NOT_STARTED("not started"), 
    IN_PROGRESS("in progress"), 
    COMPLETE("opted out"), 
    OPTED_OUT("complete");

    private String desc;

    private StatusDefinitions(String desc)
    {
        this.desc = desc.toLowerCase();
    }

    public String getDescription()
    {
        return desc;
    }

    public static StatusDefinitions getStatus( String desc )
    {
        desc = desc.toLowerCase();
        for( StatusDefinitions it: StatusDefinitions.values() )
        {
            if( it.getDescription().equals( desc ) ) return it;
        }
        return StatusDefinitions.NOT_STARTED;
    }
}

Now, in my JSP, I loop for(StatusDefinitions it: StatusDefinitions.values()) and have selected="selected" based on if(modelObjStatus == it). When pulling this from/pushing it to the db, I have status = StatusDefinitions.getStatus( rs.getString( "status" ) );. And there is where I have this sensation that I've missed something. Looping through all of the Strings and comparing them seems like I lose the benefit of the Enum. It would almost seem better to have a series of public static final Strings.

(Before anyone asks, because of the scope of this particular project, it doesn't make sense to make this an ORM.)

\$\endgroup\$
  • \$\begingroup\$ Looks good. I'd probably go with something like ProjectStatus or StatusType. I don't like naming types plural unless each instance holds multiple things. Each instance of an enum is a single value, so ProjectStatus.COMPLETE reads best to me. \$\endgroup\$ – David Harkness Jun 14 '11 at 7:19
6
\$\begingroup\$

To start with, this is not good:

COMPLETE("opted out"), 
OPTED_OUT("complete");

Otherwise, this is pretty standard code. There are two things I'd note:

  1. If desc is coming from the database, you may have a risk of NullPointerException if it comes as null once.
  2. Instead of looping and searching, you can create a map from description to value once and use it each time. Since your set of values is so small, performance difference is probably minimal, so this is question of taste.
\$\endgroup\$
  • \$\begingroup\$ LOL. I forgot that noticed that on the actual code, but I never fixed it in the example. This is the one in the java file: OPTED_OUT("opted out"), COMPLETE("complete"); \$\endgroup\$ – cwallenpoole Jun 13 '11 at 19:20
  • \$\begingroup\$ But I'll definitely give you a 1+ for the spot \$\endgroup\$ – cwallenpoole Jun 13 '11 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.