Class StreamListProcessor

java.lang.Object
com.singularsys.extensions.stream.StreamListProcessor
All Implemented Interfaces:
ListProcessor, JepComponent, Serializable
Direct Known Subclasses:
StreamMatrixListProcessor

public class StreamListProcessor extends Object implements ListProcessor
A ListProcessor implementation that can process lists, vectors and streams. It is used by the stream functions to convert their arguments to streams.
See Also:
  • Constructor Details

    • StreamListProcessor

      public StreamListProcessor()
  • Method Details

    • init

      public void init(Jep jep)
      Description copied from interface: JepComponent
      Initialize the component. This method is called whenever a component is added to Jep. Hence, it allows components to keep track of the other components they may rely on.
      Specified by:
      init in interface JepComponent
      Parameters:
      jep - the current Jep instance
    • getLightWeightInstance

      public JepComponent getLightWeightInstance()
      Description copied from interface: JepComponent
      Gets a light-weight instance suitable for using in multiple threads.
      Specified by:
      getLightWeightInstance in interface JepComponent
      Returns:
      either a new instance, null or 'this'.
    • fromArgs

      public Iterable<Object> fromArgs(Object arg) throws EvaluationException
      Description copied from interface: ListProcessor
      Returns an iterable view of the input arguments.
      Specified by:
      fromArgs in interface ListProcessor
      Parameters:
      arg - typically a list, but can be any type. If the type is not a list, an iterator with a single element is returned.
      Returns:
      an Iterable view of the argument
      Throws:
      EvaluationException - on error
    • argAsStream

      public Stream<Object> argAsStream(Object arg) throws EvaluationException
      Converts the argument to a stream if possible. If the argument is already a stream it is returned as is, if it is a list or a VectorI it is converted to a stream. Otherwise an stream consisting of a single element is created.
      Throws:
      EvaluationException
    • argToList

      public Object argToList(Object arg) throws EvaluationException
      Converts the argument to a list. If the argument is a List, Stream or a VectorI it is converted to a list individual elements of these sequence are recursively converted to lists as well. If the argument is not a list, stream or VectorI a list consisting of a single element is created.
      Parameters:
      arg - the argument to convert
      Returns:
      a list
      Throws:
      EvaluationException
    • argToListOrElement

      public Object argToListOrElement(Object arg) throws EvaluationException
      Converts the argument to a list or single value. If the argument is a List, Stream or a VectorI it is converted to a list individual elements of these sequence are recursively converted to lists as well. If the argument is not a list, stream or VectorI it is returned as is.
      Parameters:
      arg - the argument to convert
      Returns:
      a list or single value
      Throws:
      EvaluationException
    • makeList

      public List<Object> makeList()
      Description copied from interface: ListProcessor
      Get an empty list which elements can be added.
      Specified by:
      makeList in interface ListProcessor
      Returns:
      an empty list
    • fromList

      public Object fromList(List<Object> list) throws EvaluationException
      Description copied from interface: ListProcessor
      Convert a list produced by ListProcessor.makeList() to the type used by Jep environment. Some implementations just return the input.
      Specified by:
      fromList in interface ListProcessor
      Parameters:
      list - a list
      Returns:
      a type suitable for use by Jep.
      Throws:
      EvaluationException - on error
    • fromCollection

      public Object fromCollection(Collection<Object> tree) throws EvaluationException
      Description copied from interface: ListProcessor
      Convert a general collection into a type used the Jep environment.
      Specified by:
      fromCollection in interface ListProcessor
      Parameters:
      tree - input collection
      Returns:
      a type suitable for use by Jep
      Throws:
      EvaluationException - on error
    • cloneStream

      public Stream<Object> cloneStream(Stream<Object> stream)
      Clone a stream. A reference to the original stream is kept in a Map this is used by subsequent calls cloneStream to so the all clones receive the same data. During the evaluation a a list of the elements from the original stream that have been accessed is kept, which may grow indefinitely. The clearClones() method should be called after the clones are no longer needed to prevent memory leaks.
      Parameters:
      stream - the stream to clone
      Returns:
      a clone of the stream
    • clearClones

      public void clearClones()
      Clear the clones map created by cloneStream(Stream). This should be called after the clones are no longer needed to prevent memory leaks.
    • car

      public Optional<Object> car(Stream<Object> stream)
      Get the first element of a stream. This method can be called multiple times with the same stream, it will always return the same first element.
      Parameters:
      stream - input stream
      Returns:
    • cdr

      public Stream<Object> cdr(Stream<Object> stream)
      Get a stream with the first element removed. This method should not can be called multiple times.
      Parameters:
      stream - input stream
      Returns:
      a stream with the first elements removed