Package org.pipecraft.pipes.sync.inter
Class MultiSortedBasePipe<T>
- java.lang.Object
-
- org.pipecraft.pipes.sync.inter.MultiSortedBasePipe<T>
-
- Type Parameters:
T- The data type of the items this pipe works with
- All Implemented Interfaces:
Closeable,AutoCloseable,BasePipe,Pipe<T>
- Direct Known Subclasses:
SortedIntersectionPipe,SortedSubtractionPipe,SortedUnionPipe
public abstract class MultiSortedBasePipe<T> extends Object implements Pipe<T>
A base class for pipes performing some set operation on a collection of sorted input pipes. The input pipes must have the same item data type, and must all be ordered by the same order relation. The order relation (comparator) must be consistent with equality (java's Object.equals): a equals b if and only if compare(a,b) == 0. Input pipes may have item repetitions, but they are always treated as sets, where repetitions make no difference. The output of the pipe is always sorted and includes no duplicates.- Author:
- Eyal Schneider
-
-
Constructor Summary
Constructors Constructor Description MultiSortedBasePipe(Comparator<? super T> comparator, Pipe<T>... inputPipes)ConstructorMultiSortedBasePipe(List<? extends Pipe<T>> inputPipes, Comparator<? super T> comparator)Constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract booleancanTerminate(BitSet activePipes)Called whenever one of the input pipes terminated.voidclose()protected List<? extends Pipe<T>>getInputPipes()Tnext()Tpeek()protected abstract booleanshouldOutput(T item, BitSet pipesSeen)The method to override for determining whether a visited value should be part of the output or notvoidstart()Performs pre-processing prior to item flow throw the pipe.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.pipecraft.pipes.BasePipe
getProgress
-
-
-
-
Constructor Detail
-
MultiSortedBasePipe
public MultiSortedBasePipe(List<? extends Pipe<T>> inputPipes, Comparator<? super T> comparator)
Constructor- Parameters:
inputPipes- The input pipes. Each of them must be sorted by the order defined in the provided comparator, and items may have duplicates. The order in which the pipes collection is given is used in the shouldOutput(..) method.comparator- The total order relation to use when merging the data from the different input pipes. Should be consistent with the ordering of the input pipes. IMPORTANT NOTE: comparator must be consistent with equals: a.equals(b) if and only if comparator.compare(a,b)==0.
-
MultiSortedBasePipe
@SafeVarargs public MultiSortedBasePipe(Comparator<? super T> comparator, Pipe<T>... inputPipes)
Constructor- Parameters:
comparator- The total order relation to use when merging the data from the different input pipes. Should be consistent with the ordering of the input pipes. IMPORTANT NOTE: comparator must be consistent with equals: a.equals(b) if and only if comparator.compare(a,b)==0.inputPipes- The input pipes. Each of them must be sorted by the order defined in the provided comparator, and items may have duplicates. The order in which the pipes collection is given is used in the shouldOutput(..) method.
-
-
Method Detail
-
start
public void start() throws PipeException, InterruptedExceptionDescription copied from interface:BasePipePerforms pre-processing prior to item flow throw the pipe. Implementations must call the same method for all their input pipes before accessing their items. This is typically done here.- Specified by:
startin interfaceBasePipe- Throws:
PipeException- In case of pipe errors in this pipe or somewhere up-stream.InterruptedException- In case that the operation has been interrupted by another thread.
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
next
public T next() throws PipeException, InterruptedException
- Specified by:
nextin interfacePipe<T>- Returns:
- The next item in this pipe output, or null if the output end has been reached. May be a blocking operation.
- Throws:
PipeException- In case of pipe errors in this pipe or somewhere up-stream while trying to prepare next item to return.InterruptedException- In case that the operation has been interrupted by another thread.
-
peek
public T peek()
-
shouldOutput
protected abstract boolean shouldOutput(T item, BitSet pipesSeen)
The method to override for determining whether a visited value should be part of the output or not- Parameters:
item- The itempipesSeen- A bitset specifying the different input pipes in which the item is found. Index #i corresponds to pipe #i in the pipes list provided in the ctor. Note that it is guaranteed that at least one bit is turned on.- Returns:
- true for outputting this item, false for skipping
-
canTerminate
protected abstract boolean canTerminate(BitSet activePipes)
Called whenever one of the input pipes terminated. Allows implementations to decide whether to exit early if this there's no point in continuing with the remaining active pipes.- Parameters:
activePipes- A bitset describing the currently active pipes. Index #i corresponds to pipe #i in the pipes list provided in the ctor.- Returns:
- true for terminating the output, false for continuing.
-
-