public final class Converter extends Object
Converter is the opposite of toString(). It turns strings into objects. Useful,
for example, when obtaining Java value types from XML.
Supports registered types explicitly with default registered conversions:
ClassInetAddressInetSocketAddressPathPatternResourceResourcesResourceBundleTimeZoneURISupports other types implicitly by looking for a single-argument string factory method or constructor in this order:
parse(String)valueOf(String)of(String)T(String)T(CharSequence)register(Class, Conversion),
register(TypeToken, Conversion)| Modifier and Type | Class and Description |
|---|---|
static interface |
Converter.Conversion<T,E extends Exception>
Converts a string property value into a typed object.
|
static class |
Converter.DuplicateConversion |
static class |
Converter.UnsupportedConversion |
| Constructor and Description |
|---|
Converter() |
| Modifier and Type | Method and Description |
|---|---|
<T> T |
convert(Class<T> type,
String value)
Converts the given value into an instance of type.
|
<T> T |
convert(com.google.common.reflect.TypeToken<T> type,
String value)
Converts the given value into an instance of type.
|
<T> void |
register(Class<T> type,
Converter.Conversion<T,?> factory)
Registers an object conversion.
|
<T> void |
register(com.google.common.reflect.TypeToken<T> type,
Converter.Conversion<T,?> factory)
Registers an object conversion.
|
void |
registerDate(String pattern)
Registers a date format pattern for legacy
java.util.Date. |
Set<com.google.common.reflect.TypeToken<?>> |
registered()
Get an unmodifiable view of registered conversions.
|
public Converter()
public <T> void register(@Nonnull Class<T> type, @Nonnull Converter.Conversion<T,?> factory) throws Converter.DuplicateConversion
T - the conversion typetype - the class token, never missingfactory - the converter, never missingConverter.DuplicateConversion - if the conversion is already registeredpublic <T> void register(@Nonnull com.google.common.reflect.TypeToken<T> type, @Nonnull Converter.Conversion<T,?> factory) throws Converter.DuplicateConversion
T - the conversion typetype - the Guava type token, never missingfactory - the converter, never missingConverter.DuplicateConversion - if the conversion is already registeredpublic void registerDate(@Nonnull String pattern)
java.util.Date. By default Converter uses the deprecated Date.Date(String) constructor.
This method is a convenience and a caution for surprising legacy date parsing. Better is to
use java.time classes.
pattern - the date format pattern, never missing@Nonnull public <T> T convert(@Nonnull Class<T> type, @Nonnull String value) throws Exception
T - the conversion typetype - the target type, never missingvalue - the string to convert, never missingException - if conversion fails@Nonnull public <T> T convert(@Nonnull com.google.common.reflect.TypeToken<T> type, @Nonnull String value) throws Exception
T - the conversion typetype - the target type, never missingvalue - the string to convert, never missingException - if conversion fails@Nonnull public Set<com.google.common.reflect.TypeToken<?>> registered()
Copyright © 2013–2015 binkley's BLOG. All rights reserved.