|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.ricebridge.data.BeanSpec
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 |
protected HashMap iStringConverterMap
StringConverter objects.
protected Class iBeanClass
protected HashMap iSetMethodMap
Methods.
protected HashMap iGetMethodMap
Methods.
protected char iSepChar
:'.
protected char iEscChar
\'.
protected static HashMap sStringConverterClassMap
StringConverter map.
protected static HashMap sNativeArrayNameMap
StringConverter map for indexed fields.
| Constructor Detail |
public BeanSpec()
BeanSpec (prepareBeanClass(java.lang.Class) must be subsequently called).
public BeanSpec(Class pBeanClass)
BeanSpec for the specified bean type.
pBeanClass - Class of bean
public BeanSpec(Class pBeanClass,
char pSepChar,
char pEscChar)
BeanSpec for the specified bean type, with custom indexed property delimiters
pBeanClass - Class of beanpSepChar - separator character for text values of indexed propertiespEscChar - escape character for separators occuring inside indexed properties
public BeanSpec(Class pBeanClass,
Map pStringConverterMap)
BeanSpec for the specified bean type.
pBeanClass - Class of beanpStringConverterMap - specify set of custom String converters
public BeanSpec(Class pBeanClass,
Map pStringConverterMap,
char pSepChar,
char pEscChar)
BeanSpec for the specified bean type.
pBeanClass - Class of beanpStringConverterMap - specify set of custom String converterspSepChar - separator char for text values of indexed propertiespEscChar - escape character for separators occuring inside indexed properties| Method Detail |
public Class getBeanClass()
prepareBeanClass(java.lang.Class)public String[] getSetterNames()
public String[] getGetterNames()
public void setStringValue(Object pBean,
String pMethodRootName,
String pValue,
boolean pUseDefault)
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.
pBean - object of bean typepMethodRootName - root name of property methodpValue - text representation of property valuepUseDefault - use default value if conversion failsgetStringValue(java.lang.Object, java.lang.String, boolean),
setStringConverterMap(java.util.Map),
setDelimiters(char, char),
prepareBeanClass(java.lang.Class),
StringConverter
public String getStringValue(Object pBean,
String pMethodRootName,
boolean pUseDefault)
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.
pBean - object of bean typepMethodRootName - root name of property methodpUseDefault - use default value if conversion failssetStringValue(java.lang.Object, java.lang.String, java.lang.String, boolean),
setStringConverterMap(java.util.Map),
setDelimiters(char, char),
prepareBeanClass(java.lang.Class),
StringConverterpublic String toString()
BeanSpec suitable for debugging.
protected void setStringConverterMap(Map pStringConverterMap)
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).
pStringConverterMap - mapping from method root names to StringConvertersStringConverter
protected void setDelimiters(char pSepChar,
char pEscChar)
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.
pSepChar - separator character for indexed propertiespEscChar - escape character for indexed propertiesprotected void prepareBeanClass(Class pBeanClass)
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.
getBeanClass(),
setStringValue(java.lang.Object, java.lang.String, java.lang.String, boolean),
getStringValue(java.lang.Object, java.lang.String, boolean)protected void build()
protected void handleSet(String pMethodName,
Method pMethod)
pMethodName - full method namepMethod - method reflection object
protected void handleGet(String pMethodName,
Method pMethod)
pMethodName - full method namepMethod - method reflection object
protected void handleStringConverter(String pRootName,
Class pTypeClass)
StringConverter for the property value class type,
and perform any required initialisation of the StringConverter.
protected Object makeObject(String pRootName,
String pValue,
boolean pUseDefault)
pRootName - root name of property methodpValue - text representation of valuepUseDefault - use default value if conversion fails
protected String makeString(String pRootName,
Object pValue,
boolean pUseDefault)
pRootName - root name of property methodpValue - Java object to representpUseDefault - use default string if conversion fails
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||