org.hyphenType.util.soc
Class StringObjectConversion

java.lang.Object
  extended by org.hyphenType.util.soc.StringObjectConversion

public final class StringObjectConversion
extends Object

Converts strings to objects and vice-versa.

Author:
Aurelio Akira M. Matsui

Method Summary
static
<T> T
fromString(Class<? extends T> clazz, String value)
          Translates a string into an object.
static
<T> T
fromString(Class<? extends T> clazz, String value, boolean arrayForgiving)
          Translates a string into an object.
static
<T> T
fromString(Class<? extends T> clazz, String value, boolean arrayForgiving, String separator)
          Translates a string into an object.
static
<T> T
fromString(Class<? extends T> clazz, String value, boolean arrayForgiving, String arrayStart, String arrayEnd, String arraySeparator)
          Translates a string into an object.
static Object toPrimitive(Class clazz, Object object)
          Parses the object into a primitive.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fromString

public static <T> T fromString(Class<? extends T> clazz,
                               String value)
                    throws StringParsingError
Translates a string into an object. The algorithm used is as follows. If clazz is String, simply returns the string value received. If clazz is primitive or a wrapper for a primitive, returns the result of calling a valueOf method. If clazz is of a type Class, it searches for the class using Class.forName(String). Finally, if clazz does not match any of those tests, tries to construct a new object, guessing that clazz has a constructor that receives only a String.

Type Parameters:
T - The type to convert to.
Parameters:
clazz - The same as the type T.
value - The string value of the object.
Returns:
The object equivalent to the string.
Throws:
StringParsingError - If there is any reflection problem.

fromString

public static <T> T fromString(Class<? extends T> clazz,
                               String value,
                               boolean arrayForgiving)
                    throws StringParsingError
Translates a string into an object. The algorithm used is as follows. If clazz is String, simply returns the string value received. If clazz is primitive or a wrapper for a primitive, returns the result of calling a valueOf method. If clazz is of a type Class, it searches for the class using Class.forName(String). Finally, if clazz does not match any of those tests, tries to construct a new object, guessing that clazz has a constructor that receives only a String.

Type Parameters:
T - The type to convert to.
Parameters:
clazz - The same as the type T.
value - The string value of the object.
arrayForgiving - Whether it will forgive the absence of square brackets.
Returns:
The object equivalent to the string.
Throws:
StringParsingError - If there is any reflection problem.

fromString

public static <T> T fromString(Class<? extends T> clazz,
                               String value,
                               boolean arrayForgiving,
                               String separator)
                    throws StringParsingError
Translates a string into an object. The algorithm used is as follows. If clazz is String, simply returns the string value received. If clazz is primitive or a wrapper for a primitive, returns the result of calling a valueOf method. If clazz is of a type Class, it searches for the class using Class.forName(String). Finally, if clazz does not match any of those tests, tries to construct a new object, guessing that clazz has a constructor that receives only a String.

Type Parameters:
T - The type to convert to.
Parameters:
clazz - The same as the type T.
value - The string value of the object.
arrayForgiving - Whether it will forgive the absence of square brackets.
Returns:
The object equivalent to the string.
Throws:
StringParsingError - If there is any reflection problem.

fromString

public static <T> T fromString(Class<? extends T> clazz,
                               String value,
                               boolean arrayForgiving,
                               String arrayStart,
                               String arrayEnd,
                               String arraySeparator)
                    throws StringParsingError
Translates a string into an object. The algorithm used is as follows. If clazz is String, simply returns the string value received. If clazz is primitive or a wrapper for a primitive, returns the result of calling a valueOf method. If clazz is of a type Class, it searches for the class using Class.forName(String). Finally, if clazz does not match any of those tests, tries to construct a new object, guessing that clazz has a constructor that receives only a String.

Type Parameters:
T - The type to convert to.
Parameters:
clazz - The same as the type T.
value - The string value of the object.
arrayForgiving - Whether it will forgive the absence of square brackets.
arrayStart -
arrayEng -
arraySeparator -
Returns:
Throws:
StringParsingError

toPrimitive

public static Object toPrimitive(Class clazz,
                                 Object object)
Parses the object into a primitive.

Parameters:
clazz - The primitive class.
object - The object that will be converted to a primitive.
Returns:
The equivalent primitive, or null if there is no equivalent.


Copyright © 2013. All Rights Reserved.