Package cdc.converters
Class Converters
- java.lang.Object
-
- cdc.converters.Converters
-
public final class Converters extends Object
Registry of converters.- Author:
- Damien Carbonne
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classConverters.Printer
-
Field Summary
Fields Modifier and Type Field Description static cdc.util.debug.PrintablePRINTER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Tconvert(Object source, Class<T> targetClass)Converts 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(String name, Object source)Converts a source object to a target object using a converter known by its name and parameters.static booleanelaborate()static List<Converter<?,?>>flatten(Converter<?,?> converter)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.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.static <S,T>
Set<Converter<? super S,? extends T>>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.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.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.static Set<String>getNames()static <S> Set<Converter<? super S,?>>getSourceCompliantConverters(Class<S> sourceClass, SpecialConverterKind kind)static <S> List<Converter<S,?>>getSourceMatchingConverters(Class<S> sourceClass)static <T> Set<Converter<?,? extends T>>getTargetCompliantConverters(Class<T> targetClass, SpecialConverterKind kind)static <T> List<Converter<?,T>>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 voidregister(Converter<?,?> converter, boolean isDefault)static voidregister(Converter<?,?> converter, boolean isDefault, cdc.util.lang.FailureReaction reaction)Registers 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 StringtoString(Object object)static StringtoString(Object object, cdc.util.lang.FailureReaction reaction)Converts an object to a string using default or single converters.
-
-
-
Method Detail
-
elaborate
public static boolean 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
public static void register(Converter<?,?> converter, boolean isDefault)
-
hasConverter
public static boolean hasConverter(String name)
Returnstruewhen a converter with a given name exists.- Parameters:
name- The converter name.- Returns:
truewhen a converter namednameexists.
-
hasMatchingConverters
public static boolean hasMatchingConverters(Class<?> sourceClass, Class<?> targetClass)
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
public static boolean hasCompliantConverters(Class<?> sourceClass, Class<?> targetClass)
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
public static boolean isSpecialConverter(Converter<?,?> converter, SpecialConverterKind kind)
Returnstruewhen a converter is declared as a special one.- Parameters:
converter- The converter.kind- The special converter kind.- Returns:
truewhenconverteris a special converter.
-
isSpecialConverterName
public static boolean isSpecialConverterName(String name, SpecialConverterKind kind)
Returnstrueif a name is the name of a default converter.- Parameters:
name- The name.kind- The special converter kind.- Returns:
trueif [@code name} is the name of a default converter.
-
getConverter
public static Converter<?,?> getConverter(String name, cdc.util.lang.FailureReaction reaction)
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
public static Converter<?,?> getConverter(String name)
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
public static <S> List<Converter<S,?>> getSourceMatchingConverters(Class<S> sourceClass)
-
getTargetMatchingConverters
public static <T> List<Converter<?,T>> getTargetMatchingConverters(Class<T> targetClass)
-
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
public static Object convertRaw(String name, Object source)
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
public static Object convertRaw(Object source, Class<?> targetClass)
Converts an object to a target class using an appropriate default converter.nullis converted tonull.- Parameters:
source- The source object.targetClass- The target class.- Returns:
- The conversion of
sourceto [@code targetClass}. - Throws:
ConversionException- when no matching default converter is found.
-
convert
public static <T> T convert(Object source, Class<T> targetClass)
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
sourceto [@code targetClass}. - 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
public static String toString(Object object, cdc.util.lang.FailureReaction reaction)
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.
-
-