Class ElementChangeRecorder


  • public class ElementChangeRecorder
    extends java.lang.Object
    Records changes in the state of an element tree between two discrete points in time and produces a delta tree representing the changes.

    This implementation caches locally the state of an element tree at the time the recorder begins recording. When endRecording() is called, creates a delta tree over the cached state and the new state.

    Clients can use this class as it stands or subclass it as circumstances warrant.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void beginRecording​(IElement inputElement)
      Begins recording changes in the element tree rooted at the given input element, reporting the changes to a new instance of default delta builder.
      void beginRecording​(IElement inputElement, IElementDeltaBuilder deltaBuilder)
      Begins recording changes in the element tree rooted at the given input element, reporting the changes to the given delta builder.
      void beginRecording​(IElement inputElement, IElementDeltaBuilder deltaBuilder, int maxDepth)
      Begins recording changes in the element tree rooted at the given input element for the specified maximum depth, reporting the changes to the given delta builder.
      IElementDeltaBuilder endRecording()
      Ends the current recording and returns a delta builder with the consolidated changes between the state of the element tree at the time the recording was started and its current state.
      protected void findContentChange​(java.lang.Object oldBody, java.lang.Object newBody, IElement element)
      Finds whether the given element has had a content change.
      protected IElementDeltaBuilder getDeltaBuilder()
      Returns the current delta builder.
      protected IElement getInputElement()
      Returns the current input element.
      protected int getMaxDepth()
      Returns the current maximum depth.
      boolean isRecording()
      Returns whether this change recorder is currently recording.
      protected IElementDeltaBuilder newDeltaBuilder​(IElement element)
      Returns a new instance of default delta builder rooted at the given element.
      protected void recordBody​(java.lang.Object body, IElement element)
      Remembers the given body for the given element.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ElementChangeRecorder

        public ElementChangeRecorder()
    • Method Detail

      • isRecording

        public final boolean isRecording()
        Returns whether this change recorder is currently recording.
        Returns:
        true if this change recorder is recording, and false otherwise
      • beginRecording

        public final void beginRecording​(IElement inputElement)
        Begins recording changes in the element tree rooted at the given input element, reporting the changes to a new instance of default delta builder. The delta builder is rooted at the given input element.
        Parameters:
        inputElement - not null
      • beginRecording

        public final void beginRecording​(IElement inputElement,
                                         IElementDeltaBuilder deltaBuilder)
        Begins recording changes in the element tree rooted at the given input element, reporting the changes to the given delta builder. The delta builder may be null, in which case a new instance of default delta builder rooted at the given input element will be used.
        Parameters:
        inputElement - not null
        deltaBuilder - may be null
      • beginRecording

        public void beginRecording​(IElement inputElement,
                                   IElementDeltaBuilder deltaBuilder,
                                   int maxDepth)
        Begins recording changes in the element tree rooted at the given input element for the specified maximum depth, reporting the changes to the given delta builder. The delta builder may be null, in which case a new instance of default delta builder rooted at the given input element will be used.
        Parameters:
        inputElement - not null
        deltaBuilder - may be null
        maxDepth - the maximum depth the recorder should look into (>= 0)
      • endRecording

        public IElementDeltaBuilder endRecording()
        Ends the current recording and returns a delta builder with the consolidated changes between the state of the element tree at the time the recording was started and its current state.
        Returns:
        a delta builder with the consolidated changes (never null)
        Throws:
        java.lang.IllegalStateException - if this recorder is not recording
      • getInputElement

        protected final IElement getInputElement()
        Returns the current input element.
        Returns:
        the current input element
      • getDeltaBuilder

        protected final IElementDeltaBuilder getDeltaBuilder()
        Returns the current delta builder.
        Returns:
        the current delta builder
      • getMaxDepth

        protected final int getMaxDepth()
        Returns the current maximum depth.
        Returns:
        the current maximum depth
      • newDeltaBuilder

        protected IElementDeltaBuilder newDeltaBuilder​(IElement element)
        Returns a new instance of default delta builder rooted at the given element.

        This implementation returns a new instance of ElementDelta.Builder. The root delta is created via the ElementDelta.Factory registered in the element's model context. If no delta factory is registered, a new instance of ElementDelta is used.

        Parameters:
        element - never null
        Returns:
        a new instance of default delta builder (never null)
      • recordBody

        protected void recordBody​(java.lang.Object body,
                                  IElement element)
        Remembers the given body for the given element. Subclasses may override this method, but must make sure to call the super implementation. This method is not intended to be invoked by subclasses.
        Parameters:
        body - never null
        element - never null
      • findContentChange

        protected void findContentChange​(java.lang.Object oldBody,
                                         java.lang.Object newBody,
                                         IElement element)
        Finds whether the given element has had a content change. Subclasses may override this method, but are not intended to invoke it.

        Implementations can compare the given bodies and, if there are differences (excepting children), insert an appropriate change delta (such as F_CONTENT) for the given element into the delta tree being built. Implementations should not take changes in children into account.

        This implementation invokes ((Body)newBody).findContentChange((Body)oldBody, element, getDeltaBuilder()).

        Parameters:
        oldBody - the old version of the element's body (never null)
        newBody - the new version of the element's body (never null)
        element - the element whose bodies are to be compared (never null)