All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ix.Ix Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright 2011-2016 David Karnok
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ix;

import java.math.*;
import java.util.*;

import ix.internal.operators.*;
import ix.internal.util.IxHelperFunctions;
import rx.*;
import rx.Observable;
import rx.functions.*;

/**
 * An iterable builder which offers methods to chain the
 * sequence of some Interactive operations.
 * 

This builder is the dual of the * {@link rx.Observable} class.

* @param the element type */ public class Ix implements Iterable { /** * Creates an iterable builder which contains the concatenation * of all the iterables returned by the iterable. * @param the value type * @param sources the source iterables * @return the created iterable */ public static Ix concat(Iterable> sources) { return from(Interactive.concat(sources)); } /** * Concatenates the array of Iterable sources into a single Iterable sequence. * @param the value type * @param sources the sources to concatenate * @return the created iterable * @since 0.92.3 */ public static Ix concat(Iterable... sources) { return from(Interactive.concat(Arrays.asList(sources))); } /** * Defers the source iterable creation to registration time and * calls the given func for the actual source. * @param the element type * @param func the function that returns an iterable. * @return the new iterable */ public static Ix defer( final Func0> func) { return from(Interactive.defer(func)); } /** * Creates a new iterable builder instance by wrapping the given * source sequence, if not already a builder. * @param the element type * @param source the source sequence * @return the created iterable builder */ public static Ix from(final Iterable source) { if (source instanceof Ix) { return (Ix)source; } return new Ix(source); } /** * Creates a new iterable builder by wrapping the given observable. *

The resulting iterable does not support the {@code remove()} method.

* @param the element type * @param source the source observable * @return the created iterable builder */ public static Ix from(Observable source) { return from(new ToIterable(source)); } /** * Creates a new iterable builder instance by wrapping the given * array. Changes to the array will be visible through * the iterator. * @param the element type * @param ts the array of ts * @return the created iterable builder */ public static Ix from(final T... ts) { return from(Interactive.toIterable(ts)); } /** * Creates an iterable which returns only a single element. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param the element type * @param value the value to return * @return the new iterable */ public static Ix just(final T value) { return from(Interactive.just(value)); } /** * Returns an iterator which will throw the given * Throwable exception when the client invokes * next() the first time. Any subsequent * next() call will simply throw a NoSuchElementException. * Calling remove() will always throw a IllegalStateException. * If the given Throwable instance extends a RuntimeException, it is throws * as is, but when the throwable is a checked exception, it is wrapped * into a RuntimeException. * @param the element type, irrelevant * @param t the exception to throw * @return the new iterable */ public static Ix error( final Throwable t) { return from(Interactive.error(t)); } /** * The singleton instance of an empty Ix. */ static final Ix EMPTY = from(Interactive.empty()); /** * Returns an empty iterable which will not produce elements. * Its hasNext() returns always false, * next() throws a NoSuchElementException * and remove() throws an IllegalStateException. * Note that the Collections.emptyIterable() static method is introduced by Java 7. * @param the element type, irrelevant * @return the iterable */ @SuppressWarnings("unchecked") public static Ix empty() { return (Ix)EMPTY; } /** * Creates a new iterable builder instance by wrapping the given * array. Changes to the array will be visible through * the iterator. * @param the element type * @param from the source index inclusive * @param to the destination index exclusive * @param ts the array of ts * @return the created iterable builder */ public static Ix fromPart(int from, int to, final T... ts) { return from(Interactive.toIterablePart(from, to, ts)); } /** * A generator function which returns Ts based on the termination condition and the way it computes the next values. * This is equivalent to: *

     * T value = seed;
     * while (predicate(value)) {
     *     yield value;
     *     value = next(value);
     * }
     * 
*

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param the element type * @param seed the initial value * @param predicate the predicate to terminate the process * @param next the function that computes the next value. * @return the new iterable */ public static Ix generate(T seed, Func1 predicate, Func1 next) { return from(Interactive.generate(seed, predicate, next)); } /** * Creates a new iterable builder instance by wrapping the given * source sequence. * @param the element type * @param source the source sequence * @return the created iterable builder */ public static Ix newBuilder(final Iterable source) { return new Ix(source); } /** * Creates an integer iteratable builder which returns numbers from the start position in the count size. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param start the starting value. * @param count the number of elements to return, negative count means counting down from the start. * @return the iterator. */ public static Ix range(int start, int count) { return from(Interactive.range(start, count)); } /** * Creates an long iterable builder which returns numbers from the start position in the count size. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param start the starting value. * @param count the number of elements to return, negative count means counting down from the start. * @return the iterator. */ public static Ix range(long start, long count) { return from(Interactive.range(start, count)); } /** * Creates an iterable builder which repeats the given * value indefinitely. *

The resulting iterable does not support the {@code remove()} method.

* @param the element type * @param t the value to repeat * @return the created iterable builder */ public static Ix repeat(final T t) { return from(Interactive.repeat(t)); } /** * Returns an iterable builder which repeats the given single value the specified number of times. *

The returned iterable does not support the {@code remove()} method.

* @param the value type * @param t the value to repeat * @param count the repeat amount * @return the iterable */ public static Ix repeat(final T t, int count) { return from(Interactive.repeat(t, count)); } /** * @param the element type * @return a function which wraps its iterable parameter into an iterablebuilder instance */ public static Func1, Ix> toBuilder() { return new Func1, Ix>() { @Override public Ix call(Iterable param1) { return from(param1); } }; } /** The backing iterable. */ protected final Iterable it; /** * Constructor. * @param source the backing iterable */ protected Ix(Iterable source) { this.it = source; } /** * Creates an iterable which traverses the source iterable and maintains a running sum value based * on the sum function parameter. Once the source is depleted, it * applies the divide function and returns its result. * This operator is a general base for averaging (where {@code sum(u, t) => u + t}, {@code divide(u, index) => u / index}), * summing (where {@code sum(u, t) => u + t}, and {@code divide(u, index) => u)}), * minimum, maximum, etc. * If the traversal of the source fails due an exception, that exception is reflected on the * {@code next()} call of the returned iterator. * The returned iterator will throw an UnsupportedOperationException * for its remove() method. * @param the itermediate aggregation type * @param the resulting aggregation type * @param sum the function which takes the current itermediate value, * the current source value and should produce a new intermediate value. * for the first element of T, the U parameter will receive null * @param divide the function which takes the last intermediate value and a total count of Ts seen and should return the final aggregation value. * @return the new iterable */ public final Ix aggregate( final Func2 sum, final Func2 divide) { return from(Interactive.aggregate(it, sum, divide)); } /** * Returns an iterable which contains true if all * elements of the source iterable satisfy the predicate. * The operator might return a false before fully iterating the source. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param predicate the predicate * @return the new iterable */ public final Ix all(final Func1 predicate) { return from(Interactive.all(it, predicate)); } /** * Determines if the given source has any elements at all. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @return the new iterable with a single true or false */ public final Ix any() { return from(Interactive.any(it)); } /** * Tests if there is any element of the source that satisfies the given predicate function. * @param predicate the predicate tester function * @return the new iterable */ public final Ix any( final Func1 predicate ) { return from(Interactive.any(it, predicate)); } /** * Returns a pair of the maximum argument and value from the given sequence. * @param the value type for the comparison, must be self comparable * @param valueSelector the value selector function * @return the pair of the first maximum element and value, null if the sequence was empty */ public final > Pair argAndMax( Func1 valueSelector) { return Interactive.argAndMax(it, valueSelector); } /** * Returns a pair of the maximum argument and value from the given sequence. * @param the value type * @param valueSelector the selector to extract the value from T * @param valueComparator the comparator to compare two values * @return the first pair of max argument and value or null if the source sequence was empty */ public final Pair argAndMax( Func1 valueSelector, Comparator valueComparator) { return Interactive.argAndMax(it, valueSelector, valueComparator); } /** * Returns a pair of the minimum argument and value from the given sequence. * @param the value type for the comparison, must be self comparable * @param valueSelector the value selector function * @return the pair of the first minimum element and value, null if the sequence was empty */ public final > Pair argAndMin( Func1 valueSelector) { return Interactive.argAndMin(it, valueSelector); } /** * Returns a pair of the minimum argument and value from the given sequence. * @param the value type * @param valueSelector the selector to extract the value from T * @param valueComparator the comparator to compare two values * @return the first pair of minimum argument and value or null if the source sequence was empty */ public final Pair argAndMin( Func1 valueSelector, Comparator valueComparator) { return Interactive.argAndMin(it, valueSelector, valueComparator); } /** * Computes and signals the average value of the BigDecimal source. * The source may not send nulls. *

Note that it uses forced cast of this sequence. If T != BigDecimal this * method is guaranteed to throw ClassCastException.

* @return the observable for the average value */ @SuppressWarnings("unchecked") public final Ix averageBigDecimal() { return from(Interactive.averageBigDecimal((Iterable)it)); } /** * Computes and signals the average value of the BigInteger source. * The source may not send nulls. *

Note that it uses forced cast of this sequence. If T != BigInteger this * method is guaranteed to throw ClassCastException.

* @return the observable for the average value */ @SuppressWarnings("unchecked") public final Ix averageBigInteger() { return from(Interactive.averageBigInteger((Iterable)it)); } /** * Computes and signals the average value of the Double source. * The source may not send nulls. * @return the observable for the average value */ @SuppressWarnings("unchecked") public final Ix averageDouble() { return from(Interactive.averageDouble((Iterable)it)); } /** * Computes and signals the average value of the Float source. * The source may not send nulls. * @return the observable for the average value */ @SuppressWarnings("unchecked") public final Ix averageFloat() { return from(Interactive.averageFloat((Iterable)it)); } /** * Computes and signals the average value of the integer source. * The source may not send nulls. * The intermediate aggregation used double values. * @return the observable for the average value */ @SuppressWarnings("unchecked") public final Ix averageInt() { return from(Interactive.averageInt((Iterable)it)); } /** * Computes and signals the average value of the Long source. * The source may not send nulls. * The intermediate aggregation used double values. * @return the observable for the average value */ @SuppressWarnings("unchecked") public final Ix averageLong() { return from(Interactive.averageLong((Iterable)it)); } /** * Returns an iterable which buffers the source elements * into bufferSize lists. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param bufferSize the buffer size. * @return the new iterable */ public final Ix> buffer(int bufferSize) { return from(Interactive.buffer(it, bufferSize)); } /** * Collects all values from this sequence into a custom collection via the collector * action and returns the collection as a single element. * @param collectionSupplier the function that creates a collection for individual iterations * @param collector the action that is called by the collection instance and the current * element in the sequence * @return the new iterable * @since 0.92.3 */ public final Ix collect(Func0 collectionSupplier, Action2 collector) { return from(new CollectIterable(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 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> 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 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 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 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> 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 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 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 keySelector, final Func1 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 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 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 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 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 keySelector, Comparator 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 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 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 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 keySelector, Comparator 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 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 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 keySelector, Comparator 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, ? 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, ? extends Iterable> 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, ? 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, ? 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, ? 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 Ix scan(final Func2 aggregator) { return from(Interactive.scan(it, aggregator)); } /** * 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. *

The returned iterator forwards all remove() calls * to the source.

* @param the destination element type * @param seed the initial value of the running aggregation * @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 Ix scan(final U seed, final Func2 aggregator) { return from(Interactive.scan(it, seed, aggregator)); } /** * Returns an iterable which ensures the source iterable is * only traversed once and clients may take values from each other, * e.g., they share the same iterator. * @return the new iterable */ public final Ix share() { return from(Interactive.share(it)); } /** * Skips the specified amount of items at the beginning of the source sequence. * @param num the number of elements to skip * @return the new iterable */ public final Ix skip(final int num) { return from(Interactive.skip(it, num)); } /** * Returns an iterable which skips the last num elements from the * source iterable. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param num the number of elements to skip at the end * @return the new iterable */ public final Ix skipLast(final int num) { return from(Interactive.skipLast(it, num)); } /** * Returns an iterable which prefixes the source iterable values * by a constant. * It is equivalent to concat(singleton(value), source). *

The returned iterator will throw an UnsupportedOperationException * for its remove() method for the first element, and might * throw for subsequent elements, depending on the source iterable.

* @param value the value to prefix * @return the new iterable. */ @SuppressWarnings("unchecked") public final Ix startWith(T value) { return from(Interactive.startWith(it, value)); } /** * Returns each pair of subsequent elements as pairs. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @return the iterable builder */ public final Ix> subsequent() { return from(Interactive.subsequent(it)); } /** * Returns each pair of subsequent elements as pairs. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param count the number of subsequent elements * @return the iterable builder */ public final Ix> subsequent(int count) { return from(Interactive.subsequent(it, count)) .map(Ix.toBuilder()); } /** * Computes and signals the sum of the values of the BigDecimal source. * The source may not send nulls. * @return the observable for the sum value */ @SuppressWarnings("unchecked") public final Ix sumBigDecimal() { return from(Interactive.sumBigDecimal((Iterable)it)); } /** * Computes and signals the sum of the values of the BigInteger source. * The source may not send nulls. * @return the observable for the sum value */ @SuppressWarnings("unchecked") public final Ix sumBigInteger() { return from(Interactive.sumBigInteger((Iterable)it)); } /** * Computes and signals the sum of the values of the Double source. * The source may not send nulls. * @return the observable for the sum value */ @SuppressWarnings("unchecked") public final Ix sumDouble() { return from(Interactive.sumDouble((Iterable)it)); } /** * Computes and signals the sum of the values of the Float source. * The source may not send nulls. * @return the observable for the sum value */ @SuppressWarnings("unchecked") public final Ix sumFloat() { return from(Interactive.sumFloat((Iterable)it)); } /** * Computes and signals the sum of the values of the Integer source. * The source may not send nulls. An empty source produces an empty sum * @return the observable for the sum value */ @SuppressWarnings("unchecked") public final Ix sumInt() { return from(Interactive.sumInt((Iterable)it)); } /** * Computes and signals the sum of the values of the Integer source by using * a double intermediate representation. * The source may not send nulls. An empty source produces an empty sum * @return the observable for the sum value */ @SuppressWarnings("unchecked") public final Ix sumIntAsDouble() { return from(Interactive.sumIntAsDouble((Iterable)it)); } /** * Computes and signals the sum of the values of the Long source. * The source may not send nulls. * @return the observable for the sum value */ @SuppressWarnings("unchecked") public final Ix sumLong() { return from(Interactive.sumLong((Iterable)it)); } /** * Computes and signals the sum of the values of the Long sourceby using * a double intermediate representation. * The source may not send nulls. * @return the observable for the sum value */ @SuppressWarnings("unchecked") public final Ix sumLongAsDouble() { return from(Interactive.sumLongAsDouble((Iterable)it)); } /** * Returns the iterable which returns the first num element. * from the source iterable. *

The returned iterator forwards all remove() calls * to the source.

* @param num the number of items to take * @return the new iterable */ public final Ix take(int num) { return from(Interactive.take(it, num)); } /** * Returns an iterable which takes only the last num elements from the * source iterable. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param num the number of elements to skip at the end * @return the new iterable */ public final Ix takeLast(int num) { return from(Interactive.takeLast(it, num)); } /** * Returns an object array of all elements in this * iterable. * @return the object array */ public final Object[] toArray() { return toList().toArray(); } /** * Returns all elements from this iterable into either * the given array or a new array if the size requires. * @param a the output array * @return the output array */ public final T[] toArray(T[] a) { return toList().toArray(a); } /** * Convinience method to create a hashmap from the elements. * @param the key type * @param keySelector the key selector * @return the map */ public final Map toHashMap(Func1 keySelector) { return toMap(keySelector, IxHelperFunctions.identity(), IxHelperFunctions.hashMapProvider()); } /** * Convinience method to create a hash-multimap with list from the elements. * @param the key type * @param keySelector the key selector * @return the multimap */ public final Map> toHashMultimap(Func1 keySelector) { return toMultimap( keySelector, IxHelperFunctions.identity(), IxHelperFunctions.>hashMapProvider(), IxHelperFunctions.arrayListProvider()); } /** * Iterates over and returns all elements in a list. * @return the list of the values from this iterable */ public final List toList() { List result = new ArrayList(); into(result); return result; } /** * Convert the iterable values into a map representation. *

If an element maps to the same key, the existing value will be overwritten.

*

See Functions.hashMapProvider() and others for some standard map implementations.

* @param the key type * @param keySelector the function to extract a key from an element * @param mapProvider the map provider * @return the filled-in map. */ public final Map toMap( Func1 keySelector, Func0> mapProvider) { return toMap(keySelector, IxHelperFunctions.identity(), mapProvider); } /** * Convert the iterable values into a map representation. *

If an element maps to the same key, the existing value will be overwritten.

* @param the key type * @param the value type * @param keySelector the function to extract a key from an element * @param valueSelector the function to extract a value from an element * @param mapProvider the map provider * @return the filled-in map. */ public final Map toMap( Func1 keySelector, Func1 valueSelector, Func0> mapProvider) { Map map = mapProvider.call(); Iterator it = iterator(); try { while (it.hasNext()) { T t = it.next(); K key = keySelector.call(t); V value = valueSelector.call(t); map.put(key, value); } } finally { Interactive.unsubscribe(it); } return map; } /** * Convert the values into a multimap representation where each * key can have multiple values. *

See Functions.hashMapProvider(), Functions.arrayListProvider() * and others for some standard map implementations.

* @param the key type * @param the value type * @param the collection type * @param keySelector the key selector * @param valueSelector the value selector * @param mapProvider the provider for the base map * @param collectionProvider the provider for the value collection * @return the multimap */ public final > Map toMultimap( Func1 keySelector, Func1 valueSelector, Func0> mapProvider, Func0 collectionProvider) { Map result = mapProvider.call(); Iterator it = iterator(); try { while (it.hasNext()) { T t = it.next(); K key = keySelector.call(t); V value = valueSelector.call(t); C coll = result.get(key); if (coll == null) { coll = collectionProvider.call(); result.put(key, coll); } coll.add(value); } } finally { Interactive.unsubscribe(it); } return result; } /** * Converts this iterable into an observable builder * which uses the default scheduler of {@link rx.Observable} to emit values. * @return the observable builder */ public final Observable toObservable() { return Observable.from(it); } /** * Converts this iterable into an observable builder * which uses the supplied Scheduler to emit values. * @param scheduler the scheduler * @return the observable builder */ public final Observable toObservable(Scheduler scheduler) { return Observable.from(it).subscribeOn(scheduler); } /** * Pairs each element from this and the oher iterable source and * combines them into a new value by using the combiner * function. *

The returned iterator will throw an UnsupportedOperationException * for its remove() method.

* @param the right source type * @param the result type * @param right the right source * @param combiner the combiner function * @return the new iterable */ public final Ix zipWith(final Iterable right, final Func2 combiner) { return from(Interactive.zip(it, right, combiner)); } }