|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.nrao.sss.util.EnumerationUtility
public class EnumerationUtility
A class that helps clients interact with enumeration classes.
Instances of this class will be used mainly, though not exclusively, by user interface classes. This class helps with setting default values for enumerations and also with restricting the choices of enumeration values available to users.
There are two ways to use this class to help manage enumerations, and these
two ways can be used together. The first is programmatic. The
setDefaultValue(Class, Enum)
method can be used to define a default
enumeration element for an enumeration class. The
addToSelectableSet(Class, Enum)
,
removeFromSelectableSet(Class, Enum)
, and
setSelectableSet(Class, EnumSet)
methods can be used to control
the elements of an enumeration class that may be chosen by a user
(or another object).
The second way to manage enumerations with this class is to use properties files. The name of each properties file is simple-enumeration-class-name.properties. These files must reside on the classpath. This class understands these two properties: default and nonselectable. The first property takes a single value; the second takes a comma-separated list of values. The text of the values must be identical to the name of the element, including capitalization.
Example Properties File:
Name: DistanceUnits.properties
Contents:
default = KILOMETER nonselectable = UNKNOWN, ANGSTROM, NANOMETER, MICROMETER, MILLIMETER,\ CENTIMETER, MILE
CVS Info:
$Revision: 1707 $ |
$Date: 2008-11-14 10:23:59 -0700 (Fri, 14 Nov 2008) $ |
$Author: dharland $ |
Constructor Summary | |
---|---|
EnumerationUtility()
Creates a new enumeration utility. |
Method Summary | ||
---|---|---|
|
addToSelectableSet(Class<E> enumType,
E enumValue)
Add enumValue to the set of selectable elements of class
enumType . |
|
|
enumFromString(Class<E> enumType,
String text)
Returns an enumeration constant from enumType represented by
text . |
|
|
enumFromStringOrDefault(Class<E> enumType,
String text)
Returns an enumeration constant from enumType based on
text . |
|
|
enumToString(E element)
Returns a text representation of element . |
|
|
enumToUpperString(E element)
Returns a text representation of element . |
|
|
getCompleteSetFor(Class<E> enumType)
Returns the complete set of enumeration elements for the given class. |
|
|
getDefaultValueFor(Class<E> enumType)
Returns a default element for the given enumeration type. |
|
|
getRandomValueFor(Class<E> enumType)
Returns a random element for the given enumeration type. |
|
|
getSelectableSetFor(Class<E> enumType)
Returns the set of enumeration elements for the given class from which a specific element may be selected. |
|
static EnumerationUtility |
getSharedInstance()
Returns a pre-made instance of this utility. |
|
void |
reload()
Reloads all internally stored information. |
|
|
reload(Class<E> enumType)
Reloads the internally stored information for the given enumeration type. |
|
|
removeFromSelectableSet(Class<E> enumType,
E enumValue)
Removes enumValue from the set of selectable elements of class
enumType . |
|
|
setDefaultValue(Class<E> enumType,
E enumValue)
Sets the default value for enumType to enumValue . |
|
|
setSelectableSet(Class<E> enumType,
EnumSet<E> selectableSet)
Sets the collection of selectable elements for the given enumeration type. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public EnumerationUtility()
Method Detail |
---|
public static EnumerationUtility getSharedInstance()
public <E extends Enum<E>> EnumSet<E> getCompleteSetFor(Class<E> enumType)
enumType
- an enumeration class.
enumType
.
NullPointerException
- if enumType
is null.public <E extends Enum<E>> EnumSet<E> getSelectableSetFor(Class<E> enumType)
enumType
- an enumeration class.
NullPointerException
- if enumType
is null.public <E extends Enum<E>> E getDefaultValueFor(Class<E> enumType)
enumType
- an enumeration class.
enumType
.
NullPointerException
- if enumType
is null.public <E extends Enum<E>> E getRandomValueFor(Class<E> enumType)
enumType
- an enumeration class.
public <E extends Enum<E>> void setSelectableSet(Class<E> enumType, EnumSet<E> selectableSet)
enumType
- an enumeration class.selectableSet
- a set of selectable elements of type E.
NullPointerException
- if either parameter is null.public <E extends Enum<E>> void addToSelectableSet(Class<E> enumType, E enumValue)
enumValue
to the set of selectable elements of class
enumType
. If enumType
is null, this method
does nothing.
enumType
- an enumeration class.enumValue
- a selectable element.
NullPointerException
- if enumType
is null.public <E extends Enum<E>> void removeFromSelectableSet(Class<E> enumType, E enumValue)
enumValue
from the set of selectable elements of class
enumType
.
enumType
- an enumeration class.enumValue
- an element that is not selectable.
NullPointerException
- if enumType
is null.public <E extends Enum<E>> void setDefaultValue(Class<E> enumType, E enumValue)
enumType
to enumValue
.
enumType
- an enumeration class.enumValue
- a default value for enumType
.public <E extends Enum<E>> void reload(Class<E> enumType)
enumType
- an enumeration class.public void reload()
reload(Class)
for details.
public <E extends Enum<E>> String enumToString(E element)
element
.
The returned string will be in lower case, except that the first letter of
each word will be in upper case. The string is based on the name
of the element (as returned by element.name()
). Any underscores
in the name are replaced with spaces.
The main users of this method will be the enumeration classes themselves.
element
- a member of an enumeration class.
element
. If element
is
null, the empty string ("") is returned.public <E extends Enum<E>> String enumToUpperString(E element)
element
.
The returned string will be in Upper case. The string is based on the name
of the element (as returned by element.name()
). Any underscores
in the name are replaced with spaces.
The main users of this method will be the enumeration classes themselves.
element
- a member of an enumeration class.
element
. If element
is
null, the empty string ("") is returned.public <E extends Enum<E>> E enumFromString(Class<E> enumType, String text)
enumType
represented by
text
.
Leading and trailing whitespace is first stripped from text
.
A case-insensitive comparison against the name
and
toString
methods of each constant in the enumeration is then
performed. If the enumeration class has a getSymbol
method,
it is also used in the comparison. If no match is found, null
is returned.
enumType
- an enumeration class.text
- a text representation of a constant in enumType
.
enumType
.enumFromStringOrDefault(Class, String)
public <E extends Enum<E>> E enumFromStringOrDefault(Class<E> enumType, String text)
enumType
based on
text
.
This method is similar to enumFromString(Class, String)
,
except that if no match is found, a default constant from
enumType
is returned.
If enumType
enumType
- an enumeration class.text
- a text representation of a constant in enumType
.
enumType
.enumFromString(Class, String)
,
getDefaultValueFor(Class)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |