From: Roedy Green on
On Mon, 08 Jun 2009 23:55:08 -0400, Lew <noone(a)lewscanon.com> wrote,
quoted or indirectly quoted someone who said :

>I need a mechanism to map string to enum.

see the built-in valueOf function. I often override it to make it
case insensitive and allow aliases.

See http://mindprod.com/jgloss/enum.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, �How would I develop if it were my money?� I�m amazed how many theoretical arguments evaporate when faced with this question.
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
From: Lew on
Lew quoted or indirectly quoted someone who said :
>>I need a mechanism to map string to enum.

Why do you not attribute the quote to the one who actually wrote it?

Roedy Green wrote:
> see the built-in valueOf function.  I often override it to make it
> case insensitive and allow aliases.

I doubt that very much. Static methods cannot be overridden.

While there is some value in hiding the 'valueOf' method, I prefer to
use a differently-named method for the same purpose. I am loathe to
(apparently) change the behavior of such a standard, language-defined
method as 'Enum.valueOf'. Instead I use a static 'fromString()'
method to be the companion for 'toString'.

The 'Enum' Javadocs suggest that '[a]n enum type should override
[toString] when a more "programmer-friendly" string form exists.' I
find it mnemonic and symmetrical to go from "programmer-friendly"
string form to enum constant with 'fromString'. This also preserves
the semantics of 'valueOf' as documented in the JLS. Furthermore, I
use 'valueOf' as the fallback if 'fromString' is otherwise unable to
locate the enum constant.

My enum template is:

/* ${name}.java
*/
package ${package};

/**
* ${name}.
*/
public enum ${name}
{

private final String repr; // "friendly" representation
/**
* Constructor.
* @param rep String representation of enum value.
*/
${name}( String rep )
{
this.repr = rep;
}

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

/**
* Look up enum constant from String representation.
* @param rep String representation of enum value.
* @return ${name} constant matching the representation.
*/
public static ${name} fromString( final String rep )
{
if ( rep == null )
{
return null;
}
for ( ${name} val : values() )
{
if ( rep.equals( val.toString() ) )
{
return val;
}
}
return valueOf( rep );
}
}

--
Lew

From: Lew on
Roedy Green wrote:
>> see the built-in valueOf function.  I often override it to make it
>> case insensitive and allow aliases.
>

Lew wrote:
> I doubt that very much.  Static methods cannot be overridden.

Then I have this, "Hey! Waaaaiiit a minute!" moment. You can't even
declare a 'valueOf()' method in an enum:

JLS 8.9:
"enum type declarations ... cannot contain methods that conflict with
the automatically generated methods (values() and valueOf(String))"

--
Lew
From: Roedy Green on
On Tue, 23 Mar 2010 07:15:37 -0700 (PDT), Lew <lew(a)lewscanon.com>
wrote, quoted or indirectly quoted someone who said :

>I doubt that very much. Static methods cannot be overridden.

Quite right. I call the method something like valueOfAlias. There is
an example at http://mindprod.com/jgloss/enum.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

If you tell a computer the same fact in more than one place, unless you have an automated mechanism to ensure they stay in sync, the versions of the fact will eventually get out of sync.
 | 
Pages: 1
Prev: InetAddress.isReachable
Next: Java in Java