Class DeepCloner
This class is intended to be used by individual map storage implementations for copying over entities into their native implementations.
For example, a MapClientEntity
interface could be implemented by MapClientEntityImpl
(used by a file-based storage in this example) and an HotRodClientEntityImpl
(for Infinispan).
Say that the Infinispan is stacked on top of the file-based storage to provide caching layer.
Upon first read, a MapClientEntityImpl
could be obtained from file-based storage and passed
to Infinispan layer for caching. Infinispan, regardless of the actual implementation, need to store
the MapClientEntity
data in a form that can be processed and sent over the wire in Infinispan
(say in an InfinispanClientEntityImpl
). To achieve this, the Infinispan store has to clone
the file entity values from the MapClientEntityImpl
to InfinispanClientEntityImpl
,
i.e. it performs deep cloning, using this helper class.
Broader context:
In tree store, map storages are agnostic to their neighbours. Therefore each implementation can be
provided with a record (a MapClientEntity
instance in the example above) originating from
any other implementation. For a map storage to process the record (beyond read-only mode),
it needs to be able to clone it into its own entity. Each of the storages thus can benefit from
the DeepCloner
capabilities.
- Author:
- hmlnarik
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Builder for theDeepCloner
helper class.static interface
Function that clones properties fromoriginal
object to atarget
object and returns the cloned object (usually the same as thetarget
).static interface
Function that instantiates a delegation object of typeV
with the given delegate providerstatic interface
Function that instantiates a delegation object of typeV
with the given per-field delegate providerstatic @interface
Marker for interfaces that could be requested for instantiation and cloning. -
Field Summary
-
Method Summary
Modifier and TypeMethodDescription<V> V
deepClone
(V from, V to) Deeply clones properties from thefrom
instance to theto
instance.<V> V
deepCloneNoId
(V from, V to) Deeply clones properties from thefrom
instance to theto
instance excluding the ID field.<D,
V extends D>
Ddelegate
(Class<V> delegateClass, DelegateProvider<D> delegateProvider) <D,
V extends D>
Ddelegate
(V delegate, DelegateProvider<D> delegateProvider) <V> V
emptyInstance
(Class<V> instanceClass) <V> V
entityFieldDelegate
(Class<V> delegateClass, EntityFieldDelegate<V> delegateProvider) <V extends AbstractEntity>
VentityFieldDelegate
(V delegate, EntityFieldDelegate<V> delegateProvider) <V extends AbstractEntity>
VCreates a new instance of the given type and copies its properties from thefrom
instance<V> V
from
(V from) Creates a new instance of the given type and copies its properties from thefrom
instance<V> V
newInstance
(Class<V> clazz) Creates a new instance of the given class or interface if the parameterless constructor for that type is known.<V> Class<? extends V>
newInstanceType
(Class<V> valueType) Returns a class type of an instance that would be instantiated bynewInstance(java.lang.Class)
method.static <T> T
Issues warning in the logs and returns the input parametero
-
Field Details
-
DUMB_CLONER
-
-
Method Details
-
delegate
-
delegate
-
entityFieldDelegate
public <V extends AbstractEntity> V entityFieldDelegate(V delegate, EntityFieldDelegate<V> delegateProvider) -
entityFieldDelegate
-
emptyInstance
-
newInstance
Creates a new instance of the given class or interface if the parameterless constructor for that type is known.- Type Parameters:
V
- Type (class or a@Root
interface) to create a new instance- Parameters:
clazz
- Type (class or a@Root
interface) to create a new instance- Returns:
- A new instance
- Throws:
IllegalStateException
- When the constructor is not known.
-
newInstanceType
Returns a class type of an instance that would be instantiated bynewInstance(java.lang.Class)
method.- Type Parameters:
V
- Type (class or a@Root
interface) to create a new instance- Parameters:
valueType
- Type (class or a@Root
interface) to create a new instance- Returns:
- See description
-
deepClone
public <V> V deepClone(V from, V to) Deeply clones properties from thefrom
instance to theto
instance.- Type Parameters:
V
- Type (class or a@Root
interface) to clone the instance- Parameters:
from
- Original instanceto
- Instance to copy the properties onto- Returns:
- Instance which has all the properties same as the
from
. Preferably,to
is returned. Howeverfrom
is returned if the cloner is not known and generic cloner is not available.
-
deepCloneNoId
public <V> V deepCloneNoId(V from, V to) Deeply clones properties from thefrom
instance to theto
instance excluding the ID field.- Type Parameters:
V
- Type (class or a@Root
interface) to clone the instance- Parameters:
from
- Original instanceto
- Instance to copy the properties onto- Returns:
- Instance which has all the properties same as the
from
. Preferably,to
is returned. Howeverfrom
is returned if the cloner is not known and generic cloner is not available.
-
from
Creates a new instance of the given type and copies its properties from thefrom
instance- Type Parameters:
V
- Type (class or a@Root
interface) to create a new instance and clone properties from- Parameters:
newId
- ID of the new objectfrom
- Original instance- Returns:
- Newly created instance or
null
iffrom
isnull
.
-
from
public <V> V from(V from) Creates a new instance of the given type and copies its properties from thefrom
instance- Type Parameters:
V
- Type (class or a@Root
interface) to create a new instance and clone properties from- Parameters:
from
- Original instance- Returns:
- Newly created instance or
null
iffrom
isnull
.
-
warnCloneNotSupported
public static <T> T warnCloneNotSupported(T o) Issues warning in the logs and returns the input parametero
- Parameters:
o
-- Returns:
- The
o
object
-