@Parallelizable public abstract class TextFormat<T> extends Object
The service for plain text parsing and formatting;
it supports CharSequence
and Appendable
interfaces
for greater flexibility.
Instances of this class are typically retrieved from the
current TextContext
(OSGi service or not).
@DefaultTextFormat(Complex.Cartesian.class) public class Complex extends Number { public static Complex valueOf(CharSequence csq) { return TextContext.getFormat(Complex.class).parse(csq); } public String toString() { return TextContext.getFormat(Complex.class).format(this); } public static class Cartesian extends javolution.text.TextFormat<Complex> { ... } public static class Polar extends javolution.text.TextFormat<Complex> { ... } }
Text formats can be locally overridden.
TextContext ctx = TextContext.enter(); try { ctx.setFormat(Complex.class, Complex.Polar.class); // No impact on others threads. System.out.println(complexMatrix); // Displays complex numbers in polar coordinates. } finally { ctx.exit(); // Reverts to previous cartesian format for complex numbers. }
For parsing/formatting of primitive types, the TypeFormat
utility class is recommended.
Constructor and Description |
---|
TextFormat() |
Modifier and Type | Method and Description |
---|---|
String |
format(T obj)
Convenience method to format the specified object to a
String . |
abstract Appendable |
format(T obj,
Appendable dest)
Formats the specified object into an
Appendable |
TextBuilder |
format(T obj,
TextBuilder dest)
Convenience method to format the specified object to a
TextBuilder ;
unlike the abstract format method, this method does not throw IOException . |
T |
parse(CharSequence csq)
Convenience method to parse the whole character sequence; if there are
unread extraneous characters after parsing then an exception is raised.
|
abstract T |
parse(CharSequence csq,
Cursor cursor)
Reads a portion of the specified
CharSequence from the
specified cursor position to produce an object. |
public abstract T parse(CharSequence csq, Cursor cursor)
CharSequence
from the
specified cursor 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 character sequence to parse.cursor
- the cursor holding the current parsing index.IllegalArgumentException
- if the syntax of the specified
character sequence is incorrect.UnsupportedOperationException
- if parsing is not supported.public abstract Appendable format(T obj, Appendable dest) throws IOException
Appendable
obj
- the object to format.dest
- the appendable destination.Appendable
.IOException
public T parse(CharSequence csq) throws IllegalArgumentException
csq
- the CharSequence
to parse from the first character
to the last.IllegalArgumentException
- if the syntax of the specified
character sequence is incorrect or if there are extraneous
characters at the end not parsed.public TextBuilder format(T obj, TextBuilder dest)
TextBuilder
;
unlike the abstract format method, this method does not throw IOException
.obj
- the object to format.dest
- the appendable destination.TextBuilder
.Copyright © 2005-2013 Javolution. All Rights Reserved.