com.marringtons.object
Class Formatter

java.lang.Object
  extended bycom.marringtons.object.Formatter

public class Formatter
extends Object

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;
    }
  
 

Author:
Paul Marrington
See Also:
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

Formatter

public Formatter()
Method Detail

getInstance

public 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.

Parameters:
object - to translate (that may have a sub-class of translator.
entry - for object - for caching translators.
Returns:
Translator to use.

format

public String format(Object object,
                     com.marringtons.object.ClassDirectory.Entry entry,
                     int fieldIndex,
                     int arrayIndex)
Format internal form into a string. Will look for specific formatter methods. If not found, default format rules are applied.

Parameters:
object - that we are getting data from
entry - details for this object
fieldIndex - index of the field to set
arrayIndex - Location in array (-1 means whole array is csv of this field) if attempting to translate an untranslatable non-primative object.
Returns:
the formatted string to display to the user.

_format_c

public String _format_c(char from)
By default a character displays as is - using the default character set.

Parameters:
from - value to format
Returns:
string representation.

_format_d

public String _format_d(double from)
By default a double displays as Java defines - using scientific notation if it gets large.

Parameters:
from - value to format
Returns:
string representation.

_format_f

public String _format_f(float from)
By default a float displays as Java defines - using scientific notation if it gets large.

Parameters:
from - value to format
Returns:
string representation.

_format_i

public String _format_i(int from)
By default an integer displays as Java defines - decimal with optional leading -.

Parameters:
from - value to format
Returns:
string representation.

_format_l

public String _format_l(long from)
By default an long displays as Java defines - decimal with optional leading -.

Parameters:
from - value to format
Returns:
string representation.

_format_s

public String _format_s(short from)
By default an short integer displays as Java defines - decimal with optional leading -.

Parameters:
from - value to format
Returns:
string representation.

_format_b

public String _format_b(byte from)
By default a byte displays as Java defines - decimal.

Parameters:
from - value to format
Returns:
string representation.

_format_B

public String _format_B(boolean from)
By default an boolean displays as 'true' or 'false'.

Parameters:
from - value to format
Returns:
string representation.

_format_S

public String _format_S(String from)
By default a string displays as it was stored.

Parameters:
from - value to format
Returns:
string representation.

_format_C

public String _format_C(Collection from)
By default an Collection uses the default toString() method.

Parameters:
from - value to format
Returns:
string representation.

_format_M

public String _format_M(Map from)
By default an Map uses the default toString() method.

Parameters:
from - value to format
Returns:
string representation.

_format_O

public String _format_O(Object from)
By default an object uses the default toString() method - which is the address if not over-ridden.

Parameters:
from - value to format
Returns:
string representation.


Copyright © 2005 Paul Marrington http://library.marringtons.com