|
J avolution v5.4 (J2SE 1.6+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavolution.text.TextFormat<T>
public abstract class TextFormat<T>
This class represents the base format for text parsing and formatting;
it supports the CharSequence
and Appendable
interfaces
for greater flexibility.
Typically, classes for which textual parsing/formatting is supported
have a protected static final
TextFormat instance
holding the default format for the class. For input/output
(e.g. valueOf(CharSequence), toString()), the context-local
format (or current format) should be used.
public class Complex implements ValueType {
// Defines the default format for Complex (cartesian).
protected static final TextFormat<Complex> TEXT_FORMAT = new TextFormat<Complex>(Complex.class) {
...
}
public Complex valueOf(CharSequence csq) {
// Uses the local format for Complex numbers.
return TextFormat.getInstance(Complex.class).parse(csq);
}
public Text toText() {
// Uses the local format for Complex numbers.
return TextFormat.getInstance(Complex.class).format(this);
}
}
It is possible to retrieve either the default
format
for any class or the current
format which
can be locally overriden on a thread basis.
Complex c = Complex.valueOf(3, 4);
System.out.println(c); // Display using the default cartesian format (e.g. "2.1 - 3.2i")
TextFormat<Complex> polarFormat = new TextFormat<Complex>(null) { ... }; // Creates an unbounded format.
LocalContext.enter();
try {
TextFormat.setInstance(polarFormat, Complex.class); // Context-local setting.
System.out.println(c); // Display using the local polar format.
} finally {
LocalContext.exit(); // Reverts to the default cartesian format for complex numbers.
}
For parsing/formatting of primitive types, the TypeFormat
utility class is recommended.
Constructor Summary | |
---|---|
protected |
TextFormat(java.lang.Class<T> cls)
Creates a new text format and if a class is specified, makes it the default format for all instances of the class. |
Method Summary | ||
---|---|---|
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 |
|
java.lang.Appendable |
format(T obj,
TextBuilder dest)
Formats the specified object into a TextBuilder (convenience
method which does not raise IOException). |
|
java.lang.Class<T> |
getBoundClass()
Returns the class/interface statically bound to this format or null if this text format is not the default format
for the specified class. |
|
static
|
getDefault(java.lang.Class<? extends T> forClass)
Returns the default text format for instances of specified type. |
|
static
|
getInstance(java.lang.Class<? extends T> forClass)
Returns the current text format for instances of specified type. |
|
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
|
setInstance(TextFormat<T> format,
java.lang.Class<T> forClass)
Overrides the current format for the specified class ( context-local ). |
|
java.lang.String |
toString()
Returns textual information about this format. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
protected TextFormat(java.lang.Class<T> cls)
getInstance(java.lang.Class)
static method.
cls
- the class for which this format is the default format or
null
to return an unbounded format.
java.lang.IllegalArgumentException
- if the specified class has already a
default text format associated with (which can only be overriden
by local formats, see setInstance(javolution.text.TextFormat, java.lang.Class)
Method Detail |
---|
public static <T> TextFormat<T> getDefault(java.lang.Class<? extends T> forClass)
Returns the default text format for instances of specified type. If there is no text format for the specified type, a text format for the implementing interfaces is searched. If still none is found a recurcive search is performed on the parent class.
Default format instances are typically defined as protected
static final
instances in their bound
class.
The following predefined types have a default format:
forClass
- the class for which the default format is returned.
null
is none found for the class itself, its
parent classes or implementing interfaces.public static <T> TextFormat<T> getInstance(java.lang.Class<? extends T> forClass)
Returns the current text format for instances of specified type. If there is no text format for the specified type, a text format for the implementing interfaces is searched. If still none is found a recurcive search is performed on the parent class.
If the text format has not been overriden
then the default
is returned.
forClass
- the class for which the current format is returned.
null
if none.LocalMap
public static <T> void setInstance(TextFormat<T> format, java.lang.Class<T> forClass)
context-local
).
format
- the format for instances of the specified class.forClass
- the class for which the text format is overriden.getInstance(java.lang.Class extends T>)
,
LocalMap
public final java.lang.Class<T> getBoundClass()
null
if this text format is not the default format
for the specified class.
null
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 java.lang.Appendable 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 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).public java.lang.String toString()
toString
in class java.lang.Object
|
J avolution v5.4 (J2SE 1.6+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |