Javolution 6.0.0 java
javolution.text.TextFormat< T > Class Template Referenceabstract
Inheritance diagram for javolution.text.TextFormat< T >:
[legend]

Public Member Functions

abstract T parse (CharSequence csq, Cursor cursor)
 
abstract Appendable format (T obj, Appendable dest) throws IOException
 
parse (CharSequence csq) throws IllegalArgumentException
 
TextBuilder format (T obj, TextBuilder dest)
 
String format (T obj)
 

Detailed Description

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). [code] @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> { ... } }[/code]

Text formats can be locally overridden. [code] 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. }[/code]

For parsing/formatting of primitive types, the TypeFormat utility class is recommended.

Author
Jean-Marie Dautelle
Version
6.0, July 21, 2013

Definition at line 52 of file TextFormat.java.

Member Function Documentation

◆ format() [1/3]

String javolution.text.TextFormat< T >.format ( obj)

Convenience method to format the specified object to a String.

Parameters
objthe object to format.
Returns
the formatting result as a string.

Definition at line 121 of file TextFormat.java.

121  {
122  return this.format(obj, new TextBuilder()).toString();
123 
124  }

◆ format() [2/3]

abstract Appendable javolution.text.TextFormat< T >.format ( obj,
Appendable  dest 
) throws IOException
abstract

Formats the specified object into an Appendable

Parameters
objthe object to format.
destthe appendable destination.
Returns
the specified Appendable.

Referenced by javolution.text.TextBuilder.append(), javolution.text.TextFormat< FastCollection<?> >.format(), and javolution.util.FastCollection< E >.Format.format().

Here is the caller graph for this function:

◆ format() [3/3]

TextBuilder javolution.text.TextFormat< T >.format ( obj,
TextBuilder  dest 
)

Convenience method to format the specified object to a TextBuilder; unlike the abstract format method, this method does not throw IOException.

Parameters
objthe object to format.
destthe appendable destination.
Returns
the specified TextBuilder.

Definition at line 106 of file TextFormat.java.

106  {
107  try {
108  this.format(obj, (Appendable) dest);
109  return dest;
110  } catch (IOException e) {
111  throw new Error(e); // Cannot happens.
112  }
113  }

◆ parse() [1/2]

T javolution.text.TextFormat< T >.parse ( CharSequence  csq) throws IllegalArgumentException

Convenience method to parse the whole character sequence; if there are unread extraneous characters after parsing then an exception is raised.

Parameters
csqthe CharSequence to parse from the first character to the last.
Exceptions
IllegalArgumentExceptionif the syntax of the specified character sequence is incorrect or if there are extraneous characters at the end not parsed.

Definition at line 89 of file TextFormat.java.

89  {
90  Cursor cursor = new Cursor();
91  T obj = parse(csq, cursor);
92  if (!cursor.atEnd(csq))
93  throw new IllegalArgumentException("Extraneous character(s) \""
94  + cursor.tail(csq) + "\"");
95  return obj;
96  }

◆ parse() [2/2]

abstract T javolution.text.TextFormat< T >.parse ( CharSequence  csq,
Cursor  cursor 
)
abstract

Reads a portion of the specified 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.

Parameters
csqthe character sequence to parse.
cursorthe cursor holding the current parsing index.
Returns
the object parsed.
Exceptions
IllegalArgumentExceptionif the syntax of the specified character sequence is incorrect.
UnsupportedOperationExceptionif parsing is not supported.

Reimplemented in javolution.util.FastCollection< E >.Format, and javolution.util.Index.Decimal.

Referenced by javolution.xml.XMLFormat< T >.InputElement.getAttribute(), javolution.xml.XMLFormat< T >.Default.newInstance(), and javolution.text.TextFormat< FastCollection<?> >.parse().

Here is the caller graph for this function:

The documentation for this class was generated from the following file:
javolution.text.TextFormat.parse
abstract T parse(CharSequence csq, Cursor cursor)
javolution.text.TextFormat.format
abstract Appendable format(T obj, Appendable dest)