Class Constants
asXXXX
methods of this class
allow these constant values to be accessed via their string names.
Consider class Foo containing public final static int CONSTANT1 = 66;
An instance of this class wrapping Foo.class
will return the constant value
of 66 from its asNumber
method given the argument "CONSTANT1"
.
This class is ideal for use in PropertyEditors, enabling them to recognize the same names as the constants themselves, and freeing them from maintaining their own mapping.
- Since:
- 16.03.2003
- Author:
- Rod Johnson, Juergen Hoeller
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Exception thrown when theConstants
class is asked for an invalid constant name. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturn a constant value cast to a Number.Parse the given String (upper or lower case accepted) and return the appropriate value if it's the name of a constant field in the class that we're analyzing.Return a constant value as a String.final String
Return the name of the analyzed class.Exposes the field cache to subclasses: a Map from String field name to object value.Return all names of the given group of constants.getNamesForProperty
(String propertyName) Return all names of the group of constants for the given bean property name.getNamesForSuffix
(String nameSuffix) Return all names of the given group of constants.final int
getSize()
Return the number of constants exposed.Return all values of the given group of constants.getValuesForProperty
(String propertyName) Return all values of the group of constants for the given bean property name.getValuesForSuffix
(String nameSuffix) Return all values of the given group of constants.propertyToConstantNamePrefix
(String propertyName) Convert the given bean property name to a constant name prefix.Look up the given value within the given group of constants.toCodeForProperty
(Object value, String propertyName) Look up the given value within the group of constants for the given bean property name.toCodeForSuffix
(Object value, String nameSuffix) Look up the given value within the given group of constants.
-
Constructor Details
-
Constants
Create a new Constants converter class wrapping the given class.All public static final variables will be exposed, whatever their type.
- Parameters:
clazz
- the class to analyze- Throws:
IllegalArgumentException
- if the suppliedclazz
isnull
-
-
Method Details
-
getClassName
Return the name of the analyzed class. -
getSize
public final int getSize()Return the number of constants exposed. -
getFieldCache
Exposes the field cache to subclasses: a Map from String field name to object value. -
asNumber
Return a constant value cast to a Number.- Parameters:
code
- the name of the field (nevernull
)- Returns:
- the Number value
- Throws:
Constants.ConstantException
- if the field name wasn't found or if the type wasn't compatible with Number- See Also:
-
asString
Return a constant value as a String.- Parameters:
code
- the name of the field (nevernull
)- Returns:
- the String value
Works even if it's not a string (invokes
toString()
). - Throws:
Constants.ConstantException
- if the field name wasn't found- See Also:
-
asObject
Parse the given String (upper or lower case accepted) and return the appropriate value if it's the name of a constant field in the class that we're analyzing.- Parameters:
code
- the name of the field (nevernull
)- Returns:
- the Object value
- Throws:
Constants.ConstantException
- if there's no such field
-
getNames
Return all names of the given group of constants.Note that this method assumes that constants are named in accordance with the standard Java convention for constant values (i.e. all uppercase). The supplied
namePrefix
will be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.- Parameters:
namePrefix
- prefix of the constant names to search (may benull
)- Returns:
- the set of constant names
-
getNamesForProperty
Return all names of the group of constants for the given bean property name.- Parameters:
propertyName
- the name of the bean property- Returns:
- the set of values
- See Also:
-
getNamesForSuffix
Return all names of the given group of constants.Note that this method assumes that constants are named in accordance with the standard Java convention for constant values (i.e. all uppercase). The supplied
nameSuffix
will be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.- Parameters:
nameSuffix
- suffix of the constant names to search (may benull
)- Returns:
- the set of constant names
-
getValues
Return all values of the given group of constants.Note that this method assumes that constants are named in accordance with the standard Java convention for constant values (i.e. all uppercase). The supplied
namePrefix
will be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.- Parameters:
namePrefix
- prefix of the constant names to search (may benull
)- Returns:
- the set of values
-
getValuesForProperty
Return all values of the group of constants for the given bean property name.- Parameters:
propertyName
- the name of the bean property- Returns:
- the set of values
- See Also:
-
getValuesForSuffix
Return all values of the given group of constants.Note that this method assumes that constants are named in accordance with the standard Java convention for constant values (i.e. all uppercase). The supplied
nameSuffix
will be uppercased (in a locale-insensitive fashion) prior to the main logic of this method kicking in.- Parameters:
nameSuffix
- suffix of the constant names to search (may benull
)- Returns:
- the set of values
-
toCode
Look up the given value within the given group of constants.Will return the first match.
- Parameters:
value
- constant value to look upnamePrefix
- prefix of the constant names to search (may benull
)- Returns:
- the name of the constant field
- Throws:
Constants.ConstantException
- if the value wasn't found
-
toCodeForProperty
public String toCodeForProperty(Object value, String propertyName) throws Constants.ConstantException Look up the given value within the group of constants for the given bean property name. Will return the first match.- Parameters:
value
- constant value to look uppropertyName
- the name of the bean property- Returns:
- the name of the constant field
- Throws:
Constants.ConstantException
- if the value wasn't found- See Also:
-
toCodeForSuffix
public String toCodeForSuffix(Object value, @Nullable String nameSuffix) throws Constants.ConstantException Look up the given value within the given group of constants.Will return the first match.
- Parameters:
value
- constant value to look upnameSuffix
- suffix of the constant names to search (may benull
)- Returns:
- the name of the constant field
- Throws:
Constants.ConstantException
- if the value wasn't found
-
propertyToConstantNamePrefix
Convert the given bean property name to a constant name prefix.Uses a common naming idiom: turning all lower case characters to upper case, and prepending upper case characters with an underscore.
Example: "imageSize" → "IMAGE_SIZE"
Example: "imagesize" → "IMAGESIZE".
Example: "ImageSize" → "_IMAGE_SIZE".
Example: "IMAGESIZE" → "_I_M_A_G_E_S_I_Z_E"- Parameters:
propertyName
- the name of the bean property- Returns:
- the corresponding constant name prefix
- See Also:
-