File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Determine if enum belongs in a category Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Determine if enum belongs in a category" Watch "Determine if enum belongs in a category" New topic
Author

Determine if enum belongs in a category

John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
I'm using enum to represent the state / status. Generally, the state / status can be fit into one of the following categories "can execute", "can not execute", or "unknown". I would like to have a function that determine if the enum can not be executed. Is the following a good solution?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

I think it's a better approach to put the flag inside the enum and initialise it with a constructor. That way you avoid the big boolean statement, and it doesn't get more complicated if you add more options. So something like this:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

And even if you would want to check for constants (Matthew's solution is a lot better though), why not compare on instances?
At least you wouldn't be able to make a typo like this.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Vorwald
Ranch Hand

Joined: Sep 26, 2010
Posts: 139
Matt, that is a better approach.
If I have

Is the constructor called with false and true, respectively? I'm not familiar with this approach to calling the constructor.

Rob, that is a good suggestion too. I didn't know how to to use the this variable for an enum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Determine if enum belongs in a category
 
Similar Threads
To set tool tip only for the leaf nodes
Copy a Class
modifying enums
howto avoid this complexity (state pattern?)
Java 5 enum - How to display a different value?