Class TextFormat<T>
- java.lang.Object
-
- javolution37.javolution.lang.TextFormat<T>
-
public abstract class TextFormat<T> extends java.lang.ObjectThis class represents the base format for text parsing and formatting; it supports
CharSequenceandAppendableinterfaces for greater flexibility.The default format (used by
valueOf(CharSequence),toString()ortoText()) can belocally scoped. For example:public class Complex extends RealtimeObject { public static final LocalReference<TextFormat<Complex>> FORMAT = new LocalReference<TextFormat<Complex>>(new TextFormat<Complex>() { ... // Default format (cartesien form). }); public Complex valueOf(CharSequence csq) { return FORMAT.get().parse(csq); } public Text toText() { return FORMAT.get().format(this); } } ... Matrix<Complex> M = ...; LocalContext.enter(); try { Complex.FORMAT.set(POLAR); // Current thread displays complex numbers System.out.prinln(M); // using the polar form. } finally { LocalContext.exit(); // Current thread reverts to previous format. }For parsing/formatting of primitive types, the
TypeFormatutility class is recommended.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classTextFormat.CursorThis class represents a parsing cursor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description Textformat(T obj)Formats the specified object to aTextinstance (convenience method).abstract java.lang.Appendableformat(T obj, java.lang.Appendable dest)Formats the specified object into anAppendableTparse(java.lang.CharSequence csq)Parses a whole character sequence from the beginning to produce an object (convenience method).abstract Tparse(java.lang.CharSequence csq, TextFormat.Cursor cursor)Parses a portion of the specifiedCharSequencefrom the specified position to produce an object.
-
-
-
Method Detail
-
format
public abstract java.lang.Appendable format(T obj, java.lang.Appendable dest) throws java.io.IOException
Formats the specified object into anAppendable- Parameters:
obj- the object to format.dest- the appendable destination.- Returns:
- the specified
Appendable. - Throws:
java.io.IOException- if an I/O exception occurs.
-
parse
public abstract T parse(java.lang.CharSequence csq, TextFormat.Cursor cursor)
Parses a portion of the specifiedCharSequencefrom the specified position to produce an object. If parsing succeeds, then the index of thecursorargument is updated to the index after the last character used.- Parameters:
csq- theCharSequenceto parse.cursor- the cursor holding the current parsing index.- Returns:
- the object parsed from the specified character sub-sequence.
- Throws:
java.lang.RuntimeException- if any problem occurs while parsing the specified character sequence (e.g. illegal syntax).
-
format
public final Text format(T obj)
Formats the specified object to aTextinstance (convenience method).- Parameters:
obj- the object being formated.- Returns:
- the text representing the specified object.
-
parse
public final T parse(java.lang.CharSequence csq)
Parses a whole character sequence from the beginning to produce an object (convenience method).- Parameters:
csq- the whole character sequence to parse.- Returns:
- the corresponding object.
- Throws:
java.lang.IllegalArgumentException- if the specified character sequence cannot be fully parsed.
-
-
DMelt 3.0 © DataMelt by jWork.ORG