public class

Constants

extends Object
java.lang.Object
   ↳ org.springframework.core.Constants

Class Overview

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.

Summary

Public Constructors
Constants(Class clazz)
Create a new Constants converter class wrapping the given class.
Public Methods
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 that we're analysing.
String asString(String code)
Return a constant value as a String.
final String getClassName()
Return the name of the analyzed class.
Set<String> getNames(String namePrefix)
Return all names of the given group of constants.
Set<String> getNamesForProperty(String propertyName)
Return all names of the group of constants for the given bean property name.
Set getNamesForSuffix(String nameSuffix)
Return all names of the given group of constants.
final int getSize()
Return the number of constants exposed.
Set<Object> getValues(String namePrefix)
Return all values of the given group of constants.
Set<Object> getValuesForProperty(String propertyName)
Return all values of the group of constants for the given bean property name.
Set<Object> getValuesForSuffix(String nameSuffix)
Return all values of the given group of constants.
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.
String toCodeForSuffix(Object value, String nameSuffix)
Look up the given value within the given group of constants.
Protected Methods
final Map<StringObject> getFieldCache()
Exposes the field cache to subclasses: a Map from String field name to object value.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Constants (Class clazz)

Also: SpringCore

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

Public Methods

public Number asNumber (String code)

Also: SpringCore

Return a constant value cast to a Number.

Parameters
code the name of the field (never null)
Returns
  • the Number value
Throws
ConstantException if the field name wasn't found or if the type wasn't compatible with Number
See Also

public Object asObject (String code)

Also: SpringCore

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.

Parameters
code the name of the field (never null)
Returns
  • the Object value
Throws
ConstantException if there's no such field

public String asString (String code)

Also: SpringCore

Return a constant value as a String.

Parameters
code the name of the field (never null)
Returns
  • the String value Works even if it's not a string (invokes toString()).
Throws
ConstantException if the field name wasn't found
See Also

public final String getClassName ()

Also: SpringCore

Return the name of the analyzed class.

public Set<String> getNames (String namePrefix)

Also: SpringCore

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 be null)
Returns
  • the set of constant names

public Set<String> getNamesForProperty (String propertyName)

Also: SpringCore

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

public Set getNamesForSuffix (String nameSuffix)

Also: SpringCore

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 be null)
Returns
  • the set of constant names

public final int getSize ()

Also: SpringCore

Return the number of constants exposed.

public Set<Object> getValues (String namePrefix)

Also: SpringCore

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 be null)
Returns
  • the set of values

public Set<Object> getValuesForProperty (String propertyName)

Also: SpringCore

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

public Set<Object> getValuesForSuffix (String nameSuffix)

Also: SpringCore

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 be null)
Returns
  • the set of values

public String propertyToConstantNamePrefix (String propertyName)

Also: SpringCore

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

public String toCode (Object value, String namePrefix)

Also: SpringCore

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 (may be null)
Returns
  • the name of the constant field
Throws
ConstantException if the value wasn't found

public String toCodeForProperty (Object value, String propertyName)

Also: SpringCore

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

public String toCodeForSuffix (Object value, String nameSuffix)

Also: SpringCore

Look up the given value within the given group of constants.

Will return the first match.

Parameters
value constant value to look up
nameSuffix suffix of the constant names to search (may be null)
Returns
  • the name of the constant field
Throws
ConstantException if the value wasn't found

Protected Methods

protected final Map<StringObject> getFieldCache ()

Also: SpringCore

Exposes the field cache to subclasses: a Map from String field name to object value.