Skip to content

Commit 069726c

Browse files
Mauinabersnaze
authored andcommitted
Change Observable methods to return Single, Maybe, or Completable in cases where is it returning a 1 or 0 values.
1 parent 12ca280 commit 069726c

File tree

62 files changed

+1024
-1173
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1024
-1173
lines changed

src/main/java/io/reactivex/Maybe.java

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public static <T> Maybe<T> just(T item) {
440440
ObjectHelper.requireNonNull(item, "item is null");
441441
return RxJavaPlugins.onAssembly(new MaybeJust<T>(item));
442442
}
443-
443+
444444
/**
445445
* Merges an Iterable sequence of MaybeSource instances into a single Flowable sequence,
446446
* running all MaybeSources at once.
@@ -700,6 +700,33 @@ public static <T> Maybe<T> wrap(MaybeSource<T> source) {
700700
// Instance methods
701701
// ------------------------------------------------------------------
702702

703+
/**
704+
* Waits in a blocking fashion until the current Maybe signals a success value (which is returned) or
705+
* defaultValue if completed or an exception (which is propagated).
706+
* <dl>
707+
* <dt><b>Scheduler:</b></dt>
708+
* <dd>{@code blockingGet} does not operate by default on a particular {@link Scheduler}.</dd>
709+
* </dl>
710+
* @return the success value
711+
*/
712+
public T blockingGet() {
713+
return MaybeAwait.get(this, null);
714+
}
715+
716+
/**
717+
* Waits in a blocking fashion until the current Maybe signals a success value (which is returned) or
718+
* defaultValue if completed or an exception (which is propagated).
719+
* <dl>
720+
* <dt><b>Scheduler:</b></dt>
721+
* <dd>{@code blockingGet} does not operate by default on a particular {@link Scheduler}.</dd>
722+
* </dl>
723+
* @return the success value
724+
*/
725+
public T blockingGet(T defaultValue) {
726+
ObjectHelper.requireNonNull(defaultValue, "defaultValue is null");
727+
return MaybeAwait.get(this, defaultValue);
728+
}
729+
703730
/**
704731
* Casts the success value of the current Maybe into the target type or signals a
705732
* ClassCastException if not compatible.
@@ -739,7 +766,7 @@ public final <U> Maybe<U> cast(final Class<? extends U> clazz) {
739766
public final <R> Maybe<R> compose(Function<? super Maybe<T>, ? extends MaybeSource<R>> transformer) {
740767
return wrap(to(transformer));
741768
}
742-
769+
743770
/**
744771
* Returns a Maybe that is based on applying a specified function to the item emitted by the source Maybe,
745772
* where that function returns a MaybeSource.
@@ -857,7 +884,7 @@ public final Maybe<T> doOnError(Consumer<? super Throwable> onError) {
857884
Functions.EMPTY_ACTION // dispose
858885
));
859886
}
860-
887+
861888
/**
862889
* Calls the given onEvent callback with the (success value, null) for an onSuccess, (null, throwabe) for
863890
* an onError or (null, null) for an onComplete signal from this Maybe before delivering said
@@ -964,6 +991,69 @@ public final <R> Maybe<R> flatMap(Function<? super T, ? extends MaybeSource<? ex
964991
return RxJavaPlugins.onAssembly(new MaybeFlatten<T, R>(this, mapper));
965992
}
966993

994+
/**
995+
* Returns a Observable that is based on applying a specified function to the item emitted by the source Maybe,
996+
* where that function returns a ObservableSource.
997+
* <p>
998+
* <img width="640" height="300" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Maybe.flatMap.png" alt="">
999+
* <dl>
1000+
* <dt><b>Scheduler:</b></dt>
1001+
* <dd>{@code flatMap} does not operate by default on a particular {@link Scheduler}.</dd>
1002+
* </dl>
1003+
*
1004+
* @param <R> the result value type
1005+
* @param mapper
1006+
* a function that, when applied to the item emitted by the source Maybe, returns a ObservableSource
1007+
* @return the Observable returned from {@code func} when applied to the item emitted by the source Maybe
1008+
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a>
1009+
*/
1010+
public final <R> Observable<R> flatMapObservable(Function<? super T, ? extends ObservableSource<? extends R>> mapper) {
1011+
return toObservable().flatMap(mapper);
1012+
}
1013+
1014+
/**
1015+
* Returns a Flowable that emits items based on applying a specified function to the item emitted by the
1016+
* source Maybe, where that function returns a Publisher.
1017+
* <p>
1018+
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.flatMapObservable.png" alt="">
1019+
* <dl>
1020+
* <dt><b>Scheduler:</b></dt>
1021+
* <dd>{@code flatMapObservable} does not operate by default on a particular {@link Scheduler}.</dd>
1022+
* </dl>
1023+
*
1024+
* @param <R> the result value type
1025+
* @param mapper
1026+
* a function that, when applied to the item emitted by the source Maybe, returns an
1027+
* Flowable
1028+
* @return the Flowable returned from {@code func} when applied to the item emitted by the source Maybe
1029+
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a>
1030+
*/
1031+
public final <R> Flowable<R> flatMapPublisher(Function<? super T, ? extends Publisher<? extends R>> mapper) {
1032+
return toFlowable().flatMap(mapper);
1033+
}
1034+
1035+
/**
1036+
* Returns a {@link Completable} that completes based on applying a specified function to the item emitted by the
1037+
* source {@link Single}, where that function returns a {@link Completable}.
1038+
* <p>
1039+
* <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.flatMapCompletable.png" alt="">
1040+
* <dl>
1041+
* <dt><b>Scheduler:</b></dt>
1042+
* <dd>{@code flatMapCompletable} does not operate by default on a particular {@link Scheduler}.</dd>
1043+
* </dl>
1044+
*
1045+
* @param mapper
1046+
* a function that, when applied to the item emitted by the source Single, returns a
1047+
* Completable
1048+
* @return the Completable returned from {@code func} when applied to the item emitted by the source Single
1049+
* @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a>
1050+
* @since 2.0
1051+
*/
1052+
public final Completable flatMapCompletable(final Function<? super T, ? extends Completable> mapper) {
1053+
ObjectHelper.requireNonNull(mapper, "mapper is null");
1054+
return RxJavaPlugins.onAssembly(new MaybeFlatMapCompletable<T>(this, mapper));
1055+
}
1056+
9671057
/**
9681058
* Maps the onSuccess, onError or onComplete signals of this Maybe into MaybeSource and emits that
9691059
* MaybeSource's signals
@@ -1148,6 +1238,20 @@ public final Observable<T> toObservable() {
11481238
return RxJavaPlugins.onAssembly(new MaybeToObservable<T>(this));
11491239
}
11501240

1241+
/**
1242+
* Converts this Maybe into an Single instance composing cancellation
1243+
* through and turing an empty Maybe into a signal of NoSuchElementException.
1244+
* <dl>
1245+
* <dt><b>Scheduler:</b></dt>
1246+
* <dd>{@code create} does not operate by default on a particular {@link Scheduler}.</dd>
1247+
* </dl>
1248+
* @return the new Single instance
1249+
*/
1250+
public final Single<T> toSingle(T defaultValue) {
1251+
ObjectHelper.requireNonNull(defaultValue, "defaultValue is null");
1252+
return RxJavaPlugins.onAssembly(new MaybeToSingle<T>(this, defaultValue));
1253+
}
1254+
11511255
/**
11521256
* Converts this Maybe into an Single instance composing cancellation
11531257
* through and turing an empty Maybe into a signal of NoSuchElementException.
@@ -1158,7 +1262,7 @@ public final Observable<T> toObservable() {
11581262
* @return the new Single instance
11591263
*/
11601264
public final Single<T> toSingle() {
1161-
return RxJavaPlugins.onAssembly(new MaybeToSingle<T>(this));
1265+
return RxJavaPlugins.onAssembly(new MaybeToSingle<T>(this, null));
11621266
}
11631267

11641268
/**

0 commit comments

Comments
 (0)