(this, collectionSupplier, collector));
- }
-
- /**
- * Concatenate this iterable with the other iterable in a way, that calling the second iterator()
- * only happens when there is no more element in the first iterator.
- * The returned iterator forwards all remove() calls
- * to the current source (first or next).
- * @param other the second iterable
- * @return the new iterable
- */
- public final Ix concatWith(Iterable extends T> other) {
- return from(Interactive.concat(it, other));
- }
- /**
- * Concatenate this iterable with the sequence of array values.
- * @param values the array values
- * @return the created iterable builder
- */
- public final Ix concatWith(final T... values) {
- return concatWith(Interactive.toIterable(values));
- }
- /**
- * Creates an iterable builder which contains the concatenation
- * of this iterable and the rest iterable provided.
- * @param others the other iterables
- * @return the created iterable
- */
- @SuppressWarnings("unchecked")
- public final Ix concatWithAll(Iterable extends Iterable extends T>> others) {
- return from(Interactive.concat(Interactive.startWith(others, it)));
- }
- /**
- * Returns an iterable which checks for the existence of the supplied
- * value by comparing the elements of the source iterable using reference
- * and equals(). The iterable then returns a single true or false.
- * @param value the value to check
- * @return the new iterable
- */
- public final Ix contains(final Object value) {
- return from(Interactive.contains(it, value));
- }
- /**
- * Counts the elements of the iterable source by using a 32 bit int.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @return the new iterable
- */
- public final Ix count() {
- return from(Interactive.count(it));
- }
- /**
- * Counts the elements of the iterable source by using a 64 bit long.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @return the new iterable
- */
- public final Ix countLong() {
- return from(Interactive.countLong(it));
- }
- /**
- * Convert the source materialized elements into normal iterator behavior.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @return the new iterable
- */
- @SuppressWarnings("unchecked")
- public final Ix dematerialize() {
- return from(Interactive.dematerialize((Iterable>)it));
- }
- /**
- * Returns an iterable which filters its elements based if they vere ever seen before in
- * the current iteration.
- * Value equality is computed by reference equality and equals()
- * @return the new iterable
- */
- public final Ix distinct() {
- return from(Interactive.distinct(it));
- }
- /**
- * Returns an iterable which filters its elements by an unique key
- * in a way that when multiple source items produce the same key, only
- * the first one ever seen gets relayed further on.
- * Key equality is computed by reference equality and equals()
- * @param the key element type
- * @param keySelector the key selector for only-once filtering
- * @return the new iterable
- */
- public final Ix distinct(Func1 super T, ? extends U> keySelector) {
- return from(Interactive.distinct(it, keySelector, IxHelperFunctions.identity()));
- }
- /**
- * Creates an iterable which ensures that subsequent values of T are not equal
- * (reference and equals).
- * @return the new iterable
- */
- public final Ix distinctNext() {
- return from(Interactive.distinctNext(it));
- }
- /**
- * Creates an iterable which ensures that subsequent values of
- * T are not equal in respect to the extracted keys (reference and equals).
- * @param the key type
- * @param keySelector the function to extract the keys which will be compared
- * @return the new iterable
- */
- public final Ix distinctNext(final Func1 keySelector) {
- return from(Interactive.distinctNext(it, keySelector));
- }
- /**
- * Returns an iterable which executes the given action after
- * the stream completes.
- * The returned iterator forwards all remove() calls
- * to the source.
- * @param action the action to invoke
- * @return the new iterable
- */
- public final Ix doOnCompleted(Action0 action) {
- return from(Interactive.doOnCompleted(it, action));
- }
- /**
- * Construct a new iterable which will invoke the specified action
- * before the source value gets relayed through it.
- * Can be used to inject side-effects before returning a value.
- * The returned iterator forwards all remove() calls
- * to the source.
- * @param action the action to invoke before each next() is returned.
- * @return the new iterable
- */
- public final Ix doOnNext(Action1 super T> action) {
- return from(Interactive.doOnNext(it, action));
- }
- /**
- * Returns an iterable which reiterates over and over again on source
- * as long as the gate is true. The gate function is checked only
- * when a pass over the source stream was completed.
- * Note that using this operator on an empty iterable may result
- * in a direct infinite loop in hasNext() or next() calls depending on the gate function.
- * The returned iterator forwards all remove() calls
- * to the source.
- * @param gate the gate function to stop the repeat
- * @return the new iterable
- */
-
- public final Ix doWhile(
- final Func0 gate) {
- return from(Interactive.doWhile(it, gate));
- }
- /**
- * Creates an iterable sequence which returns all elements from source
- * followed by the supplied value as last.
- * The returned iterable forwards all {@code remove()}
- * methods to the source iterable, except the last element where it
- * throws UnsupportedOperationException.
- * @param value the value to append
- * @return the new iterable
- */
- public final Ix endWith(final T value) {
- return from(Interactive.endWith(it, value));
- }
- /**
- * Creates an iterable which filters this iterable with the
- * given predicate factory function. The predicate returned by the factory receives an index
- * telling how many elements were processed thus far.
- * Use this construct if you want to use some memorizing predicat function (e.g., filter by subsequent distinct, filter by first occurrences only)
- * which need to be invoked per iterator() basis.
- * The returned iterator forwards all remove() calls
- * to the source.
- * @param predicate the predicate function
- * @return the new iterable
- */
- public final Ix filter(final Func1 super T, Boolean> predicate) {
- return from(Interactive.filter(it, predicate));
- }
- /**
- * Creates an iterable which filters this iterable with the
- * given predicate factory function. The predicate returned by the factory receives an index
- * telling how many elements were processed thus far.
- * @param predicate the predicate
- * @return the new iterable
- */
- public final Ix filterIndexed(final Func2 predicate) {
- return from(Interactive.filterIndexed(it, predicate));
- }
- /**
- * Returns the first element from the iterable sequence or
- * throws a NoSuchElementException.
- * @return the first element
- */
- public final T first() {
- return Interactive.first(it);
- }
-
- /**
- * Returns the first element from the sequence or the default
- * value if this sequence is empty
- * @param defaultValue the default value to return
- * @return the first or default value
- * @since 0.91.2
- */
- public final T firstOrDefault(T defaultValue) {
- return Interactive.firstOrDefault(it, defaultValue);
- }
-
- /**
- * Creates an iterable which returns a stream of Us for each source Ts.
- * The iterable stream of Us is returned by the supplied selector function.
- * The returned iterator forwards all remove() calls
- * to the current source (which might not accept it).
- * @param the output element type
- * @param selector the selector for multiple Us for each T
- * @return the new iterable
- */
- public final Ix flatMap(final Func1 super T, ? extends Iterable extends U>> selector) {
- return from(Interactive.flatMap(it, selector));
- }
- /**
- * Iterate over the source and submit each value to the
- * given action. Basically, a for-each loop with pluggable
- * action.
- * This method is useful when the concrete values from the iterator
- * are not needed but the iteration itself implies some side effects.
- * @param action the action to invoke on with element
- */
- public final void forEach(
- final Action1 super T> action) {
- Interactive.forEach(it, action);
- }
- /**
- * Creates an iterable which traverses the source iterable,
- * and based on the key selector, groups values the elements into GroupedIterables,
- * which can be interated over later on.
- * The equivalence of the keys are determined via reference
- * equality and equals() equality.
- *
The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param the result group element type
- * @param keySelector the key selector
- * @return the new iterable
- */
- public final Ix> groupBy(
- final Func1 super T, ? extends K> keySelector) {
- return from(Interactive.groupBy(it, keySelector, IxHelperFunctions.identity()));
- }
- /**
- * Creates an iterable which traverses the source iterable,
- * and based on the key selector, groups values extracted by valueSelector into GroupedIterables,
- * which can be interated over later on.
- * The equivalence of the keys are determined via reference
- * equality and equals() equality.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param the result group element type
- * @param the result group keys
- * @param keySelector the key selector
- * @param valueSelector the value selector
- * @return the new iterable
- */
- public final Ix> groupBy(
- final Func1 super T, ? extends K> keySelector,
- final Func1 super T, ? extends V> valueSelector) {
- return from(Interactive.groupBy(it, keySelector, valueSelector));
- }
- /**
- * Add the elements of the sequence into the supplied collection.
- * @param a collection type
- * @param out the output collection
- * @return the same out value
- */
- public final > U into(U out) {
- Iterator it = iterator();
- try {
- while (it.hasNext()) {
- out.add(it.next());
- }
- } finally {
- Interactive.unsubscribe(it);
- }
- return out;
- }
- /**
- * Returns a single true if the target iterable is empty.
- * @return the new iterable
- */
- public final Ix isEmpty() {
- return from(Interactive.isEmpty(it));
- }
- @Override
- public final Iterator iterator() {
- return it.iterator();
- }
- /**
- * Concatenates the source strings one after another and uses the given separator.
- * The returned iterator forwards all remove() calls
- * to the source.
- * @param separator the separator to use
- * @return the new iterable
- */
- public final Ix join(String separator) {
- return from(Interactive.join(it, separator));
- }
- /**
- * Returns the last element of the iterable or throws a
- * NoSuchElementException if the iterable is empty.
- * @return the last value
- */
- public final T last() {
- return Interactive.last(it);
- }
-
- /**
- * Returns the last element of this sequence or the default value if
- * this sequence is empty.
- * @param defaultValue the default value to return if this sequence is empty
- * @return the last or default value
- */
- public final T lastOrDefault(T defaultValue) {
- return Interactive.lastOrDefault(it, defaultValue);
- }
-
- /**
- * Creates an iterable which is a transforms the source
- * elements by using the selector function.
- * The function receives the current element.
- * @param the output element type
- * @param selector the selector function
- * @return the new iterable
- */
- public final Ix map(final Func1 super T, ? extends U> selector) {
- return from(Interactive.map(it, selector));
- }
- /**
- * Creates an iterable which is a transforms the source
- * elements by using the selector function.
- * The function receives the current index and the current element.
- * The returned iterator forwards all remove() calls
- * to the source.
- * @param the output element type
- * @param selector the selector function
- * @return the new iterable
- */
- public final Ix mapIndexed(final Func2 selector) {
- return from(Interactive.mapIndexed(it, selector));
- }
- /**
- * Transforms the sequence of the source iterable into an option sequence of
- * Option.some(), Option.none() and Option.error() values, depending on
- * what the source's hasNext() and next() produces.
- * The returned iterator will throw an UnsupportedOperationException for its remove() method.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @return the new iterable
- */
- public final Ix> materialize() {
- return from(Interactive.materialize(it));
- }
- /**
- * Returns the maximum value of the given iterable source.
- * @param the self comparable type
- * @return the new iterable
- */
- @SuppressWarnings("unchecked")
- public final > Ix max() {
- return from(Interactive.max((Iterable)it));
- }
- /**
- * Returns the maximum value of the given iterable source in respect to the supplied comparator.
- * @param comparator the comparator to use
- * @return the new iterable
- */
- public final Ix max(Comparator super T> comparator) {
- return from(Interactive.max(it, comparator));
- }
- /**
- * Returns an iterator which will produce a single List of the maximum values encountered
- * in the source stream based on the supplied key selector.
- * @param the source element type, which must be self comparable
- * @return the new iterable
- */
- @SuppressWarnings("unchecked")
- public final > Ix> maxBy() {
- return from(Interactive.maxBy((Iterable)it));
- }
- /**
- * Returns an iterator which will produce a single List of the maximum values encountered
- * in the source stream based on the supplied comparator.
- * @param comparator the key comparator
- * @return the new iterable
- */
- public final Ix> maxBy(Comparator super T> comparator) {
- return from(Interactive.maxBy(it, comparator));
- }
- /**
- * Returns an iterator which will produce a single List of the maximum values encountered
- * in the source stream based on the supplied key selector.
- * @param the key type, which must be self-comparable
- * @param keySelector the selector for keys
- * @return the new iterable
- */
- public final > Ix> mayBy(Func1 super T, U> keySelector) {
- return from(Interactive.maxBy(it, keySelector));
- }
- /**
- * Returns an iterator which will produce a single List of the minimum values encountered
- * in the source stream based on the supplied key selector and comparator.
- * @param the key type
- * @param keySelector the selector for keys
- * @param keyComparator the key comparator
- * @return the new iterable
- */
- public final Ix> mayBy(Func1 super T, U> keySelector, Comparator super U> keyComparator) {
- return from(Interactive.maxBy(it, keySelector, keyComparator));
- }
- /**
- * Enumerates the source iterable once and caches its results.
- * Any iterator party will basically drain this cache, e.g.,
- * reiterating over this iterable will produce no results.
- * Note: the name is not a misspelling, see Memoization.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param bufferSize the size of the buffering
- * @return the new iterable
- */
- public final Ix memoize(int bufferSize) {
- return from(Interactive.memoize(it, bufferSize));
- }
- /**
- * The returned iterable ensures that the source iterable is only traversed once, regardless of
- * how many iterator attaches to it and each iterator see only the values.
- * Note: the name is not a misspelling, see Memoization.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @return the new iterable
- */
- public final Ix memoizeAll() {
- return from(Interactive.memoizeAll(it));
- }
- /**
- * Returns the maximum value of the given iterable source.
- * @param the self comparable type
- * @return the new iterable
- */
- @SuppressWarnings("unchecked")
- public final > Ix min() {
- return from(Interactive.min((Iterable)it));
- }
- /**
- * Returns the minimum value of the given iterable source in respect to the supplied comparator.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param comparator the comparator to use
- * @return the new iterable
- */
- public final Ix min(Comparator super T> comparator) {
- return from(Interactive.min(it, comparator));
- }
- /**
- * Returns an iterator which will produce a single List of the minimum values encountered
- * in the source stream based on the supplied comparator.
- * @param comparator the key comparator
- * @return the new iterable
- */
- public final Ix> minBy(Comparator super T> comparator) {
- return from(Interactive.minBy(it, comparator));
- }
- /**
- * Returns an iterator which will produce a single List of the minimum values encountered
- * in the source stream based on the supplied key selector.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param the key type, which must be self-comparable
- * @param keySelector the selector for keys
- * @return the new iterable
- */
- public final > Ix> minBy(Func1 super T, U> keySelector) {
- return from(Interactive.minBy(it, keySelector));
- }
- /**
- * Returns an iterator which will produce a single List of the minimum values encountered
- * in the source stream based on the supplied key selector and comparator.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param the key type
- * @param keySelector the selector for keys
- * @param keyComparator the key comparator
- * @return the new iterable
- */
- public final Ix> minBy(Func1 super T, U> keySelector, Comparator super U> keyComparator) {
- return from(Interactive.minBy(it, keySelector, keyComparator));
- }
- /**
- * Returns an iterator which will produce a single List of the minimum values encountered
- * in the source stream based on the supplied key selector.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param the source element type, which must be self comparable
- * @return the new iterable
- */
- @SuppressWarnings("unchecked")
- public final > Ix> minxBy() {
- return from(Interactive.minBy((Iterable)it));
- }
- /**
- * Casts the source iterable into a different typ by using a type token.
- * If the source contains a wrong element, the next()
- * will throw a ClassCastException.
- * The returned iterator forwards all remove() calls
- * to the source.
- * @param the result element type
- * @param token the type token
- * @return the new iterable
- */
- public final Ix ofType(final Class token) {
- return from(Interactive.ofType(it, token));
- }
- /**
- * Returns an iterable which traverses the entire
- * source iterable and creates an ordered list
- * of elements. Once the source iterator completes,
- * the elements are streamed to the output.
- * Note: the element type should be self comparable or a ClassCastException is thrown.
- * @param the source element type, which must be self comparable
- * @return the new iterable
- */
- @SuppressWarnings("unchecked")
- public final > Ix orderBy() {
- return from(Interactive.orderBy((Iterable)it));
- }
- /**
- * Returns an iterable which traverses the entire
- * source iterable and creates an ordered list
- * of elements. Once the source iterator completes,
- * the elements are streamed to the output.
- * @param comparator the value comparator
- * @return the new iterable
- */
- public final Ix orderBy(Comparator super T> comparator) {
- return from(Interactive.orderBy(it, comparator));
- }
- /**
- * Returns an iterable which traverses the entire
- * source iterable and creates an ordered list
- * of elements. Once the source iterator completes,
- * the elements are streamed to the output.
- * @param the key type for the ordering, must be self comparable
- * @param keySelector the key selector for comparison
- * @return the new iterable
- */
- public final > Ix orderBy(Func1 super T, ? extends U> keySelector) {
- return from(Interactive.orderBy(it, keySelector));
- }
- /**
- * Returns an iterable which traverses the entire
- * source iterable and creates an ordered list
- * of elements. Once the source iterator completes,
- * the elements are streamed to the output.
- * @param the key type for the ordering
- * @param keySelector the key selector for comparison
- * @param keyComparator the key comparator function
- * @return the new iterable
- */
- public final Ix orderBy(Func1 super T, ? extends U> keySelector, Comparator super U> keyComparator) {
- return from(Interactive.orderBy(it, keySelector, keyComparator));
- }
- /**
- * Runs this iterable and prints the values.
- * Is the same as using {@code this.run(Interactive.print())}.
- */
- public final void print() {
- Interactive.forEach(it, Interactive.print());
- }
- /**
- * Runs this iterable and prints the values.
- * Is the same as using {@code this.run(Interactive.println())}.
- */
- public final void println() {
- Interactive.forEach(it, Interactive.println());
- }
- /**
- * Applies the func function for a shared instance of the source,
- * e.g., func.invoke(share(source)).
- * @param the return types
- * @param func invoke the function on the buffering iterable and return an iterator over it.
- * @return the new iterable
- */
- public final Ix prune(Func1 super Iterable extends T>, ? extends Iterable> func) {
- return from(Interactive.prune(it, func));
- }
- /**
- * The returned iterable ensures that the source iterable is only traversed once, regardless of
- * how many iterator attaches to it and each iterator see only the same cached values.
- * The returned iterator will throw an UnsupportedOperationException
- * for remove() method of its first element, then it might throw for any
- * subsequent element, depending on the source iterable.
- * @param the return types
- * @param func invoke the function on the buffering iterable and return an iterator over it.
- * @param initial the initial value to append to the output stream
- * @return the new iterable
- */
- public final Ix publish(final Func1 super Iterable super T>, ? extends Iterable extends U>> func,
- final U initial) {
- return from(Interactive.publish(it, func, initial));
- }
- /**
- * The returned iterable ensures that the source iterable is only traversed once, regardless of
- * how many iterator attaches to it and each iterator see only the values.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param the return types
- * @param func invoke the function on the buffering iterable and return an iterator over it.
- * @return the new iterable
- */
- public final Ix publish(final Func1 super Iterable, ? extends Iterable> func) {
- return from(Interactive.publish(it, func));
- }
- /**
- * Reduces the values of this sequence into a single value by using a
- * reducer function.
- * @param reducer the reducer function that receives the first item or
- * the result of the previous application of the function as the first parameter
- * and the current item as the second parameter.
- * @return the new Iterable
- * @since 0.92.3
- */
- public final Ix reduce(Func2 reducer) {
- return from(new ReduceIterable(this, reducer));
- }
- /**
- * Consumes the sequence and removes all items via the Iterator.remove().
- */
- public final void removeAll() {
- Iterator it = iterator();
- try {
- while (it.hasNext()) {
- it.next();
- it.remove();
- }
- } finally {
- Interactive.unsubscribe(it);
- }
- }
- /**
- * The returned iterable ensures that the source iterable is only traversed once, regardless of
- * how many iterator attaches to it and each iterator may only see one source element.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param the return types
- * @param func invoke the function on the buffering iterable and return an iterator over it.
- * @return the new iterable
- */
- public final Ix replay(final Func1 super Iterable, ? extends Iterable> func) {
- return from(Interactive.replay(it, func));
- }
- /**
- * The returned iterable ensures that the source iterable is only traversed once, regardless of
- * how many iterator attaches to it and each iterator see only the some cached values.
- * The returned iterator will throw an UnsupportedOperationException
- * for its remove() method.
- * @param the return types
- * @param func invoke the function on the buffering iterable and return an iterator over it.
- * @param bufferSize the buffer size
- * @return the new iterable
- */
- public final Ix replay(final Func1 super Iterable, ? extends Iterable> func,
- final int bufferSize) {
- return from(Interactive.replay(it, func, bufferSize));
- }
- /**
- * Iterates over the given source without using its returned value.
- * This method is useful when the concrete values from the iterator
- * are not needed but the iteration itself implies some side effects.
- */
- public final void run() {
- Interactive.run(it);
- }
- /**
- * Generates an iterable which acts like a running sum when iterating over the source iterable, e.g.,
- * For each element in T, it computes a value by using the current aggregation value and returns it.
- * The first call to the aggregator function will receive a zero for its first argument.
- * @param the destination element type
- * @param aggregator the function which takes the current running aggregation value, the current element and produces a new aggregation value.
- * @return the new iterable
- */
- public final