From 3639b87bd62f39b936c580c2a0e9db083fab8d31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Karnok?= Date: Fri, 15 Jul 2016 22:25:19 +0200 Subject: [PATCH] Javadoc part 2 --- src/main/java/ix/Ix.java | 779 +++++++++++++++++++++++---------------- 1 file changed, 461 insertions(+), 318 deletions(-) diff --git a/src/main/java/ix/Ix.java b/src/main/java/ix/Ix.java index 6bfeb09..508093a 100644 --- a/src/main/java/ix/Ix.java +++ b/src/main/java/ix/Ix.java @@ -635,48 +635,57 @@ public static Ix zip( //--------------------------------------------------------------------------------------- // Instance operators //--------------------------------------------------------------------------------------- - - public final R as(Func1, R> transformer) { - return transformer.call(this); - } - - public final Ix compose(Func1, ? extends Iterable> transformer) { - return new IxCompose(this, transformer); + + public final Ix all(Pred predicate) { + return new IxAll(this, predicate); } - public final Ix any(Pred predicate) { return new IxAny(this, predicate); } + public final R as(Func1, R> transformer) { + return transformer.call(this); + } - public final Ix all(Pred predicate) { - return new IxAll(this, predicate); + @SuppressWarnings("unchecked") + public final Ix averageFloat() { + return new IxAverageFloat((Iterable)this); + } + + @SuppressWarnings("unchecked") + public final Ix averageDouble() { + return new IxAverageDouble((Iterable)this); } - public final Ix hasElements() { - return new IxHasElements(this); + public final Ix> buffer(int size) { + return new IxBuffer(this, size); } - public final Ix ignoreElements() { - return new IxIgnoreElements(this); + public final Ix> buffer(int size, int skip) { + if (size == skip) { + return buffer(size); + } + if (size < skip) { + return new IxBufferSkip(this, size, skip); + } + return new IxBufferOverlap(this, size, skip); } - public final Ix max(Comparator comparator) { - return new IxMinMax(this, comparator, -1); + public final Ix collect(Func0 initialFactory, Action2 collector) { + return new IxCollect(this, initialFactory, collector); } - @SuppressWarnings({ "rawtypes", "unchecked" }) - public final Ix max() { - return max((Comparator)SelfComparator.INSTANCE); + public final Ix compose(Func1, ? extends Iterable> transformer) { + return new IxCompose(this, transformer); } - - public final Ix min(Comparator comparator) { - return new IxMinMax(this, comparator, 1); + + public final Ix concatMap(Func1> mapper) { + return new IxFlattenIterable(this, mapper); } - - @SuppressWarnings({ "rawtypes", "unchecked" }) - public final Ix min() { - return min((Comparator)SelfComparator.INSTANCE); + + @SuppressWarnings("unchecked") + public final Ix concatWith(Iterable other) { + return concatArray(this, other); } public final Ix contains(Object o) { @@ -690,6 +699,10 @@ public final Ix count() { public final Ix countLong() { return new IxCountLong(this); } + + public final Ix defaultIfEmpty(T value) { + return switchIfEmpty(Ix.just(value)); + } public final Ix distinct() { return distinct(IdentityHelper.instance()); @@ -718,71 +731,77 @@ public final Ix doOnNext(Action1 action) { public final Ix doOnCompleted(Action0 action) { return new IxDoOn(this, IxEmptyAction.instance1(), action); } - - @SuppressWarnings("unchecked") - public final Ix startWith(T... values) { - return concatArray(fromArray(values), this); - } @SuppressWarnings("unchecked") public final Ix endWith(T... values) { return concatArray(this, fromArray(values)); } - public final Ix join() { - return join(", "); + public final Ix except(Iterable other) { + return new IxExcept(this, other); } - public final Ix join(CharSequence separator) { - return new IxJoin(this, separator); + public final Ix filter(Pred predicate) { + return new IxFilter(this, predicate); } - public final Ix map(Func1 mapper) { - return new IxMap(this, mapper); + public final Ix flatMap(Func1> mapper) { + return new IxFlattenIterable(this, mapper); } - public final Ix filter(Pred predicate) { - return new IxFilter(this, predicate); + public final Ix> groupBy(Func1 keySelector) { + return groupBy(keySelector, IdentityHelper.instance()); } - - public final Ix collect(Func0 initialFactory, Action2 collector) { - return new IxCollect(this, initialFactory, collector); + + public final Ix> groupBy(Func1 keySelector, + Func1 valueSelector) { + return new IxGroupBy(this, keySelector, valueSelector); } - public final Ix reduce(Func0 initialFactory, Func2 reducer) { - return new IxReduce(this, initialFactory, reducer); + public final Ix hasElements() { + return new IxHasElements(this); } public final Ix hide() { return new IxWrapper(this); } - public final Ix> toList() { - return collect(ToListHelper.initialFactory(), ToListHelper.collector()); + public final Ix intersect(Iterable other) { + return new IxIntersect(this, other); } - public final Ix toArray() { - return collect(ToListHelper.initialFactory(), ToListHelper.collector()) - .map(ToListHelper.toArray()); + public final Ix ignoreElements() { + return new IxIgnoreElements(this); } - public final Ix reduce(Func2 reducer) { - return new IxAggregate(this, reducer); + public final Ix join() { + return join(", "); } - @SuppressWarnings("unchecked") - public final Ix maxInt() { - return new IxMaxInt((Ix)this); + public final Ix join(CharSequence separator) { + return new IxJoin(this, separator); } - - @SuppressWarnings("unchecked") - public final Ix minInt() { - return new IxMinInt((Ix)this); + + public final Ix lift(Func1, ? extends Iterator> lifter) { + return new IxLift(this, lifter); + } + + public final Ix map(Func1 mapper) { + return new IxMap(this, mapper); + } + + public final Ix max(Comparator comparator) { + return new IxMinMax(this, comparator, -1); } + @SuppressWarnings({ "rawtypes", "unchecked" }) + public final Ix max() { + return max((Comparator)SelfComparator.INSTANCE); + } + @SuppressWarnings("unchecked") - public final Ix sumInt() { - return new IxSumInt((Ix)this); + public final Ix maxInt() { + return new IxMaxInt((Ix)this); } @SuppressWarnings("unchecked") @@ -790,84 +809,73 @@ public final Ix maxLong() { return new IxMaxLong((Ix)this); } - @SuppressWarnings("unchecked") - public final Ix minLong() { - return new IxMinLong((Ix)this); + public final Ix mergeWith(Iterable other) { + return concatWith(other); } - @SuppressWarnings("unchecked") - public final Ix sumLong() { - return new IxSumLong((Ix)this); - } - - @SuppressWarnings("unchecked") - public final Ix toLong() { - return ((Ix)this).map(NumberToLongHelper.INSTANCE); + public final Ix min(Comparator comparator) { + return new IxMinMax(this, comparator, 1); } - public final Ix skip(int n) { - if (n == 0) { - return this; - } - return new IxSkip(this, n); + @SuppressWarnings({ "rawtypes", "unchecked" }) + public final Ix min() { + return min((Comparator)SelfComparator.INSTANCE); } - public final Ix take(int n) { - return new IxTake(this, n); + @SuppressWarnings("unchecked") + public final Ix minInt() { + return new IxMinInt((Ix)this); } - public final Ix skipLast(int n) { - if (n == 0) { - return this; - } - return new IxSkipLast(this, n); + @SuppressWarnings("unchecked") + public final Ix minLong() { + return new IxMinLong((Ix)this); } - public final Ix takeLast(int n) { - return new IxTakeLast(this, n); + @SuppressWarnings({ "unchecked", "rawtypes" }) + public final Ix orderBy() { + return orderBy((Comparator)SelfComparator.INSTANCE); } - public final Ix flatMap(Func1> mapper) { - return new IxFlattenIterable(this, mapper); + public final Ix orderBy(Comparator comparator) { + return new IxOrderBy(this, IdentityHelper.instance(), comparator, 1); } - public final Ix concatMap(Func1> mapper) { - return new IxFlattenIterable(this, mapper); + public final > Ix orderBy(Func1 keySelector) { + return new IxOrderBy(this, keySelector, SelfComparator.INSTANCE, 1); } - public final Ix skipWhile(Pred predicate) { - return new IxSkipWhile(this, predicate); + @SuppressWarnings({ "unchecked", "rawtypes" }) + public final Ix orderByReverse() { + return orderByReverse((Comparator)SelfComparator.INSTANCE); } - public final Ix takeWhile(Pred predicate) { - return new IxTakeWhile(this, predicate); + public final Ix orderByReverse(Comparator comparator) { + return new IxOrderBy(this, IdentityHelper.instance(), comparator, -1); } - public final Ix takeUntil(Pred stopPredicate) { - return new IxTakeUntil(this, stopPredicate); + public final > Ix orderByReverse(Func1 keySelector) { + return new IxOrderBy(this, keySelector, SelfComparator.INSTANCE, -1); } - - public final Ix> buffer(int size) { - return new IxBuffer(this, size); + + public final Ix publish() { + return new IxPublish(this); + } + + public final Ix publish(Func1, ? extends Iterable> transform) { + return new IxPublishSelector(this, transform); } - public final Ix> buffer(int size, int skip) { - if (size == skip) { - return buffer(size); - } - if (size < skip) { - return new IxBufferSkip(this, size, skip); - } - return new IxBufferOverlap(this, size, skip); + public final Ix reduce(Func2 reducer) { + return new IxAggregate(this, reducer); } - public final Ix> groupBy(Func1 keySelector) { - return groupBy(keySelector, IdentityHelper.instance()); + public final Ix reduce(Func0 initialFactory, Func2 reducer) { + return new IxReduce(this, initialFactory, reducer); } - - public final Ix> groupBy(Func1 keySelector, - Func1 valueSelector) { - return new IxGroupBy(this, keySelector, valueSelector); + + public final Ix remove(Pred predicate) { + return new IxRemove(this, predicate); } public final Ix repeat() { @@ -886,14 +894,6 @@ public final Ix repeat(long times, Pred0 predicate) { return concat(repeatValue(this, times, predicate)); } - public final Ix publish() { - return new IxPublish(this); - } - - public final Ix publish(Func1, ? extends Iterable> transform) { - return new IxPublishSelector(this, transform); - } - public final Ix replay() { return new IxReplay(this); } @@ -909,152 +909,158 @@ public final Ix replay(Func1, ? extends Iterable Ix replay(int size, Func1, ? extends Iterable> transform) { return new IxReplaySizeSelector(this, size, transform); } + + public final Ix retain(Pred predicate) { + return new IxRetain(this, predicate); + } - public final Ix> toMap(Func1 keySelector) { - Func1 f = IdentityHelper.instance(); - return this.toMap(keySelector, f); + public final Ix reverse() { + return new IxReverse(this); } - public final Ix> toMap(Func1 keySelector, Func1 valueSelector) { - return new IxToMap(this, keySelector, valueSelector); + public final Ix scan(Func2 scanner) { + return new IxScan(this, scanner); } - public final Ix>> toMultimap(Func1 keySelector) { - Func1 f = IdentityHelper.instance(); - return this.toMultimap(keySelector, f); + public final Ix scan(Func0 initialFactory, Func2 scanner) { + return new IxScanSeed(this, initialFactory, scanner); } - - public final Ix>> toMultimap(Func1 keySelector, Func1 valueSelector) { - return new IxToMultimap(this, keySelector, valueSelector); + + public final Ix sequenceEqual(Iterable other) { + return sequenceEqual(other, EqualityHelper.INSTANCE); } - public final Ix> window(int size) { - return new IxWindow(this, size); + public final Ix sequenceEqual(Iterable other, Pred2 comparer) { + return new IxSequenceEqual(this, other, comparer); } - public final Ix> window(int size, int skip) { - if (size == skip) { - return window(size); - } - if (size < skip) { - return new IxWindowSkip(this, size, skip); + public final Ix skip(int n) { + if (n == 0) { + return this; } - return new IxWindowOverlap(this, size, skip); + return new IxSkip(this, n); } - - @SuppressWarnings("unchecked") - public final Ix concatWith(Iterable other) { - return concatArray(this, other); + + public final Ix skipLast(int n) { + if (n == 0) { + return this; + } + return new IxSkipLast(this, n); } - public final Ix mergeWith(Iterable other) { - return concatWith(other); + public final Ix skipWhile(Pred predicate) { + return new IxSkipWhile(this, predicate); } - public final Ix zipWith(Iterable other, Func2 zipper) { - return zip(this, other, zipper); + @SuppressWarnings("unchecked") + public final Ix startWith(T... values) { + return concatArray(fromArray(values), this); } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - public final Ix orderBy() { - return orderBy((Comparator)SelfComparator.INSTANCE); + + @SuppressWarnings("unchecked") + public final Ix sumInt() { + return new IxSumInt((Ix)this); } - - public final Ix orderBy(Comparator comparator) { - return new IxOrderBy(this, IdentityHelper.instance(), comparator, 1); + + @SuppressWarnings("unchecked") + public final Ix sumLong() { + return new IxSumLong((Ix)this); } - public final > Ix orderBy(Func1 keySelector) { - return new IxOrderBy(this, keySelector, SelfComparator.INSTANCE, 1); + public final Ix switchIfEmpty(Iterable other) { + return new IxSwitchIfEmpty(this, other); } - @SuppressWarnings({ "unchecked", "rawtypes" }) - public final Ix orderByReverse() { - return orderByReverse((Comparator)SelfComparator.INSTANCE); + public final Ix take(int n) { + return new IxTake(this, n); } - public final Ix orderByReverse(Comparator comparator) { - return new IxOrderBy(this, IdentityHelper.instance(), comparator, -1); + public final Ix takeLast(int n) { + return new IxTakeLast(this, n); } - public final > Ix orderByReverse(Func1 keySelector) { - return new IxOrderBy(this, keySelector, SelfComparator.INSTANCE, -1); - } - - public final Ix scan(Func2 scanner) { - return new IxScan(this, scanner); - } - - public final Ix scan(Func0 initialFactory, Func2 scanner) { - return new IxScanSeed(this, initialFactory, scanner); + public final Ix takeUntil(Pred stopPredicate) { + return new IxTakeUntil(this, stopPredicate); } - public final Ix remove(Pred predicate) { - return new IxRemove(this, predicate); - } - - public final Ix retain(Pred predicate) { - return new IxRetain(this, predicate); + public final Ix takeWhile(Pred predicate) { + return new IxTakeWhile(this, predicate); } - - public final Ix transform(IxTransform transformer) { - return new IxTransformer(this, transformer); + + public final Ix toArray() { + return collect(ToListHelper.initialFactory(), ToListHelper.collector()) + .map(ToListHelper.toArray()); } - public final Ix lift(Func1, ? extends Iterator> lifter) { - return new IxLift(this, lifter); + public final Ix> toList() { + return collect(ToListHelper.initialFactory(), ToListHelper.collector()); } @SuppressWarnings("unchecked") - public final Ix averageFloat() { - return new IxAverageFloat((Iterable)this); + public final Ix toLong() { + return ((Ix)this).map(NumberToLongHelper.INSTANCE); } - - @SuppressWarnings("unchecked") - public final Ix averageDouble() { - return new IxAverageDouble((Iterable)this); + + public final Ix> toMap(Func1 keySelector) { + Func1 f = IdentityHelper.instance(); + return this.toMap(keySelector, f); } - public final Ix defaultIfEmpty(T value) { - return switchIfEmpty(Ix.just(value)); + public final Ix> toMap(Func1 keySelector, Func1 valueSelector) { + return new IxToMap(this, keySelector, valueSelector); } - - public final Ix switchIfEmpty(Iterable other) { - return new IxSwitchIfEmpty(this, other); + + public final Ix>> toMultimap(Func1 keySelector) { + Func1 f = IdentityHelper.instance(); + return this.toMultimap(keySelector, f); } - - public final Ix except(Iterable other) { - return new IxExcept(this, other); + + public final Ix>> toMultimap(Func1 keySelector, Func1 valueSelector) { + return new IxToMultimap(this, keySelector, valueSelector); } - public final Ix intersect(Iterable other) { - return new IxIntersect(this, other); + public final Ix> toSet() { + return new IxToSet(this); } - - public final Ix reverse() { - return new IxReverse(this); + + public final Ix transform(IxTransform transformer) { + return new IxTransformer(this, transformer); } - public final Ix sequenceEqual(Iterable other) { - return sequenceEqual(other, EqualityHelper.INSTANCE); + public final Ix union(Iterable other) { + return new IxUnion(this, other); } - public final Ix sequenceEqual(Iterable other, Pred2 comparer) { - return new IxSequenceEqual(this, other, comparer); + public final Ix> window(int size) { + return new IxWindow(this, size); } - - public final Ix> toSet() { - return new IxToSet(this); + + 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 IxWindowOverlap(this, size, skip); } - public final Ix union(Iterable other) { - return new IxUnion(this, other); + public final Ix zipWith(Iterable other, Func2 zipper) { + return zip(this, other, zipper); } //--------------------------------------------------------------------------------------- // Leaving the Iterable world //--------------------------------------------------------------------------------------- + /** + * Returns the first element of this sequence. + * @return the first element + * @throws NoSuchElementException if this sequence is empty + * @since 1.0 + * @see #first(Object) + * @see #last(Object) + */ @SuppressWarnings("unchecked") public final T first() { if (this instanceof Callable) { @@ -1063,6 +1069,15 @@ public final T first() { return iterator().next(); } + /** + * Returns the first element of this sequence or the defaultValue + * if this sequence is empty. + * @param defaultValue the value to return if this sequence is empty + * @return the first element or the default value + * @since 1.0 + * @see #first() + * @see #last() + */ @SuppressWarnings("unchecked") public final T first(T defaultValue) { if (this instanceof Callable) { @@ -1074,7 +1089,60 @@ public final T first(T defaultValue) { } return defaultValue; } + + /** + * Consumes the entire sequence and calls the given action with each value. + * @param action the action to call + * @throws NullPointerException if action is null + * @since 1.0 + * @see #foreachWhile(Pred) + */ + public final void foreach(Action1 action) { + for (T t : this) { + action.call(t); + } + } + + /** + * Consumes the entire sequence and calls the given predicate with each value; + * which can stop the iteration by returning false. + * @param predicate the predicate to call with the current element and should + * return true to continue the loop or false to quit the loop. + * @throws NullPointerException if action is null + * @since 1.0 + * @see #foreach(Pred) + */ + public final void foreachWhile(Pred predicate) { + for (T t : this) { + if (!predicate.test(t)) { + break; + } + } + } + + /** + * Consumes the entire sequence and adds each element into the given collection + * that is also returned. + * @param the collection of type accepting a (super)type of this element type + * @param collection the collection to collect into + * @return the collection itself + * @throws NullPointerException if collection is null + * @since 1.0 + */ + public final > U into(U collection) { + for (T v : this) { + collection.add(v); + } + return collection; + } + /** + * Returns the last element of this sequence. + * @return the last element of this sequence + * @throws NoSuchElementException if the sequence is empty + * @since 1.0 + * @see #last(Object) + */ @SuppressWarnings("unchecked") public final T last() { if (this instanceof Callable) { @@ -1093,6 +1161,15 @@ public final T last() { } } + /** + * Returns the last element of this sequence or the defaultValue if + * this sequence is empty. + * @param defaultValue the value to return if this sequence is empty + * @return the last element or the default value + * @since 1.0 + * @see #last() + * @see #first() + */ @SuppressWarnings("unchecked") public final T last(T defaultValue) { if (this instanceof Callable) { @@ -1110,40 +1187,23 @@ public final T last(T defaultValue) { } } } - - public final void removeAll() { - Iterator it = iterator(); - while (it.hasNext()) { - it.next(); - it.remove(); - } - } - - public final void foreach(Action1 action) { - for (T t : this) { - action.call(t); - } - } - - public final void foreachWhile(Pred action) { - for (T t : this) { - if (!action.test(t)) { - break; - } - } - } - - public final > U into(U collection) { - for (T v : this) { - collection.add(v); - } - return collection; - } + /** + * Prints the elements of this sequence to the console, separated + * by a comma+space and with a line break after roughly 80 characters. + * @since 1.0 + */ public final void print() { print(", ", 80); } + /** + * Prints the elements of this sequence to the console, separated + * by the given separator and with a line break after roughly the + * given charsPerLine amount. + * @param separator the characters to separate the elements + * @param charsPerLine indicates how long a line should be + */ public final void print(CharSequence separator, int charsPerLine) { boolean first = true; int len = 0; @@ -1172,21 +1232,87 @@ public final void print(CharSequence separator, int charsPerLine) { } } + /** + * Prints each element of this sequence into a new line on the console. + * @since 1.0 + */ public final void println() { for (T v : this) { System.out.println(v); } } + /** + * Prints each element of this sequence into a new line on the console, prefixed + * by the given character sequence. + * @param prefix the prefix before each line + */ public final void println(CharSequence prefix) { for (T v : this) { System.out.print(prefix); System.out.println(v); } + } + /** + * Removes all elements by repeatedly calling this sequence's Iterator.remove(). + */ + public final void removeAll() { + Iterator it = iterator(); + while (it.hasNext()) { + it.next(); + it.remove(); + } + } + + /** + * Consumes this Iterable and removes all elements for + * which the predicate returns true; in other words, + * remove those elements of a mutable source that match + * the predicate. + * @param predicate the predicate called with the current + * element and should return true for elements to remove, false + * for elements to keep. + * @throws UnsupportedOperationException if the this Iterable + * doesn't allow removing elements. + * @see #retainAll(Pred) + * @see #removeAll() + * @since 1.0 + */ + public final void removeAll(Pred predicate) { + Iterator it = iterator(); + while (it.hasNext()) { + T v = it.next(); + if (predicate.test(v)) { + it.remove(); + } + } + } + /** + * Consumes this Iterable and removes all elements for + * which the predicate returns false; in other words, + * retain those elements of a mutable source that match + * the predicate. + * @param predicate the predicate called with the current + * element and should return true for elements to keep, false + * for elements to remove. + * @throws UnsupportedOperationException if the this Iterable + * doesn't allow removing elements. + * @see #removeAll(Pred) + * @since 1.0 + */ + public final void retainAll(Pred predicate) { + Iterator it = iterator(); + while (it.hasNext()) { + T v = it.next(); + if (!predicate.test(v)) { + it.remove(); + } + } } /** * Iterates over this instance, dropping all values it produces. + * @see #subscribe() */ public final void run() { Iterator it = iterator(); @@ -1195,20 +1321,85 @@ public final void run() { } } + /** + * Returns the single element of this sequence or throws a NoSuchElementException + * if this sequence is empty or IndexOutOfBoundsException if this sequence has more + * than on element + * @return the single element of the sequence + * @throws IndexOutOfBoundsException if the sequence has more than one element + * @throws NoSuchElementException if the sequence is empty + * @since 1.0 + * @see #single(Object) + */ + public final T single() { + Iterator it = iterator(); + if (it.hasNext()) { + T v = it.next(); + if (it.hasNext()) { + throw new IndexOutOfBoundsException("The source has more than one element."); + } + return v; + } + throw new NoSuchElementException("The source is empty."); + } + + + /** + * Returns the single element of this sequence, the defaltValue + * if this sequence is empty or IndexOutOfBoundsException if this sequence has more + * than on element + * @param defaultValue the value to return if this sequence is empty + * @return the single element of the sequence + * @throws IndexOutOfBoundsException if the sequence has more than one element + * @since 1.0 + * @see #single(Object) + */ + public final T single(T defaultValue) { + Iterator it = iterator(); + if (it.hasNext()) { + T v = it.next(); + if (it.hasNext()) { + throw new IndexOutOfBoundsException("The source has more than one element."); + } + return v; + } + return defaultValue; + } + + /** + * Iterates over this instance, dropping all values it produces. + * @see #run() + */ public final void subscribe() { run(); } - public final void subscribe(Action1 consumer) { + /** + * Iterates over this sequence and calls the given onNext action with + * each element. + * @param onNext the consumer to call with each element + * @throws NullPointerException if consumer is null + * @since 1.0 + */ + public final void subscribe(Action1 onNext) { for (T v : this) { - consumer.call(v); + onNext.call(v); } } - public final void subscribe(Action1 consumer, Action1 onError) { + /** + * Iterates over this sequence and calls the given onNext action with + * each element and calls the onError with any exception thrown by the iteration + * or the onNext action. + * @param onNext the consumer to call with each element + * @param onError the consumer to call with the exception thrown + * @throws NullPointerException if onError is null + * @since 1.0 + */ + public final void subscribe(Action1 onNext, Action1 onError) { try { for (T v : this) { - consumer.call(v); + onNext.call(v); } } catch (Throwable ex) { Exceptions.throwIfFatal(ex); @@ -1216,10 +1407,21 @@ public final void subscribe(Action1 consumer, Action1 onEr } } - public final void subscribe(Action1 consumer, Action1 onError, Action0 onCompleted) { + /** + * Iterates over this sequence and calls the given onNext action with + * each element and calls the onError with any exception thrown by the iteration + * or the onNext action; otherwise calls the onCompleted action when the sequence completes + * without exception. + * @param onNext the consumer to call with each element + * @param onError the consumer to call with the exception thrown + * @param onCompleted the action called after the sequence has been consumed + * @throws NullPointerException if onError or onCompleted is null + * @since 1.0 + */ + public final void subscribe(Action1 onNext, Action1 onError, Action0 onCompleted) { try { for (T v : this) { - consumer.call(v); + onNext.call(v); } } catch (Throwable ex) { Exceptions.throwIfFatal(ex); @@ -1229,6 +1431,12 @@ public final void subscribe(Action1 consumer, Action1 onEr onCompleted.call(); } + /** + * Consumes this sequence and calls the appropriate onXXX method on the given Observer instance. + * @param observer the observer to forward values, error or completion to. + * @throws NullPointerException if observer is null + * @since 1.0 + */ public final void subscribe(Observer observer) { try { for (T v : this) { @@ -1242,6 +1450,13 @@ public final void subscribe(Observer observer) { observer.onCompleted(); } + /** + * Consumes this sequence and calls the appropriate onXXX method on the given Subscriber instance + * as long as it has not unsubscribed. + * @param subscriber the subscriber to forward values, error or completion to. + * @throws NullPointerException if subscriber is null + * @since 1.0 + */ public final void subscribe(Subscriber subscriber) { try { for (T v : this) { @@ -1263,78 +1478,6 @@ public final void subscribe(Subscriber subscriber) { } subscriber.onCompleted(); } - - /** - * Consumes this Iterable and removes all elements for - * which the predicate returns false; in other words, - * retain those elements of a mutable source that match - * the predicate. - * @param predicate the predicate called with the current - * element and should return true for elements to keep, false - * for elements to remove. - * @throws UnsupportedOperationException if the this Iterable - * doesn't allow removing elements. - * @see #removeAll(Pred) - * @since 1.0 - */ - public final void retainAll(Pred predicate) { - Iterator it = iterator(); - while (it.hasNext()) { - T v = it.next(); - if (!predicate.test(v)) { - it.remove(); - } - } - } - - /** - * Consumes this Iterable and removes all elements for - * which the predicate returns true; in other words, - * remove those elements of a mutable source that match - * the predicate. - * @param predicate the predicate called with the current - * element and should return true for elements to remove, false - * for elements to keep. - * @throws UnsupportedOperationException if the this Iterable - * doesn't allow removing elements. - * @see #retainAll(Pred) - * @see #removeAll() - * @since 1.0 - */ - public final void removeAll(Pred predicate) { - Iterator it = iterator(); - while (it.hasNext()) { - T v = it.next(); - if (predicate.test(v)) { - it.remove(); - } - } - } - - - public final T single() { - Iterator it = iterator(); - if (it.hasNext()) { - T v = it.next(); - if (it.hasNext()) { - throw new IndexOutOfBoundsException("The source has more than one element."); - } - return v; - } - throw new NoSuchElementException("The source is empty."); - } - - public final T single(T defaultValue) { - Iterator it = iterator(); - if (it.hasNext()) { - T v = it.next(); - if (it.hasNext()) { - throw new IndexOutOfBoundsException("The source has more than one element."); - } - return v; - } - return defaultValue; - } // -------------------------------------------------------------------------------------------- // Helper methods