Package org.pipecraft.pipes.async.inter
Class AsyncProgressPipe<T>
- java.lang.Object
-
- org.pipecraft.pipes.async.AsyncPipe<T>
-
- org.pipecraft.pipes.async.inter.AsyncProgressPipe<T>
-
- All Implemented Interfaces:
Closeable,AutoCloseable,BasePipe
public class AsyncProgressPipe<T> extends AsyncPipe<T>
A no-op async pipe that reports progress. Similar toProgressPipe, but for async pipes. Due to the fact that in the async version is more complex to track, locking is used to guarantee monotonous progress reporting. This means that it is recommended not to use small sample rates, and the progress reporting callback should be as light as possible.- Author:
- Eyal Schneider
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.pipecraft.pipes.async.AsyncPipe
AsyncPipe.Status
-
-
Constructor Summary
Constructors Constructor Description AsyncProgressPipe(AsyncPipe<T> inputPipe, int sampleRate, Consumer<Integer> callback)Constructor
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()floatgetProgress()voidstart()Performs pre-processing prior to item flow throw the pipe.-
Methods inherited from class org.pipecraft.pipes.async.AsyncPipe
getStatus, notifyDone, notifyError, notifyNext, setListener
-
-
-
-
Constructor Detail
-
AsyncProgressPipe
public AsyncProgressPipe(AsyncPipe<T> inputPipe, int sampleRate, Consumer<Integer> callback)
Constructor- Parameters:
inputPipe- The upstream pipesampleRate- For reducing computation overhead, The progress is being checked only every sampleRate items (approximately).callback- The callback to be called with the new progress percentage when an increase is noticed. The callback will be called only for unique integer percentage values, so it will be called at most 101 times (0-100). The callback is invoked under a lock, so it doesn't have to use additional locking, unless it changes to state, and some other caller thread is reading that state. It's guaranteed that: 1) The callback is first invoked with value 0 2) The callback is invoked in strictly ascending progress order. 3) Upon successful termination, the callback is invoked with value 100
-
-
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.- 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()
- 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- Overrides:
closein classAsyncPipe<T>- Throws:
IOException
-
-