Package org.coliper.ibean.extension
Interface CloneableBean<T>
-
- All Superinterfaces:
Cloneable
public interface CloneableBean<T> extends Cloneable
Enables cloning of beans viaObject.clone(). To enable cloning the bean type needs to extendCloneableBean.clone()will always return a bean of exactly the same type as the origin bean. Therefore the generic type parameter must always be set to the declared bean type or at least to one of its super types. The generic type here gives the advantage of not having to cast the returned clone.Example:
public interface Customer extends CloneableBean<Customer> { ...Attention:
- A cloned bean has all field values copied from its origin but does
not have all extension interface states replicated as well. The
extension interface states are those of a newly created bean.
For example if your bean type extendsFreezableand you clone a frozen bean the cloned bean will not be frozen as well. - Although
clone()is a method fromObjectyou always need a dedicated handler for cloning in yourIBeanFactory. This needs to be considered when creating custom factories. If you useIBeanwith its default configuration this can be neglected.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Tclone()Creates a copy of this bean, having the same field values but having an initial state on any extension interface.TdeepClone()Creates a copy of this bean with the same field values.
-
-
-
Method Detail
-
clone
T clone()
Creates a copy of this bean, having the same field values but having an initial state on any extension interface. If field value is an object the newly created bean will also reference the same object. If you need the field values also cloned usedeepClone().- Returns:
- an IBean of the same type
-
deepClone
T deepClone()
Creates a copy of this bean with the same field values. In opposite toclone()field values are also cloned with following logic:- Fields of type
Cloneableare just cloned but no special deep cloning algorithm is executed on them. - There is a special logic for fields of type
Collection. If the collection isCloneableit is cloned. If in addition it is also modifiable deep cloning is also executed on its elements, that is, all contained elements will be replaced by their deep clones. A collection is considered as modifiable when it supports methodsCollection.clear()andCollection.add(Object). - Fields of type
CloneableBeanare recursively deep cloned. - Any field that is not of type
CloneableorCloneableBeanis not cloned.
deepClone()has no cycle detection. If you need cycle detection or a more sophisticated cloning use one of the deep cloning Java frameworks out there.- Returns:
- an IBean of the same type
- Fields of type
-
-