Class CollectionsDiffBuilder<T>

  • Type Parameters:
    T - the type of elements within the collections to be compared

    public class CollectionsDiffBuilder<T>
    extends java.lang.Object
    An utility class that can compute the difference between two collections containing the same object types. Common objects will can be retrieved through getCommons(), while ones available only in one of them can be retrieved through getOnlyFirst() and getOnlySecond().
    • Constructor Summary

      Constructors 
      Constructor Description
      CollectionsDiffBuilder​(java.lang.Class<T> elementType, java.util.Collection<T> first, java.util.Collection<T> second)
      Builds the diff builder.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void compute​(java.util.Comparator<T> comparer)
      Computes the diff between the collections used to create this object.
      java.util.Collection<org.apache.commons.lang3.tuple.Pair<T,​T>> getCommons()
      Yields a collection containing all the pair of elements (deemed to be equal) that are contained in both collections used to create this builder.
      java.util.Collection<T> getOnlyFirst()
      Yields a collection containing all the elements that are contained only in the first collection used to create this builder.
      java.util.Collection<T> getOnlySecond()
      Yields a collection containing all the elements that are contained only in the second collection used to create this builder.
      boolean sameContent()
      Yields true if and only if the two collections used to create this builder contain the same elements, that is, if both getOnlyFirst() and getOnlySecond() are empty.
      • Methods inherited from class java.lang.Object

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

      • CollectionsDiffBuilder

        public CollectionsDiffBuilder​(java.lang.Class<T> elementType,
                                      java.util.Collection<T> first,
                                      java.util.Collection<T> second)
        Builds the diff builder.
        Parameters:
        elementType - the type of elements that are stored into the collections (used for internal temporary array creation)
        first - the first collection
        second - the second colelction
    • Method Detail

      • compute

        public void compute​(java.util.Comparator<T> comparer)
        Computes the diff between the collections used to create this object. After sorting the two collections independently according to the given Comparator, the two collections are simultaneously iterated over, and, for each pair <f, s> where f comes from the first collection and s comes from the second one, comparer is used to determine the relationship of the two elements, exploiting their ordering:
        • if both f and s are null, the comparison interrupts
        • if f is null and s is not, s is added to getOnlySecond() and the iterator of the second collection moves forward
        • if s is null and f is not, f is added to getOnlyFirst() and the iterator of the first collection moves forward
        • if comparer.compare(f, s) == 0, <f, s> is added to getCommons() and the iterators of both collections move forward
        • if comparer.compare(f, s) > 0, s is added to getOnlySecond() and the iterator of the second collection moves forward
        • if comparer.compare(f, s) < 0, f is added to getOnlyFirst() and the iterator of the first collection moves forward
        Parameters:
        comparer - the Comparator establishing the ordering between elements in the two collections
      • getOnlySecond

        public java.util.Collection<T> getOnlySecond()
        Yields a collection containing all the elements that are contained only in the second collection used to create this builder. The returned collection will be empty unless compute(Comparator) has been called.
        Returns:
        the elements contained only in the second collection
      • getOnlyFirst

        public java.util.Collection<T> getOnlyFirst()
        Yields a collection containing all the elements that are contained only in the first collection used to create this builder. The returned collection will be empty unless compute(Comparator) has been called.
        Returns:
        the elements contained only in the first collection
      • getCommons

        public java.util.Collection<org.apache.commons.lang3.tuple.Pair<T,​T>> getCommons()
        Yields a collection containing all the pair of elements (deemed to be equal) that are contained in both collections used to create this builder. Pair.getLeft() will return the element coming from the first collection, while Pair.getRight() will return the one coming from the second one. The returned collection will be empty unless compute(Comparator) has been called.
        Returns:
        the elements contained only in both collections
      • sameContent

        public boolean sameContent()
        Yields true if and only if the two collections used to create this builder contain the same elements, that is, if both getOnlyFirst() and getOnlySecond() are empty. This method will always return true unless compute(Comparator) has been called.
        Returns:
        true if the collections contain the same elements, false otherwise