com.interface21.util
Class Constants

java.lang.Object
  |
  +--com.interface21.util.Constants

public class Constants
extends java.lang.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 asInt() 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.

TODO: add asBoolean, asDouble methods, keys method

Since:
16-Mar-2003
Version:
$Id: Constants.java,v 1.2 2003/06/25 18:12:18 jhoeller Exp $
Author:
Rod Johnson

Constructor Summary
Constants(java.lang.Class clazz)
          Create a new Constants converter class wrapping the given class.
 
Method Summary
 int asInt(java.lang.String code)
          Return a constant value cast to an int
 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 we're analysing.
 java.lang.String asString(java.lang.String code)
          Return a constant value as a String
 int getSize()
          Return the number of constants exposed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Constants

public Constants(java.lang.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
Returns:
int the number of constants exposed

asInt

public int asInt(java.lang.String code)
          throws ConstantException
Return a constant value cast to an int
Parameters:
code - name of the field
Returns:
int int value if successfuly
Throws:
ConstantException - if the field name wasn't found or if the type wasn't compatible with int
See Also:
asObject(java.lang.String)

asString

public java.lang.String asString(java.lang.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 java.lang.Object asObject(java.lang.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


Rod Johnson and Spring contributors 2001-2003.