|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.joda.convert.StringConvert
public final class StringConvert
Manager for conversion to and from a String
, acting as the main client interface.
Support is provided for conversions based on the StringConverter
interface
or the ToString
and FromString
annotations.
StringConvert is thread-safe with concurrent caches.
Field Summary | |
---|---|
static StringConvert |
INSTANCE
An immutable global instance. |
Constructor Summary | |
---|---|
StringConvert()
Creates a new conversion manager including the JDK converters. |
|
StringConvert(boolean includeJdkConverters)
Creates a new conversion manager. |
Method Summary | ||
---|---|---|
|
convertFromString(java.lang.Class<T> cls,
java.lang.String str)
Converts the specified object from a String . |
|
|
convertToString(java.lang.Class<T> cls,
T object)
Converts the specified object to a String . |
|
|
convertToString(T object)
Converts the specified object to a String . |
|
|
findConverter(java.lang.Class<T> cls)
Finds a suitable converter for the type. |
|
|
register(java.lang.Class<T> cls,
StringConverter<T> converter)
Registers a converter for a specific type. |
|
|
registerMethodConstructor(java.lang.Class<T> cls,
java.lang.String toStringMethodName)
Registers a converter for a specific type by method and constructor. |
|
|
registerMethods(java.lang.Class<T> cls,
java.lang.String toStringMethodName,
java.lang.String fromStringMethodName)
Registers a converter for a specific type by method names. |
|
java.lang.String |
toString()
Returns a simple string representation of the object. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final StringConvert INSTANCE
This instance cannot be added to using register(java.lang.Class
, however annotated classes
are picked up. To register your own converters, simply create an instance of this class.
Constructor Detail |
---|
public StringConvert()
public StringConvert(boolean includeJdkConverters)
includeJdkConverters
- true to include the JDK convertersMethod Detail |
---|
public <T> java.lang.String convertToString(T object)
String
.
This uses findConverter(java.lang.Class
to provide the converter.
T
- the type to convert fromobject
- the object to convert, null returns null
java.lang.RuntimeException
- (or subclass) if unable to convertpublic <T> java.lang.String convertToString(java.lang.Class<T> cls, T object)
String
.
This uses findConverter(java.lang.Class
to provide the converter.
The class can be provided to select a more specific converter.
T
- the type to convert fromcls
- the class to convert from, not nullobject
- the object to convert, null returns null
java.lang.RuntimeException
- (or subclass) if unable to convertpublic <T> T convertFromString(java.lang.Class<T> cls, java.lang.String str)
String
.
This uses findConverter(java.lang.Class
to provide the converter.
T
- the type to convert tocls
- the class to convert to, not nullstr
- the string to convert, null returns null
java.lang.RuntimeException
- (or subclass) if unable to convertpublic <T> StringConverter<T> findConverter(java.lang.Class<T> cls)
This returns an instance of StringConverter
for the specified class.
This could be useful in other frameworks.
The search algorithm first searches the registered converters.
It then searches for ToString
and FromString
annotations on the specified class.
Both searches consider superclasses, but not interfaces.
T
- the type of the convertercls
- the class to find a converter for, not null
java.lang.RuntimeException
- (or subclass) if no converter foundpublic <T> void register(java.lang.Class<T> cls, StringConverter<T> converter)
The converter will be used for subclasses unless overidden.
No new converters may be registered for the global singleton.
T
- the type of the convertercls
- the class to register a converter for, not nullconverter
- the String converter, not null
java.lang.IllegalArgumentException
- if unable to register
java.lang.IllegalStateException
- if class already registeredpublic <T> void registerMethods(java.lang.Class<T> cls, java.lang.String toStringMethodName, java.lang.String fromStringMethodName)
This method allows the converter to be used when the target class cannot have annotations added.
The two method names must obey the same rules as defined by the annotations
ToString
and FromString
.
The converter will be used for subclasses unless overidden.
No new converters may be registered for the global singleton.
For example, convert.registerMethods(Distance.class, "toString", "parse");
T
- the type of the convertercls
- the class to register a converter for, not nulltoStringMethodName
- the name of the method converting to a string, not nullfromStringMethodName
- the name of the method converting from a string, not null
java.lang.IllegalArgumentException
- if unable to register
java.lang.IllegalStateException
- if class already registeredpublic <T> void registerMethodConstructor(java.lang.Class<T> cls, java.lang.String toStringMethodName)
This method allows the converter to be used when the target class cannot have annotations added.
The two method name and constructor must obey the same rules as defined by the annotations
ToString
and FromString
.
The converter will be used for subclasses unless overidden.
No new converters may be registered for the global singleton.
For example, convert.registerMethodConstructor(Distance.class, "toString");
T
- the type of the convertercls
- the class to register a converter for, not nulltoStringMethodName
- the name of the method converting to a string, not null
java.lang.IllegalArgumentException
- if unable to register
java.lang.IllegalStateException
- if class already registeredpublic java.lang.String toString()
toString
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |