Interface CloneableBean<T>

  • All Superinterfaces:
    Cloneable

    public interface CloneableBean<T>
    extends Cloneable
    Enables cloning of beans via Object.clone(). To enable cloning the bean type needs to extend CloneableBean. 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 extends Freezable and you clone a frozen bean the cloned bean will not be frozen as well.
    • Although clone() is a method from Object you always need a dedicated handler for cloning in your IBeanFactory. This needs to be considered when creating custom factories. If you use IBean with its default configuration this can be neglected.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      T clone()
      Creates a copy of this bean, having the same field values but having an initial state on any extension interface.
      T deepClone()
      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 use deepClone().
        Returns:
        an IBean of the same type
      • deepClone

        T deepClone()
        Creates a copy of this bean with the same field values. In opposite to clone() field values are also cloned with following logic:
        • Fields of type Cloneable are 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 is Cloneable it 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 methods Collection.clear() and Collection.add(Object).
        • Fields of type CloneableBean are recursively deep cloned.
        • Any field that is not of type Cloneable or CloneableBean is 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