Top Description Fields Constructors Methods
org.joda.convert

public final Class StringConvert

extends Object
Class Inheritance
Imports
java.lang.reflect.Constructor, .Method, .Modifier, java.util.Arrays, .SimpleTimeZone, .TimeZone, java.util.concurrent.ConcurrentHashMap, .ConcurrentMap, .CopyOnWriteArrayList, org.joda.convert.factory.BooleanArrayStringConverterFactory, .BooleanObjectArrayStringConverterFactory, .ByteObjectArrayStringConverterFactory, .CharObjectArrayStringConverterFactory, .NumericArrayStringConverterFactory, .NumericObjectArrayStringConverterFactory

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

Modifier and TypeField and Description
private static final TypedStringConverter<?>
CACHED_NULL

The cached null object.

private final CopyOnWriteArrayList<StringConverterFactory>
factories

The list of factories.

private final ConcurrentMap<Class<?>, FromStringConverter<?>>
fromStrings

The cache of from-strings.

public static final StringConvert
INSTANCE

An immutable global instance.

pack-priv static final boolean
LOG

Errors in class initialization are hard to debug.

private final ConcurrentMap<Class<?>, TypedStringConverter<?>>
registered

The cache of converters.

Constructor Summary

AccessConstructor and Description
public
StringConvert()

Creates a new conversion manager including the JDK converters.

public
StringConvert(boolean
true to include the JDK converters
includeJdkConverters
,
StringConverterFactory...
optional array of factories to use, not null
factories
)

Creates a new conversion manager.

Method Summary

Modifier and TypeMethod and Description
public <
the type to convert to
T
>
T

Returns:

the converted object, may be null
convertFromString
(Class<T>
the class to convert to, not null
cls
,
String
the string to convert, null returns null
str
)

Converts the specified object from a String.

public String

Returns:

the converted string, may be null
convertToString
(Object
the object to convert, null returns null
object
)

Converts the specified object to a String.

public String

Returns:

the converted string, may be null
convertToString
(Class<?>
the class to convert from, not null
cls
,
Object
the object to convert, null returns null
object
)

Converts the specified object to a String.

public static StringConvert

Returns:

the new converter, not null
create
()

Creates a new conversion manager including the extended standard set of converters.

public <
the type of the converter
T
>
StringConverter<T>

Returns:

the converter, not null
findConverter
(final Class<T>
the class to find a converter for, not null
cls
)

Finds a suitable converter for the type.

public StringConverter<Object>

Returns:

the converter, using Object to avoid generics, not null
findConverterNoGenerics
(final Class<?>
the class to find a converter for, not null
cls
)

Finds a suitable converter for the type with open generics.

private <T> TypedStringConverter<T>
findConverterQuiet(final Class<T> cls)

private <
the type of the converter
T
>
Constructor<T>

Returns:

the method to call, null means use toString
findFromStringConstructorByType
(Class<T>
the class to find a method for, not null
cls
)

Finds the conversion method.

public <
the type of the converter
T
>
FromStringConverter<T>

Returns:

the converter, not null
findFromStringConverter
(final Class<T>
the class to find a converter for, not null
cls
)

Finds a suitable from-string converter for the type.

private Method

Returns:

the method to call, null means use toString
findFromStringMethod
(Class<?>
the class to find a method for, not null
cls
,
String
the name of the method to find, not null
methodName
)

Finds the conversion method.

private Method

Returns:

the method to call, null means use toString
findToStringMethod
(Class<?>
the class to find a method for, not null
cls
,
String
the name of the method to find, not null
methodName
)

Finds the conversion method.

public <
the type of the converter
T
>
TypedStringConverter<T>

Returns:

the converter, not null
findTypedConverter
(final Class<T>
the class to find a converter for, not null
cls
)

Finds a suitable converter for the type.

public TypedStringConverter<Object>

Returns:

the converter, using Object to avoid generics, not null
findTypedConverterNoGenerics
(final Class<?>
the class to find a converter for, not null
cls
)

Finds a suitable converter for the type with open generics.

public boolean

Returns:

true if convertible
isConvertible
(final Class<?>
the class to find a converter for, null returns false
cls
)

Checks if a suitable converter exists for the type.

private static Class<?>
pack-priv static Class<?>
loadType(String fullName)

private <
the type of the converter
T
>
TypedStringConverter<T>

Returns:

the converter, not null
lookupConverter
(final Class<T>
the class to find a method for, not null
cls
)

Lookup a converter searching registered and annotated.

public <
the type of the converter
T
>
void
register(final Class<T>
the class to register a converter for, not null
cls
,
StringConverter<T>
the String converter, not null
converter
)

Registers a converter for a specific type.

public <
the type of the converter
T
>
void
register(final Class<T>
the class to register a converter for, not null
cls
,
final ToStringConverter<T>
the to String converter, typically a method reference, not null
toString
,
final FromStringConverter<T>
the from String converter, typically a method reference, not null
fromString
)

Registers a converter for a specific type using two separate converters.

public void
registerFactory(final StringConverterFactory
the converter factory, not null
factory
)

Registers a converter factory.

public <
the type of the converter
T
>
void
registerMethodConstructor(final Class<T>
the class to register a converter for, not null
cls
,
String
the name of the method converting to a string, not null
toStringMethodName
)

Registers a converter for a specific type by method and constructor.

public <
the type of the converter
T
>
void
registerMethods(final Class<T>
the class to register a converter for, not null
cls
,
String
the name of the method converting to a string, not null
toStringMethodName
,
String
the name of the method converting from a string, not null
fromStringMethodName
)

Registers a converter for a specific type by method names.

public String

Returns:

the string representation, never null
toString
()

Overrides java.lang.Object.toString.

Returns a simple string representation of the object.
private void
tryRegister(String
the class name, not null
className
,
String fromStringMethodName)

Tries to register a class using the standard toString/parse pattern.

private void
tryRegisterGuava()

Tries to register the Guava converters class.

private void
tryRegisterJava8()

Tries to register Java 8 classes.

private void
tryRegisterJava8Optionals()

Tries to register the Java 8 optional classes.

private void
tryRegisterThreeTenBackport()

Tries to register ThreeTen backport classes.

private void
tryRegisterThreeTenOld()

Tries to register ThreeTen ThreeTen/JSR-310 classes v0.6.3 and beyond.

private void
tryRegisterTimeZone()

Tries to register the subclasses of TimeZone.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

CACHED_NULLback to summary
private static final TypedStringConverter<?> CACHED_NULL

The cached null object. Ensure this is above public constants.

factoriesback to summary
private final CopyOnWriteArrayList<StringConverterFactory> factories

The list of factories.

fromStringsback to summary
private final ConcurrentMap<Class<?>, FromStringConverter<?>> fromStrings

The cache of from-strings.

INSTANCEback to summary
public static final StringConvert INSTANCE

An immutable global instance.

This instance cannot be added to using register, however annotated classes are picked up. To register your own converters, simply create an instance of this class.

LOGback to summary
pack-priv static final boolean LOG

Errors in class initialization are hard to debug. Set -Dorg.joda.convert.debug=true on the command line to add extra logging to System.err

registeredback to summary
private final ConcurrentMap<Class<?>, TypedStringConverter<?>> registered

The cache of converters.

Constructor Detail

StringConvertback to summary
public StringConvert()

Creates a new conversion manager including the JDK converters.

The convert instance is mutable in a thread-safe manner. Converters may be altered at any time, including the JDK converters. It is strongly recommended to only alter the converters before performing actual conversions.

StringConvertback to summary
public StringConvert(boolean includeJdkConverters, StringConverterFactory... factories)

Creates a new conversion manager.

The convert instance is mutable in a thread-safe manner. Converters may be altered at any time, including the JDK converters. It is strongly recommended to only alter the converters before performing actual conversions.

If specified, the factories will be queried in the order specified.

Parameters
includeJdkConverters:boolean

true to include the JDK converters

factories:StringConverterFactory[]

optional array of factories to use, not null

Method Detail

convertFromStringback to summary
public <T> T convertFromString(Class<T> cls, String str)

Converts the specified object from a String.

This uses findFromStringConverter to provide the converter.

Parameters
<T>
the type to convert to
cls:Class<T>

the class to convert to, not null

str:String

the string to convert, null returns null

Returns:T

the converted object, may be null

Exceptions
RuntimeException:
(or subclass) if unable to convert
convertToStringback to summary
public String convertToString(Object object)

Converts the specified object to a String.

This uses findConverter to provide the converter.

Parameters
object:Object

the object to convert, null returns null

Returns:String

the converted string, may be null

Exceptions
RuntimeException:
(or subclass) if unable to convert
convertToStringback to summary
public String convertToString(Class<?> cls, Object object)

Converts the specified object to a String.

This uses findConverter to provide the converter. The class can be provided to select a more specific converter.

Parameters
cls:Class<?>

the class to convert from, not null

object:Object

the object to convert, null returns null

Returns:String

the converted string, may be null

Exceptions
RuntimeException:
(or subclass) if unable to convert
createback to summary
public static StringConvert create()

Creates a new conversion manager including the extended standard set of converters.

The returned converter is a new instance that includes additional converters:

The convert instance is mutable in a thread-safe manner. Converters may be altered at any time, including the JDK converters. It is strongly recommended to only alter the converters before performing actual conversions.

Returns:StringConvert

the new converter, not null

Since
1.5
findConverterback to summary
public <T> StringConverter<T> findConverter(final Class<T> cls)

Finds a suitable converter for the type.

This returns an instance of StringConverter for the specified class. This is designed for user code where the Class object generics is known.

The search algorithm first searches the registered converters. It then searches for ToString and FromString annotations on the specified class, class hierarchy or immediate parent interfaces. Finally, it handles Enum instances.

Parameters
<T>
the type of the converter
cls:Class<T>

the class to find a converter for, not null

Returns:StringConverter<T>

the converter, not null

Exceptions
RuntimeException:
(or subclass) if no converter found
findConverterNoGenericsback to summary
public StringConverter<Object> findConverterNoGenerics(final Class<?> cls)

Finds a suitable converter for the type with open generics.

This returns an instance of StringConverter for the specified class. This is designed for framework usage where the Class object generics are unknown'?'. The returned type is declared with Object instead of '?' to allow the ToStringConverter to be invoked.

The search algorithm first searches the registered converters. It then searches for ToString and FromString annotations on the specified class, class hierarchy or immediate parent interfaces. Finally, it handles Enum instances.

Parameters
cls:Class<?>

the class to find a converter for, not null

Returns:StringConverter<Object>

the converter, using Object to avoid generics, not null

Exceptions
RuntimeException:
(or subclass) if no converter found
Since
1.5
findConverterQuietback to summary
private <T> TypedStringConverter<T> findConverterQuiet(final Class<T> cls)
Annotations
@SuppressWarnings:unchecked
findFromStringConstructorByTypeback to summary
private <T> Constructor<T> findFromStringConstructorByType(Class<T> cls)

Finds the conversion method.

Parameters
<T>
the type of the converter
cls:Class<T>

the class to find a method for, not null

Returns:Constructor<T>

the method to call, null means use toString

findFromStringConverterback to summary
public <T> FromStringConverter<T> findFromStringConverter(final Class<T> cls)

Finds a suitable from-string converter for the type.

This returns an instance of FromStringConverter for the specified class. In most cases this is identical to findConverter(Class). However, it is permitted to have a FromString annotation without a ToString annotation, and this method catches that use case.

Parameters
<T>
the type of the converter
cls:Class<T>

the class to find a converter for, not null

Returns:FromStringConverter<T>

the converter, not null

Annotations
@SuppressWarnings:unchecked
Exceptions
RuntimeException:
(or subclass) if no converter found
findFromStringMethodback to summary
private Method findFromStringMethod(Class<?> cls, String methodName)

Finds the conversion method.

Parameters
cls:Class<?>

the class to find a method for, not null

methodName:String

the name of the method to find, not null

Returns:Method

the method to call, null means use toString

findToStringMethodback to summary
private Method findToStringMethod(Class<?> cls, String methodName)

Finds the conversion method.

Parameters
cls:Class<?>

the class to find a method for, not null

methodName:String

the name of the method to find, not null

Returns:Method

the method to call, null means use toString

findTypedConverterback to summary
public <T> TypedStringConverter<T> findTypedConverter(final Class<T> cls)

Finds a suitable converter for the type.

This returns an instance of TypedStringConverter for the specified class. This is designed for user code where the Class object generics is known.

The search algorithm first searches the registered converters. It then searches for ToString and FromString annotations on the specified class, class hierarchy or immediate parent interfaces. Finally, it handles Enum instances.

The returned converter may be queried for the effective type of the conversion. This can be used to find the best type to send in a serialized form.

Note

Changing the method return type of findConverter(Class) would be source compatible but not binary compatible. As this is a low-level library, binary compatibility is important, hence the addition of this method.

Parameters
<T>
the type of the converter
cls:Class<T>

the class to find a converter for, not null

Returns:TypedStringConverter<T>

the converter, not null

Exceptions
RuntimeException:
(or subclass) if no converter found
Since
1.7
findTypedConverterNoGenericsback to summary
public TypedStringConverter<Object> findTypedConverterNoGenerics(final Class<?> cls)

Finds a suitable converter for the type with open generics.

This returns an instance of TypedStringConverter for the specified class. This is designed for framework usage where the Class object generics are unknown'?'. The returned type is declared with Object instead of '?' to allow the ToStringConverter to be invoked.

The search algorithm first searches the registered converters. It then searches for ToString and FromString annotations on the specified class, class hierarchy or immediate parent interfaces. Finally, it handles Enum instances.

The returned converter may be queried for the effective type of the conversion. This can be used to find the best type to send in a serialized form.

Note

Changing the method return type of findConverterNoGenerics(Class) would be source compatible but not binary compatible. As this is a low-level library, binary compatibility is important, hence the addition of this method.

Parameters
cls:Class<?>

the class to find a converter for, not null

Returns:TypedStringConverter<Object>

the converter, using Object to avoid generics, not null

Annotations
@SuppressWarnings:unchecked
Exceptions
RuntimeException:
(or subclass) if no converter found
Since
1.7
isConvertibleback to summary
public boolean isConvertible(final Class<?> cls)

Checks if a suitable converter exists for the type.

This performs the same checks as the findConverter methods. Calling this before findConverter will cache the converter.

Note that all exceptions, including developer errors are caught and hidden.

Parameters
cls:Class<?>

the class to find a converter for, null returns false

Returns:boolean

true if convertible

Since
1.5
loadPrimitiveTypeback to summary
private static Class<?> loadPrimitiveType(String fullName, ClassNotFoundException ex) throws ClassNotFoundException
loadTypeback to summary
pack-priv static Class<?> loadType(String fullName) throws ClassNotFoundException
lookupConverterback to summary
private <T> TypedStringConverter<T> lookupConverter(final Class<T> cls)

Lookup a converter searching registered and annotated.

Parameters
<T>
the type of the converter
cls:Class<T>

the class to find a method for, not null

Returns:TypedStringConverter<T>

the converter, not null

Annotations
@SuppressWarnings:unchecked
Exceptions
RuntimeException:
if invalid
registerback to summary
public <T> void register(final Class<T> cls, StringConverter<T> converter)

Registers a converter for a specific type.

The converter will be used for subclasses unless overidden.

No new converters may be registered for the global singleton.

Parameters
<T>
the type of the converter
cls:Class<T>

the class to register a converter for, not null

converter:StringConverter<T>

the String converter, not null

Exceptions
IllegalArgumentException:
if the class or converter are null
IllegalStateException:
if trying to alter the global singleton
registerback to summary
public <T> void register(final Class<T> cls, final ToStringConverter<T> toString, final FromStringConverter<T> fromString)

Registers a converter for a specific type using two separate converters.

This method registers a converter for the specified class. It is primarily intended for use with JDK 1.8 method references or lambdas:

 sc.register(Distance.class, Distance::toString, Distance::parse);
The converter will be used for subclasses unless overidden.

No new converters may be registered for the global singleton.

Parameters
<T>
the type of the converter
cls:Class<T>

the class to register a converter for, not null

toString:ToStringConverter<T>

the to String converter, typically a method reference, not null

fromString:FromStringConverter<T>

the from String converter, typically a method reference, not null

Exceptions
IllegalArgumentException:
if the class or converter are null
IllegalStateException:
if trying to alter the global singleton
Since
1.3
registerFactoryback to summary
public void registerFactory(final StringConverterFactory factory)

Registers a converter factory.

This will be registered ahead of all existing factories.

No new factories may be registered for the global singleton.

Parameters
factory:StringConverterFactory

the converter factory, not null

Exceptions
IllegalStateException:
if trying to alter the global singleton
Since
1.5
registerMethodConstructorback to summary
public <T> void registerMethodConstructor(final Class<T> cls, String toStringMethodName)

Registers a converter for a specific type by method and constructor.

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");

Parameters
<T>
the type of the converter
cls:Class<T>

the class to register a converter for, not null

toStringMethodName:String

the name of the method converting to a string, not null

Exceptions
IllegalArgumentException:
if the class or method name are null or invalid
IllegalStateException:
if trying to alter the global singleton
registerMethodsback to summary
public <T> void registerMethods(final Class<T> cls, String toStringMethodName, String fromStringMethodName)

Registers a converter for a specific type by method names.

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");

Parameters
<T>
the type of the converter
cls:Class<T>

the class to register a converter for, not null

toStringMethodName:String

the name of the method converting to a string, not null

fromStringMethodName:String

the name of the method converting from a string, not null

Exceptions
IllegalArgumentException:
if the class or method name are null or invalid
IllegalStateException:
if trying to alter the global singleton
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns a simple string representation of the object.

Returns:String

the string representation, never null

Annotations
@Override
tryRegisterback to summary
private void tryRegister(String className, String fromStringMethodName) throws ClassNotFoundException

Tries to register a class using the standard toString/parse pattern.

Parameters
className:String

the class name, not null

Exceptions
ClassNotFoundException:
if the class does not exist
tryRegisterGuavaback to summary
private void tryRegisterGuava()

Tries to register the Guava converters class.

tryRegisterJava8back to summary
private void tryRegisterJava8()

Tries to register Java 8 classes.

tryRegisterJava8Optionalsback to summary
private void tryRegisterJava8Optionals()

Tries to register the Java 8 optional classes.

tryRegisterThreeTenBackportback to summary
private void tryRegisterThreeTenBackport()

Tries to register ThreeTen backport classes.

tryRegisterThreeTenOldback to summary
private void tryRegisterThreeTenOld()

Tries to register ThreeTen ThreeTen/JSR-310 classes v0.6.3 and beyond.

tryRegisterTimeZoneback to summary
private void tryRegisterTimeZone()

Tries to register the subclasses of TimeZone. Try various things, doesn't matter if the map entry gets overwritten.