Interface CopyContext


public interface CopyContext
Context of the single cloning process.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Receives a clone.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Object
    Marker returned by copyNow(Object) when the clone cannot be got now.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Registers a task which is invoked when the whole object graph is copied, e.g.
    <T> T
    copy(T original)
    Returns the instant copy of the object.
    default <T> void
    copy(T original, CopyContext.CloneSetter<? super T> setter)
    Copies the object and passes the clone to the setter, now or later, see copyNow(Object).
    <T> T
    copyComplete(T original)
    Returns the copy of the object with its own state already copied (for a two-phase copier the second phase is done, its referenced objects may still be incomplete).
    copyNow(Object original)
    Copies the object if its clone exists or can be created now.
    boolean
    isComplete(Object original)
    Tells whether the clone of the object has its own state copied.
    <T> T
    register(T clone)
    Registers clone in the context.
    <T> void
    subscribe(T original, CopyContext.CloneSetter<? super T> setter)
    Passes the clone of the object to the setter when it is registered, see copyNow(Object).
    void
    Invokes task.
  • Field Details

    • PENDING

      static final Object PENDING
      Marker returned by copyNow(Object) when the clone cannot be got now.
  • Method Details

    • register

      <T> T register(T clone)
      Registers clone in the context.
      Type Parameters:
      T - object type
      Parameters:
      clone - clone
      Returns:
      clone
    • copy

      <T> T copy(T original) throws Exception
      Returns the instant copy of the object. It can be a copy, saved previously into cache, or fresh copy. The copy may not be the full copy of the original object, the inner state can be processed later.
      Type Parameters:
      T - object type
      Parameters:
      original - original
      Returns:
      clone
      Throws:
      Exception - if something went wrong
    • copyNow

      Object copyNow(Object original) throws Exception
      Copies the object if its clone exists or can be created now. Returns PENDING if the object is being created right now (a reference cycle through a single-phase copier, or another thread); then the caller subscribes to the clone. Outside a second phase of a two-phase copier works as copy(Object) and never returns PENDING.
      Parameters:
      original - original
      Returns:
      clone or PENDING
      Throws:
      Exception - if something went wrong
    • subscribe

      <T> void subscribe(T original, CopyContext.CloneSetter<? super T> setter) throws Exception
      Passes the clone of the object to the setter when it is registered, see copyNow(Object). Until then the object copied by the current second phase is not complete. If the clone is already available, the setter is called at once.
      Type Parameters:
      T - object type
      Parameters:
      original - original
      setter - receiver of the clone
      Throws:
      Exception - if something went wrong
    • copy

      default <T> void copy(T original, CopyContext.CloneSetter<? super T> setter) throws Exception
      Copies the object and passes the clone to the setter, now or later, see copyNow(Object). Convenient, but allocates the setter for every call, copiers on hot paths should use copyNow(Object).
      Type Parameters:
      T - object type
      Parameters:
      original - original
      setter - receiver of the clone
      Throws:
      Exception - if something went wrong
    • copyComplete

      <T> T copyComplete(T original) throws Exception
      Returns the copy of the object with its own state already copied (for a two-phase copier the second phase is done, its referenced objects may still be incomplete). Needed by copiers which create the clone from copied values, e.g. canonical constructors of records. If the object is part of a reference cycle with an object being created by the current thread, its clone cannot be completed before that object's clone exists: then the clone is returned incomplete, see isComplete(Object).
      Type Parameters:
      T - object type
      Parameters:
      original - original
      Returns:
      clone
      Throws:
      Exception - if something went wrong
    • isComplete

      boolean isComplete(Object original)
      Tells whether the clone of the object has its own state copied. A copier which has got an incomplete clone from copyComplete(Object) must keep exactly this clone in its own clone: it will be completed after the own clone is registered.
      Parameters:
      original - original
      Returns:
      true if the clone is complete or the object needs no copying
    • thenInvoke

      void thenInvoke(Callable<?> task) throws Exception
      Invokes task. It can be invoked immediately or later depending on the context implementation. The result of callable is ignored.
      Parameters:
      task - task
      Throws:
      Exception - if something went wrong
    • afterCopy

      void afterCopy(Callable<?> task)
      Registers a task which is invoked when the whole object graph is copied, e.g. to rebuild a container whose structure depends on hash codes of copied objects. Tasks are invoked in a single thread.
      Parameters:
      task - task