public abstract class ConverterFactory extends AnnotationSupport
Converter
instances.
This factory has default converters for all primitive types, their wrappers and enums. Also the following types are supported by default:
java.lang.Class
java.math.BigInteger
java.math.BigDecimal
java.util.Date
java.util.Locale
java.util.TimeZone
java.util.logging.Level
java.net.URL
java.net.URI
java.io.File
One-dimensional arrays of any registered types are supported through
ArrayConverter
. The java.util.Collection
and
java.util.Map
JDK types are supported through
CollectionConverter
and MapConverter
respectively.
Additional default converters would be automatically obtained through SPI
interface. The META-INF/services/org.foxlabs.validation.converter.Converter
file should be used to list such converter types. Any default converter can
be registered/overriden from addDefaultConverter(Converter)
method
at any time.
ConverterFactory
is thread-safe.
Converter
DEFAULT_GROUP_SET, DEFAULT_TARGET_SET
Modifier and Type | Method and Description |
---|---|
static <V> Converter<V> |
addDefaultConverter(Converter<V> converter)
Registers a new default converter or overrides existing one.
|
static <V> Converter<V> |
createConverter(Class<V> type,
String prefix)
Returns converter for the specified value type.
|
static <V> Converter<V> |
createConverter(Type type,
Annotation[] annotations,
String namespace)
Creates a new converter for the specified generic value type using
configuration from the specified annotations.
|
static <V> V[] |
decode(Class<V> type,
String... values)
Converts array of default string representations of values into array of
objects of the specified type using registered converter.
|
static <V> V |
decode(Class<V> type,
String value)
Converts default string representation of value into object of the
specified type using registered converter.
|
static <V> String[] |
encode(Class<V> type,
V... values)
Converts array of values into array of their raw string representations
using registered converter.
|
static <V> String |
encode(Class<V> type,
V value)
Converts value into default string representation using registered
converter.
|
static <V> Converter<V[]> |
forArray(Converter<V> converter)
Creates a new array converter with the specified converter of array
elements and default tokenizer.
|
static <V> Converter<V[]> |
forArray(Converter<V> converter,
String delims)
Creates a new array converter with the specified converter of array
elements and allowed elements delimiters.
|
static <V> Converter<V[]> |
forArray(Converter<V> converter,
Tokenizer tokenizer)
Creates a new array converter with the specified converter of array
elements and tokenzier of array elements.
|
static Converter<BigDecimal> |
forBigDecimal(String pattern)
Creates a new
java.math.BigDecimal converter with the
specified pattern. |
static Converter<BigInteger> |
forBigInteger(String pattern)
Creates a new
java.math.BigInteger converter with the
specified pattern. |
static Converter<Boolean> |
forBoolean(Class<Boolean> type,
String prefix)
Creates a new converter for the specified boolean type and prefix.
|
static Converter<Byte> |
forByte(String pattern)
Creates a new
java.lang.Byte converter with the specified
pattern. |
static Converter<Class<?>> |
forClass(ClassLoader loader)
Creates a new
java.lang.Class converter with the specified
class loader. |
static <T extends Collection<V>,V> |
forCollection(Class<? extends Collection> type,
Converter<V> converter)
Creates a new collection converter with the specified collection type,
converter of collection elements and default tokenzier.
|
static <T extends Collection<V>,V> |
forCollection(Class<? extends Collection> type,
Converter<V> converter,
String delims)
Creates a new collection converter with the specified collection type,
converter of collection elements and allowed elements delimiters.
|
static <T extends Collection<V>,V> |
forCollection(Class<? extends Collection> type,
Converter<V> converter,
Tokenizer tokenizer)
Creates a new collection converter with the specified collection type,
converter of collection elements and tokenzier of collection elements.
|
static Converter<Date> |
forDate(int dateStyle,
int timeStyle)
Creates a new
java.util.Date converter with the specified
date and time style. |
static Converter<Date> |
forDate(String pattern)
Creates a new
java.util.Date converter with the specified
date/time pattern. |
static Converter<Double> |
forDouble(String pattern)
Creates a new
java.lang.Double converter with the specified
pattern. |
static <V extends Enum<V>> |
forEnum(Class<V> type)
Creates a new converter for the specified enumeration type.
|
static <V extends Enum<V>> |
forEnum(Class<V> type,
String prefix)
Creates a new converter for the specified enumeration type and prefix.
|
static Converter<File> |
forFile(String parent)
Creates a new
java.io.File converter with the specified
parent directory pathname. |
static Converter<Float> |
forFloat(String pattern)
Creates a new
java.lang.Float converter with the specified
pattern. |
static Converter<Integer> |
forInteger(String pattern)
Creates a new
java.lang.Integer converter with the
specified pattern. |
static Converter<Long> |
forLong(String pattern)
Creates a new
java.lang.Long converter with the specified
pattern. |
static <T extends Map<K,V>,K,V> |
forMap(Class<? extends Map> type,
Converter<K> kconverter,
Converter<V> vconverter)
Creates a new map converter with the specified map type, key and value
converters and default tokenzier.
|
static <T extends Map<K,V>,K,V> |
forMap(Class<? extends Map> type,
Converter<K> kconverter,
Converter<V> vconverter,
String delims)
Creates a new map converter with the specified map type, key and value
converters, allowed key-value pairs delimiters.
|
static <T extends Map<K,V>,K,V> |
forMap(Class<? extends Map> type,
Converter<K> kconverter,
Converter<V> vconverter,
Tokenizer tokenizer)
Creates a new map converter with the specified map type, key and value
converters, key-value pairs tokenzier.
|
static Converter<Short> |
forShort(String pattern)
Creates a new
java.lang.Short converter with the specified
pattern. |
static <V> Converter<V> |
getDefaultConverter(Class<V> type)
Returns default converter for the specified value type.
|
static Set<Class<?>> |
getSupportedTypes()
Returns all supported value types by default.
|
static boolean |
hasWrappedMessage(Converter<?> converter)
Determines if the specified converter has wrapped error message.
|
static boolean |
isConverterAnnotation(Annotation annotation)
Determines if the specified annotation is either converter annotation
or custom converter annotation.
|
static Tokenizer |
tokenizer(String delims)
Creates a new tokenizer with the specified elements delimiters.
|
static <V> Converter<V> |
unwrapConverter(Converter<V> converter)
Returns unwrapped converter for the specified one.
|
static <V> Converter<V> |
wrapMessage(Converter<V> converter,
String message)
Wraps error message of the specified converter.
|
addToCache, addToCache, createValidation, getAnnotationGroups, getAnnotationList, getAnnotationMessage, getAnnotationProperty, getAnnotationTargets, getFromCache, isAnnotationPresent
public static Converter<Byte> forByte(String pattern)
java.lang.Byte
converter with the specified
pattern.pattern
- java.text.DecimalFormat
pattern.java.lang.Byte
type.ByteConverter
public static Converter<Short> forShort(String pattern)
java.lang.Short
converter with the specified
pattern.pattern
- java.text.DecimalFormat
pattern.java.lang.Short
type.ShortConverter
public static Converter<Integer> forInteger(String pattern)
java.lang.Integer
converter with the
specified pattern.pattern
- java.text.DecimalFormat
pattern.java.lang.Integer
type.IntegerConverter
public static Converter<Long> forLong(String pattern)
java.lang.Long
converter with the specified
pattern.pattern
- java.text.DecimalFormat
pattern.java.lang.Long
type.LongConverter
public static Converter<Float> forFloat(String pattern)
java.lang.Float
converter with the specified
pattern.pattern
- java.text.DecimalFormat
pattern.java.lang.Float
type.FloatConverter
public static Converter<Double> forDouble(String pattern)
java.lang.Double
converter with the specified
pattern.pattern
- java.text.DecimalFormat
pattern.java.lang.Double
type.DoubleConverter
public static Converter<BigInteger> forBigInteger(String pattern)
java.math.BigInteger
converter with the
specified pattern.pattern
- java.text.DecimalFormat
pattern.java.math.BigInteger
type.BigIntegerConverter
public static Converter<BigDecimal> forBigDecimal(String pattern)
java.math.BigDecimal
converter with the
specified pattern.pattern
- java.text.DecimalFormat
pattern.java.math.BigDecimal
type.BigDecimalConverter
public static Converter<Boolean> forBoolean(Class<Boolean> type, String prefix)
type
- Boolean type (can be either primitive or object type).prefix
- Prefix of message keys of boolean constants.BooleanConverter
public static <V extends Enum<V>> Converter<V> forEnum(Class<V> type)
V
- The enumeration type.type
- Enumeration type.forEnum(Class, String)
public static <V extends Enum<V>> Converter<V> forEnum(Class<V> type, String prefix)
V
- The enumeration type.type
- Enumeration type.prefix
- Prefix of message keys of enumeration constants.EnumConverter
public static Converter<Date> forDate(String pattern)
java.util.Date
converter with the specified
date/time pattern.pattern
- java.text.SimpleDateFormat
date/time pattern.java.util.Date
converter.DateConverter
public static Converter<Date> forDate(int dateStyle, int timeStyle)
java.util.Date
converter with the specified
date and time style. If negative value will be specified as date or
time style then corresponding part would not be used.dateStyle
- java.text.DateFormat
date style.timeStyle
- java.text.DateFormat
time style.java.util.Date
converter.DateConverter
public static Converter<Class<?>> forClass(ClassLoader loader)
java.lang.Class
converter with the specified
class loader.loader
- Class loader to be used for class loading.java.lang.Class
converter.ClassConverter
public static Converter<File> forFile(String parent)
java.io.File
converter with the specified
parent directory pathname.parent
- Parent directory pathname.java.io.File
converter.FileConverter
public static <V> Converter<V[]> forArray(Converter<V> converter)
V
- The array elements type.converter
- Converter of array elements.forArray(Converter, Tokenizer)
,
ArrayConverter
,
SimpleTokenizer
public static <V> Converter<V[]> forArray(Converter<V> converter, String delims)
V
- The array elements type.converter
- Converter of array elements.delims
- Delimiters of array elements.forArray(Converter, Tokenizer)
,
ArrayConverter
,
SimpleTokenizer
public static <V> Converter<V[]> forArray(Converter<V> converter, Tokenizer tokenizer)
V
- The array elements type.converter
- Converter of array elements.tokenizer
- Tokenizer of array elements.ArrayConverter
,
Tokenizer
public static <T extends Collection<V>,V> Converter<T> forCollection(Class<? extends Collection> type, Converter<V> converter)
T
- The collection type.V
- The collection elements type.type
- Collection type.converter
- Converter of collection elements.forCollection(Class, Converter, Tokenizer)
,
CollectionConverter
,
SimpleTokenizer
public static <T extends Collection<V>,V> Converter<T> forCollection(Class<? extends Collection> type, Converter<V> converter, String delims)
T
- The collection type.V
- The collection elements type.type
- Collection type.converter
- Converter of collection elements.delims
- Delimiters of collection elements.forCollection(Class, Converter, Tokenizer)
,
CollectionConverter
,
SimpleTokenizer
public static <T extends Collection<V>,V> Converter<T> forCollection(Class<? extends Collection> type, Converter<V> converter, Tokenizer tokenizer)
T
- The collection type.V
- The collection elements type.type
- Collection type.converter
- Converter of collection elements.tokenizer
- Tokenizer of collection elements.CollectionConverter
,
Tokenizer
public static <T extends Map<K,V>,K,V> Converter<T> forMap(Class<? extends Map> type, Converter<K> kconverter, Converter<V> vconverter)
T
- The map type.K
- The map keys type.V
- The map values type.type
- Map type.kconverter
- Converter of map keys.vconverter
- Converter of map values.forMap(Class, Converter, Converter, Tokenizer)
,
MapConverter
,
SimpleTokenizer
public static <T extends Map<K,V>,K,V> Converter<T> forMap(Class<? extends Map> type, Converter<K> kconverter, Converter<V> vconverter, String delims)
T
- The map type.K
- The map keys type.V
- The map values type.type
- Map type.kconverter
- Converter of map keys.vconverter
- Converter of map values.delims
- Delimiters of key-value pairs.forMap(Class, Converter, Converter, Tokenizer)
,
MapConverter
,
SimpleTokenizer
public static <T extends Map<K,V>,K,V> Converter<T> forMap(Class<? extends Map> type, Converter<K> kconverter, Converter<V> vconverter, Tokenizer tokenizer)
T
- The map type.K
- The map keys type.V
- The map values type.type
- Map type.kconverter
- Converter of map keys.vconverter
- Converter of map values.tokenizer
- Tokenizer of key-value pairs.MapConverter
,
Tokenizer
public static Tokenizer tokenizer(String delims)
delims
- Allowed elements delimiter characters.SimpleTokenizer
public static <V> Converter<V> wrapMessage(Converter<V> converter, String message)
Note that this method has no effect if the specified constraint is
instance of the UnsupportedConverter
.
V
- The value type.converter
- Converter to be wrapped.message
- Error message template key.ConverterMessageWrapper
public static boolean hasWrappedMessage(Converter<?> converter)
converter
- Converter to be tested.true
if the specified converter has wrapped error
message; false
otherwise.ConverterMessageWrapper
public static <V> Converter<V> unwrapConverter(Converter<V> converter)
V
- The value type.converter
- Converter to be unwrapped.ConverterWrapper
public static Set<Class<?>> getSupportedTypes()
public static <V> Converter<V> addDefaultConverter(Converter<V> converter)
V
- The value type.converter
- A new converter.public static <V> Converter<V> getDefaultConverter(Class<V> type)
UnsupportedConverter
will be returned.V
- The value type.type
- The type of value to be converted.public static <V> Converter<V> createConverter(Class<V> type, String prefix)
This method differs from getDefaultConverter(Class)
in that
it allows to override prefix of message keys for boolean and enumeration
constants.
V
- The value type.type
- The type of value to be converted.prefix
- Prefix of message keys of boolean and enumeration constants.getDefaultConverter(Class)
,
AnnotationSupport.createValidation(Class, Annotation, Class)
public static boolean isConverterAnnotation(Annotation annotation)
annotation
- Annotation to be tested.true
if the specified annotation is either
converter annotation or custom converter annotation;
false
otherwise.public static <V> Converter<V> createConverter(Type type, Annotation[] annotations, String namespace)
Annotations that are not converter annotations will be skipped. Simple converters require one or none annotations. In case array/collection/map value type additional tokenizer annotation can be specified. If there are no annotations specified (empty array) then default converter will be returned.
The converter returned may be instance of ConverterMessageWrapper
if annotation contains overriding message template.
V
- The value type.type
- The type of value to be converted.annotations
- Array of annotations.namespace
- Namespace (for example, qualified property name).ValidationTargetException
- if there is an error in converter declaration.createConverter(Annotation, Class, String)
,
createConverter(Class, String)
public static <V> V decode(Class<V> type, String value)
V
- The value type.type
- The type of value to be converted.value
- Default string representation of value.MalformedValueException
- if conversion fails.public static <V> V[] decode(Class<V> type, String... values)
V
- The array elements type.type
- The type of value to be converted.values
- Array of default string representations of values.MalformedValueException
- if conversion fails.public static <V> String encode(Class<V> type, V value)
V
- The value type.type
- The type of value to be converted.value
- Value to be encoded.public static <V> String[] encode(Class<V> type, V... values)
V
- The array elements type.type
- The type of value to be converted.values
- Array of values to be encoded.Copyright © 2011–2020 FoxLabs. All rights reserved.