public abstract class Enumerator extends Object
toString()
and
fromString(String, Class)
method, which map names to values and vice versa.
To use this class, derive from it and define one or more
public static final
fields, as follows:
public final class Suit extends Enumerator { // Exactly N instances of "Suit" exist to represent the N possible values. public static final Suit CLUBS = new Suit("clubs"); public static final Suit DIAMONDS = new Suit("diamonds"); public static final Suit HEARTS = new Suit("hearts"); public static final Suit SPADES = new Suit("spades"); // Optional, if you want to use EumeratorSet arithmetics. public static final EnumeratorSet NONE = new EnumeratorSet(Suit.class ).setName("none"); public static final EnumeratorSet ALL = new EnumeratorSet(Suit.class, true).setName("all"); // These MUST be declared exactly like this: private Suit(String name) { super(name); } public static Suit fromString(String name) throws EnumeratorFormatException { return (Suit) Enumerator.fromString(name, Suit.class); } }
Modifier | Constructor and Description |
---|---|
protected |
Enumerator(String name)
Initialize the enumerator to the given value.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object that)
Equality is reference identity.
|
protected static Enumerator |
fromString(String name,
Class enumeratorClass)
Initialize an
Enumerator from a string. |
int |
hashCode()
Enforce
Object 's notion of Object.hashCode() . |
String |
toString()
Returns the
name passed to Enumerator(String) . |
protected Enumerator(String name)
public final boolean equals(Object that)
public final int hashCode()
Object
's notion of Object.hashCode()
.protected static final Enumerator fromString(String name, Class enumeratorClass) throws EnumeratorFormatException
Enumerator
from a string.
The given string is converted into a value by looking at all instances of the given type created so far.
Derived classes should invoke this method as follows:
public class Suit extends Enumerator { ... public static Suit fromString(String name) throws EnumeratorFormatException { return (Suit) Enumerator.fromString(name, Suit.class); } }
EnumeratorFormatException
- if the string cannot be identifiedpublic String toString()
name
passed to Enumerator(String)
.Copyright © 2001–2016. All rights reserved.