public class Constants
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
static class |
Constants.ConstantException
Exception thrown when the
Constants class is asked for
an invalid constant name. |
Constructor and Description |
---|
Constants(java.lang.Class<?> clazz)
Create a new Constants converter class wrapping the given class.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Number |
asNumber(java.lang.String code)
Return a constant value cast to a Number.
|
java.lang.Object |
asObject(java.lang.String code)
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 analysing.
|
java.lang.String |
asString(java.lang.String code)
Return a constant value as a String.
|
java.lang.String |
getClassName()
Return the name of the analyzed class.
|
protected java.util.Map<java.lang.String,java.lang.Object> |
getFieldCache()
Exposes the field cache to subclasses:
a Map from String field name to object value.
|
java.util.Set<java.lang.String> |
getNames(java.lang.String namePrefix)
Return all names of the given group of constants.
|
java.util.Set<java.lang.String> |
getNamesForProperty(java.lang.String propertyName)
Return all names of the group of constants for the
given bean property name.
|
java.util.Set<java.lang.String> |
getNamesForSuffix(java.lang.String nameSuffix)
Return all names of the given group of constants.
|
int |
getSize()
Return the number of constants exposed.
|
java.util.Set<java.lang.Object> |
getValues(java.lang.String namePrefix)
Return all values of the given group of constants.
|
java.util.Set<java.lang.Object> |
getValuesForProperty(java.lang.String propertyName)
Return all values of the group of constants for the
given bean property name.
|
java.util.Set<java.lang.Object> |
getValuesForSuffix(java.lang.String nameSuffix)
Return all values of the given group of constants.
|
java.lang.String |
propertyToConstantNamePrefix(java.lang.String propertyName)
Convert the given bean property name to a constant name prefix.
|
java.lang.String |
toCode(java.lang.Object value,
java.lang.String namePrefix)
Look up the given value within the given group of constants.
|
java.lang.String |
toCodeForProperty(java.lang.Object value,
java.lang.String propertyName)
Look up the given value within the group of constants for
the given bean property name.
|
java.lang.String |
toCodeForSuffix(java.lang.Object value,
java.lang.String nameSuffix)
Look up the given value within the given group of constants.
|
public Constants(java.lang.Class<?> clazz)
All public static final variables will be exposed, whatever their type.
clazz
- the class to analyzejava.lang.IllegalArgumentException
- if the supplied clazz
is null
public final java.lang.String getClassName()
public final int getSize()
protected final java.util.Map<java.lang.String,java.lang.Object> getFieldCache()
public java.lang.Number asNumber(java.lang.String code) throws Constants.ConstantException
code
- the name of the field (never null
)Constants.ConstantException
- if the field name wasn't found
or if the type wasn't compatible with NumberasObject(java.lang.String)
public java.lang.String asString(java.lang.String code) throws Constants.ConstantException
code
- the name of the field (never null
)toString()
).Constants.ConstantException
- if the field name wasn't foundasObject(java.lang.String)
public java.lang.Object asObject(java.lang.String code) throws Constants.ConstantException
code
- the name of the field (never null
)Constants.ConstantException
- if there's no such fieldpublic java.util.Set<java.lang.String> getNames(@Nullable java.lang.String namePrefix)
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.
namePrefix
- prefix of the constant names to search (may be null
)public java.util.Set<java.lang.String> getNamesForProperty(java.lang.String propertyName)
propertyName
- the name of the bean propertypropertyToConstantNamePrefix(java.lang.String)
public java.util.Set<java.lang.String> getNamesForSuffix(@Nullable java.lang.String nameSuffix)
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.
nameSuffix
- suffix of the constant names to search (may be null
)public java.util.Set<java.lang.Object> getValues(@Nullable java.lang.String namePrefix)
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.
namePrefix
- prefix of the constant names to search (may be null
)public java.util.Set<java.lang.Object> getValuesForProperty(java.lang.String propertyName)
propertyName
- the name of the bean propertypropertyToConstantNamePrefix(java.lang.String)
public java.util.Set<java.lang.Object> getValuesForSuffix(@Nullable java.lang.String nameSuffix)
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.
nameSuffix
- suffix of the constant names to search (may be null
)public java.lang.String toCode(java.lang.Object value, @Nullable java.lang.String namePrefix) throws Constants.ConstantException
Will return the first match.
value
- constant value to look upnamePrefix
- prefix of the constant names to search (may be null
)Constants.ConstantException
- if the value wasn't foundpublic java.lang.String toCodeForProperty(java.lang.Object value, java.lang.String propertyName) throws Constants.ConstantException
value
- constant value to look uppropertyName
- the name of the bean propertyConstants.ConstantException
- if the value wasn't foundpropertyToConstantNamePrefix(java.lang.String)
public java.lang.String toCodeForSuffix(java.lang.Object value, @Nullable java.lang.String nameSuffix) throws Constants.ConstantException
Will return the first match.
value
- constant value to look upnameSuffix
- suffix of the constant names to search (may be null
)Constants.ConstantException
- if the value wasn't foundpublic java.lang.String propertyToConstantNamePrefix(java.lang.String propertyName)
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"
propertyName
- the name of the bean propertygetValuesForProperty(java.lang.String)
,
toCodeForProperty(java.lang.Object, java.lang.String)