Class Data.Sink<T>

java.lang.Object
org.praxislive.code.userapi.Data.Sink<T>
Type Parameters:
T - type of data
All Implemented Interfaces:
Lookup.Provider
Enclosing class:
Data

public abstract static class Data.Sink<T> extends Object implements Lookup.Provider
Data sink to drive pipe graph. Use @Inject Sink<TYPE> sink; to create a sink. By default the pass the same instance of T through the pipe graph. To create new type T, accumulate values, validate values, etc. provide the related functions. Use input() to get a Data.Pipe to link to the sink. Use process() every time you want to process a graph of T.
  • Constructor Details

    • Sink

      public Sink()
  • Method Details

    • attach

      protected void attach(CodeContext<?> context)
    • reset

      public void reset()
      Reset all functions and disconnect all sources.
    • input

      public Data.Pipe<T> input()
      Get the input pipe for this sink. The input pipe only supports the addition of sources - it cannot be used as a source.
      Returns:
      input pipe
    • process

      public T process(T data)
      Process an instance of type T through the data graph. The data returned may not be the same as the data provided, depending on how you have configured the sink, whether you use Data.supply() / Data.apply(), etc.
      Parameters:
      data - instance of T to process
      Returns:
      data of type T (may or may not be the input data)
    • onCreate

      public Data.Sink<T> onCreate(UnaryOperator<T> creator)
      Function to get an instance of T when a new data packet is being created. This function is not required to return a new instance. The default onCreate function returns the provided value.
      Parameters:
      creator - function to get an instance of T
      Returns:
      this sink for chaining
    • onClear

      public Data.Sink<T> onClear(UnaryOperator<T> clearer)
      Function to clear an instance of T when required, at the head of a pipe chain, prior to accumulation, etc. This might eg. zero out an array or empty a list. The default onClear function does nothing.
      Parameters:
      clearer - function to clear an instance of T
      Returns:
      this sink for chaining
    • onAccumulate

      public Data.Sink<T> onAccumulate(BinaryOperator<T> accumulator)
      Function to accumulate instances of T. The first argument is the existing destination instance, the second is the source instance. The function may modify and return the existing destination, or return a different instance of T. If a different instance of T is returned, the existing destination will be disposed of.

      The default implementation returns the source instance of T - (dst, src) -> src.

      Parameters:
      accumulator - function to accumulate instances of T
      Returns:
      this sink for chaining
    • onValidate

      public Data.Sink<T> onValidate(BiPredicate<T,T> validator)
      Function to validate a source Data.Packet value against a destination Data.Packet value. The first argument is the destination, the second the existing source value. If this function returns false then the onCreate function will be called to create a new value for the source Data.Packet

      Packets from different sinks are always treated as invalid.

      The default function always returns true.

      Parameters:
      validator - function to validate source T against destination T
      Returns:
      this sink for chaining
    • onDispose

      public Data.Sink<T> onDispose(Consumer<T> disposer)
      Function to dispose of an instance of T.

      The default function will call close() if T is an instance of AutoCloseable. Otherwise it is a no-op.

      Parameters:
      disposer - function to dispose of instance of T
      Returns:
      this sink for chaining