Package org.plumelib.util
Class OrderedPairIterator<T extends @Nullable Object>
java.lang.Object
org.plumelib.util.OrderedPairIterator<T>
- Type Parameters:
T- the element type of each component iterator; this OrderedPairIterator has elements of typeMPair<T,T>
public class OrderedPairIterator<T extends @Nullable Object>
extends Object
implements Iterator<MPair<@Nullable T,@Nullable T>>
Given two sorted iterators, this class returns a new iterator that pairs equal elements of the
inputs, according to the sort order or the given comparator. If an element has no equal element
in the other iterator, then the element is paired with null.
For example, suppose that the inputs are
[1, 2, 3, 5] and [1, 3, 5, 7, 9].
Then the output is
[(1,1), (2,null), (3,3), (5,5), (null,7), (null, 9)].
(This operation is similar to, but not the same as, the operation called "zipping".)
In some cases this is just the right abstraction. But in some cases it's appropriate to use set intersection/difference instead.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) @Nullable Comparator<? super T>The comparator to be used to compare elements from the two iterators, to determine whether they match.The iterator for first elements of pairs.The iterator for second elements of pairs.(package private) @Nullable TThe next element to be read by itor1.(package private) @Nullable TThe next element to be read by itor2. -
Constructor Summary
ConstructorsConstructorDescriptionOrderedPairIterator(Iterator<T> itor1, Iterator<T> itor2) Create an iterator that returns pairs, where each pair contains has an element from each iterator and the two elements are equal.OrderedPairIterator(Iterator<T> itor1, Iterator<T> itor2, Comparator<T> comparator) Create an iterator that returns pairs, where each pair contains has an element from each iterator and the two elements are equal according to the comparator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanhasNext()next()voidremove()return1()Returns an element of the first iterator, paired with null.return2()Returns a pair of null and an element of the second iterator.Returns a pair containing an element from each iterator.private voidsetnext1()Set the next1 variable.private voidsetnext2()Set the next2 variable.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Iterator
forEachRemaining
-
Field Details
-
itor1
The iterator for first elements of pairs. -
itor2
The iterator for second elements of pairs. -
next1
The next element to be read by itor1. -
next2
The next element to be read by itor2. -
comparator
@Nullable Comparator<? super T extends @Nullable Object> comparatorThe comparator to be used to compare elements from the two iterators, to determine whether they match. Null to use the natural comparison.
-
-
Constructor Details
-
OrderedPairIterator
Create an iterator that returns pairs, where each pair contains has an element from each iterator and the two elements are equal.- Parameters:
itor1- iterator for first elements of pairsitor2- iterator for second elements of pairs
-
OrderedPairIterator
Create an iterator that returns pairs, where each pair contains has an element from each iterator and the two elements are equal according to the comparator.- Parameters:
itor1- iterator for first elements of pairsitor2- iterator for second elements of pairscomparator- determines whether two elements are equal and should be paired together
-
-
Method Details
-
setnext1
@RequiresNonNull("itor1") private void setnext1(@GuardSatisfied @UnknownInitialization OrderedPairIterator<T extends @Nullable Object> this) Set the next1 variable. -
setnext2
@RequiresNonNull("itor2") private void setnext2(@GuardSatisfied @UnknownInitialization OrderedPairIterator<T extends @Nullable Object> this) Set the next2 variable. -
hasNext
-
return1
private MPair<@Nullable T,@Nullable T> return1(@GuardSatisfied OrderedPairIterator<T extends @Nullable Object> this) Returns an element of the first iterator, paired with null.- Returns:
- an element of the first iterator, paired with null
-
return2
private MPair<@Nullable T,@Nullable T> return2(@GuardSatisfied OrderedPairIterator<T extends @Nullable Object> this) Returns a pair of null and an element of the second iterator.- Returns:
- a pair of null and an element of the second iterator
-
returnboth
private MPair<@Nullable T,@Nullable T> returnboth(@GuardSatisfied OrderedPairIterator<T extends @Nullable Object> this) Returns a pair containing an element from each iterator.- Returns:
- a pair containing an element from each iterator
-
next
-
remove
-