aspose file tools*
The moose likes Beginning Java and the fly likes Enumerated type toString() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Enumerated type toString()" Watch "Enumerated type toString()" New topic
Author

Enumerated type toString()

Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Say you have an enumerated type whose members are basic color names (RED, BLUE, YELLOW, GREEN, etc). You do some bitwise arithmatic on vars of that type, resulting in a value that is constrained by the nature of the operations to always still be a member of that type.

Is there any direct way to translate that new value into the string of the name for that enumerated value, "GREEN", say?

The simplest thing I could think of was to build an array of those strings:
{ "RED", "BLUE", "YELLOW", "GREEN" }
and index into it with the result var.

I suspect that that's about all you can do here. Is that right? Or is there some native way to go from the enumerated value GREEN to the string "GREEN"?

Thanks, as always.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If by "enumerated type" you're just talking about a set of integer constants, then yes, using the constants as indices into an array of Strings is about the best you can do.

But note that JDK 1.5 now has a formal "enum" type, and objects of an enum class have a toString() method which returns the "name" of the value. Check it out here.


[Jess in Action][AskingGoodQuestions]
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Originally posted by Ernest Friedman-Hill:
note that JDK 1.5 now has a formal "enum" type, and objects of an enum class have a toString() method which returns the "name" of the value.

Ah, that's exactly what I was looking for. And foo on me for not checking the docs...

I'm not sure I get the difference between name() and toString(). Is it that for enums built into java, name() won't change from release to release, and toString() might? They'd be equivalent for our own enums? Or am I off base here?
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Arrgghh. Getting tangled up in my shorts here... guess that's why I'm on the beginners list .

The method blendColor() doesn't compile:
Operator | cannot be applied to Color.ColorValue, Color.ColorValue
Seems like you can't do a bit OR on this enumerated type, even though it's actually an int.

I tried these...but both give the error "incompatible types".

If I'm understanding enums right, I could add blendColor() directly as a method of the ColorValue enumerated type, but the typing problem between a ColorValue value and the | operator would still exist.

How can I accomplish this?
[ April 25, 2005: Message edited by: Dave Merrill ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hmmm. Well, let's see. You could have a HashMap that stored all the members of the enum with their ordinal values as their keys. You could create the HashMap by iterating over the enum using the "values()" method. Then the blendColor() method could do something like

int color = ColorNum.ordinal() | c.getColorNum().ordinal();
ColorNum = colorMap.get(color);

(I assumed autoboxing of the argument to "get," and that colorMap was declared as HashMap<Integer, ColorValue>
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
OK, this works, as far as it goes:
The problem isn't in doing the bit math, it's converting the resulting int into the corresponding ColorValue. You can't just cast it like this:
Is there any way to convert an integer value corresponding to a particular enumerated type to that actual enumerated type? Or is java being so safe about typing that if the value ever left the realm of that type, it can never be converted back, just in case it's not a legal value? You can't read an enum value out of a text file, for instance?
[ April 25, 2005: Message edited by: Dave Merrill ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Well, as I said in my last post, you have to set up a Map that has the ordinals as the keys, and the enum members as the values. Then you can look up the ColorValues by ordinal value in this map.
Creating the map would be as simple as

Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Originally posted by Ernest Friedman-Hill:
you have to set up a Map that has the ordinals as the keys, and the enum members as the values. Then you can look up the ColorValues by ordinal value in this map.

So there's no direct language feature to handle this? You can get to an enum member from the string of its name, but not from its actual value?

Not that I don't believe you, it just seems like an odd omission.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't believe there is, no.
Dave Merrill
Greenhorn

Joined: Apr 16, 2005
Posts: 25
Seems like I should be able to create a decendant class of enum, with some implementation of fromValue(), or something like that.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You could put the Map lookup code I showed you into a static method in the enum class itself -- enums can have regular methods like any other class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enumerated type toString()