| java.lang.Object javolution.text.TextFormat
TextFormat | abstract public class TextFormat (Code) | | This class represents the base format for text parsing and formatting;
it supports
CharSequence and
javolution.text.Appendable
interfaces for greater flexibility.
It is possible to retrieve the format for any class for which the
format has been registered (typically during class initialization).
For example:[code]
public class Complex extends RealtimeObject {
private static final TextFormat CARTESIAN = ...;
static { // Sets default format to cartesian, users may change it later (e.g. polar).
TextFormat.setInstance(Complex.class, CARTESIAN);
}
public Complex valueOf(CharSequence csq) {
return TextFormat.getInstance(Complex.class).parse(csq);
}
public Text toText() {
return TextFormat.getInstance(Complex.class).format(this);
}
}[/code]
For parsing/formatting of primitive types, the
TypeFormat utility class is recommended.
Note: The format behavior may depend upon
javolution.context.LocalContext local settings in which
case concurrent threads may parse differently!
author: Jean-Marie Dautelle version: 5.1, July 4, 2007 |
Constructor Summary | |
protected | TextFormat() Default constructor. |
Method Summary | |
abstract public Appendable | format(Object obj, Appendable dest) Formats the specified object into an Appendable
Parameters: obj - the object to format. Parameters: dest - the appendable destination. | final public Appendable | format(Object obj, TextBuilder dest) Formats the specified object into a
TextBuilder (convenience
method which does not raise IOException). | final public Text | format(Object obj) Formats the specified object to a
Text instance
(convenience method).
Parameters: obj - the object being formated. | public static TextFormat | getInstance(Class cls) Returns the text format for instances of specified type (class or
interface). | abstract public Object | parse(CharSequence csq, Cursor cursor) Parses a portion of the specified CharSequence from the
specified position to produce an object. | final public Object | parse(CharSequence csq) Parses a whole character sequence from the beginning to produce an object
(convenience method). | public static void | setInstance(Class cls, TextFormat format) Associates the specified format to the specified type (class or
interface). |
TextFormat | protected TextFormat()(Code) | | Default constructor.
|
format | abstract public Appendable format(Object obj, Appendable dest) throws IOException(Code) | | Formats the specified object into an Appendable
Parameters: obj - the object to format. Parameters: dest - the appendable destination. the specified Appendable . throws: IOException - if an I/O exception occurs. |
format | final public Appendable format(Object obj, TextBuilder dest)(Code) | | Formats the specified object into a
TextBuilder (convenience
method which does not raise IOException).
Parameters: obj - the object to format. Parameters: dest - the text builder destination. the specified text builder. |
format | final public Text format(Object obj)(Code) | | Formats the specified object to a
Text instance
(convenience method).
Parameters: obj - the object being formated. the text representing the specified object. |
getInstance | public static TextFormat getInstance(Class cls)(Code) | | Returns the text format for instances of specified type (class or
interface). The following types are always recognized:
- java.lang.Boolean
- java.lang.Character
- java.lang.Byte
- java.lang.Short
- java.lang.Integer
- java.lang.Long
- java.lang.Float
- java.lang.Double
- java.lang.Class
Users may register additional types using the
TextFormat.setInstance TextFormat.setInstance(Class, TextFormat) static method.
For example:[code]
TextFormat fontFormat = new TextFormat() {
public Appendable format(Font font, Appendable dest) throws IOException {
return dest.append(font.getName());
}
public Font parse(CharSequence csq, Cursor cursor) {
CharSequence fontName = csq.subSequence(cursor.getIndex(), cursor.getEndIndex());
cursor.increment(fontName.length());
return Font.decode(fontName.toString());
}
});
TextFormat.setInstance(Font.class, fontFormat); // Registers format for java.awt.Font
[/code]
Parameters: cls - the class for which the default format is returned. the format for instances of the specified class or null if unkown. |
parse | abstract public Object parse(CharSequence csq, Cursor cursor)(Code) | | Parses a portion of the specified CharSequence from the
specified position to produce an object. If parsing succeeds, then the
index of the cursor argument is updated to the index after
the last character used.
Parameters: csq - the CharSequence to parse. Parameters: cursor - the cursor holding the current parsing index. the object parsed from the specified character sub-sequence. throws: RuntimeException - if any problem occurs while parsing the specified character sequence (e.g. illegal syntax). |
parse | final public Object parse(CharSequence csq)(Code) | | Parses a whole character sequence from the beginning to produce an object
(convenience method).
Parameters: csq - the whole character sequence to parse. the corresponding object. throws: IllegalArgumentException - if the specified character sequence cannot be fully parsed. |
setInstance | public static void setInstance(Class cls, TextFormat format)(Code) | | Associates the specified format to the specified type (class or
interface).
Parameters: cls - the class for which the default format is returned. Parameters: format - the format for instances of the specified calss class. |
|
|