diff --git a/src/main/java/ix/Ix.java b/src/main/java/ix/Ix.java index 82c9b64..945c040 100644 --- a/src/main/java/ix/Ix.java +++ b/src/main/java/ix/Ix.java @@ -552,8 +552,7 @@ public static Ix zip(Iterable> sources } /** - * Combines the next element from each source Iterable, provided as an Iterable itself, - * via a zipper function. + * Combines the next element from each source Iterable via a zipper function. *

* If one of the source Iterables is sorter the sequence terminates eagerly. *

@@ -577,8 +576,7 @@ public static Ix zip( } /** - * Combines the next element from each source Iterable, provided as an Iterable itself, - * via a zipper function. + * Combines the next element from each source Iterable via a zipper function. *

* If one of the source Iterables is sorter the sequence terminates eagerly. *

@@ -605,8 +603,7 @@ public static Ix zip( } /** - * Combines the next element from each source Iterable, provided as an Iterable itself, - * via a zipper function. + * Combines the next element from each source Iterable via a zipper function. *

* If one of the source Iterables is sorter the sequence terminates eagerly. *

@@ -987,6 +984,8 @@ public final Ix endWith(T... values) { * @return the new Ix instance * @throws NullPointerException if other is null * @since 1.0 + * @see #union(Iterable) + * @see #intersect(Iterable) */ public final Ix except(Iterable other) { return new IxExcept(this, nullCheck(other, "other is null")); @@ -1099,6 +1098,8 @@ public final Ix hide() { * @return the new Ix instance * @throws NullPointerException if other is null * @since 1.0 + * @see #except(Iterable) + * @see #union(Iterable) */ public final Ix intersect(Iterable other) { return new IxIntersect(this, nullCheck(other, "other is null")); @@ -1585,11 +1586,11 @@ public final Ix replay(int size) { * @param transform the function receiving a view into the cache and returns an Iterable sequence whose * elements will be emitted by this Ix. * @return the new Ix instance - * @throws NullPointerException + * @throws NullPointerException if transform is null * @since 1.0 */ public final Ix replay(Func1, ? extends Iterable> transform) { - return new IxReplaySelector(this, nullCheck(transform, "transformer is null")); + return new IxReplaySelector(this, nullCheck(transform, "transform is null")); } /** @@ -1605,7 +1606,8 @@ public final Ix replay(Func1, ? extends Iterable Ix replay(int size, Func1, ? extends Iterable> transform) { @@ -1765,94 +1767,317 @@ public final Ix startWith(T... values) { return concat(fromArray(values), this); } + /** + * Sums values of this sequence as integer. + *

+ * The operation may throw a ClassCastException if any of the elements + * is not an Integer. + *

+ * An empty sequence yields an empty sum. + *

+ * The result's iterator() doesn't support remove(). + * @return the new Ix instance + * @since 1.0 + */ @SuppressWarnings("unchecked") public final Ix sumInt() { return new IxSumInt((Ix)this); } + /** + * Sums values of this sequence as long. + *

+ * The operation may throw a ClassCastException if any of the elements + * is not an Long. + *

+ * An empty sequence yields an empty sum. + *

+ * The result's iterator() doesn't support remove(). + * @return the new Ix instance + * @since 1.0 + */ @SuppressWarnings("unchecked") public final Ix sumLong() { return new IxSumLong((Ix)this); } + /** + * Emits the elements of the other sequence if this sequence is empty. + *

+ * The result's Iterator forwards calls of remove() to this' or the other's Iterator. + * @param other the other Iterable instance, not null + * @return the new Ix instance + * @throws NullPointerException if other is null + * @since 1.0 + */ public final Ix switchIfEmpty(Iterable other) { - return new IxSwitchIfEmpty(this, other); + return new IxSwitchIfEmpty(this, nullCheck(other, "other is null")); } + /** + * Emits the first n elements (or less) of this sequence. + *

+ * The result's Iterator forwards calls of remove() to this' Iterator. + * @param n the number of items to emit at most, non-negative + * @return the new Ix instance + * @throws IllegalArgumentException if n is negative + * @since 1.0 + * @see #skip(int) + */ public final Ix take(int n) { - return new IxTake(this, n); + return new IxTake(this, nonNegative(n, "n")); } + /** + * Emits the last n elements (or fewer) of this sequence. + *

+ * The result's iterator() doesn't support remove(). + * @param n the number of last elements to emit + * @return the new Ix instance + * @throws IllegalArgumentException if n is negative + * @since 1.0 + * @see #skipLast(int) + */ public final Ix takeLast(int n) { - return new IxTakeLast(this, n); + return new IxTakeLast(this, nonNegative(n, "n")); } + /** + * Take elements from this sequence while the given predicate returns true + * for the current element (checked after emission of that element). + *

+ * The result's Iterator forwards calls of remove() to this' Iterator. + * @param stopPredicate the function receiving the current element and returns + * true if no further elements should be emitted after this element. + * @return the new Ix instance + * @throws NullPointerException if stopPredicate is null + * @since 1.0 + */ public final Ix takeUntil(Pred stopPredicate) { - return new IxTakeUntil(this, stopPredicate); + return new IxTakeUntil(this, nullCheck(stopPredicate, "stopPredicate is null")); } + /** + * Take elements from this sequence until the given predicate returns true + * for the current element (checked before emission of that element). + *

+ * The result's Iterator forwards calls of remove() to this' Iterator. + * @param predicate the function receiving the current element and returns + * false if no further elements should be emitted (not even the current). + * @return the new Ix instance + * @throws NullPointerException if predicate is null + * @since 1.0 + */ public final Ix takeWhile(Pred predicate) { - return new IxTakeWhile(this, predicate); + return new IxTakeWhile(this, nullCheck(predicate, "predicate is null")); } + /** + * Collects the elements of this sequence into an Object array. + *

+ * The result's iterator() doesn't support remove(). + * @return the new Ix instance + * @since 1.0 + */ public final Ix toArray() { return collect(ToListHelper.initialFactory(), ToListHelper.collector()) .map(ToListHelper.toArray()); } + /** + * Collects the elements of this sequence into a List. + *

+ * The result's iterator() doesn't support remove(). + * @return the new Ix instance + * @since 1.0 + */ public final Ix> toList() { return collect(ToListHelper.initialFactory(), ToListHelper.collector()); } + /** + * Maps this sequence of numbers into a sequence of longs. + *

+ * The sequence may throw a ClassCastException if any of the elements + * is not a subclass of Number. + *

+ * The result's Iterator forwards calls of remove() to this' Iterator. + * @return the new Ix instance + * @since 1.0 + */ @SuppressWarnings("unchecked") public final Ix toLong() { return ((Ix)this).map(NumberToLongHelper.INSTANCE); } + /** + * Collects the elements of this sequence into a Map where the key is + * determined from each element via the keySelector function; duplicates are + * overwritten. + *

+ * The result's iterator() doesn't support remove(). + * @param the key type + * @param keySelector the function that receives the current element and returns + * a key for it to be used as the Map key. + * @return the new Ix instance + * @throws NullPointerException if keySelector is null + * @since 1.0 + */ public final Ix> toMap(Func1 keySelector) { Func1 f = IdentityHelper.instance(); return this.toMap(keySelector, f); } + /** + * Collects the elements of this sequence into a Map where the key is + * determined from each element via the keySelector function and + * the value is derived from the same element via the valueSelector function; duplicates are + * overwritten. + *

+ * The result's iterator() doesn't support remove(). + * @param the key type + * @param the value type + * @param keySelector the function that receives the current element and returns + * a key for it to be used as the Map key. + * @param valueSelector the function that receives the current element and returns + * a value for it to be used as the Map value + * @return the new Ix instance + * @throws NullPointerException if keySelector or valueSelector is null + * @since 1.0 + */ public final Ix> toMap(Func1 keySelector, Func1 valueSelector) { - return new IxToMap(this, keySelector, valueSelector); + return new IxToMap(this, nullCheck(keySelector, "keySelector is null"), nullCheck(valueSelector, "valueSelector is null")); } + /** + * Collects the elements of this sequence into a multi-Map where the key is + * determined from each element via the keySelector function. + *

+ * The result's iterator() doesn't support remove(). + * @param the key type + * @param keySelector the function that receives the current element and returns + * a key for it to be used as the Map key. + * @return the new Ix instance + * @throws NullPointerException if keySelector is null + * @since 1.0 + */ public final Ix>> toMultimap(Func1 keySelector) { Func1 f = IdentityHelper.instance(); return this.toMultimap(keySelector, f); } + /** + * Collects the elements of this sequence into a multi-Map where the key is + * determined from each element via the keySelector function and + * the value is derived from the same element via the valueSelector function. + *

+ * The result's iterator() doesn't support remove(). + * @param the key type + * @param the value type + * @param keySelector the function that receives the current element and returns + * a key for it to be used as the Map key. + * @param valueSelector the function that receives the current element and returns + * a value for it to be used as the Map value + * @return the new Ix instance + * @throws NullPointerException if keySelector or valueSelector is null + * @since 1.0 + */ public final Ix>> toMultimap(Func1 keySelector, Func1 valueSelector) { return new IxToMultimap(this, keySelector, valueSelector); } + /** + * Collects the elements of this sequence into a Set. + *

+ * The result's iterator() doesn't support remove(). + * @return the new Ix instance + * @since 1.0 + */ public final Ix> toSet() { return new IxToSet(this); } + /** + * Allows working with the Iterator of this sequence and emit elements in + * a more flexible way + *

+ * The result's iterator() doesn't support remove(). + * @param the result value type + * @param transformer the functional interface whose moveNext is called with the + * current Iterator and should signal a value to be emitted for it. + * @return the new Ix instance + * @throws NullPointerException if transformer is null + */ public final Ix transform(IxTransform transformer) { - return new IxTransformer(this, transformer); + return new IxTransformer(this, nullCheck(transformer, "transformer is null")); } + /** + * Emits a distinct set of values from both this and the other sequence. + *

+ * The result's iterator() doesn't support remove(). + * @param other the other Iterable sequence, not null + * @return the new Ix instance + * @throws NullPointerException if other is null + * @since 1.0 + * @see #except(Iterable) + * @see #intersect(Iterable) + */ public final Ix union(Iterable other) { - return new IxUnion(this, other); + return new IxUnion(this, nullCheck(other, "other is null")); } + /** + * Emits inner Ix Iterables of non-overapping sequences mapped from this sequence + * with the given maximum size each. + * + *

+ * The result's and the inner Ix' iterator() don't support remove(). + * @param size the maximum size of the inner windows, positive + * @return the new Ix instance + * @throws IllegalArgumentException if size is non-positive + * @since 1.0 + * @see #window(int, int) + */ public final Ix> window(int size) { - return new IxWindow(this, size); + return new IxWindow(this, positive(size, "size")); } + /** + * Emits inner Ix Iterables of potentially overlapping sequences mapped from this + * sequence with the given maximum size each and started each {@code skip} source elements. + * @param size the maximum size of the inner windows, positive + * @param skip after how many elements to start a new window (repeatedly), positive + * @return the new Ix Iinstance + * @throws IllegalArgumentException if size or skip is non-positive + * @since 1.0 + * @see #window(int) + */ public final Ix> window(int size, int skip) { if (size == skip) { return window(size); } if (size < skip) { - return new IxWindowSkip(this, size, skip); + return new IxWindowSkip(this, positive(size, "size"), positive(skip, "skip")); } - return new IxWindowOverlap(this, size, skip); + return new IxWindowOverlap(this, positive(size, "size"), positive(skip, "skip")); } + /** + * Combines the next element from this and the other source Iterable via a zipper function. + *

+ * If one of the source Iterables is sorter the sequence terminates eagerly. + *

+ * The result's iterator() doesn't support remove(). + * + * @param the other source's element type + * @param the result value type + * @param other the the other source Iterable + * @param zipper the function that takesone from each source, not null + * @return the new Ix instance + * @throws NullPointerException if other or zipper is null + * @since 1.0 + */ public final Ix zipWith(Iterable other, Func2 zipper) { return zip(this, other, zipper); } @@ -1918,7 +2143,7 @@ public final void foreach(Action1 action) { * return true to continue the loop or false to quit the loop. * @throws NullPointerException if action is null * @since 1.0 - * @see #foreach(Pred) + * @see #foreach(Action1) */ public final void foreachWhile(Pred predicate) { for (T t : this) {