Package org.pipecraft.pipes.sync.inter
Class AsyncToSyncPipe<T>
- java.lang.Object
-
- org.pipecraft.pipes.sync.inter.AsyncToSyncPipe<T>
-
- All Implemented Interfaces:
Closeable,AutoCloseable,BasePipe,Pipe<T>
public class AsyncToSyncPipe<T> extends Object implements Pipe<T>
A pipe which acts as a converter from async pipe/s to a sync pipe. Uses a blocking queue for collecting items and supplying them to the downstream synchronous consumer.- Author:
- Eyal Schneider
-
-
Constructor Summary
Constructors Constructor Description AsyncToSyncPipe(AsyncPipe<T> inputPipe, int queueCapacity, Supplier<T> markerFactory)Constructor Uses a LinkedBlockingQueue with the given capacityAsyncToSyncPipe(AsyncPipe<T> inputPipe, BlockingQueue<T> queue, Supplier<T> markerFactory)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()floatgetProgress()Tnext()Tpeek()voidstart()Performs pre-processing prior to item flow throw the pipe.
-
-
-
Constructor Detail
-
AsyncToSyncPipe
public AsyncToSyncPipe(AsyncPipe<T> inputPipe, BlockingQueue<T> queue, Supplier<T> markerFactory)
Constructor- Parameters:
inputPipe- The single input async pipequeue- The blocking queue to use for storing the items produced by the input pipes and supplying them. May be bounded/unbounded. Use unbounded queues with caution.markerFactory- A generator of special instances of the item data type, for internal uses. Should generate new instances, and the instances should not be used by the caller.
-
AsyncToSyncPipe
public AsyncToSyncPipe(AsyncPipe<T> inputPipe, int queueCapacity, Supplier<T> markerFactory)
Constructor Uses a LinkedBlockingQueue with the given capacity- Parameters:
inputPipe- The single input async pipequeueCapacity- The queue capacity. When reached, the input pipe's threads are blocked.markerFactory- A generator of special instances of the item data type, for internal uses. Should generate new instances, and the instances should not be used by the caller.
-
-
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.
-
getProgress
public float getProgress()
- Specified by:
getProgressin interfaceBasePipe- Returns:
- The pipe flow progress, as a floating number between 0.0 and 1.0. Important implementation rules: 1) Calling this method before start() call is complete isn't allowed and has an undefined behavior. 2) Implementation should do best effort to provide an estimate of the progress this pipe has made (0.0 - 1.0) 3) When the pipe is fully consumed, getProgress() should return 1.0. 4) Results must be monotonous, i.e. results of consecutive calls may never be decreasing. 5) Thread safety: progress may be maintained by some thread/s but monitoring by other threads. Implementations must be thread safe.
-
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() throws PipeException
- Specified by:
peekin interfacePipe<T>- Returns:
- The next item in the pipe's output. Does not remove it, so next call to next() will return it.
- Throws:
PipeException- In case of pipe errors in this pipe or somewhere up-stream while trying to prepare next item to return.
-
-