|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.marringtons.object.Formatter
A Formatter is a class to format strings from DAO entries to convert from an internal representation to strings for display. It supports all primatives, arrays of primatives and Strings in this the default form. The main power derives from sub-classing the Formatter. _format_* can be over-ridden to change the way various field types are formatted or methods with the same name as objects being formatted can be created to tune formats on a field-by-field basis. The default format for collections are comma separated strings. Maps create a string of the type key=value. getInstance() can be used to extract the formatter from inside the class - where it has access to the complete class.
static class TestObject
{
int intValue;
String stringValue, string2Value;
int[] intArray;
ArrayList collection;
HashMap map;
public class TestObject2
{ int intValue; }
TestObject2 testObject2;
class CustomFormatter extends Translator
{
public String intValue( int from)
{ return "'" + from + "'"; }
public String stringValue( String from)
{ return _format_S(from) + " customised"; }
public String intArray( int from)
{ return "'" + from + "'"; }
public String _format_S( String from)
{ return convertFromFrench(from); }
}
}
In the rare case where you are formatting multiple sub-objects with the same
field name, and you need to do a specific translation on one of them, use the
protected Object currentObject to determine the source.
public int intValue( String from)
{
int add = (translationObject instanceOf TestObject2) ? 5678 : 9012;
return Integer.parseInt( from) + add;
}
ObjectScraper.fromProperties(Object, Properties, Translator),
ObjectXML.fromXML( Database, BufferedReader,
boolean, Translator)| Nested Class Summary | |
class |
Formatter.Error
Thrown when formatting encounters an exception. |
| Constructor Summary | |
Formatter()
|
|
| Method Summary | |
String |
_format_B(boolean from)
By default an boolean displays as 'true' or 'false'. |
String |
_format_b(byte from)
By default a byte displays as Java defines - decimal. |
String |
_format_c(char from)
By default a character displays as is - using the default character set. |
String |
_format_C(Collection from)
By default an Collection uses the default toString() method. |
String |
_format_d(double from)
By default a double displays as Java defines - using scientific notation if it gets large. |
String |
_format_f(float from)
By default a float displays as Java defines - using scientific notation if it gets large. |
String |
_format_i(int from)
By default an integer displays as Java defines - decimal with optional leading -. |
String |
_format_l(long from)
By default an long displays as Java defines - decimal with optional leading -. |
String |
_format_M(Map from)
By default an Map uses the default toString() method. |
String |
_format_O(Object from)
By default an object uses the default toString() method - which is the address if not over-ridden. |
String |
_format_s(short from)
By default an short integer displays as Java defines - decimal with optional leading -. |
String |
_format_S(String from)
By default a string displays as it was stored. |
String |
format(Object object,
com.marringtons.object.ClassDirectory.Entry entry,
int fieldIndex,
int arrayIndex)
Format internal form into a string. |
static Formatter |
getInstance(Object object,
com.marringtons.object.ClassDirectory.Entry entry)
Get an instance of a formatter - from the class being translated or the default if no others exist. |
| Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public Formatter()
| Method Detail |
public static Formatter getInstance(Object object,
com.marringtons.object.ClassDirectory.Entry entry)
object - to translate (that may have a sub-class of translator.entry - for object - for caching translators.
public String format(Object object,
com.marringtons.object.ClassDirectory.Entry entry,
int fieldIndex,
int arrayIndex)
object - that we are getting data fromentry - details for this objectfieldIndex - index of the field to setarrayIndex - Location in array (-1 means whole array is csv of this field)
if attempting to translate an untranslatable non-primative object.
public String _format_c(char from)
from - value to format
public String _format_d(double from)
from - value to format
public String _format_f(float from)
from - value to format
public String _format_i(int from)
from - value to format
public String _format_l(long from)
from - value to format
public String _format_s(short from)
from - value to format
public String _format_b(byte from)
from - value to format
public String _format_B(boolean from)
from - value to format
public String _format_S(String from)
from - value to format
public String _format_C(Collection from)
from - value to format
public String _format_M(Map from)
from - value to format
public String _format_O(Object from)
from - value to format
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||