public abstract class TextFormat<T>
extends java.lang.Object
This class represents the base format for text parsing and formatting;
it supports the CharSequence
and Appendable
interfaces
for greater flexibility.
Instances of this class are typically used as static member of a
class to define the default textual representation of its instances.
The format associated to any given class/object can be dynamically retrieved.
public class Complex extends Number {
// Defines the default format for complex numbers (Cartesian form)
protected static TextFormat<Complex> TEXT_FORMAT = new TextFormat<Complex> (Complex.class) { ... }
public static Complex valueOf(CharSequence csq) {
return TEXT_FORMAT.parse(csq);
}
}
The default format can be locally overriden.
public abstract class Number implements ValueType {
public final Text toText() {
return TextFormat.getInstance(this.getClass()).format(this);
}
public final String toString() {
return TextFormat.getInstance(this.getClass()).formatToString(this);
}
}
LocalContext.enter();
try {
TextFormat<Complex> polarFormat = new TextFormat<Complex>(null) {...} // Unbound format.
TextFormat.setInstance(Complex.class, polarFormat); // Local setting (no impact on others thread).
System.out.println(complex); // Displays complex in polar coordinates.
} finally {
LocalContext.exit(); // Reverts to previous cartesian setting.
}
For parsing/formatting of primitive types, the TypeFormat
utility class is recommended.
Modifier | Constructor and Description |
---|---|
protected |
TextFormat(java.lang.Class<T> forClass)
Defines the static format bound to the specified class.
|
Modifier and Type | Method and Description |
---|---|
Text |
format(T obj)
Formats the specified object to a
Text instance
(convenience method equivalent to
format(obj, TextBuilder.newInstance()).toText() ). |
abstract java.lang.Appendable |
format(T obj,
java.lang.Appendable dest)
Formats the specified object into an
Appendable |
TextBuilder |
format(T obj,
TextBuilder dest)
Formats the specified object into a
TextBuilder (convenience
method which does not raise IOException). |
java.lang.String |
formatToString(T obj)
Convenience methods equivalent to but faster than
format(obj).toString()) |
static <T> TextFormat<T> |
getDefault(java.lang.Class<? extends T> forClass)
Returns the default format for instances of the specified class.
|
static <T> TextFormat<T> |
getInstance(java.lang.Class<? extends T> forClass)
Returns the current format for instances of the specified class.
|
boolean |
isParsingSupported()
Indicates if this format supports parsing (default
true ). |
T |
parse(java.lang.CharSequence csq)
Parses a whole character sequence from the beginning to produce an object
(convenience method).
|
abstract T |
parse(java.lang.CharSequence csq,
Cursor cursor)
Parses a portion of the specified
CharSequence from the
specified position to produce an object. |
static <T> void |
setInstance(java.lang.Class<? extends T> forClass,
TextFormat<T> format)
Overrides the default format for the specified class (
local setting ). |
protected TextFormat(java.lang.Class<T> forClass)
forClass
- the class to which the format is bound or null
if the format is not bound to any class.java.lang.IllegalArgumentException
- if the specified class is already
bound to another format.public static <T> TextFormat<T> getDefault(java.lang.Class<? extends T> forClass)
Returns the default format for instances of the specified class.
A default format exist for the following predefined types:
If there is no format found for the specified class, the
default format for java.lang.Object
is returned.
forClass
- the class for which a compatible format is returned.public static <T> TextFormat<T> getInstance(java.lang.Class<? extends T> forClass)
Returns the current format for instances of the specified class.
forClass
- the class to which a format has been bound.public static <T> void setInstance(java.lang.Class<? extends T> forClass, TextFormat<T> format)
local setting
).forClass
- the class for which the format is locally overriden.format
- the new format (typically unbound).java.lang.IllegalArgumentException
- if the speficied class has not default format defined.public boolean isParsingSupported()
true
).false
if any of the parse method throws
UnsupportedOperationException
public abstract java.lang.Appendable format(T obj, java.lang.Appendable dest) throws java.io.IOException
Appendable
obj
- the object to format.dest
- the appendable destination.Appendable
.java.io.IOException
- if an I/O exception occurs.public abstract T parse(java.lang.CharSequence csq, Cursor cursor) throws java.lang.IllegalArgumentException
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.csq
- the CharSequence
to parse.cursor
- the cursor holding the current parsing index.java.lang.IllegalArgumentException
- if any problem occurs while parsing the
specified character sequence (e.g. illegal syntax).public final TextBuilder format(T obj, TextBuilder dest)
TextBuilder
(convenience
method which does not raise IOException).obj
- the object to format.dest
- the text builder destination.public final Text format(T obj)
Text
instance
(convenience method equivalent to
format(obj, TextBuilder.newInstance()).toText()
).obj
- the object being formated.public final java.lang.String formatToString(T obj)
format(obj).toString())
obj
- the object being formated.public final T parse(java.lang.CharSequence csq) throws java.lang.IllegalArgumentException
csq
- the whole character sequence to parse.parse(csq, new Cursor())
java.lang.IllegalArgumentException
- if the specified character sequence
cannot be fully parsed (e.g. extraneous characters).Copyright © 2005 - 2007 Javolution.