net.sf.staccatocommons.collections.stream
Class AbstractStream<A>

java.lang.Object
  extended by net.sf.staccatocommons.lang.AbstractProtoMonad<Stream<A>,Stream,A>
      extended by net.sf.staccatocommons.collections.stream.AbstractBasicStream<A>
          extended by net.sf.staccatocommons.collections.stream.AbstractStream<A>
Type Parameters:
A -
All Implemented Interfaces:
Iterable<A>, Stream<A>, ContainsAware<A>, EmptyAware, SizeAware, ProtoMonad<Stream<A>,Stream,A>

public abstract class AbstractStream<A>
extends AbstractBasicStream<A>

An abstract implementation of a Stream. Only it Iterator method is abstract. Implementors must grant that the following methods remain consistent:

Author:
flbulgarelli

Nested Class Summary
 
Nested classes/interfaces inherited from interface net.sf.staccatocommons.collections.stream.Stream
Stream.DeconsApplicable<A,B>, Stream.DelayedDeconsApplicable<A,B>, Stream.EmptyApplicable<A>
 
Constructor Summary
AbstractStream()
           
 
Method Summary
 boolean all(Evaluable<? super A> predicate)
          Tests if all elements satisfy the given Evaluable
 boolean allEquiv()
          Tests if all elements are equal
 boolean allEquivBy(Evaluable2<? super A,? super A> equivTest)
          Tests if all elements are equivalent, using the given equivTest
 boolean any(Evaluable<? super A> predicate)
          Tests if at least one element satisfies the given Evaluable
 A anyOrElse(A value)
          Shorthand for anyOrNone().valueOrElse(value)
 A anyOrElse(Thunk<A> thunk)
          Shorthand for anyOrNone().valueOrElse(thunk)
 Option<A> anyOrNone()
          Returns the Stream.head() of the given Stream, just like Stream.any(), but as an option.
 A anyOrNull()
          Shorthand for anyOrNone().valueOrNull()
 Stream<A> append(A element)
          Adds an element as the last one of the stream.
 Stream<A> appendUndefined()
          Concatenates this Stream with the undefined Stream.
 A average(NumberType<A> numberType)
          Answers the average of the stream elements, using the given NumberType for performing addition and division.
<B,C> Stream<Tuple2<B,C>>
branch(Applicable<? super A,? extends B> function0, Applicable<? super A,? extends C> function1)
          Answers a Stream of pairs, where each one contains both results of applying the given functions.
<B> Stream<Tuple2<A,B>>
clone(Applicable<? super A,? extends B> function)
          Answers a stream that retrieves a tuple per each element, formed by the original element as the first component, and the result of applying the given function to it as the second component.
 Stream<A> concat(A... elements)
          Concatenates this with the given elements It answers an Stream that retrieves elements from this Stream, and then, after its last element, the given elements.
 Stream<A> concat(Iterable<? extends A> other)
          Concatenates this with other It answers an Stream that retrieves elements from this Stream, and then, after its last element, from the given iterable.
 Stream<A> concat(Iterator<? extends A> other)
          Concatenates this with the given Iterator It answers an Stream that retrieves elements from this Stream, and then, after its last element, the given iterator's elements.
 boolean contains(A element)
           
 boolean containsBefore(A element, A reference)
          Answers if element is contained by this stream, and its first occurence is before the first occurence of the second one, if any.
 boolean containsBeforeIndex(A element, int index)
          Answers if element is contained by this stream, and its first occurence is before the given index.
 int countOf(Evaluable<? super A> predicate)
          Answers the number of element that satisfy the given predicate
 Stream<Tuple2<A,A>> cross()
          Answers the Cartesian product of this stream and itself
<B> Stream<Tuple2<A,B>>
cross(B... elements)
          Answers the cartesian product of this Stream and the given elements.
<B> Stream<Tuple2<A,B>>
cross(Iterable<B> other)
          Answers the cartesian product of this Stream and the given Iterable
<B> Stream<Tuple2<A,B>>
cross(Iterator<B> other)
          Answers the cartesian product of this Stream and the given Iterator
<B> Stream<Tuple2<A,B>>
cross(Stream<B> other)
          Answers the Cartesian product of this stream and the given one
 Stream<Stream<A>> crossStreams(Stream<Stream<A>> other)
          Answers the cartesian product of this Stream and each one of the given streamOfStreams
 Tuple2<A,Stream<A>> decons()
          Answers this stream split into head and tail.
 Stream<A> delayedAppend(Thunk<A> thunk)
          Adds an element's thunk as the last one of the stream.
 Tuple2<Thunk<A>,Stream<A>> delayedDecons()
          Answers this non-empty stream split into a head thunk and tail.
 Stream<A> delayedPrepend(Thunk<A> thunk)
          Adds an element's thunk as the first one of the stream.
 Stream<A> drop(int amountOfElements)
          Discards up to N elements from this Stream.
 Stream<A> dropWhile(Evaluable<? super A> predicate)
          Discards all elements while they satisfy the given predicate
 boolean equiv(Iterable<? extends A> other)
          Test that the elements of this stream are equal to the elements of the given Iterable, and in the same order.
 boolean equivBy(Evaluable2<? super A,? super A> equalty, Iterable<? extends A> other)
          Test that the elements of this stream are equivalent to the elements of the given Iterable, and in the same order, using the given equalityTest for determining equivalence between elements.
 Stream<A> filter(Evaluable<? super A> predicate)
          Preserves elements that satisfy the given predicate

Example: //Answers stream [hello, world], which has only strings whose length is 5 Streams.cons("a", "hello", "world", "of", "streams", "!").

 Stream<A> filterIndex(Evaluable<Integer> predicate)
          Preserves elements that whose index satisfy the given predicate
 A find(Evaluable<? super A> predicate)
          Looks for a element that satisfies the given Evaluable.
 int findIndex(Evaluable<? super A> predicate)
          Answers the zero-based index of first element that matches the given predicate
 A findOrElse(Evaluable<? super A> predicate, A element)
          Looks for an element that satisfies the given Evaluable.
 A findOrElse(Evaluable<? super A> predicate, Thunk<? extends A> thunk)
          Looks for an element that satisfies the given Evaluable.
 Option<A> findOrNone(Evaluable<? super A> predicate)
          Looks for an element that satisfies the given Evaluable.
 A findOrNull(Evaluable<? super A> predicate)
          Looks for an element that satisfies the given Evaluable.
<B> Stream<B>
flatMap(Function<? super A,? extends Iterable<? extends B>> function)
          Transformes each element into an iterable using the given function, and concatenates (flattens) the result
<B> Stream<B>
flatMapArray(Function<? super A,? extends B[]> function)
          Transformes each element into an array using the given function, and concatenates (flattens) the result
<O> O
fold(O initial, Applicable2<? super O,? super A,? extends O> function)
          (Left)folds this Stream using an initial value and the given two-arg function, producing a single aggregated result from all the Stream elements.
 Stream<A> force()
          Forces stream elements evaluation by converting it into a new ordered stream one that is not lazy and that has repeatable iteration order.
 void forEach(Executable<? super A> block)
          Executes the given Executable block for each element.
 A get(int n)
          Answers the n-th element.
<K,V> Map<K,V>
groupOn(Applicable<? super A,K> groupFunction, Reduction<A,V> reduction)
          Groups elements by the given groupingFunction, and reduces each group using the given reduction.
 A head()
          Answers the head of the Stream, which is the first element of the stream.
 Stream<A> incorporate(A element)
          Inserts the given value after each element of the stream.
 Stream<A> incorporate(Function<? super A,? extends A> function)
          Inserts after each element the result of applying the given function to it.
 int indexOf(A element)
          Answers the zero-based index of the given element
 Stream<Integer> indices(Evaluable<? super A> predicate)
          Answers a stream containing all the zero-based indices of the elements that matches the given predicate
 Stream<A> insertBefore(A element, A reference)
          Inserts element before the first occurrence of reference.
 Stream<A> insertBeforeIndex(A element, int position)
          Inserts element at index - 1.
 Stream<A> intersperse(A sep)
           intersperse _ [] = [] intersperse _ [x] = [x] intersperse sep (x:xs) = x : sep : intersperse sep xs
 boolean isBefore(A previous, A next)
          Answers if both arguments are contained by this stream, and the first one is before the second one.
 boolean isEmpty()
           
 String joinStrings(String separator)
          (Left)folds this Stream concatenating each elements toString with a separator
 A last()
          Answers the last element of this stream.
<B> Stream<B>
map(Function<? super A,? extends B> function)
          Transforms each element using the given function.
 A maximum()
          Answers the maximum element of the stream, using elements natural order.
 A maximumBy(Comparator<? super A> comparator)
          Answers the maximum element of the stream, using the given comparator to compare elements.
 Stream<A> memoize()
          Memoizes stream elements and their order, by answering a lazy stream with Repeatable iteration order that caches elements evaluated during iteration.
 A minimum()
          Answers the minimum element of the stream, using elements natural order.
 A minimumBy(Comparator<? super A> comparator)
          Answers the min element of the stream, using the given comparator to compare elements.
 Tuple2<List<A>,List<A>> partition(Evaluable<? super A> predicate)
          Splits stream elements into two lists using a predicate - elements that evaluate to true will be returned in the first component, the rest will be returned in the second component
 Stream<A> prepend(A element)
          Adds an element as the first one of the stream.
 void print()
          Prints the stream elements to System.out
 void print(Appendable o)
          Prints the stream elements to an appendable, like StringBuilder or a Writer
 void println(Appendable o)
          Prints the stream elements to an appendable, like StringBuilder or a Writer, followed by a newline character
 String printString()
          Prints the stream elements to a string
 A product(NumberType<A> numberType)
          Answers the product of the elements of this Stream using the given NumberType
 A reduce(Applicable2<? super A,? super A,? extends A> function)
          (Left)folds the tail of this Stream using the first element of the stream as initial value, producing a single aggregated result from all the Stream elements.
<B> B
reduce(Reduction<? super A,B> reduction)
          Answers the result of aggregating this stream using the given reduction
 Stream<A> reverse()
          Reverses this Stream, by returning a new Stream that retrieves elements in the inverse order of this Stream.
 int size()
           
 Stream<A> skipIndex(int index)
          Answers a streams that retrieves all the elements of this one, except of that at the given index
 Stream<A> slice(int lowerBound, int upperBound)
          Answers a stream that is a substream of this one.
 Stream<A> sort()
          Sorts this Stream, using their element's natural ordering
 Stream<A> sortBy(Comparator<A> comparator)
          Sorts this Stream, using the given comparator
 Tuple2<Stream<A>,Stream<A>> streamPartition(Evaluable<? super A> predicate)
          Splits stream elements into two ordered streams, that support random access.
 A sum(NumberType<A> numberType)
          Answers the sum of the elements of this Stream using the given NumberType
 Stream<A> tail()
          Answers the tail of the Stream
 Stream<A> take(int amountOfElements)
          Preserves up to N elements.
 Stream<A> takeWhile(Evaluable<? super A> predicate)
          Preserves all elements while they satisfy the given predicate
 A[] toArray(Class<? super A> clazz)
          Creates a new array that has the same elements that the retrived by this Stream
protected  A[] toArray(Class<? super A> clazz, Collection<A> readOnlyColView)
           
 List<A> toList()
          * Converts this Stream into a List, by adding all its elements to a new List.
 Set<A> toSet()
          Converts this Stream into a Set, by adding all its elements to a new Set.
 String toString()
           
<B> Stream<B>
transform(Applicable<Stream<A>,? extends Stream<B>> function)
          Lazily applies the given function to this Stream.
<B> Stream<B>
transform(Stream.DeconsApplicable<A,B> function)
          Lazily applies the given function to this stream, deconstructing this stream into head and tail, or into an empty stream.
<B> Stream<B>
transform(Stream.DelayedDeconsApplicable<A,B> function)
          Lazily applies the given function to this stream, deconstructing this stream into a head thunk and tail, or into an empty stream.
<B,C> Stream<C>
zipWith(Function2<? super A,? super B,C> function, B... elements)
          Returns a Stream formed by the result of applying the given function to each pair of elements from this and the given iterable.
<B,C> Stream<C>
zipWith(Function2<? super A,? super B,C> function, Iterable<B> other)
          Returns a Stream formed by the result of applying the given function to each pair of elements from this and the given iterable.
<B,C> Stream<C>
zipWith(Function2<? super A,? super B,C> function, Iterator<B> other)
          Returns a Stream formed by the result of applying the given function to each pair of elements from this and the other.
 
Methods inherited from class net.sf.staccatocommons.collections.stream.AbstractBasicStream
any, append, append, equiv, equiv, equivBy, equivBy, equivOn, equivOn, equivOn, findPosition, first, map, maximumOn, memorize, minimumOn, positionOf, prepend, println, second, sortOn, third, zip, zip, zip, zip
 
Methods inherited from class net.sf.staccatocommons.lang.AbstractProtoMonad
each, replace, replaceNull, skip, skipNull
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.staccatocommons.collections.stream.Stream
each, iterator, skip
 
Methods inherited from interface net.sf.staccatocommons.defs.ProtoMonad
replace, replaceNull, skipNull
 

Constructor Detail

AbstractStream

public AbstractStream()
Method Detail

size

public int size()

countOf

public int countOf(Evaluable<? super A> predicate)
Description copied from interface: Stream
Answers the number of element that satisfy the given predicate

Returns:
filter(predicate).size()

isEmpty

public boolean isEmpty()

forEach

public void forEach(Executable<? super A> block)
Description copied from interface: Stream
Executes the given Executable block for each element. This message is equivalent to a for-each loop over this Stream


contains

public boolean contains(A element)

filter

public Stream<A> filter(Evaluable<? super A> predicate)
Description copied from interface: Stream
Preserves elements that satisfy the given predicate

Example:

  //Answers stream [hello, world], which has only strings whose length is 5   
  Streams.cons("a", "hello", "world", "of", "streams", "!").filter(length().equal(5));
 

Returns:
a new Stream projection that will retrieve only elements that evaluate to true

takeWhile

public Stream<A> takeWhile(Evaluable<? super A> predicate)
Description copied from interface: Stream
Preserves all elements while they satisfy the given predicate

Returns:
a new Stream projection that will retrieve all elements from this stream, as long as none of them evaluates to false.

take

public Stream<A> take(@NotNegative
                      int amountOfElements)
Description copied from interface: Stream
Preserves up to N elements. It this Stream size is shorter than the given amountOfElements, the resulting stream will retrieve the same elements than this stream.

Returns:
a new Stream projection that will retrieve up to N elements

dropWhile

public Stream<A> dropWhile(Evaluable<? super A> predicate)
Description copied from interface: Stream
Discards all elements while they satisfy the given predicate

Returns:
a new Stream projection that will skip all elements as long as they satisfy the given Evaluable

drop

public Stream<A> drop(@NotNegative
                      int amountOfElements)
Description copied from interface: Stream
Discards up to N elements from this Stream. Is the Stream size is shorter than the given amountOfElements, the resulting stream will be empty.

Parameters:
amountOfElements - the amount of elements to discard
Returns:
a new Stream that discards up to the given amountOfElements

slice

public Stream<A> slice(@NotNegative
                       int lowerBound,
                       @NotNegative
                       int upperBound)
Description copied from interface: Stream
Answers a stream that is a substream of this one. The substream begins at the specified beginIndex, and extends to the element at index endIndex - 1, if it has enough elements, or up to its last element, otherwise.

The resulting stream is always finite, and satisfies that size() <= endIndex - beginIndex

As a particular case, if beginIndex == endIndex, an empty stream is returned.

Examples:
  Streams.from("hamburger").slice(4, 8).joinStrings(""); //answers "urge"
  Streams.from("smiles").slice(1, 5).joinStrings(""); //answers "mile"
  Streams.from("hello world!").slice(6, 309).joinStrings(""); //answers "world!"
  Streams.enumerate(1,20).slice(5,5).toList(); //answers []  

Parameters:
lowerBound - the lower bound, inclusive
upperBound - the upper bound, exclusive
Returns:
drop(beginIndex).take(endIndex - beginIndex)

reduce

public A reduce(Applicable2<? super A,? super A,? extends A> function)
Description copied from interface: Stream
(Left)folds the tail of this Stream using the first element of the stream as initial value, producing a single aggregated result from all the Stream elements.

Returns:
the folding result
See Also:
Stream.fold(Object, Applicable2), Folds

reduce

public <B> B reduce(Reduction<? super A,B> reduction)
         throws NoSuchElementException
Description copied from interface: Stream
Answers the result of aggregating this stream using the given reduction

Returns:
the folding result
Throws:
NoSuchElementException

fold

public <O> O fold(O initial,
                  Applicable2<? super O,? super A,? extends O> function)
Description copied from interface: Stream
(Left)folds this Stream using an initial value and the given two-arg function, producing a single aggregated result from all the Stream elements. This consist of taking the initial value and a Stream element, applying the function to them, and then repeating the process with this result as the next initial value and the next element from the stream. The last returned value is the folding result.

Returns:
the aggregates result
See Also:
Folds

anyOrNone

public Option<A> anyOrNone()
Description copied from interface: Stream
Returns the Stream.head() of the given Stream, just like Stream.any(), but as an option. If Stream has no elements, instead of throwing a EmptySourceException, it returns None

Returns:
Option.some(element) if there is at least one element, or Option.none(), otherwise.

anyOrNull

public A anyOrNull()
Description copied from interface: Stream
Shorthand for anyOrNone().valueOrNull()

Returns:
anyOrNone().valueOrNull()

anyOrElse

public A anyOrElse(Thunk<A> thunk)
Description copied from interface: Stream
Shorthand for anyOrNone().valueOrElse(thunk)

Returns:
anyOrNone().valueOrElse(thunk)

anyOrElse

public A anyOrElse(A value)
Description copied from interface: Stream
Shorthand for anyOrNone().valueOrElse(value)

Returns:
anyOrNone().valueOrElse(value)

find

public A find(Evaluable<? super A> predicate)
Description copied from interface: Stream
Looks for a element that satisfies the given Evaluable. If such element does not exist, throws NoSuchElementException

Returns:
the first elements that the predicate satisfies, if exists.

findOrNone

public Option<A> findOrNone(Evaluable<? super A> predicate)
Description copied from interface: Stream
Looks for an element that satisfies the given Evaluable. If such element exists, returns some(element). Otherwise, returns None.

Returns:
None if no element matches the predicate, or some(element) if at least one exists. As a particular case, this method will return None if Stream is empty, regardless of the given predicate

findOrNull

public A findOrNull(Evaluable<? super A> predicate)
Description copied from interface: Stream
Looks for an element that satisfies the given Evaluable. If such element exists, returns it. Otherwise, returns null.

Returns:
null if no element matches the predicate, or an element that satisfies it, if at least one exists. As a particular case, this method will return null if Stream is empty, regardless of the given predicate

findOrElse

public A findOrElse(Evaluable<? super A> predicate,
                    Thunk<? extends A> thunk)
Description copied from interface: Stream
Looks for an element that satisfies the given Evaluable. If such element exists, returns it. Otherwise, returns the given thunk's value.

Returns:
thunk.value() if no element matches the predicate, or an element that satisfies it, if at least one exists. As a particular case, this method will return the thunk's value if Stream is empty, regardless of the given predicate

findOrElse

public A findOrElse(Evaluable<? super A> predicate,
                    A element)
Description copied from interface: Stream
Looks for an element that satisfies the given Evaluable. If such element exists, returns it. Otherwise, returns the given element .

Returns:
findOrElse(predicate, Thunks.constant(element))

all

public boolean all(Evaluable<? super A> predicate)
Description copied from interface: Stream
Tests if all elements satisfy the given Evaluable

Parameters:
predicate - an Evaluable to evaluate each element
Returns:
if all the elements evaluate to true

allEquiv

public boolean allEquiv()
Description copied from interface: Stream
Tests if all elements are equal

Returns:
if all the elements are equal

allEquivBy

public boolean allEquivBy(Evaluable2<? super A,? super A> equivTest)
Description copied from interface: Stream
Tests if all elements are equivalent, using the given equivTest

Parameters:
equivTest - an Evaluable2 used to testing if an element is equivalent to another
Returns:
if all the elements are equal

any

public boolean any(Evaluable<? super A> predicate)
Description copied from interface: Stream
Tests if at least one element satisfies the given Evaluable

Parameters:
predicate - an Evaluable to evaluate each element
Returns:
if any element evaluate to true

clone

public <B> Stream<Tuple2<A,B>> clone(Applicable<? super A,? extends B> function)
Description copied from interface: Stream
Answers a stream that retrieves a tuple per each element, formed by the original element as the first component, and the result of applying the given function to it as the second component.

This message is equivalent to map(Tuples.clone(function))

Parameters:
function - the function to apply to each element
Returns:
a new Stream
See Also:
Tuples.clone(Applicable)

branch

public <B,C> Stream<Tuple2<B,C>> branch(Applicable<? super A,? extends B> function0,
                                        Applicable<? super A,? extends C> function1)
Description copied from interface: Stream
Answers a Stream of pairs, where each one contains both results of applying the given functions. Equivalent to this.map(Tuples.branch(function0, function1))

Returns:
a new Stream
See Also:
Tuples.branch(Applicable, Applicable)

map

public <B> Stream<B> map(Function<? super A,? extends B> function)
Description copied from interface: Stream
Transforms each element using the given function.

Example:

 
  import static import net.sf.staccatocommons.util.Strings.*;
  //Answers [HELLO, WORLD, STREAMS]
  Streams.cons("hello", "world", "streams").map(toUpperCase());
 
 import static net.sf.staccatocommons.numbers.NumberTypes.*;
  ...
 //answers Stream [6, 9, 11, 26]
 Streams.cons(5, 8, 10, 25).map(add(1)).println(); 
 

Parameters:
function - the mapper used to transform each element, applying it
Returns:
a new Stream projection that will retrieve the result of applying the given function to each element

flatMap

public <B> Stream<B> flatMap(Function<? super A,? extends Iterable<? extends B>> function)
Description copied from interface: Stream
Transformes each element into an iterable using the given function, and concatenates (flattens) the result

Returns:
a new Stream that will retrieve the result of transforming each element and concatenating those transformations

flatMapArray

public <B> Stream<B> flatMapArray(@NonNull
                                  Function<? super A,? extends B[]> function)
Description copied from interface: Stream
Transformes each element into an array using the given function, and concatenates (flattens) the result

Returns:
a new Stream that will retrieve the result of transforming each element and concatenating those trsansformations

appendUndefined

public Stream<A> appendUndefined()
Description copied from interface: Stream
Concatenates this Stream with the undefined Stream. Equivalent to concat(Streams.undefined())

Returns:
a new Stream, Repeatable as long as this is repeatable
See Also:
Streams.undefined()

head

public A head()
Description copied from interface: Stream
Answers the head of the Stream, which is the first element of the stream. However, if you need both head and tail in order to perfom functional-style transformations over the stream, consider using Stream.decons().

Returns:
Stream.first()

last

public A last()
Description copied from interface: Stream
Answers the last element of this stream. This method only works with finite streams

Returns:
the last element

get

public A get(int n)
Description copied from interface: Stream
Answers the n-th element.

Returns:
the n-th element, zero based

filterIndex

public Stream<A> filterIndex(Evaluable<Integer> predicate)
Description copied from interface: Stream
Preserves elements that whose index satisfy the given predicate

Returns:
a new Stream projection that will retrieve only elements whose index evaluate to true

skipIndex

public Stream<A> skipIndex(int index)
Description copied from interface: Stream
Answers a streams that retrieves all the elements of this one, except of that at the given index

Returns:
a new Stream that skips the element at the given index

indexOf

public int indexOf(A element)
Description copied from interface: Stream
Answers the zero-based index of the given element

Returns:
the index of the element first element equal to element, or -1, if it is not contained by this stream

findIndex

public int findIndex(Evaluable<? super A> predicate)
Description copied from interface: Stream
Answers the zero-based index of first element that matches the given predicate

Returns:
the index of the first element that evaluates the predicate to true, or -1, if no element satisfies it

indices

public Stream<Integer> indices(Evaluable<? super A> predicate)
Description copied from interface: Stream
Answers a stream containing all the zero-based indices of the elements that matches the given predicate

Returns:
a Stream with the indices of the elements that satisfy the given predicate

isBefore

public boolean isBefore(A previous,
                        A next)
Description copied from interface: Stream
Answers if both arguments are contained by this stream, and the first one is before the second one. This method works even for stream that can be iterated only once

Returns:
whether both elements are contained by this Stream, and the first is before the second one.
See Also:
Stream.containsBefore(Object, Object)

toSet

public Set<A> toSet()
Description copied from interface: Stream
Converts this Stream into a Set, by adding all its elements to a new Set. The resulting Set is Serializable and non-lazy.

Returns:
a new Set that contains all elements retrieved from this Stream

toList

public List<A> toList()
Description copied from interface: Stream
* Converts this Stream into a List, by adding all its elements to a new List. The resulting List is Serializable and non-lazy.

Returns:
a new List that contains all elements retrieved from this Stream

force

public Stream<A> force()
Description copied from interface: Stream
Forces stream elements evaluation by converting it into a new ordered stream one that is not lazy and that has repeatable iteration order.

Returns:
a new Stream that retrieves elements from the next iteration of this Stream.

toArray

public A[] toArray(Class<? super A> clazz)
Description copied from interface: Stream
Creates a new array that has the same elements that the retrived by this Stream

Parameters:
clazz - the array component class
Returns:
a new array

toArray

protected A[] toArray(Class<? super A> clazz,
                      Collection<A> readOnlyColView)

joinStrings

public String joinStrings(@NonNull
                          String separator)
Description copied from interface: Stream
(Left)folds this Stream concatenating each elements toString with a separator

Returns:
the string representation of each element concatenated using a separator

partition

public Tuple2<List<A>,List<A>> partition(Evaluable<? super A> predicate)
Description copied from interface: Stream
Splits stream elements into two lists using a predicate - elements that evaluate to true will be returned in the first component, the rest will be returned in the second component

Returns:
a new Tuple2 that contains this stream partitioned into two lists.

streamPartition

public final Tuple2<Stream<A>,Stream<A>> streamPartition(Evaluable<? super A> predicate)
Description copied from interface: Stream
Splits stream elements into two ordered streams, that support random access. This method just converts list returned by Stream.partition(Evaluable) into Streams.

Returns:
a new Tuple2 that contains this stream partitioned into two other streams.

equiv

public boolean equiv(Iterable<? extends A> other)
Description copied from interface: Stream
Test that the elements of this stream are equal to the elements of the given Iterable, and in the same order.

Returns:
true if this stream has the same number of elements that other, and each pair formed by elements of this stream and given iterable at same position are equal. false otherwise

equivBy

public boolean equivBy(Evaluable2<? super A,? super A> equalty,
                       Iterable<? extends A> other)
Description copied from interface: Stream
Test that the elements of this stream are equivalent to the elements of the given Iterable, and in the same order, using the given equalityTest for determining equivalence between elements.

Returns:
true if this stream has the same number of elements that the given iterable, and each pair formed by elements of this stream and given iterable at same position satisfies the given Evaluable2

transform

public <B> Stream<B> transform(Applicable<Stream<A>,? extends Stream<B>> function)
Description copied from interface: Stream
Lazily applies the given function to this Stream.

Parameters:
function - the function to apply to this stream
Returns:
a new stream that will retrieve elements from the result of applying the given function to this stream

intersperse

public Stream<A> intersperse(A sep)
 intersperse _   []      = []
 intersperse _   [x]     = [x]
 intersperse sep (x:xs)  = x : sep : intersperse sep xs
 

Returns:
a new Stream

incorporate

public Stream<A> incorporate(Function<? super A,? extends A> function)
Description copied from interface: Stream
Inserts after each element the result of applying the given function to it. For example:
 Streams.cons(10, 9, 90).incorporate(integer().negate()).equiv(10, -10, 9, -9, 90, -90);
 

Returns:
a new Stream

incorporate

public Stream<A> incorporate(A element)
Description copied from interface: Stream
Inserts the given value after each element of the stream. This message is similar to Stream.intersperse(Object), but inserts the value also at the end of the stream.

Parameters:
element - Example:
 Streams.cons('a', 'b', 'c').incorporate('d').equiv('a', 'd', 'b', 'd', 'c', 'd');
 
Returns:
a new Stream

append

public Stream<A> append(A element)
Description copied from interface: Stream
Adds an element as the last one of the stream.

Returns:
a new Stream that retrieves this Stream elements, and then, the given element

prepend

public Stream<A> prepend(A element)
Description copied from interface: Stream
Adds an element as the first one of the stream.

Returns:
a new Stream that retrieves the given element, and then, this Stream elements.

transform

public <B> Stream<B> transform(Stream.DeconsApplicable<A,B> function)
Description copied from interface: Stream
Lazily applies the given function to this stream, deconstructing this stream into head and tail, or into an empty stream. Unlike Stream.transform(Applicable), whose function will receive the whole stream, the given Stream.DeconsApplicable, when applied, will take the head and tail of this Stream, if non empty, or no arguments, if the stream is empty.

Returns:
a new stream that will retrieve elements from the result of applying the given function to this stream
See Also:
Stream.decons()

transform

public <B> Stream<B> transform(Stream.DelayedDeconsApplicable<A,B> function)
Description copied from interface: Stream
Lazily applies the given function to this stream, deconstructing this stream into a head thunk and tail, or into an empty stream. Unlike Stream.transform(Applicable), whose function will receive the whole stream, the given Stream.DeconsApplicable, when applied, will take a head's thunk and tail of this Stream, if non empty, or no arguments, if the stream is empty.

Returns:
a new stream that will retrieve elements from the result of applying the given function to this stream
See Also:
Stream.delayedDecons()

decons

public Tuple2<A,Stream<A>> decons()
Description copied from interface: Stream
Answers this stream split into head and tail. This method is preferred over Stream.head() and Stream.tail(), as it will work as expected even on non repeatable iteration streams.

Returns:
a pair containing the head and the tail of this stream. The tail is NonNull and a Projection, but it is always non-repeatable.

delayedDecons

public Tuple2<Thunk<A>,Stream<A>> delayedDecons()
Description copied from interface: Stream
Answers this non-empty stream split into a head thunk and tail. This method is preferred over Stream.decons() when the head value of the Stream is potentially irrelevant, as this methods grants to suceeds even in those cases where Stream.head() fails.

Returns:
a pair containing a thunk that will provide the head, and the tail of this stream. The tail is NonNull and a Projection, but it is always non-repeatable.

tail

public Stream<A> tail()
Description copied from interface: Stream
Answers the tail of the Stream

Returns:
an Stream that retrieves all its elements, except of the first one

sum

public A sum(NumberType<A> numberType)
Description copied from interface: Stream
Answers the sum of the elements of this Stream using the given NumberType

Returns:
the result of adding each element, or zero, if this stream is empty
See Also:
Iterables.sum(Iterable, NumberType)

product

public A product(NumberType<A> numberType)
Description copied from interface: Stream
Answers the product of the elements of this Stream using the given NumberType

Returns:
the result of multiplying each element, or one, if this stream is empty
See Also:
Iterables.product(Iterable, NumberType)

average

public A average(NumberType<A> numberType)
Description copied from interface: Stream
Answers the average of the stream elements, using the given NumberType for performing addition and division.

Returns:
the average of the stream elements

maximum

public A maximum()
Description copied from interface: Stream
Answers the maximum element of the stream, using elements natural order.

Returns:
the maximum element.

minimum

public A minimum()
Description copied from interface: Stream
Answers the minimum element of the stream, using elements natural order.

Returns:
the minimum element.

maximumBy

public A maximumBy(Comparator<? super A> comparator)
Description copied from interface: Stream
Answers the maximum element of the stream, using the given comparator to compare elements.

Returns:
the maximum element.

minimumBy

public A minimumBy(Comparator<? super A> comparator)
Description copied from interface: Stream
Answers the min element of the stream, using the given comparator to compare elements.

Returns:
the minimum element.

sort

public Stream<A> sort()
Description copied from interface: Stream
Sorts this Stream, using their element's natural ordering

Returns:
a new Stream

sortBy

public Stream<A> sortBy(Comparator<A> comparator)
Description copied from interface: Stream
Sorts this Stream, using the given comparator

Returns:
a new Stream

reverse

public Stream<A> reverse()
Description copied from interface: Stream
Reverses this Stream, by returning a new Stream that retrieves elements in the inverse order of this Stream. This may not be a Projection, depending on if the stream's source permits it.

Returns:
a new Stream that retrieves elements in the inverse order of this stream.

groupOn

public <K,V> Map<K,V> groupOn(Applicable<? super A,K> groupFunction,
                              Reduction<A,V> reduction)
Description copied from interface: Stream
Groups elements by the given groupingFunction, and reduces each group using the given reduction.

The grouping is performed so that two elements a and b will be put in the same group if and only if groupFunction.apply(a).equals(groupFunction.apply(b))

Returns:
a Map with an entry for each group, where its key is the result of the grouping function, and the value is the result of the reduction of the elements for that group

cross

public Stream<Tuple2<A,A>> cross()
Description copied from interface: Stream
Answers the Cartesian product of this stream and itself

Returns:
a new Stream projection
See Also:
Iterables.cross(Iterable, Iterable)

cross

public final <B> Stream<Tuple2<A,B>> cross(B... elements)
Description copied from interface: Stream
Answers the cartesian product of this Stream and the given elements. Example:
 //Answers true
 Streams
   .cons("male", "female")
   .cross("young", "middle-aged", "old")
   .equiv(
     _("male", "young"),
     _("male", "middle-aged"),
     _("male", "old"),
     _("female", "young"),
     _("female", "middle-aged"),
     _("female", "old"));
 

Returns:
cross(Streams.cons(elements))
See Also:
Stream.cross(Stream)

cross

public final <B> Stream<Tuple2<A,B>> cross(Iterator<B> other)
Description copied from interface: Stream
Answers the cartesian product of this Stream and the given Iterator

Returns:
cross(Streams.from(other))
See Also:
Stream.cross(Stream)

cross

public <B> Stream<Tuple2<A,B>> cross(@NonNull
                                     Iterable<B> other)
Description copied from interface: Stream
Answers the cartesian product of this Stream and the given Iterable

Returns:
cross(Streams.from(other))
See Also:
Stream.cross(Stream)

cross

public <B> Stream<Tuple2<A,B>> cross(@NonNull
                                     Stream<B> other)
Description copied from interface: Stream
Answers the Cartesian product of this stream and the given one

Returns:
a new Stream projection
See Also:
Iterables.cross(Iterable, Iterable)

crossStreams

public Stream<Stream<A>> crossStreams(@NonNull
                                      Stream<Stream<A>> other)
Description copied from interface: Stream
Answers the cartesian product of this Stream and each one of the given streamOfStreams

Returns:
a new Stream projection

print

public final void print(Appendable o)
                 throws IOException
Description copied from interface: Stream
Prints the stream elements to an appendable, like StringBuilder or a Writer

Parameters:
o - the appendable were print stream elements
Throws:
IOException - if any io error occurs

print

public final void print()
Description copied from interface: Stream
Prints the stream elements to System.out


println

public final void println(Appendable o)
                   throws IOException
Description copied from interface: Stream
Prints the stream elements to an appendable, like StringBuilder or a Writer, followed by a newline character

Throws:
IOException - if any io error occurs

printString

public final String printString()
Description copied from interface: Stream
Prints the stream elements to a string

Returns:
a string with the stream elements

toString

public final String toString()
Overrides:
toString in class Object

concat

public final Stream<A> concat(Iterable<? extends A> other)
Description copied from interface: Stream
Concatenates this with other It answers an Stream that retrieves elements from this Stream, and then, after its last element, from the given iterable. As a particular case, if this Stream is infinite, the resulting Stream will retrieve the same elements than this one.

Returns:
a new Stream

concat

public final Stream<A> concat(A... elements)
Description copied from interface: Stream
Concatenates this with the given elements It answers an Stream that retrieves elements from this Stream, and then, after its last element, the given elements.

As a particular case, if this Stream is infinite, the resulting Stream will retrieve the same elements than this one.

Example:

 //answers the Stream [0, 1, 2, 3]
 Streams.cons(0, 1).concat(2, 3); 
 

Parameters:
elements - the element to add at the end of the stream
Returns:
a new Stream

concat

public Stream<A> concat(Iterator<? extends A> other)
Description copied from interface: Stream
Concatenates this with the given Iterator It answers an Stream that retrieves elements from this Stream, and then, after its last element, the given iterator's elements. As a particular case, if this Stream is infinite, the resulting Stream will retrieve the same elements than this one.

Parameters:
other - the iterator whose elements will be added at the end of the stream
Returns:
a new Stream

delayedAppend

public Stream<A> delayedAppend(Thunk<A> thunk)
Description copied from interface: Stream
Adds an element's thunk as the last one of the stream.

Returns:
a new Stream that retrieves this Stream elements, and then, the value of the given thunk

delayedPrepend

public Stream<A> delayedPrepend(Thunk<A> thunk)
Description copied from interface: Stream
Adds an element's thunk as the first one of the stream.

Returns:
a new Stream that retrieves the value of the given thunk, and then, this Stream elements.

zipWith

public final <B,C> Stream<C> zipWith(Function2<? super A,? super B,C> function,
                                     B... elements)
Description copied from interface: Stream
Returns a Stream formed by the result of applying the given function to each pair of elements from this and the given iterable. If either this or the given iterable is shorter than the other given number of elements, the remaining elements of this are discarded

Example:

 //Answers the stream [1 + 4, 2 + 5] == [5, 7];
 Streams.cons(1, 2).zipWith(integer().add(), 4, 5);  
 

Parameters:
function - the function to apply to each pair
elements - the elements to zip with this Stream
Returns:
a new Stream which is the result of applying the given Applicable2 to each pair this Stream and the elements. The resulting Stream size is the minimum of this size and the number of elements
See Also:
Iterables.zip(Iterable, Iterable)

zipWith

public final <B,C> Stream<C> zipWith(Function2<? super A,? super B,C> function,
                                     Iterable<B> other)
Description copied from interface: Stream
Returns a Stream formed by the result of applying the given function to each pair of elements from this and the given iterable. If either this or the given iterable is shorter than the other one, the remaining elements are discarded.

Type Parameters:
B - the type to the iterable to zip with this Stream
C - the resulting Stream element type
Parameters:
function - the function to apply to each pair
Returns:
a new Stream formed applying the given Applicable2 to each pair this Stream and the given iterable. The resulting Stream size is the minimum of both iterables sizes, or infinite, if both this and iterable are
See Also:
Iterables.zip(Iterable, Iterable)

zipWith

public <B,C> Stream<C> zipWith(Function2<? super A,? super B,C> function,
                               Iterator<B> other)
Description copied from interface: Stream
Returns a Stream formed by the result of applying the given function to each pair of elements from this and the other. If either this or the given Iterator is shorter than the other one, the remaining elements are discarded.

Type Parameters:
B - the type to the iterable to zip with this Stream
C - the resulting Stream element type
Parameters:
function - the function to apply to each pair
other - the Iterator to zip with this Stream
Returns:
a new Stream formed applying the given Applicable2 to each pair this Stream and the given iterator. The resulting Stream size is the minimum of both iterables sizes, or infinite, if both this and other are
See Also:
Iterables.zip(Iterable, Iterable)

memoize

public Stream<A> memoize()
Description copied from interface: Stream
Memoizes stream elements and their order, by answering a lazy stream with Repeatable iteration order that caches elements evaluated during iteration.

Returns:
a new Stream that memoizes elements evaluated during iteration

insertBeforeIndex

public Stream<A> insertBeforeIndex(A element,
                                   @NotNegative
                                   int position)
Description copied from interface: Stream
Inserts element at index - 1. If stream has not enough elements, that is, stream size is less than or equal to index elements, the element is inserted at its end.

This message ensures that for any finite stream, and any element and index

  stream.insertBeforeIndex(element, index).containsBeforeIndex(element, index + 1)
 
is true.

Examples:

  Streams.cons(4, 5, 6).insertBeforeIndex(0, 2); //answers [4, 5, 0, 6]
  Streams.repeat(1).take(4).insertBeforeIndex(0, 2); //answers [1, 1, 0, 1, 1]
  Streams.repeat(1).take(4).insertBeforeIndex(0, 20); //answers [1, 1, 1, 1, 0]
 

Parameters:
element - the element to insert
position - the index before inserting it
Returns:
a Stream that lazily inserts the given element before the given index

insertBefore

public Stream<A> insertBefore(A element,
                              A reference)
Description copied from interface: Stream
Inserts element before the first occurrence of reference. If reference is not contained by this stream, the element is inserted at its end.

This message ensures that for any finite stream, and any element and reference

  stream.insertBefore(element, ref).containsBefore(element, ref)
 
is true.

Examples:

  Streams.cons('a', 'b', 'c').insertBefore('x', 'a'); //answers [x,a,b,c]
  Streams.cons('a', 'b', 'c').insertBefore('x', 'c'); //answers [a,b,x,c]
  Streams.cons('a', 'b', 'c').insertBefore('x', 'd'); //answers [a,b,c,x]
 

Returns:
a Stream that lazily inserts the given element before the reference

containsBeforeIndex

public boolean containsBeforeIndex(A element,
                                   @NotNegative
                                   int index)
Description copied from interface: Stream
Answers if element is contained by this stream, and its first occurence is before the given index. As a particular case, if index is 0, return always false

Examples:

   Streams.from("abcd").containsBeforeIndex('x', 2); //false. x is not present
   Streams.from("abcd").containsBeforeIndex('c', 1); //false. c is present but at index 2
   Streams.from("abcd").containsBeforeIndex('a', 0); //false. a is present but at index 0
   Streams.from("abcd").containsBeforeIndex('a', 1); //true
   Streams.from("abcd").containsBeforeIndex('b', 3); //true
   Streams.from("abcd").containsBeforeIndex('b', 40); //true
 

Returns:
whether elements is containted and before index
See Also:
Stream.insertBeforeIndex(Object, int)

containsBefore

public boolean containsBefore(A element,
                              A reference)
Description copied from interface: Stream
Answers if element is contained by this stream, and its first occurence is before the first occurence of the second one, if any.

This message is similar to Stream.isBefore(Object, Object), but may be true even if the reference is not present at the stream. It will be always true if both element and reference are equal, too.

Examples:

  Streams.from("abcd").containsBefore('c', 'a'); //false. c is after a
  Streams.from("abcd").containsBefore('x', 'd'); //false. x is not present
  Streams.from("abcd").containsBefore('a', 'b'); //true
  Streams.from("abcd").containsBefore('b', 'd'); //true
  Streams.from("abcd").containsBefore('a', 'a'); //true
  Streams.from("abcd").containsBefore('x', 'x'); //false. x is not present
 

Parameters:
element - the element to test whether it is contained by this stream, and appears before reference
Returns:
whether both elements are contained by this Stream, and the first is before the second one.
See Also:
Stream.insertBefore(Object, Object), Stream.isBefore(Object, Object)


Get Staccatocommons at SourceForge.net. Fast, secure and Free Open Source software downloads