fr.umlv.tatoo.runtime.util
Class DefaultStringConverter

java.lang.Object
  extended by fr.umlv.tatoo.runtime.util.DefaultStringConverter
All Implemented Interfaces:
StringConverter

public class DefaultStringConverter
extends Object
implements StringConverter

Default converter implementation. This converter convert a string to a value by using of one of its 'parse' method.

  1. The 'parse' method are recognized by the converter if a method obey to the following rules :
  2. The converter choose the 'parse' method that have the most specific type comparing with the type argument of the method convert(String,T.class).

Author:
Remi

Nested Class Summary
static interface DefaultStringConverter.ParseMethod
          Marker annotation on method, indicates a 'parse' method.
 
Constructor Summary
DefaultStringConverter()
          Create a converter.
 
Method Summary
<T> T
convert(String value, Class<T> clazz)
          Convert a string to a value of type T.
protected  boolean parseBoolean(String text)
          Converts a text to a boolean
protected  char parseChar(String text)
          Converts a text to a single char.
protected  double parseDouble(String text)
          Converts a text to a double.
protected
<E extends Enum<E>>
E
parseEnum(String text, Class<E> clazz)
          Convert a text containing the name of an enumeration item to its value.
protected  int parseInt(String text)
          Converts a text to an integer.
protected  String parseString(String text)
          Converts a text to a string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultStringConverter

public DefaultStringConverter()
Create a converter.

Method Detail

parseChar

protected char parseChar(String text)
Converts a text to a single char.

Parameters:
text - the text to parse.
Returns:
a single char.
Throws:
NumberFormatException

parseString

protected String parseString(String text)
Converts a text to a string.

Parameters:
text - the text to parse.
Returns:
the texte take as argument.

parseInt

protected int parseInt(String text)
Converts a text to an integer.

Parameters:
text - text to parse.
Returns:
an integer corresponding to the text.
Throws:
NumberFormatException - if the text is not an integer.

parseDouble

protected double parseDouble(String text)
Converts a text to a double.

Parameters:
text - text to parse.
Returns:
a double corresponding to the text.
Throws:
NumberFormatException - if the text is not a double.

parseBoolean

protected boolean parseBoolean(String text)
Converts a text to a boolean

Parameters:
text - text to parse.
Returns:
a boolean corresponding to the text.
Throws:
NumberFormatException - if the text is not a boolean.

parseEnum

protected <E extends Enum<E>> E parseEnum(String text,
                                          Class<E> clazz)
Convert a text containing the name of an enumeration item to its value.

Type Parameters:
E - type of the enumeration.
Parameters:
text - texte to parse.
clazz - the class of the enumeration.
Returns:
the corresponding enumeration object.
Throws:
IllegalArgumentException - if the text isn't a name of one of the item of the enumeration.

convert

public <T> T convert(String value,
                     Class<T> clazz)
Description copied from interface: StringConverter
Convert a string to a value of type T.

Specified by:
convert in interface StringConverter
Type Parameters:
T - the type of the value.
Parameters:
value - text representation of the value.
clazz - type of the value.
Returns:
the converted value.