Klasse ByteAsyncBuilder

java.lang.Object
speiger.src.collections.bytes.utils.ByteAsyncBuilder

public class ByteAsyncBuilder extends Object
The Async API allows you to process collections on a different thread without having to deal with the Multithreading complexity.
It uses the Lightweight Stream Replace API to do its work, which can make it faster then sequential streams.
This feature isn't designed to do Multithreading work, but to allow moving work off the main thread into a worker thread.
So anything executed on this is still Singlethreaded.

How it works is you create the AsyncBuilder using a Iterable of the Type.
Then select things you want to use on the Iterable.
- For example: map/filter/peek/limit/etc

After that you select your action the Iterable should be applied on.
- For Example: forEach/findFirst/matchAny/etc

Then optionally a custom Executor or callback can be applied
At the end either execute or join can be called to start the task.
- execute will return the Task reference so that the task can be traced back.
- join will await the completion of the task and return the return value

During Construction a couple Disposable Builder Objects will be created.
These will be only used during construction.

The Task Object is also pause-able/Interruptable at any moment during the Processing.
A small example

 public void processFiles(ObjectCollection<String> potentialFiles) {
        potentialFiles.asAsync()
                .map(Paths::get).filter(Files::exists) //Modifies the collection (Optional)
                .forEach(Files::delete) //Creates the action (Required)
                .onCompletion(T -> {}} //Callback on completion (Optional)
                .execute() //Starts the task. (Required)
 }
 
  • Konstruktordetails

    • ByteAsyncBuilder

      public ByteAsyncBuilder(ByteIterable iterable)
      Main Constructor that uses a Iterable to build a Offthread Task.
      Parameter:
      iterable - that should be processed
    • ByteAsyncBuilder

      public ByteAsyncBuilder(ByteAsyncBuilder.BaseByteTask task)
      Helper constructor.
      Parameter:
      task - that had been build
  • Methodendetails

    • of

      public static ByteAsyncBuilder of(Iterable<Byte> iterable)
      Helper function that automatically wraps a Iterable into a AsyncBuilder since it forces this collections Iterable.
      Parameter:
      iterable - that should be wrapped
      Gibt zurück:
      a AsyncBuilder with the iterable wrapped
    • of

      public static ByteAsyncBuilder of(byte... values)
      Helper function that automatically wraps a array into a AsyncBuilder since it forces this collections Iterable.
      Parameter:
      values - that should be wrapped
      Gibt zurück:
      a AsyncBuilder with the values wrapped
    • map

      public <E> ObjectAsyncBuilder<E> map(ByteFunction<E> mapper)
      Maps the elements to something else
      Typparameter:
      E - The return type.
      Parameter:
      mapper - the mapping function
      Gibt zurück:
      a new Builder Object with the mapped Iterable
    • flatMap

      public <E, V extends Iterable<E>> ObjectAsyncBuilder<E> flatMap(ByteFunction<V> mapper)
      Maps the elements to something else
      Typparameter:
      E - The return type.
      V - The return type supplier.
      Parameter:
      mapper - the flatMapping function
      Gibt zurück:
      a new Builder Object with the mapped Iterable
    • arrayflatMap

      public <E> ObjectAsyncBuilder<E> arrayflatMap(ByteFunction<E[]> mapper)
      Maps the elements to something else
      Typparameter:
      E - The return type.
      Parameter:
      mapper - the flatMapping function
      Gibt zurück:
      a new Builder Object with the mapped Iterable
    • filter

      public ByteAsyncBuilder filter(BytePredicate filter)
      Filters out the unwanted elements out of the Iterable
      Parameter:
      filter - the elements that should be kept
      Gibt zurück:
      Self with a filter applied
    • distinct

      public ByteAsyncBuilder distinct()
      Removes duplicated elements out of the Iterable
      Gibt zurück:
      Self with a deduplicator applied
    • repeat

      public ByteAsyncBuilder repeat(int repeats)
      Repeats the elements inside of the Iterable
      Parameter:
      repeats - the amount of times the elements should be repeated
      Gibt zurück:
      self with a repeater applied
    • limit

      public ByteAsyncBuilder limit(long limit)
      Limits how many elements are inside of the Iterable
      Parameter:
      limit - how many elements should max be iterated through
      Gibt zurück:
      self with a limiter applied
    • sorted

      public ByteAsyncBuilder sorted(ByteComparator sorter)
      Sorts the elements inside of the Iterable. This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it, and this will affect the pausing feature.
      Parameter:
      sorter - that sorts the elements.
      Gibt zurück:
      self with a sorter applied
    • peek

      public ByteAsyncBuilder peek(ByteConsumer action)
      Allows to preview elements before they are processed
      Parameter:
      action - the action that should be applied
      Gibt zurück:
      self with a preview applied
    • forEach

      public ObjectAsyncBuilder<Void> forEach(ByteConsumer action)
      Iterates over the Iterable with a desired action
      Parameter:
      action - that should be applied
      Gibt zurück:
      a new Builder with the forEach action applied.
    • reduce

      public ByteAsyncBuilder reduce(ByteByteUnaryOperator operator)
      Reduces the elements inside of the Iterable down to one element
      Parameter:
      operator - that reduces the elements.
      Gibt zurück:
      self with the reduce action applied
    • reduce

      public ByteAsyncBuilder reduce(byte identity, ByteByteUnaryOperator operator)
      Reduces the elements inside of the Iterable down to one element using a identity element.
      Parameter:
      identity - the element the reduce function should start with
      operator - that reduces the elements.
      Gibt zurück:
      a new Builder with the reduce function applied
    • toByteArray

      public ObjectAsyncBuilder<byte[]> toByteArray()
      Pours all elements of the Iterable down into a Array.
      Gibt zurück:
      a new Builder with the ToArray function applied
    • pourAsList

      public ObjectAsyncBuilder<ByteList> pourAsList()
      Pours all elements into a List that can be later
      Gibt zurück:
      a new Builder with the pour function applied
    • pourAsSet

      public ObjectAsyncBuilder<ByteSet> pourAsSet()
      Pours all elements into a Set that can be later
      Gibt zurück:
      a new Builder with the pour function applied
    • pour

      public <E extends ByteCollection> ObjectAsyncBuilder<E> pour(E collection)
      Pours all elements into a collection that can be later
      Typparameter:
      E - the return type
      Parameter:
      collection - the collection the elements
      Gibt zurück:
      a new Builder with the pour function applied
    • matchAny

      public BooleanAsyncBuilder matchAny(BytePredicate filter)
      Searches through the elements of the Iterable to find if the desired element is present.
      Parameter:
      filter - that decides the desired elements
      Gibt zurück:
      a new Builder with the matchAny function applied
    • matchNone

      public BooleanAsyncBuilder matchNone(BytePredicate filter)
      Searches through the elements of the Iterable to find if unwanted elements are present.
      Parameter:
      filter - that decides the unwanted elements
      Gibt zurück:
      a new Builder with the matchNone function applied
    • matchAll

      public BooleanAsyncBuilder matchAll(BytePredicate filter)
      Searches through the elements of the Iterable to find if all the desired elements are present.
      Parameter:
      filter - that decides the desired elements
      Gibt zurück:
      a new Builder with the matchAll function applied
    • findFirst

      public ByteAsyncBuilder findFirst(BytePredicate filter)
      Searches through the elements of the Iterable to find if the desired element. If not present it will return the default value of the type
      Parameter:
      filter - that decides the desired elements
      Gibt zurück:
      self with the findFirst function applied
    • count

      public IntAsyncBuilder count(BytePredicate filter)
      Counts all desired elements inside the Iterable
      Parameter:
      filter - that decides the desired elements
      Gibt zurück:
      a new Builder with the count function applied
    • executor

      public ByteAsyncBuilder executor(Executor executor)
      Optional way to add a custom executor that runs this offthread task. Can only be set after the action was decided on.
      Parameter:
      executor - that executes the task, defaults to SanityChecks.invokeAsyncTask(Runnable)
      Gibt zurück:
      self with the executor set
      Note:
      has to be NonNull
    • onCompletion

      public ByteAsyncBuilder onCompletion(Consumer<ByteTask> callback)
      Optional way to set a callback that allows to compute actions after the task was completed. The state of the task has to be validated by the callback.
      Parameter:
      callback - that should be notified after completion of the task
      Gibt zurück:
      self with the callback set
      Note:
      can be null
    • execute

      public ByteTask execute()
      Starts the Execution of the task without awaiting its result
      Gibt zurück:
      the task object that allow to trace it.
    • join

      public byte join() throws ExecutionException, InterruptedException
      Starts the Execution of the task and will await its completion, returning the result.
      Gibt zurück:
      the result of the task provided.
      Löst aus:
      ExecutionException - if the task threw a exception
      InterruptedException - if the caller thread was interrupted
    • join

      public byte join(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Starts the Execution of the task and will await its completion with a timeout, returning the result.
      Parameter:
      timeout - of how long the thread should wait the task to be completed
      unit - of the desired waiting time.
      Gibt zurück:
      the result of the provided task
      Löst aus:
      InterruptedException - if the caller thread was interrupted
      ExecutionException - if the task threw a exception
      TimeoutException - if the timeout was reached before the task was finished