public class DynamicComparisonPerformer extends AbstractComparisonPerformer<java.lang.Object>
This performer allows you to register delegates that will be dynamically selected at the moment of execution.
All registered delegates are mapped by a class. When apply(Object, Object) is called -
getDelegate(Object, Object) is called and result delegate is used to perform actual comparison;
| Constructor and Description |
|---|
DynamicComparisonPerformer() |
| Modifier and Type | Method and Description |
|---|---|
ComparisonResult |
apply(java.lang.Object actual,
java.lang.Object expected) |
ComparisonPerformer<?> |
findRegisteredDelegate(java.lang.Object actual,
java.lang.Object expected)
If any registered delegate is matched - it will be returned first.
|
java.util.Map<java.lang.Class<?>,java.util.function.Function<?,?>> |
getArrayDelegateProviders()
Returns map of registered array delegates providers.
|
java.util.Map<java.lang.Class<? extends java.util.Collection<?>>,java.util.Map<java.lang.Class<?>,java.util.function.Function<?,?>>> |
getCollectionDelegateProviders()
Returns map of registered collection delegate providers.
|
ComparisonPerformer<java.lang.Object> |
getDefaultDelegate()
Default delegate to be used when no other delegate is found for a pair of objects.
|
ComparisonPerformer<?> |
getDelegate(java.lang.Object actual,
java.lang.Object expected)
This method calls
findRegisteredDelegate(Object, Object) and if its result is null -
result of the getDefaultDelegate() is returned. |
java.util.Map<java.lang.Class<? extends java.util.Map<?,?>>,java.util.Map<java.lang.Class<?>,java.util.function.Function<?,?>>> |
getMapDelegateProviders()
Returns map of registered map delegate providers.
|
java.util.Map<java.lang.Class<?>,ComparisonPerformer<?>> |
getRegisteredDelegates() |
<V> DynamicComparisonPerformer |
registerArrayDelegateProvider(java.lang.Class<V> arrayType,
java.util.function.Function<ComparisonPerformer<? super V>,ComparisonPerformer<? super V[]>> provider)
Method allows you to register delegate providers, used to created delegates for arrays of different types.
|
<V,C extends java.util.Collection<?>> |
registerCollectionDelegateProvider(java.lang.Class<C> collectionType,
java.lang.Class<V> valueType,
java.util.function.Function<ComparisonPerformer<V>,ComparisonPerformer<? super C>> provider)
Method allows you to register delegate providers, used to created delegates for collections
of different types and different contents.
|
<V> DynamicComparisonPerformer |
registerDelegate(java.lang.Class<V> valueType,
ComparisonPerformer<? super V> delegate) |
<V,M extends java.util.Map<?,V>> |
registerMapDelegateProvider(java.lang.Class<M> mapType,
java.lang.Class<V> valueType,
java.util.function.Function<ComparisonPerformer<V>,ComparisonPerformer<? super M>> provider)
Method allows you to register delegate providers, used to created delegates for maps of different
types and different contents.
|
void |
setDefaultDelegate(ComparisonPerformer<java.lang.Object> defaultDelegate)
Default delegate to be used when no other delegate is found for a pair of objects.
|
getName, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waittestpublic ComparisonPerformer<?> getDelegate(java.lang.Object actual, java.lang.Object expected)
findRegisteredDelegate(Object, Object) and if its result is null -
result of the getDefaultDelegate() is returned.public void setDefaultDelegate(ComparisonPerformer<java.lang.Object> defaultDelegate)
null or this object.public ComparisonPerformer<java.lang.Object> getDefaultDelegate()
setDefaultDelegate(ComparisonPerformer)public <V> DynamicComparisonPerformer registerDelegate(java.lang.Class<V> valueType, ComparisonPerformer<? super V> delegate)
public java.util.Map<java.lang.Class<?>,ComparisonPerformer<?>> getRegisteredDelegates()
public <V> DynamicComparisonPerformer registerArrayDelegateProvider(java.lang.Class<V> arrayType, java.util.function.Function<ComparisonPerformer<? super V>,ComparisonPerformer<? super V[]>> provider)
Method allows you to register delegate providers, used to created delegates for arrays of different types. Delegate is matched if both compared values are instances of the key class. Delegates are matched in the order of registration, so first matched delegate will be used.
Function will always receive this dynamic performer and should produce comparison performer able to accept two arrays of the specified type, and compare them properly.
By default: ArrayComparisonPerformer is used for all arrays.
getArrayDelegateProviders()public java.util.Map<java.lang.Class<?>,java.util.function.Function<?,?>> getArrayDelegateProviders()
Returns map of registered array delegates providers. Map is fully mutable, so you can do whatever you want
with it, just remember that types should be thoroughly checked, or there's a big possibility of type cast error
at the moment of performance. It is recommended to use registerArrayDelegateProvider(Class, Function)
method.
public <V,C extends java.util.Collection<?>> DynamicComparisonPerformer registerCollectionDelegateProvider(java.lang.Class<C> collectionType, java.lang.Class<V> valueType, java.util.function.Function<ComparisonPerformer<V>,ComparisonPerformer<? super C>> provider)
Method allows you to register delegate providers, used to created delegates for collections of different types and different contents. Provider is matched if both compared values are instances of the specified collection type, and all of its elements are instances of the specified value type. So you can register specific provider for types: List of Object, to match ALL lists; or you can register provider for types: Collection of String, to cover any collections of strings.
Both collection type and element types are checked in the order of registration, so if you register provider for types Collection of Object, and then register provider for types: List of Object - second one will never be used. The same if you register second provider for types: Collection of String - it will never be used.
Function will always receive this dynamic performer and should produce comparison performer able to accept two collections of the specified type, and compare them propertly.
By default: SetComparisonPerformer is used for all collections.
getCollectionDelegateProviders()public java.util.Map<java.lang.Class<? extends java.util.Collection<?>>,java.util.Map<java.lang.Class<?>,java.util.function.Function<?,?>>> getCollectionDelegateProviders()
Returns map of registered collection delegate providers. Map is fully mutable, so you can do whatever you want
with it, just remember that types should be thoroughly checked, or there's a big possibility of type cast error
at the moment of performance. It is recommended to use registerCollectionDelegateProvider(Class, Class, Function)
method.
public <V,M extends java.util.Map<?,V>> DynamicComparisonPerformer registerMapDelegateProvider(java.lang.Class<M> mapType, java.lang.Class<V> valueType, java.util.function.Function<ComparisonPerformer<V>,ComparisonPerformer<? super M>> provider)
Method allows you to register delegate providers, used to created delegates for maps of different types and different contents. Provider is matched if both compared values are instances of the specified map type, and all of its values are instances of the specified value type. So you can register specific provider for types: HashMap of Object, to match ALL hashmaps; or you can register provider for types: Map of String, to cover any map with string values.
Note: keys are ignored - they can be of any type.
Both map type and value type are checked in the order of registration, so if you register provider for types Map of Object, and then register provider for types: HashMap of Object - second one will never be used. The same if you register second provider for types: Map of String - it will never be used.
Function will always receive this dynamic performer and should produce comparison performer able to accept two maps of the specified type, and compare them propertly.
By default: MapComparisonPerformer is used for all maps.
getMapDelegateProviders()public java.util.Map<java.lang.Class<? extends java.util.Map<?,?>>,java.util.Map<java.lang.Class<?>,java.util.function.Function<?,?>>> getMapDelegateProviders()
Returns map of registered map delegate providers. Map is fully mutable, so you can do whatever you want
with it, just remember that types should be thoroughly checked, or there's a big possibility of type cast error
at the moment of performance. It is recommended to use registerMapDelegateProvider(Class, Class, Function)
method.
public ComparisonPerformer<?> findRegisteredDelegate(java.lang.Object actual, java.lang.Object expected)
If any registered delegate is matched - it will be returned first. Delegate is matched if both specified objects are instances of the key class. So if you will register a delegate for the Object class - it will match any specified objects (if both are not null).
Note: delegates are checked in the order of registration. So if you will register a delegate for the CharSequence class, and then register a delegate for the String class - second one will never be used.
If no registered delegate is found - special cases are checked. Which are:
Note: each specific performer receives this dynamic performer as delegate which means that elements are compared with the same registered delegates.
You can configure special cases using methods:
registerArrayDelegateProvider(Class, Function)
registerCollectionDelegateProvider(Class, Class, Function)
registerMapDelegateProvider(Class, Class, Function)
If non of the special cases has matched - null is returned.
public ComparisonResult apply(java.lang.Object actual, java.lang.Object expected)