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.
| Modifier and Type | Field and Description |
|---|---|
| private static final TypedStringConverter | CACHED_NULL
The cached null object. |
| private final CopyOnWriteArrayList | factories
The list of factories. |
| private final ConcurrentMap | 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 | registered
The cache of converters. |
| Access | Constructor and Description |
|---|---|
| public | |
| 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. |
| Modifier and Type | Method and Description |
|---|---|
| public < the type to convert to T> T | Returns: the converted object, may be nullthe class to convert to, not null cls, String the string to convert, null returns null str)Converts the specified object from a |
| public String | Returns: the converted string, may be nullthe object to convert, null returns null object)Converts the specified object to a |
| public String | Returns: the converted string, may be nullthe class to convert from, not null cls, Object the object to convert, null returns null object)Converts the specified object to a |
| public static StringConvert | Returns: the new converter, not nullCreates a new conversion manager including the extended standard set of converters. |
| public < the type of the converter T> StringConverter | Returns: the converter, not nullthe class to find a converter for, not null cls)Finds a suitable converter for the type. |
| public StringConverter | Returns: the converter, usingObject to avoid generics, not nullthe class to find a converter for, not null cls)Finds a suitable converter for the type with open generics. |
| private <T> TypedStringConverter | |
| private < the type of the converter T> Constructor | Returns: the method to call, null means usetoStringthe class to find a method for, not null cls)Finds the conversion method. |
| public < the type of the converter T> FromStringConverter | Returns: the converter, not nullthe 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 usetoStringthe 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 usetoStringthe 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 | Returns: the converter, not nullthe class to find a converter for, not null cls)Finds a suitable converter for the type. |
| public TypedStringConverter | Returns: the converter, usingObject to avoid generics, not nullthe class to find a converter for, not null cls)Finds a suitable converter for the type with open generics. |
| public boolean | Returns: true if convertiblethe 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 | |
| private < the type of the converter T> TypedStringConverter | Returns: the converter, not nullthe 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 nullOverrides java. |
| 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 | |
| private void | |
| private void | |
| private void | |
| private void | |
| private void |
| CACHED_NULL | back to summary |
|---|---|
| private static final TypedStringConverter<?> CACHED_NULL The cached null object. Ensure this is above public constants. | |
| factories | back to summary |
|---|---|
| private final CopyOnWriteArrayList<StringConverterFactory> factories The list of factories. | |
| fromStrings | back to summary |
|---|---|
| private final ConcurrentMap<Class<?>, FromStringConverter<?>> fromStrings The cache of from-strings. | |
| INSTANCE | back to summary |
|---|---|
| public static final StringConvert INSTANCE An immutable global instance.
This instance cannot be added to using | |
| LOG | back 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 | |
| registered | back to summary |
|---|---|
| private final ConcurrentMap<Class<?>, TypedStringConverter<?>> registered The cache of converters. | |
| StringConvert | back 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. | |
| StringConvert | back 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.
| |
| convertFromString | back to summary |
|---|---|
| public <T> T convertFromString(Class<T> cls, String str) Converts the specified object from a
This uses
| |
| convertToString | back to summary |
|---|---|
| public String convertToString(Object object) Converts the specified object to a
This uses
| |
| convertToString | back to summary |
|---|---|
| public String convertToString(Class<?> cls, Object object) Converts the specified object to a
This uses
| |
| create | back 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.
| |
| findConverter | back to summary |
|---|---|
| public <T> StringConverter Finds a suitable converter for the type.
This returns an instance of
The search algorithm first searches the registered converters.
It then searches for
| |
| findConverterNoGenerics | back to summary |
|---|---|
| public StringConverter Finds a suitable converter for the type with open generics.
This returns an instance of
The search algorithm first searches the registered converters.
It then searches for
| |
| findConverterQuiet | back to summary |
|---|---|
private <T> TypedStringConverter
| |
| findFromStringConstructorByType | back to summary |
|---|---|
| private <T> Constructor Finds the conversion method.
| |
| findFromStringConverter | back to summary |
|---|---|
| public <T> FromStringConverter Finds a suitable from-string converter for the type.
This returns an instance of
| |
| findFromStringMethod | back to summary |
|---|---|
| private Method findFromStringMethod(Class<?> cls, String methodName) Finds the conversion method. | |
| findToStringMethod | back to summary |
|---|---|
| private Method findToStringMethod(Class<?> cls, String methodName) Finds the conversion method. | |
| findTypedConverter | back to summary |
|---|---|
| public <T> TypedStringConverter Finds a suitable converter for the type.
This returns an instance of
The search algorithm first searches the registered converters.
It then searches for 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
| |
| findTypedConverterNoGenerics | back to summary |
|---|---|
| public TypedStringConverter Finds a suitable converter for the type with open generics.
This returns an instance of
The search algorithm first searches the registered converters.
It then searches for 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
| |
| isConvertible | back to summary |
|---|---|
| public boolean isConvertible(final Class<?> cls) Checks if a suitable converter exists for the type.
This performs the same checks as the Note that all exceptions, including developer errors are caught and hidden.
| |
| loadPrimitiveType | back to summary |
|---|---|
| private static Class | |
| loadType | back to summary |
|---|---|
| pack-priv static Class | |
| lookupConverter | back to summary |
|---|---|
| private <T> TypedStringConverter Lookup a converter searching registered and annotated.
| |
| register | back 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.
| |
| register | back 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.
| |
| registerFactory | back 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.
| |
| registerMethodConstructor | back 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
No new converters may be registered for the global singleton.
For example,
| |
| registerMethods | back 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
No new converters may be registered for the global singleton.
For example,
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Returns a simple string representation of the object. | |
| tryRegister | back to summary |
|---|---|
| private void tryRegister(String className, String fromStringMethodName) throws ClassNotFoundException Tries to register a class using the standard toString/parse pattern.
| |
| tryRegisterGuava | back to summary |
|---|---|
| private void tryRegisterGuava() Tries to register the Guava converters class. | |
| tryRegisterJava8 | back to summary |
|---|---|
| private void tryRegisterJava8() Tries to register Java 8 classes. | |
| tryRegisterJava8Optionals | back to summary |
|---|---|
| private void tryRegisterJava8Optionals() Tries to register the Java 8 optional classes. | |
| tryRegisterThreeTenBackport | back to summary |
|---|---|
| private void tryRegisterThreeTenBackport() Tries to register ThreeTen backport classes. | |
| tryRegisterThreeTenOld | back to summary |
|---|---|
| private void tryRegisterThreeTenOld() Tries to register ThreeTen ThreeTen/JSR-310 classes v0.6.3 and beyond. | |
| tryRegisterTimeZone | back to summary |
|---|---|
| private void tryRegisterTimeZone() Tries to register the subclasses of TimeZone. Try various things, doesn't matter if the map entry gets overwritten. | |