Class DiffNode

java.lang.Object
de.danielbechler.diff.node.DiffNode

public class DiffNode extends Object
Represents a part of an object. It could be the object itself, one of its properties, an item in a collection or a map entry. A node may have one parent node and any number of children. It also provides methods to read and write the property represented by this node on any object of the same type as the original object. Last but not least, a node knows how the associated property has changed compared to the base object.
  • Field Details

    • ROOT

      public static final DiffNode ROOT
  • Constructor Details

  • Method Details

    • setChildIdentityStrategy

      public void setChildIdentityStrategy(IdentityStrategy identityStrategy)
    • newRootNode

      public static DiffNode newRootNode()
    • newRootNodeWithType

      public static DiffNode newRootNodeWithType(Class<?> valueType)
    • getState

      public DiffNode.State getState()
      Returns:
      The state of this node.
    • setState

      public void setState(DiffNode.State state)
      Parameters:
      state - The state of this node.
    • matches

      public boolean matches(NodePath path)
    • hasChanges

      public boolean hasChanges()
    • isAdded

      public final boolean isAdded()
      Convenience method for getState() == DiffNode.State.ADDED
    • isChanged

      public final boolean isChanged()
      Convenience method for getState() == DiffNode.State.CHANGED
    • isRemoved

      public final boolean isRemoved()
      Convenience method for getState() == DiffNode.State.REMOVED
    • isUntouched

      public final boolean isUntouched()
      Convenience method for getState() == DiffNode.State.UNTOUCHED
    • isCircular

      public boolean isCircular()
      Convenience method for getState() == DiffNode.State.CIRCULAR
    • getPath

      public NodePath getPath()
      Returns:
      The absolute property path from the object root up to this node.
    • getElementSelector

      public ElementSelector getElementSelector()
    • getValueType

      public Class<?> getValueType()
      Returns:
      Returns the type of the property represented by this node, or null if unavailable.
    • setType

      public final void setType(Class<?> aClass)
      Allows to explicity set the type of this node. This will overshadow the type returned by getValueTypeInfo() as well as the one returned by the accessor.
      Parameters:
      aClass - The type of the value represented by this node.
    • getValueTypeInfo

      public TypeInfo getValueTypeInfo()
    • setValueTypeInfo

      public void setValueTypeInfo(TypeInfo typeInfo)
    • hasChildren

      public boolean hasChildren()
      Returns:
      true if this node has children.
    • childCount

      public int childCount()
    • getChild

      public DiffNode getChild(String propertyName)
      Retrieve a child with the given property name relative to this node.
      Parameters:
      propertyName - The name of the property represented by the child node.
      Returns:
      The requested child node or null.
    • getChild

      public DiffNode getChild(ElementSelector elementSelector)
      Retrieve a child that matches the given path element relative to this node.
      Parameters:
      elementSelector - The path element of the child node to get.
      Returns:
      The requested child node or null.
    • getChild

      public DiffNode getChild(NodePath nodePath)
      Retrieve a child that matches the given absolute path, starting from the current node.
      Parameters:
      nodePath - The path from the object root to the requested child node.
      Returns:
      The requested child node or null.
    • getChild

      public DiffNode getChild(List<ElementSelector> selectors)
      Retrieve a child that matches the given path element relative to this node.
      Parameters:
      selectors - The path element of the child node to get.
      Returns:
      The requested child node or null.
    • addChild

      public void addChild(DiffNode node)
      Adds a child to this node and sets this node as its parent node.
      Parameters:
      node - The node to add.
    • visit

      public final void visit(DiffNode.Visitor visitor)
      Visit this and all child nodes.
      Parameters:
      visitor - The visitor to use.
    • visit

      protected final void visit(DiffNode.Visitor visitor, Visit visit)
    • visitChildren

      public final void visitChildren(DiffNode.Visitor visitor)
      Visit all child nodes but not this one.
      Parameters:
      visitor - The visitor to use.
    • visitParents

      public final void visitParents(DiffNode.Visitor visitor)
    • getFieldAnnotations

      public Set<Annotation> getFieldAnnotations()
      If this node represents a bean property this method returns all annotations of its field.

      Only works for fields having a name that matches the name derived from the getter.

      Returns:
      The annotations of the field, or an empty set if there is no field with the name derived from the getter.
    • getFieldAnnotation

      public <T extends Annotation> T getFieldAnnotation(Class<T> annotationClass)
      Type Parameters:
      T -
      Parameters:
      annotationClass - the annotation we are looking for
      Returns:
      The given annotation of the field, or null if not annotated or if there is no field with the name derived from the getter.
    • getPropertyAnnotations

      public Set<Annotation> getPropertyAnnotations()
      If this node represents a bean property this method returns all annotations of its getter.
      Returns:
      A set of annotations of this nodes property getter or an empty set.
    • getPropertyAnnotation

      public <T extends Annotation> T getPropertyAnnotation(Class<T> annotationClass)
    • getPropertyName

      public String getPropertyName()
      If this node represents a bean property, this method will simply return its name. Otherwise it will return the property name of its closest bean property representing ancestor. This way intermediate nodes like those representing collection, map or array items will be semantically tied to their container objects.

      That is especially useful for inclusion and exclusion rules. For example, when a List is explicitly included by property name, it would be weird if the inclusion didn't also apply to its items.

    • isPropertyAware

      public final boolean isPropertyAware()
      Returns true when this node represents a bean property and can therefore be queried for property specific information like annotations or property types. But there will also be nodes that represent collection items, map entries, etc. In those cases this method will return false.
    • isRootNode

      public final boolean isRootNode()
    • isIgnored

      public final boolean isIgnored()
      Convenience method for getState() == DiffNode.State.IGNORED
    • isExcluded

      @Deprecated public boolean isExcluded()
      Deprecated.
      This method was a shortcut to extract the "exclude" flag from the ObjectDiffProperty annotation. Since we found a better way to do that, it is not needed anymore and will be removed in future versions. The name is also misleading. It implies that here lies the truth about the exclusion, but only the InclusionService can tell for sure. This flag is just only little piece of the puzzle.
      See Also:
      • TypePropertyAnnotationInclusionResolver
    • getCategories

      public final Set<String> getCategories()
      Returns an unmodifiable Set of String with the categories of this node.
      Returns:
      an unmodifiable Set of String with the categories of this node
    • getParentNode

      public DiffNode getParentNode()
      Returns:
      The parent node, if any.
    • setParentNode

      protected final void setParentNode(DiffNode parentNode)
      Sets the parent node.
      Parameters:
      parentNode - The parent of this node. May be null, if this is a root node.
    • get

      public Object get(Object target)
    • set

      public void set(Object target, Object value)
    • unset

      public void unset(Object target)
    • canonicalGet

      public Object canonicalGet(Object target)
    • canonicalSet

      public void canonicalSet(Object target, Object value)
    • canonicalUnset

      public void canonicalUnset(Object target)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • addCategories

      public void addCategories(Collection<String> additionalCategories)
    • getCircleStartPath

      public NodePath getCircleStartPath()
      Returns:
      Returns the path to the first node in the hierarchy that represents the same object instance as this one. (Only if isCircular() returns true.
    • setCircleStartPath

      public void setCircleStartPath(NodePath circularStartPath)
    • getCircleStartNode

      public DiffNode getCircleStartNode()
    • setCircleStartNode

      public void setCircleStartNode(DiffNode circleStartNode)