com.ricebridge.data
Class BeanSpec

java.lang.Object
  extended bycom.ricebridge.data.BeanSpec

public class BeanSpec
extends Object

Stores settings for reading and writing Java Beans from text files.

This class only supports Java Bean properties specified by method names. Boolean (is/set), standard (get/set) and indexed properties are supported. Read-only and write-only properties are supported. It is assumed that all value objects can be represented as Strings.

You can use your own StringConverter to convert your own objects from and to their String representations.

This class is designed to be subclassable,so you can embed your own functionality if required.


Field Summary
protected  Class iBeanClass
          Class of the bean detailed by this specification.
protected  char iEscChar
          Escape character for indexed field value list separator, default: '\'.
protected  HashMap iGetMethodMap
          Map of property names to get Methods.
protected  char iSepChar
          Separation character for indexed field value lists, default: ':'.
protected  HashMap iSetMethodMap
          Map of property names to set Methods.
protected  HashMap iStringConverterMap
          Map of property names to StringConverter objects.
protected static HashMap sNativeArrayNameMap
          Default StringConverter map for indexed fields.
protected static HashMap sStringConverterClassMap
          Default StringConverter map.
 
Constructor Summary
BeanSpec()
          Create an empty BeanSpec (prepareBeanClass(java.lang.Class) must be subsequently called).
BeanSpec(Class pBeanClass)
          Create a BeanSpec for the specified bean type.
BeanSpec(Class pBeanClass, char pSepChar, char pEscChar)
          Create a BeanSpec for the specified bean type, with custom indexed property delimiters
BeanSpec(Class pBeanClass, Map pStringConverterMap)
          Create a BeanSpec for the specified bean type.
BeanSpec(Class pBeanClass, Map pStringConverterMap, char pSepChar, char pEscChar)
          Create a BeanSpec for the specified bean type.
 
Method Summary
protected  void build()
          Build the internal mappings for the current bean.
 Class getBeanClass()
          Get the class of the current Java Bean.
 String[] getGetterNames()
          Get the sorted property names of the getter methods.
 String[] getSetterNames()
          Get the sorted property names of the setter methods.
 String getStringValue(Object pBean, String pMethodRootName, boolean pUseDefault)
          Call a Java Bean property get method.
protected  void handleGet(String pMethodName, Method pMethod)
          Handle the converter mappings for the specified get method.
protected  void handleSet(String pMethodName, Method pMethod)
          Handle the converter mappings for the specified set method.
protected  void handleStringConverter(String pRootName, Class pTypeClass)
          Determine the correct StringConverter for the property value class type, and perform any required initialisation of the StringConverter.
protected  Object makeObject(String pRootName, String pValue, boolean pUseDefault)
          Convert a property value text representation into a Java object.
protected  String makeString(String pRootName, Object pValue, boolean pUseDefault)
          Convert a Java object into a text representation.
protected  void prepareBeanClass(Class pBeanClass)
          Set the class of the Java Bean to be described.
protected  void setDelimiters(char pSepChar, char pEscChar)
          Set the delimiters to use for delimiter separated text lists of indexed properties.
protected  void setStringConverterMap(Map pStringConverterMap)
          Set the custom StringConverters for named bean methods.
 void setStringValue(Object pBean, String pMethodRootName, String pValue, boolean pUseDefault)
          Call a Java Bean property set method.
 String toString()
          Create a description of this BeanSpec suitable for debugging.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

iStringConverterMap

protected HashMap iStringConverterMap
Map of property names to StringConverter objects.


iBeanClass

protected Class iBeanClass
Class of the bean detailed by this specification.


iSetMethodMap

protected HashMap iSetMethodMap
Map of property names to set Methods.


iGetMethodMap

protected HashMap iGetMethodMap
Map of property names to get Methods.


iSepChar

protected char iSepChar
Separation character for indexed field value lists, default: ':'.


iEscChar

protected char iEscChar
Escape character for indexed field value list separator, default: '\'.


sStringConverterClassMap

protected static HashMap sStringConverterClassMap
Default StringConverter map.


sNativeArrayNameMap

protected static HashMap sNativeArrayNameMap
Default StringConverter map for indexed fields.

Constructor Detail

BeanSpec

public BeanSpec()
Create an empty BeanSpec (prepareBeanClass(java.lang.Class) must be subsequently called).


BeanSpec

public BeanSpec(Class pBeanClass)
Create a BeanSpec for the specified bean type.

Parameters:
pBeanClass - Class of bean

BeanSpec

public BeanSpec(Class pBeanClass,
                char pSepChar,
                char pEscChar)
Create a BeanSpec for the specified bean type, with custom indexed property delimiters

Parameters:
pBeanClass - Class of bean
pSepChar - separator character for text values of indexed properties
pEscChar - escape character for separators occuring inside indexed properties

BeanSpec

public BeanSpec(Class pBeanClass,
                Map pStringConverterMap)
Create a BeanSpec for the specified bean type.

Parameters:
pBeanClass - Class of bean
pStringConverterMap - specify set of custom String converters

BeanSpec

public BeanSpec(Class pBeanClass,
                Map pStringConverterMap,
                char pSepChar,
                char pEscChar)
Create a BeanSpec for the specified bean type.

Parameters:
pBeanClass - Class of bean
pStringConverterMap - specify set of custom String converters
pSepChar - separator char for text values of indexed properties
pEscChar - escape character for separators occuring inside indexed properties
Method Detail

getBeanClass

public Class getBeanClass()
Get the class of the current Java Bean.

See Also:
prepareBeanClass(java.lang.Class)

getSetterNames

public String[] getSetterNames()
Get the sorted property names of the setter methods.


getGetterNames

public String[] getGetterNames()
Get the sorted property names of the getter methods.


setStringValue

public void setStringValue(Object pBean,
                           String pMethodRootName,
                           String pValue,
                           boolean pUseDefault)
Call a Java Bean property set method.

The pValue parameter should contain a text representation of the value expected by the set method. This text value will be converted into an appropriate data type. You can specify your own conversion using a StringConverter and calling the setStringConverterMap(java.util.Map) method.

The root name of the method is the Java source code name of the method, with the set, get or is prefix removed. Thus, setFoo has root name Foo, for example. Note that the initial capital letter remains.If the conversion to an object fails and pUseDefault is true, then the default value is used. If false, then an exception is thrown.

Parameters:
pBean - object of bean type
pMethodRootName - root name of property method
pValue - text representation of property value
pUseDefault - use default value if conversion fails
See Also:
getStringValue(java.lang.Object, java.lang.String, boolean), setStringConverterMap(java.util.Map), setDelimiters(char, char), prepareBeanClass(java.lang.Class), StringConverter

getStringValue

public String getStringValue(Object pBean,
                             String pMethodRootName,
                             boolean pUseDefault)
Call a Java Bean property get method.

The return value is a text representation of the value of the property. You can specify your own conversion of the property value into a text value using a StringConverter and calling the setStringConverterMap(java.util.Map) method.

The root name of the method is the Java source code name of the method, with the set, get or is prefix removed. Thus, setFoo has root name Foo, for example. Note that the initial capital letter remains.If the conversion to a String fails and pUseDefault is true, then the default String is used. If false, then an exception is thrown.

Parameters:
pBean - object of bean type
pMethodRootName - root name of property method
pUseDefault - use default value if conversion fails
See Also:
setStringValue(java.lang.Object, java.lang.String, java.lang.String, boolean), setStringConverterMap(java.util.Map), setDelimiters(char, char), prepareBeanClass(java.lang.Class), StringConverter

toString

public String toString()
Create a description of this BeanSpec suitable for debugging.


setStringConverterMap

protected void setStringConverterMap(Map pStringConverterMap)
Set the custom StringConverters for named bean methods.

You can specify a set of custom converters for some or all of the property methods in your bean. These converters are indexed by the root name of the property method (the root name of setFoo is Foo, for example).

Parameters:
pStringConverterMap - mapping from method root names to StringConverters
See Also:
StringConverter

setDelimiters

protected void setDelimiters(char pSepChar,
                             char pEscChar)
Set the delimiters to use for delimiter separated text lists of indexed properties.

The default delimiters are sep: :(colon) and esc: \ (backslash). Indexed properties are thus encoded like so

['foo', 'a:a', 'a\a'] => foo:a\:a:a\\a>

The escape character operates in a similar manner to the backslash escape in Java source code String literals.

Parameters:
pSepChar - separator character for indexed properties
pEscChar - escape character for indexed properties

prepareBeanClass

protected void prepareBeanClass(Class pBeanClass)
Set the class of the Java Bean to be described.

This method indexes the property methods of the bean and prepares them for use by the setStringValue(java.lang.Object, java.lang.String, java.lang.String, boolean) and getStringValue(java.lang.Object, java.lang.String, boolean) methods. This method uses the current StringConverter mapping and delimiters.

See Also:
getBeanClass(), setStringValue(java.lang.Object, java.lang.String, java.lang.String, boolean), getStringValue(java.lang.Object, java.lang.String, boolean)

build

protected void build()
Build the internal mappings for the current bean.


handleSet

protected void handleSet(String pMethodName,
                         Method pMethod)
Handle the converter mappings for the specified set method.

Parameters:
pMethodName - full method name
pMethod - method reflection object

handleGet

protected void handleGet(String pMethodName,
                         Method pMethod)
Handle the converter mappings for the specified get method.

Parameters:
pMethodName - full method name
pMethod - method reflection object

handleStringConverter

protected void handleStringConverter(String pRootName,
                                     Class pTypeClass)
Determine the correct StringConverter for the property value class type, and perform any required initialisation of the StringConverter.


makeObject

protected Object makeObject(String pRootName,
                            String pValue,
                            boolean pUseDefault)
Convert a property value text representation into a Java object.

Parameters:
pRootName - root name of property method
pValue - text representation of value
pUseDefault - use default value if conversion fails

makeString

protected String makeString(String pRootName,
                            Object pValue,
                            boolean pUseDefault)
Convert a Java object into a text representation.

Parameters:
pRootName - root name of property method
pValue - Java object to represent
pUseDefault - use default string if conversion fails


Copyright © 2003-2006 Ricebridge