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 Java enums, space, and display values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java enums, space, and display values" Watch "Java enums, space, and display values" New topic
Author

Java enums, space, and display values

Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
So I've set up some enums in my POJO, and I'm using them in the output. For example, I have the following.



It's against convention, but it works. However, I've run into the problem with using enums with spaces. For example, how do I handle "Great Dane"? I can't put it in the list because it has a space. And if I put it in as Great_Dane, now my output is messed up.

Is there a way to have enum and display names? I've done some searching, but noone seems to talk about handling spaces with enums.

TIA.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Here is a good article on enums. You should read it to get a full appreciation of their use.

Enum constants are normal Java identifiers, like class or variable names so they can not have spaces in them. But that doesn't mean all is lost. You can build your own display name/toString method.

A couple of the things you can do with enums, since they are classes is:
1) Create a constructor
2) Define members like variables and methods
3) Override some of the built-in enum methods and Object methods.

Taking advantage of these you could do something like this:


Steve
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Is there a way to break that out into a separate class? I'm doing this with multiple objects, and I'd prefer to put the display logic in just one class. And it seems you can't subclass enum.

Basically, I'd like to create a DisplayEnum class containing all of the overriden methods. Then instead of declaring things as enum in my code, I could declare it as DisplayEnum without having to constantly repeat all of the overrides.

Thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Break what out into a separate class?

The enum? Yes, just make the enum a top level class definition instead of a class.

The display name? Probably. But I don't understand the reasons. I think a display name member is a very consistent part of the enum and is properly encapsulated the way it is. It isn't like the toString method ties the enum to be used with the command line at all, it just provides a well formatted name, and how you display that name is entirely up to you.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
And it seems you can't subclass enum.


Nope, because enums have only private constructors, and you can't subclass a class that has only private constructors.

John.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Steve Luke wrote:Break what out into a separate class?

The enum? Yes, just make the enum a top level class definition instead of a class.

The display name? Probably. But I don't understand the reasons. I think a display name member is a very consistent part of the enum and is properly encapsulated the way it is. It isn't like the toString method ties the enum to be used with the command line at all, it just provides a well formatted name, and how you display that name is entirely up to you.


I know I can break the enum out into a class. However, then I need to duplicate the logic in each enum that I want the display name in. Dogs, Cats, Rodents, etc. What I'd like to do is make a class that has all the display name logic in it, and then use it in my code instead of enum.

Like this:


Does that make sense?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Oh, I see. No, you can't declare a base enum type. You might be able to do it using a Utility class or aggregation. But like I said, I think the displayName is well encapsulated inside the Breed itself.



The benefit of aggregation would be the ability to modify the Helper class without modifying the enum class, to switch to different / extend helpers etc... But it seems like a lot of work to isolate the displayName
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Bai:

Sorry, I don't think that will work with real enums. Why don't you create an interface like this:

Then, have your enums implement that interface:

You can then access your enums like this:

John.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Steve Luke wrote:Oh, I see. No, you can't declare a base enum type. You might be able to do it using a Utility class or aggregation. But like I said, I think the displayName is well encapsulated inside the Breed itself.

The benefit of aggregation would be the ability to modify the Helper class without modifying the enum class, to switch to different / extend helpers etc... But it seems like a lot of work to isolate the displayName


Yeah, right now I'm looking at doing something similar to your utility suggestion. However, I was hoping for a better method. I may end up combining an enum with a HashMap.

I'm not quite sure what you mean by displayName being well encapsulated inside the Breed itself? As I said, what happens when you want to do Cat breeds instead of Dog breeds? Or in addition to?
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
John de Michele wrote:Bai:

Sorry, I don't think that will work with real enums. Why don't you create an interface like this:

Then, have your enums implement that interface:

You can then access your enums like this:

John.


That's kind of what I'd like to do, but I want to have all of the logic in one place instead of having to copy it for each enum subclass.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
I'm not quite sure what you mean by displayName being well encapsulated inside the Breed itself? As I said, what happens when you want to do Cat breeds instead of Dog breeds? Or in addition to?


Well, the solution that I suggested will handle that case, since if you do it the way that I suggested, you can access them as DisplayableEnums.

That's kind of what I'd like to do, but I want to have all of the logic in one place instead of having to copy it for each enum subclass.


Unfortunately, it won't work that way, at least if you use enums. Implementing a single-method interface doesn't seem like too much work to me.

John.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
John de Michele wrote:Well, the solution that I suggested will handle that case, since if you do it the way that I suggested, you can access them as DisplayableEnums.


-nods- But Steve's didn't, and that's what I was confused about.

John de Michele wrote:Unfortunately, it won't work that way, at least if you use enums. Implementing a single-method interface doesn't seem like too much work to me.

John.


Correct, but you're not really consolidating things much. You still have to define a constructor and override for each method.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You might consider the difference between coding something for design reasons versus 'ease of use'. It seems trying to inherit from a common parent class for displayName purposes leans more towards sacrificing clarity in inheritance for a little bit of convenience. For convenience in coding, rely on your IDE. You could easily turn the boilerplate code which defines the displayName and the toString method into a Code Template and have your IDE generate the internals, leaving you to define the constants.

For example in Eclipse, I can generate Templates using Window -> Show View -> Template then Right-Click in the Template frame and use New... I added this Template with the name DisplayableEnum:


Then I can create a new enum (say Cats), define the constants, then insert the Template. The result would be something like this:


I use Eclipse here as an example, but other 'heavy' IDEs should give you the same functionality (perhaps in a slightly different way). The point is that you can use your IDE to make the boilerplate easier.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Sorry, kind of bombarding the Thread here, and getting staggered reads...

Bai Shen wrote:
John de Michele wrote:Well, the solution that I suggested will handle that case, since if you do it the way that I suggested, you can access them as DisplayableEnums.

-nods- But Steve's didn't, and that's what I was confused about.

What I meant was that having the Enum define its own display is simple and clean, while having some other object define how the Enum should be displayed is not so clean (I don;t like the Utility method or Helper method much, and I would bet I would hate the HashMap solution). You can do it via an interface method like John has or through a method inherited from Object, it doesn't make a lot of difference I don't think (except using the toString method gives you the result for free - other sources don't need to know the Enum is 'displayable' or not, depends on your needs).

Note that the IDE Code Template can be made to work with the interface method as well.
Pintea Haiducu
Greenhorn

Joined: Jan 06, 2010
Posts: 2
Bai Shen - I have the exact complaint, that enum class functionality cannot be reused because it does not allow inheritance.

My solution is to use a single enum class for all your subtypes, if these subtypes share common functionality:

public enum AnimalBreeds {
DOG_TERRIER("Terrier"), DOG_GREAT_DANE("Great Dane"),
CAT_TABBY("Tabby", CAT_RUSSIAN_BLUE("Russian Blue");

private final String displayName;

AnimalBreeds (final String display)
{
this.displayName = display;
}

@Override public String toString()
{
return this.displayName;
}
}

Then you can instantiate these constants from any other class.

It's a mixture between enum and class features. It lacks some of the benefits of enums (like defining a single type of data), but maybe you don't need that for your purpose. I used this approach a few times in my code.
Atul Soman
Greenhorn

Joined: Feb 10, 2010
Posts: 1
Just a different thought.
If you want to use switch statement for strings containing spaces, try following.

public enum MyEnum {
SUCCESS, FAILURE;
public static MyEnum value(String str){
if(str.equals("S UCC ESS")){
return SUCCESS;
}
return valueOf(str);
}
}

And in the program

String str = "S UCC ESS";
MyEnum en = MyEnum.value(str);
switch(en){
case SUCCESS:
System.out.println("Print success");
.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java enums, space, and display values