Package it.unive.lisa.util.collections
Class CollectionsDiffBuilder<T>
- java.lang.Object
-
- it.unive.lisa.util.collections.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 throughgetCommons()
, while ones available only in one of them can be retrieved throughgetOnlyFirst()
andgetOnlySecond()
.
-
-
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()
Yieldstrue
if and only if the two collections used to create this builder contain the same elements, that is, if bothgetOnlyFirst()
andgetOnlySecond()
are empty.
-
-
-
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 collectionsecond
- 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 givenComparator
, the two collections are simultaneously iterated over, and, for each pair<f, s>
wheref
comes from the first collection ands
comes from the second one,comparer
is used to determine the relationship of the two elements, exploiting their ordering:- if both
f
ands
arenull
, the comparison interrupts - if
f
isnull
ands
is not,s
is added togetOnlySecond()
and the iterator of the second collection moves forward - if
s
isnull
andf
is not,f
is added togetOnlyFirst()
and the iterator of the first collection moves forward - if
comparer.compare(f, s) == 0
,<f, s>
is added togetCommons()
and the iterators of both collections move forward - if
comparer.compare(f, s) > 0
,s
is added togetOnlySecond()
and the iterator of the second collection moves forward - if
comparer.compare(f, s) < 0
,f
is added togetOnlyFirst()
and the iterator of the first collection moves forward
- Parameters:
comparer
- theComparator
establishing the ordering between elements in the two collections
- if both
-
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 unlesscompute(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 unlesscompute(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, whilePair.getRight()
will return the one coming from the second one. The returned collection will be empty unlesscompute(Comparator)
has been called.- Returns:
- the elements contained only in both collections
-
sameContent
public boolean sameContent()
Yieldstrue
if and only if the two collections used to create this builder contain the same elements, that is, if bothgetOnlyFirst()
andgetOnlySecond()
are empty. This method will always returntrue
unlesscompute(Comparator)
has been called.- Returns:
true
if the collections contain the same elements,false
otherwise
-
-