org.springframework.core
Class Constants

java.lang.Object
  extended byorg.springframework.core.Constants

public class Constants
extends Object

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.

Since:
16.03.2003
Author:
Rod Johnson, Juergen Hoeller

Constructor Summary
Constants(Class clazz)
          Create a new Constants converter class wrapping the given class.
 
Method Summary
 Number asNumber(String code)
          Return a constant value cast to a Number.
 Object asObject(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 we're analysing.
 String asString(String code)
          Return a constant value as a String.
 int getSize()
          Return the number of constants exposed.
 Set getValues(String namePrefix)
          Return all values of the given group of constants.
 Set getValuesForProperty(String propertyName)
          Return all values of the group of constants for the given bean property name.
 String propertyToConstantNamePrefix(String propertyName)
          Convert the given bean property name to a constant name prefix.
 String toCode(Object value, String namePrefix)
          Look up the given value within the given group of constants.
 String toCodeForProperty(Object value, String propertyName)
          Look up the given value within the group of constants for the given bean property name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Constants

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 - class to analyze
Method Detail

getSize

public int getSize()
Return the number of constants exposed.


asNumber

public Number asNumber(String code)
                throws ConstantException
Return a constant value cast to a Number.

Parameters:
code - name of the field
Returns:
long value if successful
Throws:
ConstantException - if the field name wasn't found or if the type wasn't compatible with Number
See Also:
asObject(java.lang.String)

asString

public String asString(String code)
                throws ConstantException
Return a constant value as a String.

Parameters:
code - name of the field
Returns:
String string value if successful. Works even if it's not a string (invokes toString()).
Throws:
ConstantException - if the field name wasn't found
See Also:
asObject(java.lang.String)

asObject

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 we're analysing.

Throws:
ConstantException - if there's no such field

getValues

public Set getValues(String namePrefix)
Return all values of the given group of constants.

Parameters:
namePrefix - prefix of the constant names to search
Returns:
the set of values

getValuesForProperty

public Set getValuesForProperty(String propertyName)
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:
propertyToConstantNamePrefix(java.lang.String)

toCode

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.

Parameters:
value - constant value to look up
namePrefix - prefix of the constant names to search
Returns:
the name of the constant field
Throws:
ConstantException - if the value wasn't found

toCodeForProperty

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.

Parameters:
value - constant value to look up
propertyName - the name of the bean property
Returns:
the name of the constant field
Throws:
ConstantException - if the value wasn't found
See Also:
propertyToConstantNamePrefix(java.lang.String)

propertyToConstantNamePrefix

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".

Parameters:
propertyName - the name of the bean property
Returns:
the corresponding constant name prefix
See Also:
getValuesForProperty(java.lang.String), toCodeForProperty(java.lang.Object, java.lang.String)


Copyright (C) 2003-2004 The Spring Framework Project.