implicit test for enum membership

Is there a way to test implicitly for membership in a enum?I see here:http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.htmlone do so with a for loop:for (Planet p : Planet.values())but don't see an if() example
[261 byte] By [allelopatha] at [2007-11-27 0:16:10]
# 1
Can you give an example? Like what would you think it might be if it existed and how you'd use it?
jverda at 2007-7-11 22:03:44 > top of Java-index,Java Essentials,Java Programming...
# 2
public enum Colors{ RED, GREEN, BLUE}String myColor = "RED"; // or possibly intif ( myColor in Colors.values()){ // myColor is RED, GREEN, or BLUE}
allelopatha at 2007-7-11 22:03:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> public enum Colors

> {

>RED, GREEN, BLUE

>

>

> String myColor = "RED"; // or possibly int

>

> if ( myColor in Colors.values())

> {

>// myColor is RED, GREEN, or BLUE

Ah, I see.

try {

Color color = Color.valueOf("GRAPLE");

// member

}

catch (IllegalArgumentExeption exc) {

// not a member

}

Or just get the list of all of them, convert them to strings, and see if your string is in that list.

jverda at 2007-7-11 22:03:44 > top of Java-index,Java Essentials,Java Programming...
# 4

Cool, thanks, here's what I've got:

/*

* throws exception if not valid member

* just return false if exception is thrown

*

* @param semaphoreValue to test

*

* @return valid flag

*/

public static boolean isValidSemaphore (String semaphoreValue)

{

try

{

SemaphoreValues sv = SemaphoreValues.valueOf(semaphoreValue);

return true;

}

catch (IllegalArgumentException e)

{

// not a member

return false;

}

}

allelopatha at 2007-7-11 22:03:44 > top of Java-index,Java Essentials,Java Programming...
# 5
oops sorry, used my actual code instead of the color example, but its more or less the same.GRAPLE? :)
allelopatha at 2007-7-11 22:03:44 > top of Java-index,Java Essentials,Java Programming...
# 6

> > public enum Colors

> > {

> >RED, GREEN, BLUE

> >

> >

> > String myColor = "RED"; // or possibly int

> >

> > if ( myColor in Colors.values())

> > {

> >// myColor is RED, GREEN, or BLUE

>

> Ah, I see.

>

>

> try {

>Color color = Color.valueOf("GRAPLE");

> // member

> }

> catch (IllegalArgumentExeption exc) {

>// not a member

>

>

>

> Or just get the list of all of them, convert them to

> strings, and see if your string is in that list.

I wonder if that would work if an obfuscator was being used?

(I ask because I know it doesn't work in C#.)

jschella at 2007-7-11 22:03:44 > top of Java-index,Java Essentials,Java Programming...