52
52
import rx .operators .OperationElementAt ;
53
53
import rx .operators .OperationFilter ;
54
54
import rx .operators .OperationFinally ;
55
- import rx .operators .OperationFirstOrDefault ;
56
55
import rx .operators .OperationGroupBy ;
57
56
import rx .operators .OperationGroupByUntil ;
58
57
import rx .operators .OperationGroupJoin ;
59
58
import rx .operators .OperationInterval ;
60
59
import rx .operators .OperationJoin ;
61
60
import rx .operators .OperationJoinPatterns ;
62
- import rx .operators .OperationLast ;
63
61
import rx .operators .OperationMap ;
64
62
import rx .operators .OperationMaterialize ;
65
63
import rx .operators .OperationMerge ;
78
76
import rx .operators .OperationSample ;
79
77
import rx .operators .OperationScan ;
80
78
import rx .operators .OperationSequenceEqual ;
79
+ import rx .operators .OperationSingle ;
81
80
import rx .operators .OperationSkip ;
82
81
import rx .operators .OperationSkipLast ;
83
82
import rx .operators .OperationSkipUntil ;
@@ -5091,6 +5090,78 @@ public Observable<T> skip(int num) {
5091
5090
return create (OperationSkip .skip (this , num ));
5092
5091
}
5093
5092
5093
+ /**
5094
+ * If the Observable completes after emitting a single item, return an
5095
+ * Observable containing that item. If it emits more than one item or no
5096
+ * item, throw an IllegalArgumentException.
5097
+ *
5098
+ * @return an Observable containing the single item emitted by the source
5099
+ * Observable that matches the predicate.
5100
+ * @throws IllegalArgumentException
5101
+ * if the source emits more than one item or no item
5102
+ */
5103
+ public Observable <T > single () {
5104
+ return create (OperationSingle .<T > single (this ));
5105
+ }
5106
+
5107
+ /**
5108
+ * If the Observable completes after emitting a single item that matches a
5109
+ * predicate, return an Observable containing that item. If it emits more
5110
+ * than one such item or no item, throw an IllegalArgumentException.
5111
+ *
5112
+ * @param predicate
5113
+ * a predicate function to evaluate items emitted by the source
5114
+ * Observable
5115
+ * @return an Observable containing the single item emitted by the source
5116
+ * Observable that matches the predicate.
5117
+ * @throws IllegalArgumentException
5118
+ * if the source emits more than one item or no item matching
5119
+ * the predicate
5120
+ */
5121
+ public Observable <T > single (Func1 <? super T , Boolean > predicate ) {
5122
+ return filter (predicate ).single ();
5123
+ }
5124
+
5125
+ /**
5126
+ * If the Observable completes after emitting a single item, return an
5127
+ * Observable containing that item. If it's empty, return an Observable
5128
+ * containing the defaultValue. If it emits more than one item, throw an
5129
+ * IllegalArgumentException.
5130
+ *
5131
+ * @param defaultValue
5132
+ * a default value to return if the Observable emits no item
5133
+ * @return an Observable containing the single item emitted by the source
5134
+ * Observable, or an Observable containing the defaultValue if no
5135
+ * item.
5136
+ * @throws IllegalArgumentException
5137
+ * if the source emits more than one item
5138
+ */
5139
+ public Observable <T > singleOrDefault (T defaultValue ) {
5140
+ return create (OperationSingle .<T > singleOrDefault (this , defaultValue ));
5141
+ }
5142
+
5143
+ /**
5144
+ * If the Observable completes after emitting a single item that matches a
5145
+ * predicate, return an Observable containing that item. If it emits no such
5146
+ * item, return an Observable containing the defaultValue. If it emits more
5147
+ * than one such item, throw an IllegalArgumentException.
5148
+ *
5149
+ * @param defaultValue
5150
+ * a default value to return if the {@link Observable} emits no
5151
+ * matching items
5152
+ * @param predicate
5153
+ * a predicate function to evaluate items emitted by the
5154
+ * Observable
5155
+ * @return an Observable containing the single item emitted by the source
5156
+ * Observable that matches the predicate, or an Observable
5157
+ * containing the defaultValue if no item matches the predicate
5158
+ * @throws IllegalArgumentException
5159
+ * if the source emits more than one item matching the predicate
5160
+ */
5161
+ public Observable <T > singleOrDefault (T defaultValue , Func1 <? super T , Boolean > predicate ) {
5162
+ return filter (predicate ).singleOrDefault (defaultValue );
5163
+ }
5164
+
5094
5165
/**
5095
5166
* Returns an Observable that emits only the very first item emitted by the
5096
5167
* source Observable, or an <code>IllegalArgumentException</code> if the source
@@ -5103,7 +5174,7 @@ public Observable<T> skip(int num) {
5103
5174
* @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#first">RxJava Wiki: first()</a>
5104
5175
*/
5105
5176
public Observable <T > first () {
5106
- return takeFirst (). last ();
5177
+ return take ( 1 ). single ();
5107
5178
}
5108
5179
5109
5180
/**
@@ -5119,7 +5190,7 @@ public Observable<T> first() {
5119
5190
* @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#first">RxJava Wiki: first()</a>
5120
5191
*/
5121
5192
public Observable <T > first (Func1 <? super T , Boolean > predicate ) {
5122
- return takeFirst (predicate ).last ();
5193
+ return takeFirst (predicate ).single ();
5123
5194
}
5124
5195
5125
5196
/**
@@ -5137,7 +5208,7 @@ public Observable<T> first(Func1<? super T, Boolean> predicate) {
5137
5208
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229320.aspx">MSDN: Observable.FirstOrDefault</a>
5138
5209
*/
5139
5210
public Observable <T > firstOrDefault (T defaultValue ) {
5140
- return create ( OperationFirstOrDefault . firstOrDefault ( this , defaultValue ) );
5211
+ return take ( 1 ). singleOrDefault ( defaultValue );
5141
5212
}
5142
5213
5143
5214
/**
@@ -5155,8 +5226,8 @@ public Observable<T> firstOrDefault(T defaultValue) {
5155
5226
* @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observables#firstordefault">RxJava Wiki: firstOrDefault()</a>
5156
5227
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229759.aspx">MSDN: Observable.FirstOrDefault</a>
5157
5228
*/
5158
- public Observable <T > firstOrDefault (Func1 <? super T , Boolean > predicate , T defaultValue ) {
5159
- return create ( OperationFirstOrDefault . firstOrDefault ( this , predicate , defaultValue ) );
5229
+ public Observable <T > firstOrDefault (T defaultValue , Func1 <? super T , Boolean > predicate ) {
5230
+ return takeFirst ( predicate ). singleOrDefault ( defaultValue );
5160
5231
}
5161
5232
5162
5233
/**
@@ -5268,7 +5339,7 @@ public Observable<T> takeFirst() {
5268
5339
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229177.aspx">MSDN: Observable.First</a>
5269
5340
*/
5270
5341
public Observable <T > takeFirst (Func1 <? super T , Boolean > predicate ) {
5271
- return skipWhile ( not ( predicate ) ).take (1 );
5342
+ return filter ( predicate ).take (1 );
5272
5343
}
5273
5344
5274
5345
/**
@@ -5723,7 +5794,7 @@ public <T2, D1, D2, R> Observable<R> groupJoin(Observable<T2> right, Func1<? sup
5723
5794
public Observable <Boolean > isEmpty () {
5724
5795
return create (OperationAny .isEmpty (this ));
5725
5796
}
5726
-
5797
+
5727
5798
/**
5728
5799
* Returns an Observable that emits the last item emitted by the source or
5729
5800
* notifies observers of an <code>IllegalArgumentException</code> if the
@@ -5736,7 +5807,53 @@ public Observable<Boolean> isEmpty() {
5736
5807
* @see <a href="https://github.com/Netflix/RxJava/wiki/Filtering-Observable-Operators#last">RxJava Wiki: last()</a>
5737
5808
*/
5738
5809
public Observable <T > last () {
5739
- return create (OperationLast .last (this ));
5810
+ return takeLast (1 ).single ();
5811
+ }
5812
+
5813
+ /**
5814
+ * Returns an Observable that emits only the last item emitted by the source
5815
+ * Observable that satisfies a given condition, or an
5816
+ * IllegalArgumentException if no such items are emitted.
5817
+ *
5818
+ * @param predicate
5819
+ * the condition any source emitted item has to satisfy
5820
+ * @return an Observable that emits only the last item satisfying the given
5821
+ * condition from the source, or an IllegalArgumentException if no
5822
+ * such items are emitted.
5823
+ * @throws IllegalArgumentException
5824
+ * if no such itmes are emmited
5825
+ */
5826
+ public Observable <T > last (Func1 <? super T , Boolean > predicate ) {
5827
+ return filter (predicate ).takeLast (1 ).single ();
5828
+ }
5829
+
5830
+ /**
5831
+ * Returns an Observable that emits only the last item emitted by the source
5832
+ * Observable, or a default item if the source is empty.
5833
+ *
5834
+ * @param defaultValue
5835
+ * the default item to emit if the source Observable is empty
5836
+ * @return an Observable that emits only the last item from the source, or a
5837
+ * default item if the source is empty
5838
+ */
5839
+ public Observable <T > lastOrDefault (T defaultValue ) {
5840
+ return takeLast (1 ).singleOrDefault (defaultValue );
5841
+ }
5842
+
5843
+ /**
5844
+ * Returns an Observable that emits only the last item emitted by the source
5845
+ * Observable that satisfies a given condition, or a default item otherwise.
5846
+ *
5847
+ * @param defaultValue
5848
+ * the default item to emit if the source Observable doesn't emit
5849
+ * anything that satisfies the given condition
5850
+ * @param predicate
5851
+ * the condition any source emitted item has to satisfy
5852
+ * @return an Observable that emits only the last item from the source that
5853
+ * satisfies the given condition, or a default item otherwise
5854
+ */
5855
+ public Observable <T > lastOrDefault (T defaultValue , Func1 <? super T , Boolean > predicate ) {
5856
+ return filter (predicate ).takeLast (1 ).singleOrDefault (defaultValue );
5740
5857
}
5741
5858
5742
5859
/**
0 commit comments