Class AbstractModularValueConverter
- java.lang.Object
-
- de.gebit.integrity.parameter.conversion.AbstractModularValueConverter
-
- All Implemented Interfaces:
ValueConverter
- Direct Known Subclasses:
DefaultModularValueConverter
public abstract class AbstractModularValueConverter extends java.lang.Object implements ValueConverter
Abstract base class for a value converter which uses conversion classes to determine how a given value is to be converted into a desired form. This modularity makes it easy to extend the converter with additional conversions.- Author:
- Rene Schneider - initial API and implementation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
AbstractModularValueConverter.ConversionKey
This class defines a key for efficient searching for conversions in maps.
-
Field Summary
Fields Modifier and Type Field Description protected com.google.inject.Provider<ConversionContext>
conversionContextProvider
The conversion context provider.protected com.google.inject.Injector
injector
The Guice injector.protected ParameterResolver
parameterResolver
The parameter resolver.protected StandardOperationProcessor
standardOperationProcessor
The processor for standard operations.protected VariableManager
variableManager
The variable manager.protected WrapperFactory
wrapperFactory
The wrapper factory.
-
Constructor Summary
Constructors Constructor Description AbstractModularValueConverter()
Default constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected void
addConversion(java.lang.Class<? extends Conversion<?,?>> aConversion)
Adds the given conversion class to the map of available conversions.protected java.lang.Object
convertEncapsulatedConstantValueToTargetType(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, ConstantValue aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues)
Converts a givenValueOrEnumValueOrOperation
to a given Java type class, if possible.protected java.lang.Object
convertEncapsulatedValueCollectionToTargetType(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, ValueOrEnumValueOrOperationCollection aCollection, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues)
Converts a given value collection to a given Java type class, if possible.protected java.lang.Object
convertEncapsulatedValueToTargetType(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, ValueOrEnumValueOrOperation aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues)
Converts a givenValueOrEnumValueOrOperation
to a given Java type class, if possible.protected java.lang.Object
convertPlainValueToTargetType(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, java.lang.Object aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues)
Converts a given plain value (no instance ofValueOrEnumValueOrOperation
orValueOrEnumValueOrOperationCollection
) to a given Java type class, if possible.java.lang.Object
convertValue(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, java.lang.Object aValue, ConversionContext aConversionContext)
Convert a given single Integrity or Java type value to a given target type (which is always a Java type).java.lang.Object
convertValue(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, java.lang.Object aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedObjects)
Extended version of #convertValue(Class, Class, Object, UnresolvableVariableHandling).java.lang.Object
convertValue(java.lang.Class<?> aTargetType, java.lang.Object aValue, ConversionContext aConversionContext)
Convert a given single Integrity or Java type value to a given target type (which is always a Java type).FormattedString
convertValueToFormattedString(java.lang.Object aValue, boolean aForceIntermediateMapFlag, ConversionContext aConversionContext)
Converts a given value to a formattedString.FormattedString[]
convertValueToFormattedStringArray(java.lang.Object aValue, ConversionContext aConversionContext)
Converts a given value to a formatted String array.java.lang.String
convertValueToString(java.lang.Object aValue, boolean aForceIntermediateMapFlag, ConversionContext aConversionContext)
Converts a given value to a String.java.lang.String[]
convertValueToStringArray(java.lang.Object aValue, ConversionContext aConversionContext)
Converts a given value to a String array.FormattedString[]
convertValueToStringArray(java.lang.Object aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues)
Extended version of#convertValueToStringArray(Object, UnresolvableVariableHandling)
.protected <C extends Conversion>
CcreateConversionInstance(java.lang.Class<C> aConversionClass, java.util.Set<java.lang.Object> someVisitedValues)
Creates an instance of the given conversion class.protected int
determineConversionPriority(java.lang.Class<? extends Conversion<?,?>> aConversion)
Determines the priority of the given conversion class.protected java.lang.Class<? extends Conversion<?,?>>
filterConversionClass(java.lang.Class<? extends Conversion<?,?>> aConversionClass, java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, ConversionContext aConversionContext, boolean aDefaultConversionFlag)
This filtering method has the ability to replace a conversion class found by the normal lookup before it's actually used.protected Conversion<?,?>
findAndInstantiateConversion(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, java.util.Set<java.lang.Object> someVisitedValues, ConversionContext aConversionContext)
Searches all known conversions for a match which is able to convert a given source type into a given target type.protected java.lang.Class<? extends Conversion<?,?>>
findConversion(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, java.util.Set<java.lang.Object> someVisitedValues, ConversionContext aConversionContext)
Searches all known conversions for a match which is able to convert a given source type into a given target type.protected java.lang.Class<? extends Conversion<?,?>>
findConversionRecursive(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, ConversionContext aConversionContext)
Searches all known conversions for a match which is able to convert a given source type into a given target type.protected java.lang.Class<? extends Conversion<?,?>>
findDerivedConversionRecursive(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, ConversionContext aConversionContext)
Searches all known derived conversions for a match which is able to convert a given source type into a subclass of a given target type.protected byte[]
handleConversionOfStringToByteArray(java.lang.String aSource)
Handles the special case of issue #66: single string to byte array.protected java.lang.Byte[]
handleConversionOfStringToByteWrapperArray(java.lang.String aSource)
Handles the special case of issue #66: single string to byte array.protected byte[]
handleConversionOfStringValueToByteArray(StringValue aSource)
Handles the special case of issue #66: single string to byte array.protected java.lang.Byte[]
handleConversionOfStringValueToByteWrapperArray(StringValue aSource)
Handles the special case of issue #66: single string to byte array.protected abstract void
initializeConversions()
Implement this method to initialize known conversions.protected void
initializeSearchBlockers()
Initializes the contents ofconversionSearchBlockers
.ConversionContext
safeguardConversionContext(ConversionContext aContext)
This method creates a default conversion context in case none is provided, and returns the provided context otherwise.protected java.lang.Class<? extends Conversion<?,?>>
searchDerivedConversionMap(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType)
Searches the derived conversion map for a match.protected <T extends java.util.Collection>
TwrapInCollection(java.lang.Class<T> aCollectionType, java.lang.Object anArrayOrSingleType)
Wraps a value (or an array of values) in a collection of the given type.
-
-
-
Field Detail
-
parameterResolver
@Inject protected ParameterResolver parameterResolver
The parameter resolver.
-
variableManager
@Inject(optional=true) protected VariableManager variableManager
The variable manager.
-
wrapperFactory
@Inject(optional=true) protected WrapperFactory wrapperFactory
The wrapper factory.
-
standardOperationProcessor
@Inject protected StandardOperationProcessor standardOperationProcessor
The processor for standard operations.
-
injector
@Inject protected com.google.inject.Injector injector
The Guice injector. Required to inject stuff into instances of conversions.
-
conversionContextProvider
@Inject protected com.google.inject.Provider<ConversionContext> conversionContextProvider
The conversion context provider.
-
-
Method Detail
-
initializeConversions
protected abstract void initializeConversions()
Implement this method to initialize known conversions.
-
initializeSearchBlockers
protected void initializeSearchBlockers()
Initializes the contents ofconversionSearchBlockers
.
-
convertValue
public java.lang.Object convertValue(java.lang.Class<?> aTargetType, java.lang.Object aValue, ConversionContext aConversionContext) throws ParameterUtil.UnresolvableVariableException, UnexecutableException
Description copied from interface:ValueConverter
Convert a given single Integrity or Java type value to a given target type (which is always a Java type).- Specified by:
convertValue
in interfaceValueConverter
- Parameters:
aTargetType
- the target typeaValue
- the valueaConversionContext
- controls some conversion parameters and rules; may be null if the defaultConversionContext
values shall be used- Returns:
- the converted object
- Throws:
ParameterUtil.UnresolvableVariableException
UnexecutableException
-
convertValue
public java.lang.Object convertValue(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, java.lang.Object aValue, ConversionContext aConversionContext) throws ParameterUtil.UnresolvableVariableException, UnexecutableException
Description copied from interface:ValueConverter
Convert a given single Integrity or Java type value to a given target type (which is always a Java type).- Specified by:
convertValue
in interfaceValueConverter
- Parameters:
aTargetType
- the target typeaParameterizedType
- the parameterized (via generics) type attached to the given target type, if applicable - for example if a conversion to Listis desired, the target type is List, and the parameterized type is Integer aValue
- the valueaConversionContext
- controls some conversion parameters and rules; may be null if the defaultConversionContext
values shall be used- Returns:
- the converted object
- Throws:
ParameterUtil.UnresolvableVariableException
UnexecutableException
-
convertValue
public java.lang.Object convertValue(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, java.lang.Object aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedObjects) throws ParameterUtil.UnresolvableVariableException, UnexecutableException
Extended version of #convertValue(Class, Class, Object, UnresolvableVariableHandling).- Parameters:
aTargetType
-aParameterizedType
-aValue
-aConversionContext
-someVisitedObjects
-- Returns:
- Throws:
ParameterUtil.UnresolvableVariableException
java.lang.ClassNotFoundException
UnexecutableException
java.lang.InstantiationException
-
convertPlainValueToTargetType
protected java.lang.Object convertPlainValueToTargetType(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, java.lang.Object aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues)
Converts a given plain value (no instance ofValueOrEnumValueOrOperation
orValueOrEnumValueOrOperationCollection
) to a given Java type class, if possible.- Parameters:
aTargetType
- the target typeaParameterizedType
- the parameterized (via generics) type attached to the given target type, if applicable - for example if a conversion to Listis desired, the target type is List, and the parameterized type is Integer aValue
- the valueaConversionContext
- defines some parameters controlling how the conversion is done in detail- Returns:
- the converted value
-
convertEncapsulatedValueToTargetType
protected java.lang.Object convertEncapsulatedValueToTargetType(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, ValueOrEnumValueOrOperation aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues) throws ParameterUtil.UnresolvableVariableException, UnexecutableException
Converts a givenValueOrEnumValueOrOperation
to a given Java type class, if possible.- Parameters:
aTargetType
- the target typeaParameterizedType
- the parameterized (via generics) type attached to the given target type, if applicable - for example if a conversion to Listis desired, the target type is List, and the parameterized type is Integer aValue
- the valueaConversionContext
- defines some parameters controlling how the conversion is done in detail- Returns:
- the converted value
- Throws:
ParameterUtil.UnresolvableVariableException
java.lang.ClassNotFoundException
java.lang.InstantiationException
UnexecutableException
-
convertEncapsulatedConstantValueToTargetType
protected java.lang.Object convertEncapsulatedConstantValueToTargetType(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, ConstantValue aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues) throws ParameterUtil.UnresolvableVariableException, UnexecutableException
Converts a givenValueOrEnumValueOrOperation
to a given Java type class, if possible.- Parameters:
aTargetType
- the target typeaParameterizedType
- the parameterized (via generics) type attached to the given target type, if applicable - for example if a conversion to Listis desired, the target type is List, and the parameterized type is Integer aValue
- the valueaConversionContext
- defines some parameters controlling how the conversion is done in detail- Returns:
- the converted value
- Throws:
ParameterUtil.UnresolvableVariableException
java.lang.ClassNotFoundException
java.lang.InstantiationException
UnexecutableException
-
convertEncapsulatedValueCollectionToTargetType
protected java.lang.Object convertEncapsulatedValueCollectionToTargetType(java.lang.Class<?> aTargetType, java.lang.Class<?> aParameterizedType, ValueOrEnumValueOrOperationCollection aCollection, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues) throws ParameterUtil.UnresolvableVariableException, UnexecutableException
Converts a given value collection to a given Java type class, if possible. Will return an array if the collection contains more than one item.- Parameters:
aTargetType
- the target typeaParameterizedType
- the parameterized (via generics) type attached to the given target type, if applicable - for example if a conversion to Listis desired, the target type is List, and the parameterized type is Integer aCollection
- the value collectionaConversionContext
- defines some parameters controlling how the conversion is done in detail- Returns:
- the converted value
- Throws:
ParameterUtil.UnresolvableVariableException
java.lang.ClassNotFoundException
UnexecutableException
java.lang.InstantiationException
-
wrapInCollection
protected <T extends java.util.Collection> T wrapInCollection(java.lang.Class<T> aCollectionType, java.lang.Object anArrayOrSingleType)
Wraps a value (or an array of values) in a collection of the given type.- Parameters:
aCollectionType
- the collection typeanArrayOrSingleType
- the array or value to wrap- Returns:
- the collection
-
convertValueToString
public java.lang.String convertValueToString(java.lang.Object aValue, boolean aForceIntermediateMapFlag, ConversionContext aConversionContext)
Description copied from interface:ValueConverter
Converts a given value to a String. This method is intended to be used for the output of values (for example in test results, on the console etc). In comparison to#convertValueToStringArray(Object, UnresolvableVariableHandling)
, this method always returns only a single String value, concatenating arrays first if necessary.- Specified by:
convertValueToString
in interfaceValueConverter
- Parameters:
aValue
- the value (can be an Integrity-internal type or a plain Java Object)aForceIntermediateMapFlag
- whether the conversion should force the usage of an intermediate map (useful for bean types)aConversionContext
- controls some conversion parameters and rules; may be null if the defaultConversionContext
values shall be used- Returns:
- the string
-
convertValueToFormattedString
public FormattedString convertValueToFormattedString(java.lang.Object aValue, boolean aForceIntermediateMapFlag, ConversionContext aConversionContext)
Description copied from interface:ValueConverter
Converts a given value to a formattedString. This method is intended to be used for the output of values (for example in test results, on the console etc). In comparison to#convertValueToStringArray(Object, UnresolvableVariableHandling)
, this method always returns only a single String value, concatenating arrays first if necessary.- Specified by:
convertValueToFormattedString
in interfaceValueConverter
- Parameters:
aValue
- the value (can be an Integrity-internal type or a plain Java Object)aForceIntermediateMapFlag
- whether the conversion should force the usage of an intermediate map (useful for bean types)aConversionContext
- controls some conversion parameters and rules; may be null if the defaultConversionContext
values shall be used- Returns:
- the string
-
convertValueToStringArray
public java.lang.String[] convertValueToStringArray(java.lang.Object aValue, ConversionContext aConversionContext)
Description copied from interface:ValueConverter
Converts a given value to a String array. This method is intended to be used for the output of values (for example in test results, on the console etc).- Specified by:
convertValueToStringArray
in interfaceValueConverter
- Parameters:
aValue
- the value (can be an Integrity-internal type or a plain Java Object)aConversionContext
- controls some conversion parameters and rules; may be null if the defaultConversionContext
values shall be used- Returns:
- the string array
-
convertValueToFormattedStringArray
public FormattedString[] convertValueToFormattedStringArray(java.lang.Object aValue, ConversionContext aConversionContext)
Description copied from interface:ValueConverter
Converts a given value to a formatted String array. This method is intended to be used for the output of values (for example in test results, on the console etc).- Specified by:
convertValueToFormattedStringArray
in interfaceValueConverter
- Parameters:
aValue
- the value (can be an Integrity-internal type or a plain Java Object)aConversionContext
- controls some conversion parameters and rules; may be null if the defaultConversionContext
values shall be used- Returns:
- the string array
-
convertValueToStringArray
public FormattedString[] convertValueToStringArray(java.lang.Object aValue, ConversionContext aConversionContext, java.util.Set<java.lang.Object> someVisitedValues)
Extended version of#convertValueToStringArray(Object, UnresolvableVariableHandling)
.- Parameters:
aValue
-aConversionContext
-someVisitedValues
-- Returns:
-
addConversion
protected void addConversion(java.lang.Class<? extends Conversion<?,?>> aConversion)
Adds the given conversion class to the map of available conversions.- Parameters:
aConversion
- the conversion to add
-
determineConversionPriority
protected int determineConversionPriority(java.lang.Class<? extends Conversion<?,?>> aConversion)
Determines the priority of the given conversion class.- Parameters:
aConversion
- the conversion to introspect- Returns:
- the numeric priority
-
findAndInstantiateConversion
protected Conversion<?,?> findAndInstantiateConversion(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, java.util.Set<java.lang.Object> someVisitedValues, ConversionContext aConversionContext) throws java.lang.InstantiationException, java.lang.IllegalAccessException
Searches all known conversions for a match which is able to convert a given source type into a given target type. This instantiates a found conversion class.- Parameters:
aSourceType
- the source typeaTargetType
- the target typesomeVisitedValues
-aConversionContext
- The conversion context- Returns:
- a ready-to-use, instantiated conversion, or null if none was found
- Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
-
findConversion
protected java.lang.Class<? extends Conversion<?,?>> findConversion(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, java.util.Set<java.lang.Object> someVisitedValues, ConversionContext aConversionContext)
Searches all known conversions for a match which is able to convert a given source type into a given target type. Returns the conversion class (must be instantiated before actually converting something).- Parameters:
aSourceType
- the source typeaTargetType
- the target typeaConversionContext
- The conversion context- Returns:
- a conversion class, or null if none was found
-
findDerivedConversionRecursive
protected java.lang.Class<? extends Conversion<?,?>> findDerivedConversionRecursive(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, ConversionContext aConversionContext)
Searches all known derived conversions for a match which is able to convert a given source type into a subclass of a given target type. If there are multiple matches (which is likely, if the target type is very generic), the conversion with the highest priority wins.- Parameters:
aSourceType
- the source typeaTargetType
- the target type- Returns:
- a conversion class, or null if none was found
-
searchDerivedConversionMap
protected java.lang.Class<? extends Conversion<?,?>> searchDerivedConversionMap(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType)
Searches the derived conversion map for a match. If one or multiple are found, the one with the highest priority is returned (the lists in the map are pre-sorted that way).- Parameters:
aSourceType
- the source typeaTargetType
- the target type- Returns:
- a conversion class, or null if none was found
-
findConversionRecursive
protected java.lang.Class<? extends Conversion<?,?>> findConversionRecursive(java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, ConversionContext aConversionContext)
Searches all known conversions for a match which is able to convert a given source type into a given target type. Returns the conversion class (must be instantiated before actually converting something).- Parameters:
aSourceType
- the source typeaTargetType
- the target typeaConversionContext
- The conversion context- Returns:
- a conversion class, or null if none was found
-
filterConversionClass
protected java.lang.Class<? extends Conversion<?,?>> filterConversionClass(java.lang.Class<? extends Conversion<?,?>> aConversionClass, java.lang.Class<?> aSourceType, java.lang.Class<?> aTargetType, ConversionContext aConversionContext, boolean aDefaultConversionFlag)
This filtering method has the ability to replace a conversion class found by the normal lookup before it's actually used. This is the point at which special rules (for example dictated by theConversionContext
) may be enforced.- Parameters:
aConversionClass
- the conversion class in questionaSourceType
- the source typeaTargetType
- the target typeaConversionContext
- the conversion context to useaDefaultConversionFlag
- whether this is a default conversion or specifically requested to match the target type- Returns:
- the conversion class to use, which may be the input class or any other replacement
-
createConversionInstance
protected <C extends Conversion> C createConversionInstance(java.lang.Class<C> aConversionClass, java.util.Set<java.lang.Object> someVisitedValues) throws java.lang.InstantiationException, java.lang.IllegalAccessException
Creates an instance of the given conversion class. This also injects the Guice dependencies.- Parameters:
aConversionClass
- the conversion- Returns:
- the new instance
- Throws:
java.lang.InstantiationException
java.lang.IllegalAccessException
-
safeguardConversionContext
public ConversionContext safeguardConversionContext(ConversionContext aContext)
This method creates a default conversion context in case none is provided, and returns the provided context otherwise.- Parameters:
aContext
- the context to safeguard- Returns:
- a context (guaranteed not to return null)
-
handleConversionOfStringToByteArray
protected byte[] handleConversionOfStringToByteArray(java.lang.String aSource)
Handles the special case of issue #66: single string to byte array.- Parameters:
aSource
- the string to convert- Returns:
- the byte array
-
handleConversionOfStringToByteWrapperArray
protected java.lang.Byte[] handleConversionOfStringToByteWrapperArray(java.lang.String aSource)
Handles the special case of issue #66: single string to byte array.- Parameters:
aSource
- the string to convert- Returns:
- the byte array (using the wrapper type)
-
handleConversionOfStringValueToByteArray
protected byte[] handleConversionOfStringValueToByteArray(StringValue aSource)
Handles the special case of issue #66: single string to byte array.- Parameters:
aSource
- the string to convert (wrapper type)- Returns:
- the byte array
-
handleConversionOfStringValueToByteWrapperArray
protected java.lang.Byte[] handleConversionOfStringValueToByteWrapperArray(StringValue aSource)
Handles the special case of issue #66: single string to byte array.- Parameters:
aSource
- the string to convert (wrapper type)- Returns:
- the byte array (using the wrapper type)
-
-