Save This Page
Home » spring-framework-2.5.5-with-dependencies » org.springframework » core » [javadoc | source]
org.springframework.core
public class: Constants [javadoc | source]
java.lang.Object
   org.springframework.core.Constants
This class can be used to parse other classes containing constant definitions in public static final members. The 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.

Constructor:
 public Constants(Class clazz) 
    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 supplied clazz is null
Method from org.springframework.core.Constants Summary:
asNumber,   asObject,   asString,   getClassName,   getFieldCache,   getNames,   getNamesForProperty,   getNamesForSuffix,   getSize,   getValues,   getValuesForProperty,   getValuesForSuffix,   propertyToConstantNamePrefix,   toCode,   toCodeForProperty,   toCodeForSuffix
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.core.Constants Detail:
 public Number asNumber(String code) throws ConstantException 
    Return a constant value cast to a Number.
 public Object asObject(String code) throws ConstantException 
    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.
 public String asString(String code) throws ConstantException 
    Return a constant value as a String.
 public final String getClassName() 
    Return the name of the analyzed class.
 protected final Map getFieldCache() 
    Exposes the field cache to subclasses: a Map from String field name to object value.
 public Set getNames(String namePrefix) 
    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.

 public Set getNamesForProperty(String propertyName) 
    Return all names of the group of constants for the given bean property name.
 public Set getNamesForSuffix(String nameSuffix) 
    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.

 public final int getSize() 
    Return the number of constants exposed.
 public Set getValues(String namePrefix) 
    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.

 public Set getValuesForProperty(String propertyName) 
    Return all values of the group of constants for the given bean property name.
 public Set getValuesForSuffix(String nameSuffix) 
    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.

 public String propertyToConstantNamePrefix(String propertyName) 
    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"

 public String toCode(Object value,
    String namePrefix) throws ConstantException 
    Look up the given value within the given group of constants.

    Will return the first match.

 public String toCodeForProperty(Object value,
    String propertyName) throws ConstantException 
    Look up the given value within the group of constants for the given bean property name. Will return the first match.
 public String toCodeForSuffix(Object value,
    String nameSuffix) throws ConstantException 
    Look up the given value within the given group of constants.

    Will return the first match.