public abstract class TextFormat extends 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.
[code]
public class Complex extends Number {
// Defines the default format for complex numbers (Cartesian form)
protected static TextFormat
For parsing/formatting of primitive types, the TypeFormat
utility class is recommended.
Modifier | Constructor and Description |
---|---|
protected |
TextFormat(Class forClass)
Defines the static format bound to the specified class.
|
Modifier and Type | Method and Description |
---|---|
Text |
format(Object obj)
Formats the specified object to a
Text instance
(convenience method equivalent to
format(obj, TextBuilder.newInstance()).toText() ). |
abstract Appendable |
format(Object obj,
Appendable dest)
Formats the specified object into an
Appendable |
TextBuilder |
format(Object obj,
TextBuilder dest)
Formats the specified object into a
TextBuilder (convenience
method which does not raise IOException). |
String |
formatToString(Object obj)
Convenience methods equivalent to but faster than
format(obj).toString()) |
static TextFormat |
getDefault(Class forClass)
Returns the default format for instances of the specified class.
|
static TextFormat |
getInstance(Class forClass)
Returns the current format for instances of the specified class.
|
boolean |
isParsingSupported()
Indicates if this format supports parsing (default
true ). |
Object |
parse(CharSequence csq)
Parses a whole character sequence from the beginning to produce an object
(convenience method).
|
abstract Object |
parse(CharSequence csq,
Cursor cursor)
Parses a portion of the specified
CharSequence from the
specified position to produce an object. |
static void |
setInstance(Class forClass,
TextFormat format)
Overrides the default format for the specified class (
local setting ). |
protected TextFormat(Class forClass)
forClass
- the class to which the format is bound or null
if the format is not bound to any class.IllegalArgumentException
- if the specified class is already
bound to another format.public static TextFormat getDefault(Class 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 TextFormat getInstance(Class forClass)
Returns the current format for instances of the specified class.
forClass
- the class to which a format has been bound.public static void setInstance(Class forClass, TextFormat format)
local setting
).forClass
- the class for which the format is locally overriden.format
- the new format (typically unbound).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 Appendable format(Object obj, Appendable dest) throws IOException
Appendable
obj
- the object to format.dest
- the appendable destination.Appendable
.IOException
- if an I/O exception occurs.public abstract Object parse(CharSequence csq, Cursor cursor) throws 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.IllegalArgumentException
- if any problem occurs while parsing the
specified character sequence (e.g. illegal syntax).public final TextBuilder format(Object 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(Object obj)
Text
instance
(convenience method equivalent to
format(obj, TextBuilder.newInstance()).toText()
).obj
- the object being formated.public final String formatToString(Object obj)
format(obj).toString())
obj
- the object being formated.public final Object parse(CharSequence csq) throws IllegalArgumentException
csq
- the whole character sequence to parse.parse(csq, new Cursor())
IllegalArgumentException
- if the specified character sequence
cannot be fully parsed (e.g. extraneous characters).Copyright © 2005–2013 Javolution. All rights reserved.