Class Converters
- Author:
- Damien Carbonne
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TConverts an object to a target class using an appropriate default converter narrowed from a source classstatic <T> TConverts an object to a target class using an appropriate default converter.static <T> voidconvert(Collection<?> input, Converter<?, ? extends T> converter, Collection<T> output) Converts a collection using a converter.static ObjectconvertRaw(Object source, Class<?> targetClass) Converts an object to a target class using an appropriate default converter.static ObjectconvertRaw(Object source, Class<?> sourceClass, Class<?> targetClass) Converts an object to a target class using an appropriate default converter.static ObjectconvertRaw(String name, Object source) Converts a source object to a target object using a converter known by its name and parameters.static voidstatic <S,T> Converter<? super S, ? extends T> getCompliantConverter(String name, Class<S> sourceClass, Class<T> targetClass) Returns the converter that has a given name and is compliant with source and target classes.static <S,T> Converter<? super S, ? extends T> getCompliantConverter(String name, Class<S> sourceClass, Class<T> targetClass, cdc.util.lang.FailureReaction reaction) Returns the converter that has a given name and is compliant with source and target classes.getCompliantConverters(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind) static <S,T> Converter<? super S, ? extends T> getCompliantSpecialConverter(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind) static <S,T> Converter<? super S, ? extends T> getCompliantSpecialConverter(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind, cdc.util.lang.FailureReaction reaction) Returns a compliant default converter for a pair of classes.static Converter<?,?> getConverter(String name) Returns the converter that has a given name.static Converter<?,?> getConverter(String name, cdc.util.lang.FailureReaction reaction) Returns the converter that has a given name.static <S,T> Converter<S, T> getMatchingConverter(String name, Class<S> sourceClass, Class<T> targetClass) Returns the converter that has a given name and matches source and target classes.static <S,T> Converter<S, T> getMatchingConverter(String name, Class<S> sourceClass, Class<T> targetClass, cdc.util.lang.FailureReaction reaction) Returns the converter that has a given name and matches source and target classes.getMatchingConverters(Class<S> sourceClass, Class<T> targetClass) Returns a list of all converters from a source class to a target class.static <S,T> Converter<S, T> getMatchingSpecialConverter(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind) static <S,T> Converter<S, T> getMatchingSpecialConverter(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind, cdc.util.lang.FailureReaction reaction) Returns the default converter associated to a pair of classes, if any.getNames()getSourceCompliantConverters(Class<S> sourceClass, SpecialConverterKind kind) getSourceMatchingConverters(Class<S> sourceClass) getTargetCompliantConverters(Class<T> targetClass, SpecialConverterKind kind) getTargetMatchingConverters(Class<T> targetClass) static booleanhasCompliantConverters(Class<?> sourceClass, Class<?> targetClass) Returnstruewhen some converters compliant with source and target classes are registered.static booleanhasConverter(String name) Returnstruewhen a converter with a given name exists.static booleanhasMatchingConverters(Class<?> sourceClass, Class<?> targetClass) Returnstruewhen some converters matching source and target classes are registered.static booleanhasSpecialConverter(Class<?> sourceClass, Class<?> targetClass, SpecialConverterKind kind) Returnstruewhen a special converter matches a pair of classes.static booleanisSpecialConverter(Converter<?, ?> converter, SpecialConverterKind kind) Returnstruewhen a converter is declared as a special one.static booleanisSpecialConverterName(String name, SpecialConverterKind kind) Returnstrueif a name is the name of a default converter.static voidstatic voidRegisters a converter using its class simple name as name.static voidregister(Converter<?, ?> converter, String name, boolean isDefault, cdc.util.lang.FailureReaction reaction) Registers a converter.static Stringstatic StringConverts an object to a string using default or single converters.
-
Field Details
-
PRINTER
public static final cdc.util.debug.Printable PRINTER
-
-
Method Details
-
elaborate
public static void elaborate() -
register
public static void register(Converter<?, ?> converter, String name, boolean isDefault, cdc.util.lang.FailureReaction reaction) Registers a converter.- Parameters:
converter- The converter.name- The converter name.isDefault- Iftrue, this converter is a default one for target/source classes.reaction- The reaction to adopt in case of failure.- Throws:
IllegalArgumentException- when cconverterisnull, or a converter with the same name is already registered, or a default converter is already registered.
-
register
public static void register(Converter<?, ?> converter, boolean isDefault, cdc.util.lang.FailureReaction reaction) Registers a converter using its class simple name as name.- Parameters:
converter- The converter.isDefault- Iftrue, this converter is a default one for target/source classes.reaction- The reaction to adopt in case of failure.- Throws:
IllegalArgumentException- when cconverterisnull, or a converter with the same name is already registered, or a default converter is already registered.
-
register
-
getNames
- Returns:
- A set of all registered converters names.
-
hasConverter
Returnstruewhen a converter with a given name exists.- Parameters:
name- The converter name.- Returns:
truewhen a converter namednameexists.
-
hasMatchingConverters
Returnstruewhen some converters matching source and target classes are registered.- Parameters:
sourceClass- The source class.targetClass- The target class.- Returns:
truewhen converters matchingsourceClassandtargetClassare registered.
-
hasCompliantConverters
Returnstruewhen some converters compliant with source and target classes are registered.- Parameters:
sourceClass- The source class.targetClass- The target class.- Returns:
truewhen converters compliant withsourceClassandtargetClassare registered.
-
hasSpecialConverter
public static boolean hasSpecialConverter(Class<?> sourceClass, Class<?> targetClass, SpecialConverterKind kind) Returnstruewhen a special converter matches a pair of classes.WARNING: Only exact matching of classes is tested.
- Parameters:
sourceClass- The source class.targetClass- The target class.kind- The special converter kind.- Returns:
truewhen a special converter fromsourceClasstotargetClassexists.
-
isSpecialConverter
Returnstruewhen a converter is declared as a special one.- Parameters:
converter- The converter.kind- The special converter kind.- Returns:
truewhenconverteris a special converter.
-
isSpecialConverterName
Returnstrueif a name is the name of a default converter.- Parameters:
name- The name.kind- The special converter kind.- Returns:
trueifnameis the name of a default converter.
-
getConverter
Returns the converter that has a given name.- Parameters:
name- The name.reaction- The reaction to adopt when not converter is found.- Returns:
- The converter named
nameornull. - Throws:
cdc.util.lang.NotFoundException- WhenreactionisFailureReaction.FAILand no converter namednameexists.
-
getConverter
Returns the converter that has a given name.- Parameters:
name- The name.- Returns:
- The converter named { @code name}.
- Throws:
cdc.util.lang.NotFoundException- When no converter namednameexists.
-
getMatchingConverter
public static <S,T> Converter<S,T> getMatchingConverter(String name, Class<S> sourceClass, Class<T> targetClass, cdc.util.lang.FailureReaction reaction) Returns the converter that has a given name and matches source and target classes.- Type Parameters:
S- The source type.T- The target type.- Parameters:
name- The converter name.sourceClass- The source class.targetClass- The target class.reaction- The reaction to adopt when not converter is found.- Returns:
- The matching converter or
null. - Throws:
cdc.util.lang.NotFoundException- WhenreactionisFailureReaction.FAILand no converter namedname, matchingsourceClassandtargetClassexists.
-
getMatchingConverter
public static <S,T> Converter<S,T> getMatchingConverter(String name, Class<S> sourceClass, Class<T> targetClass) Returns the converter that has a given name and matches source and target classes.- Type Parameters:
S- The source type.T- The target type.- Parameters:
name- The converter name.sourceClass- The source class.targetClass- The target class.- Returns:
- The matching converter or
null. - Throws:
cdc.util.lang.NotFoundException- When no converter namedname, matchingsourceClassandtargetClassexists.
-
getCompliantConverter
public static <S,T> Converter<? super S,? extends T> getCompliantConverter(String name, Class<S> sourceClass, Class<T> targetClass, cdc.util.lang.FailureReaction reaction) Returns the converter that has a given name and is compliant with source and target classes.- Type Parameters:
S- The source type.T- The target type.- Parameters:
name- The converter name.sourceClass- The source class.targetClass- The target class.reaction- The reaction to adopt when not converter is found.- Returns:
- The compliant converter or
null. - Throws:
cdc.util.lang.NotFoundException- WhenreactionisFailureReaction.FAILand no converter namedname, compliant withsourceClassandtargetClassexists.
-
getCompliantConverter
public static <S,T> Converter<? super S,? extends T> getCompliantConverter(String name, Class<S> sourceClass, Class<T> targetClass) Returns the converter that has a given name and is compliant with source and target classes.- Type Parameters:
S- The source type.T- The target type.- Parameters:
name- The converter name.sourceClass- The source class.targetClass- The target class.- Returns:
- The compliant converter or
null. - Throws:
cdc.util.lang.NotFoundException- When no converter namedname, compliant withsourceClassandtargetClassexists.
-
getSourceMatchingConverters
-
getTargetMatchingConverters
-
getMatchingConverters
public static <S,T> List<Converter<S,T>> getMatchingConverters(Class<S> sourceClass, Class<T> targetClass) Returns a list of all converters from a source class to a target class.- Type Parameters:
S- The source type.T- The target type.- Parameters:
sourceClass- The source class.targetClass- The target class.- Returns:
- A list of all converters from
sourceClasstotargetClass.
-
getSourceCompliantConverters
public static <S> Set<Converter<? super S,?>> getSourceCompliantConverters(Class<S> sourceClass, SpecialConverterKind kind) -
getTargetCompliantConverters
public static <T> Set<Converter<?,? extends T>> getTargetCompliantConverters(Class<T> targetClass, SpecialConverterKind kind) -
getCompliantConverters
public static <S,T> Set<Converter<? super S,? extends T>> getCompliantConverters(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind) -
getMatchingSpecialConverter
public static <S,T> Converter<S,T> getMatchingSpecialConverter(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind, cdc.util.lang.FailureReaction reaction) Returns the default converter associated to a pair of classes, if any.WARNING: Only exact matching of classes is tested.
- Type Parameters:
S- The source type.T- The target type.- Parameters:
sourceClass- The source class.targetClass- The target class.kind- The special converter kind.reaction- The reaction to adopt when not converter is found.- Returns:
- The default converter from
sourceClasstotargetClassornull. - Throws:
cdc.util.lang.NotFoundException- WhenreactionisFailureReaction.FAILand no matching converter is found.
-
getMatchingSpecialConverter
public static <S,T> Converter<S,T> getMatchingSpecialConverter(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind) -
getCompliantSpecialConverter
public static <S,T> Converter<? super S,? extends T> getCompliantSpecialConverter(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind, cdc.util.lang.FailureReaction reaction) Returns a compliant default converter for a pair of classes.WARNING: When there is a matching converter, it is returned.
Otherwise, when several compliant converters exist, the returned one is selected using an non deterministic algorithm.- Type Parameters:
S- The source type.T- The target type.- Parameters:
sourceClass- The source class.targetClass- The target class.kind- The special converter kind.reaction- The reaction to adopt when not converter is found.- Returns:
- A compliant default converter from
sourceClasstotargetClassornull. - Throws:
cdc.util.lang.NotFoundException- WhenreactionisFailureReaction.FAILand no compliant converter is found.
-
getCompliantSpecialConverter
public static <S,T> Converter<? super S,? extends T> getCompliantSpecialConverter(Class<S> sourceClass, Class<T> targetClass, SpecialConverterKind kind) -
convertRaw
Converts a source object to a target object using a converter known by its name and parameters.- Parameters:
name- The converter name.source- The object to convert.- Returns:
- The conversion of
sourceusing the converter namednamewith parametersparams. - Throws:
ConversionException- when no converter is found.
-
convertRaw
Converts an object to a target class using an appropriate default converter.The default converter search is narrowed by source and target classes.
nullis converted tonull.WARNING: it is the caller responsibility to use a valid
sourceClass, compliant withsource.- Parameters:
source- The source object.sourceClass- The source class, used to narrow search of compliant converters. It must be compliant withsource.targetClass- The target class.- Returns:
- The conversion of
sourcetotargetClass. - Throws:
ConversionException- when no matching default converter is found.
-
convertRaw
Converts an object to a target class using an appropriate default converter.The default converter search is narrowed by class of
sourceobject and target class.nullis converted tonull.- Parameters:
source- The source object.targetClass- The target class.- Returns:
- The conversion of
sourcetotargetClass. - Throws:
ConversionException- when no matching default converter is found.
-
convert
Converts an object to a target class using an appropriate default converter narrowed from a source classWARNING: it is the caller responsibility to use a valid
sourceClass, compliant withsource.- Type Parameters:
T- The target type.- Parameters:
source- The source object.sourceClass- The source class, used to narrow search of compliant converters. It must be compliant withsource.targetClass- The target class.- Returns:
- The conversion of
sourcetotargetClass. - Throws:
ConversionException- when no matching default converter is found.
-
convert
Converts an object to a target class using an appropriate default converter.- Type Parameters:
T- The target type.- Parameters:
source- The source object.targetClass- The target class.- Returns:
- The conversion of
sourcetotargetClass. - Throws:
ConversionException- when no matching default converter is found.
-
convert
public static <T> void convert(Collection<?> input, Converter<?, ? extends T> converter, Collection<T> output) Converts a collection using a converter.- Type Parameters:
T- The elements type.- Parameters:
input- The input collection.converter- The converter to use.output- The output collection.
-
toString
Converts an object to a string using default or single converters.If
objectisnull,nullis returned.
toString()is used when no converter is found andreactionis not FAIL.- Parameters:
object- The object.reaction- The reaction to adopt when not converter is found.- Returns:
- The conversion of
objectto string using a compliant default or single converter, ortoString(). - Throws:
cdc.util.lang.NotFoundException- WhenreactionisFailureReaction.FAILand no compliant converter is found.
-
toString
-
flatten
-