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

io.reactivex.rxjava3.core.Maybe Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2016-present, RxJava Contributors.
 *
 * 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 io.reactivex.rxjava3.core;

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.*;

import org.reactivestreams.*;

import io.reactivex.rxjava3.annotations.*;
import io.reactivex.rxjava3.disposables.*;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.functions.*;
import io.reactivex.rxjava3.internal.fuseable.*;
import io.reactivex.rxjava3.internal.jdk8.*;
import io.reactivex.rxjava3.internal.observers.*;
import io.reactivex.rxjava3.internal.operators.flowable.*;
import io.reactivex.rxjava3.internal.operators.maybe.*;
import io.reactivex.rxjava3.internal.operators.mixed.*;
import io.reactivex.rxjava3.internal.operators.observable.ObservableElementAtMaybe;
import io.reactivex.rxjava3.internal.util.ErrorMode;
import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.schedulers.*;

/**
 * The {@code Maybe} class represents a deferred computation and emission of a single value, no value at all or an exception.
 * 

* The {@code Maybe} class implements the {@link MaybeSource} base interface and the default consumer * type it interacts with is the {@link MaybeObserver} via the {@link #subscribe(MaybeObserver)} method. *

* The {@code Maybe} operates with the following sequential protocol: *


 *     onSubscribe (onSuccess | onError | onComplete)?
 * 
*

* Note that {@code onSuccess}, {@code onError} and {@code onComplete} are mutually exclusive events; unlike {@link Observable}, * {@code onSuccess} is never followed by {@code onError} or {@code onComplete}. *

* Like {@code Observable}, a running {@code Maybe} can be stopped through the {@link Disposable} instance * provided to consumers through {@link MaybeObserver#onSubscribe}. *

* Like an {@code Observable}, a {@code Maybe} is lazy, can be either "hot" or "cold", synchronous or * asynchronous. {@code Maybe} instances returned by the methods of this class are cold * and there is a standard hot implementation in the form of a subject: * {@link io.reactivex.rxjava3.subjects.MaybeSubject MaybeSubject}. *

* The documentation for this class makes use of marble diagrams. The following legend explains these diagrams: *

* *

* See {@link Flowable} or {@code Observable} for the * implementation of the Reactive Pattern for a stream or vector of values. *

* Example: *


 * Disposable d = Maybe.just("Hello World")
 *    .delay(10, TimeUnit.SECONDS, Schedulers.io())
 *    .subscribeWith(new DisposableMaybeObserver<String>() {
 *        @Override
 *        public void onStart() {
 *            System.out.println("Started");
 *        }
 *
 *        @Override
 *        public void onSuccess(String value) {
 *            System.out.println("Success: " + value);
 *        }
 *
 *        @Override
 *        public void onError(Throwable error) {
 *            error.printStackTrace();
 *        }
 *
 *        @Override
 *        public void onComplete() {
 *            System.out.println("Done!");
 *        }
 *    });
 * 
 * Thread.sleep(5000);
 * 
 * d.dispose();
 * 
*

* Note that by design, subscriptions via {@link #subscribe(MaybeObserver)} can't be disposed * from the outside (hence the * {@code void} return of the {@link #subscribe(MaybeObserver)} method) and it is the * responsibility of the implementor of the {@code MaybeObserver} to allow this to happen. * RxJava supports such usage with the standard * {@link io.reactivex.rxjava3.observers.DisposableMaybeObserver DisposableMaybeObserver} instance. * For convenience, the {@link #subscribeWith(MaybeObserver)} method is provided as well to * allow working with a {@code MaybeObserver} (or subclass) instance to be applied with in * a fluent manner (such as in the example above). * * @param the value type * @since 2.0 * @see io.reactivex.rxjava3.observers.DisposableMaybeObserver */ public abstract class Maybe<@NonNull T> implements MaybeSource { /** * Runs multiple {@link MaybeSource}s provided by an {@link Iterable} sequence and * signals the events of the first one that signals (disposing the rest). *

* *

*
Scheduler:
*
{@code amb} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@code Iterable} sequence of sources. A subscription to each source will * occur in the same order as in the {@code Iterable}. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code sources} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe amb(@NonNull Iterable<@NonNull ? extends MaybeSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new MaybeAmb<>(null, sources)); } /** * Runs multiple {@link MaybeSource}s and signals the events of the first one that signals (disposing * the rest). *

* *

*
Scheduler:
*
{@code ambArray} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the array of sources. A subscription to each source will * occur in the same order as in the array. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code sources} is {@code null} */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull @SafeVarargs public static <@NonNull T> Maybe ambArray(@NonNull MaybeSource... sources) { Objects.requireNonNull(sources, "sources is null"); if (sources.length == 0) { return empty(); } if (sources.length == 1) { @SuppressWarnings("unchecked") MaybeSource source = (MaybeSource)sources[0]; return wrap(source); } return RxJavaPlugins.onAssembly(new MaybeAmb<>(sources, null)); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link MaybeSource} sources provided by * an {@link Iterable} sequence as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@code Iterable} sequence of {@code MaybeSource} instances * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat(@NonNull Iterable<@NonNull ? extends MaybeSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new MaybeConcatIterable<>(sources)); } /** * Returns a {@link Flowable} that emits the items emitted by two {@link MaybeSource}s, one after the other. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a {@code MaybeSource} to be concatenated * @param source2 * a {@code MaybeSource} to be concatenated * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} * @see ReactiveX operators documentation: Concat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat(@NonNull MaybeSource source1, @NonNull MaybeSource source2) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); return concatArray(source1, source2); } /** * Returns a {@link Flowable} that emits the items emitted by three {@link MaybeSource}s, one after the other. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a {@code MaybeSource} to be concatenated * @param source2 * a {@code MaybeSource} to be concatenated * @param source3 * a {@code MaybeSource} to be concatenated * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1}, {@code source2} or {@code source3} is {@code null} * @see ReactiveX operators documentation: Concat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); return concatArray(source1, source2, source3); } /** * Returns a {@link Flowable} that emits the items emitted by four {@link MaybeSource}s, one after the other. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param source1 * a {@code MaybeSource} to be concatenated * @param source2 * a {@code MaybeSource} to be concatenated * @param source3 * a {@code MaybeSource} to be concatenated * @param source4 * a {@code MaybeSource} to be concatenated * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3} or {@code source4} is {@code null} * @see ReactiveX operators documentation: Concat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); return concatArray(source1, source2, source3, source4); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link MaybeSource} sources provided by * a {@link Publisher} sequence as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer and * expects the {@code Publisher} to honor backpressure as well. If the sources {@code Publisher} * violates this, a {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException} is signaled.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@code Publisher} of {@code MaybeSource} instances * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concat(@NonNull Publisher<@NonNull ? extends MaybeSource> sources) { return concat(sources, 2); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link MaybeSource} sources provided by * a {@link Publisher} sequence as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer and * expects the {@code Publisher} to honor backpressure as well. If the sources {@code Publisher} * violates this, a {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException} is signaled.
*
Scheduler:
*
{@code concat} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the {@code Publisher} of {@code MaybeSource} instances * @param prefetch the number of {@code MaybeSource}s to prefetch from the {@code Publisher} * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code prefetch} is non-positive * @return the new {@code Flowable} instance */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concat(@NonNull Publisher<@NonNull ? extends MaybeSource> sources, int prefetch) { Objects.requireNonNull(sources, "sources is null"); ObjectHelper.verifyPositive(prefetch, "prefetch"); return RxJavaPlugins.onAssembly(new FlowableConcatMapMaybePublisher<>(sources, Functions.identity(), ErrorMode.IMMEDIATE, prefetch)); } /** * Concatenate the single values, in a non-overlapping fashion, of the {@link MaybeSource} sources in the array * as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code concatArray} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources the array of {@code MaybeSource} instances * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T> Flowable concatArray(@NonNull MaybeSource... sources) { Objects.requireNonNull(sources, "sources is null"); if (sources.length == 0) { return Flowable.empty(); } if (sources.length == 1) { @SuppressWarnings("unchecked") MaybeSource source = (MaybeSource)sources[0]; return RxJavaPlugins.onAssembly(new MaybeToFlowable<>(source)); } return RxJavaPlugins.onAssembly(new MaybeConcatArray<>(sources)); } /** * Concatenates a variable number of {@link MaybeSource} sources and delays errors from any of them * till all terminate as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code concatArrayDelayError} does not operate by default on a particular {@link Scheduler}.
*
* @param sources the array of sources * @param the common base value type * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs @NonNull public static <@NonNull T> Flowable concatArrayDelayError(@NonNull MaybeSource... sources) { Objects.requireNonNull(sources, "sources is null"); if (sources.length == 0) { return Flowable.empty(); } else if (sources.length == 1) { @SuppressWarnings("unchecked") MaybeSource source = (MaybeSource)sources[0]; return RxJavaPlugins.onAssembly(new MaybeToFlowable<>(source)); } return RxJavaPlugins.onAssembly(new MaybeConcatArrayDelayError<>(sources)); } /** * Concatenates a sequence of {@link MaybeSource} eagerly into a {@link Flowable} sequence. *

* Eager concatenation means that once an observer subscribes, this operator subscribes to all of the * source {@code MaybeSource}s. The operator buffers the value emitted by these {@code MaybeSource}s and then drains them * in order, each one after the previous one completes. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource}s that need to be eagerly concatenated * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull @SafeVarargs public static <@NonNull T> Flowable concatArrayEager(@NonNull MaybeSource... sources) { return Flowable.fromArray(sources).concatMapEager((Function)MaybeToPublisher.instance()); } /** * Concatenates a sequence of {@link MaybeSource} eagerly into a {@link Flowable} sequence. *

* Eager concatenation means that once an observer subscribes, this operator subscribes to all of the * source {@code MaybeSource}s. The operator buffers the value emitted by these {@code MaybeSource}s and then drains them * in order, each one after the previous one completes. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource}s that need to be eagerly concatenated * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull @SafeVarargs public static <@NonNull T> Flowable concatArrayEagerDelayError(@NonNull MaybeSource... sources) { return Flowable.fromArray(sources).concatMapEagerDelayError((Function)MaybeToPublisher.instance(), true); } /** * Concatenates the {@link Iterable} sequence of {@link MaybeSource}s into a single sequence by subscribing to each {@code MaybeSource}, * one after the other, one at a time and delays any errors till the all inner {@code MaybeSource}s terminate * as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code concatDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources the {@code Iterable} sequence of {@code MaybeSource}s * @return the new {@code Flowable} with the concatenating behavior * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable concatDelayError(@NonNull Iterable<@NonNull ? extends MaybeSource> sources) { return Flowable.fromIterable(sources).concatMapMaybeDelayError(Functions.identity()); } /** * Concatenates the {@link Publisher} sequence of {@link MaybeSource}s into a single sequence by subscribing to each inner {@code MaybeSource}, * one after the other, one at a time and delays any errors till the all inner and the outer {@code Publisher} terminate * as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
{@code concatDelayError} fully supports backpressure.
*
Scheduler:
*
{@code concatDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources the {@code Publisher} sequence of {@code MaybeSource}s * @return the new {@code Flowable} with the concatenating behavior * @throws NullPointerException if {@code sources} is {@code null} */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatDelayError(@NonNull Publisher<@NonNull ? extends MaybeSource> sources) { return Flowable.fromPublisher(sources).concatMapMaybeDelayError(Functions.identity()); } /** * Concatenates the {@link Publisher} sequence of {@link MaybeSource}s into a single sequence by subscribing to each inner {@code MaybeSource}, * one after the other, one at a time and delays any errors till the all inner and the outer {@code Publisher} terminate * as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
{@code concatDelayError} fully supports backpressure.
*
Scheduler:
*
{@code concatDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources the {@code Publisher} sequence of {@code MaybeSource}s * @param prefetch The number of upstream items to prefetch so that fresh items are * ready to be mapped when a previous {@code MaybeSource} terminates. * The operator replenishes after half of the prefetch amount has been consumed * and turned into {@code MaybeSource}s. * @return the new {@code Flowable} with the concatenating behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code prefetch} is non-positive * @since 3.0.0 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatDelayError(@NonNull Publisher<@NonNull ? extends MaybeSource> sources, int prefetch) { return Flowable.fromPublisher(sources).concatMapMaybeDelayError(Functions.identity(), true, prefetch); } /** * Concatenates a sequence of {@link MaybeSource}s eagerly into a {@link Flowable} sequence. *

* *

* Eager concatenation means that once an observer subscribes, this operator subscribes to all of the * source {@code MaybeSource}s. The operator buffers the values emitted by these {@code MaybeSource}s and then drains them * in order, each one after the previous one completes. *

*
Backpressure:
*
Backpressure is honored towards the downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource} that need to be eagerly concatenated * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatEager(@NonNull Iterable<@NonNull ? extends MaybeSource> sources) { return Flowable.fromIterable(sources).concatMapEagerDelayError((Function)MaybeToPublisher.instance(), false); } /** * Concatenates a sequence of {@link MaybeSource}s eagerly into a {@link Flowable} sequence and * runs a limited number of the inner sequences at once. *

* *

* Eager concatenation means that once an observer subscribes, this operator subscribes to all of the * source {@code MaybeSource}s. The operator buffers the values emitted by these {@code MaybeSource}s and then drains them * in order, each one after the previous one completes. *

*
Backpressure:
*
Backpressure is honored towards the downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource} that need to be eagerly concatenated * @param maxConcurrency the maximum number of concurrently running inner {@code MaybeSource}s; {@link Integer#MAX_VALUE} * is interpreted as all inner {@code MaybeSource}s can be active at the same time * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @since 3.0.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatEager(@NonNull Iterable<@NonNull ? extends MaybeSource> sources, int maxConcurrency) { return Flowable.fromIterable(sources).concatMapEagerDelayError((Function)MaybeToPublisher.instance(), false, maxConcurrency, 1); } /** * Concatenates a {@link Publisher} sequence of {@link MaybeSource}s eagerly into a {@link Flowable} sequence. *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * emitted source {@code MaybeSource}s as they are observed. The operator buffers the values emitted by these * {@code MaybeSource}s and then drains them in order, each one after the previous one completes. *

* *

*
Backpressure:
*
Backpressure is honored towards the downstream and the outer {@code Publisher} is * expected to support backpressure. Violating this assumption, the operator will * signal {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException}.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource}s that need to be eagerly concatenated * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatEager(@NonNull Publisher<@NonNull ? extends MaybeSource> sources) { return Flowable.fromPublisher(sources).concatMapEager((Function)MaybeToPublisher.instance()); } /** * Concatenates a {@link Publisher} sequence of {@link MaybeSource}s eagerly into a {@link Flowable} sequence, * running at most the given number of inner {@code MaybeSource}s at once. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * emitted source {@code MaybeSource}s as they are observed. The operator buffers the values emitted by these * {@code MaybeSource}s and then drains them in order, each one after the previous one completes. *

*
Backpressure:
*
Backpressure is honored towards the downstream and the outer {@code Publisher} is * expected to support backpressure. Violating this assumption, the operator will * signal {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException}.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource}s that need to be eagerly concatenated * @param maxConcurrency the maximum number of concurrently running inner {@code MaybeSource}s; {@link Integer#MAX_VALUE} * is interpreted as all inner {@code MaybeSource}s can be active at the same time * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @since 3.0.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatEager(@NonNull Publisher<@NonNull ? extends MaybeSource> sources, int maxConcurrency) { return Flowable.fromPublisher(sources).concatMapEager((Function)MaybeToPublisher.instance(), maxConcurrency, 1); } /** * Concatenates a sequence of {@link MaybeSource}s eagerly into a {@link Flowable} sequence, * delaying errors until all inner {@code MaybeSource}s terminate. *

* *

* Eager concatenation means that once an observer subscribes, this operator subscribes to all of the * source {@code MaybeSource}s. The operator buffers the values emitted by these {@code MaybeSource}s and then drains them * in order, each one after the previous one completes. *

*
Backpressure:
*
Backpressure is honored towards the downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource} that need to be eagerly concatenated * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatEagerDelayError(@NonNull Iterable<@NonNull ? extends MaybeSource> sources) { return Flowable.fromIterable(sources).concatMapEagerDelayError((Function)MaybeToPublisher.instance(), true); } /** * Concatenates a sequence of {@link MaybeSource}s eagerly into a {@link Flowable} sequence, * delaying errors until all inner {@code MaybeSource}s terminate and * runs a limited number of inner {@code MaybeSource}s at once. *

* *

* Eager concatenation means that once an observer subscribes, this operator subscribes to all of the * source {@code MaybeSource}s. The operator buffers the values emitted by these {@code MaybeSource}s and then drains them * in order, each one after the previous one completes. *

*
Backpressure:
*
Backpressure is honored towards the downstream.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource} that need to be eagerly concatenated * @param maxConcurrency the maximum number of concurrently running inner {@code MaybeSource}s; {@link Integer#MAX_VALUE} * is interpreted as all inner {@code MaybeSource}s can be active at the same time * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @since 3.0.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatEagerDelayError(@NonNull Iterable<@NonNull ? extends MaybeSource> sources, int maxConcurrency) { return Flowable.fromIterable(sources).concatMapEagerDelayError((Function)MaybeToPublisher.instance(), true, maxConcurrency, 1); } /** * Concatenates a {@link Publisher} sequence of {@link MaybeSource}s eagerly into a {@link Flowable} sequence, * delaying errors until all the inner and the outer sequence terminate. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * emitted source {@code MaybeSource}s as they are observed. The operator buffers the values emitted by these * {@code MaybeSource}s and then drains them in order, each one after the previous one completes. *

*
Backpressure:
*
Backpressure is honored towards the downstream and the outer {@code Publisher} is * expected to support backpressure. Violating this assumption, the operator will * signal {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException}.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource}s that need to be eagerly concatenated * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatEagerDelayError(@NonNull Publisher<@NonNull ? extends MaybeSource> sources) { return Flowable.fromPublisher(sources).concatMapEagerDelayError((Function)MaybeToPublisher.instance(), true); } /** * Concatenates a {@link Publisher} sequence of {@link MaybeSource}s eagerly into a {@link Flowable} sequence, * delaying errors until all the inner and the outer sequence terminate and * runs a limited number of the inner {@code MaybeSource}s at once. *

* *

* Eager concatenation means that once a subscriber subscribes, this operator subscribes to all of the * emitted source {@code MaybeSource}s as they are observed. The operator buffers the values emitted by these * {@code MaybeSource}s and then drains them in order, each one after the previous one completes. *

*
Backpressure:
*
Backpressure is honored towards the downstream and the outer {@code Publisher} is * expected to support backpressure. Violating this assumption, the operator will * signal {@link io.reactivex.rxjava3.exceptions.MissingBackpressureException}.
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param sources a sequence of {@code MaybeSource}s that need to be eagerly concatenated * @param maxConcurrency the maximum number of concurrently running inner {@code MaybeSource}s; {@link Integer#MAX_VALUE} * is interpreted as all inner {@code MaybeSource}s can be active at the same time * @return the new {@code Flowable} instance with the specified concatenation behavior * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @since 3.0.0 */ @SuppressWarnings({ "rawtypes", "unchecked" }) @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable concatEagerDelayError(@NonNull Publisher<@NonNull ? extends MaybeSource> sources, int maxConcurrency) { return Flowable.fromPublisher(sources).concatMapEagerDelayError((Function)MaybeToPublisher.instance(), true, maxConcurrency, 1); } /** * Provides an API (via a cold {@code Maybe}) that bridges the reactive world with the callback-style world. *

* *

* Example: *


     * Maybe.<Event>create(emitter -> {
     *     Callback listener = new Callback() {
     *         @Override
     *         public void onEvent(Event e) {
     *             if (e.isNothing()) {
     *                 emitter.onComplete();
     *             } else {
     *                 emitter.onSuccess(e);
     *             }
     *         }
     *
     *         @Override
     *         public void onFailure(Exception e) {
     *             emitter.onError(e);
     *         }
     *     };
     *
     *     AutoCloseable c = api.someMethod(listener);
     *
     *     emitter.setCancellable(c::close);
     *
     * });
     * 
*

* Whenever a {@link MaybeObserver} subscribes to the returned {@code Maybe}, the provided * {@link MaybeOnSubscribe} callback is invoked with a fresh instance of a {@link MaybeEmitter} * that will interact only with that specific {@code MaybeObserver}. If this {@code MaybeObserver} * disposes the flow (making {@link MaybeEmitter#isDisposed} return {@code true}), * other observers subscribed to the same returned {@code Maybe} are not affected. *

*
Scheduler:
*
{@code create} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param onSubscribe the emitter that is called when a {@code MaybeObserver} subscribes to the returned {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onSubscribe} is {@code null} * @see MaybeOnSubscribe * @see Cancellable */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe create(@NonNull MaybeOnSubscribe onSubscribe) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); return RxJavaPlugins.onAssembly(new MaybeCreate<>(onSubscribe)); } /** * Calls a {@link Supplier} for each individual {@link MaybeObserver} to return the actual {@link MaybeSource} source to * be subscribed to. *

* *

*
Scheduler:
*
{@code defer} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param supplier the {@code Supplier} that is called for each individual {@code MaybeObserver} and * returns a {@code MaybeSource} instance to subscribe to * @return the new {@code Maybe} instance * @throws NullPointerException if {@code supplier} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe defer(@NonNull Supplier> supplier) { Objects.requireNonNull(supplier, "supplier is null"); return RxJavaPlugins.onAssembly(new MaybeDefer<>(supplier)); } /** * Returns a (singleton) {@code Maybe} instance that calls {@link MaybeObserver#onComplete onComplete} * immediately. *

* *

*
Scheduler:
*
{@code empty} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @return the shared {@code Maybe} instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") @NonNull public static <@NonNull T> Maybe empty() { return RxJavaPlugins.onAssembly((Maybe)MaybeEmpty.INSTANCE); } /** * Returns a {@code Maybe} that invokes a subscriber's {@link MaybeObserver#onError onError} method when the * subscriber subscribes to it. *

* *

*
Scheduler:
*
{@code error} does not operate by default on a particular {@link Scheduler}.
*
* * @param throwable * the particular {@link Throwable} to pass to {@link MaybeObserver#onError onError} * @param * the type of the item (ostensibly) emitted by the {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code throwable} is {@code null} * @see ReactiveX operators documentation: Throw */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe error(@NonNull Throwable throwable) { Objects.requireNonNull(throwable, "throwable is null"); return RxJavaPlugins.onAssembly(new MaybeError<>(throwable)); } /** * Returns a {@code Maybe} that invokes a {@link MaybeObserver}'s {@link MaybeObserver#onError onError} method when the * {@code MaybeObserver} subscribes to it. *

* *

*
Scheduler:
*
{@code error} does not operate by default on a particular {@link Scheduler}.
*
* * @param supplier * a {@link Supplier} factory to return a {@link Throwable} for each individual {@code MaybeObserver} * @param * the type of the items (ostensibly) emitted by the {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code supplier} is {@code null} * @see ReactiveX operators documentation: Throw */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe error(@NonNull Supplier supplier) { Objects.requireNonNull(supplier, "supplier is null"); return RxJavaPlugins.onAssembly(new MaybeErrorCallable<>(supplier)); } /** * Returns a {@code Maybe} instance that runs the given {@link Action} for each {@link MaybeObserver} and * emits either its exception or simply completes. *

* *

*
Scheduler:
*
{@code fromAction} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the {@code Action} throws an exception, the respective {@link Throwable} is * delivered to the downstream via {@link MaybeObserver#onError(Throwable)}, * except when the downstream has disposed the resulting {@code Maybe} source. * In this latter case, the {@code Throwable} is delivered to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} as an {@link io.reactivex.rxjava3.exceptions.UndeliverableException UndeliverableException}. *
*
* @param the target type * @param action the {@code Action} to run for each {@code MaybeObserver} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code action} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe fromAction(@NonNull Action action) { Objects.requireNonNull(action, "action is null"); return RxJavaPlugins.onAssembly(new MaybeFromAction<>(action)); } /** * Wraps a {@link CompletableSource} into a {@code Maybe}. *

* *

*
Scheduler:
*
{@code fromCompletable} does not operate by default on a particular {@link Scheduler}.
*
* @param the target type * @param completableSource the {@code CompletableSource} to convert from * @return the new {@code Maybe} instance * @throws NullPointerException if {@code completableSource} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe fromCompletable(@NonNull CompletableSource completableSource) { Objects.requireNonNull(completableSource, "completableSource is null"); return RxJavaPlugins.onAssembly(new MaybeFromCompletable<>(completableSource)); } /** * Wraps a {@link SingleSource} into a {@code Maybe}. *

* *

*
Scheduler:
*
{@code fromSingle} does not operate by default on a particular {@link Scheduler}.
*
* @param the target type * @param single the {@code SingleSource} to convert from * @return the new {@code Maybe} instance * @throws NullPointerException if {@code single} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe fromSingle(@NonNull SingleSource single) { Objects.requireNonNull(single, "single is null"); return RxJavaPlugins.onAssembly(new MaybeFromSingle<>(single)); } /** * Returns a {@code Maybe} that invokes the given {@link Callable} for each individual {@link MaybeObserver} that * subscribes and emits the resulting non-{@code null} item via {@code onSuccess} while * considering a {@code null} result from the {@code Callable} as indication for valueless completion * via {@code onComplete}. *

* *

* This operator allows you to defer the execution of the given {@code Callable} until a {@code MaybeObserver} * subscribes to the returned {@code Maybe}. In other terms, this source operator evaluates the given * {@code Callable} "lazily". *

* Note that the {@code null} handling of this operator differs from the similar source operators in the other * {@link io.reactivex.rxjava3.core base reactive classes}. Those operators signal a {@link NullPointerException} if the value returned by their * {@code Callable} is {@code null} while this {@code fromCallable} considers it to indicate the * returned {@code Maybe} is empty. *

*
Scheduler:
*
{@code fromCallable} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
Any non-fatal exception thrown by {@link Callable#call()} will be forwarded to {@code onError}, * except if the {@code MaybeObserver} disposed the subscription in the meantime. In this latter case, * the exception is forwarded to the global error handler via * {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)} wrapped into a * {@link io.reactivex.rxjava3.exceptions.UndeliverableException UndeliverableException}. * Fatal exceptions are rethrown and usually will end up in the executing thread's * {@link java.lang.Thread.UncaughtExceptionHandler#uncaughtException(Thread, Throwable)} handler.
*
* * @param callable * a {@code Callable} instance whose execution should be deferred and performed for each individual * {@code MaybeObserver} that subscribes to the returned {@code Maybe}. * @param * the type of the item emitted by the {@code Maybe}. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code callable} is {@code null} * @see #defer(Supplier) * @see #fromSupplier(Supplier) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static Maybe<@NonNull T> fromCallable(@NonNull Callable callable) { Objects.requireNonNull(callable, "callable is null"); return RxJavaPlugins.onAssembly(new MaybeFromCallable<>(callable)); } /** * Converts a {@link Future} into a {@code Maybe}, treating a {@code null} result as an indication of emptiness. *

* *

* The operator calls {@link Future#get()}, which is a blocking method, on the subscription thread. * It is recommended applying {@link #subscribeOn(Scheduler)} to move this blocking wait to a * background thread, and if the {@link Scheduler} supports it, interrupt the wait when the flow * is disposed. *

* Unlike 1.x, disposing the {@code Maybe} won't cancel the future. If necessary, one can use composition to achieve the * cancellation effect: {@code futureMaybe.doOnDispose(() -> future.cancel(true));}. *

*
Scheduler:
*
{@code fromFuture} does not operate by default on a particular {@code Scheduler}.
*
* * @param future * the source {@code Future} * @param * the type of object that the {@code Future} returns, and also the type of item to be emitted by * the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code future} is {@code null} * @see ReactiveX operators documentation: From * @see #fromCompletionStage(CompletionStage) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe fromFuture(@NonNull Future future) { Objects.requireNonNull(future, "future is null"); return RxJavaPlugins.onAssembly(new MaybeFromFuture<>(future, 0L, null)); } /** * Converts a {@link Future} into a {@code Maybe}, with a timeout on the {@code Future}. *

* *

* The operator calls {@link Future#get(long, TimeUnit)}, which is a blocking method, on the subscription thread. * It is recommended applying {@link #subscribeOn(Scheduler)} to move this blocking wait to a * background thread, and if the {@link Scheduler} supports it, interrupt the wait when the flow * is disposed. *

* Unlike 1.x, disposing the {@code Maybe} won't cancel the future. If necessary, one can use composition to achieve the * cancellation effect: {@code futureMaybe.doOnCancel(() -> future.cancel(true));}. *

*
Scheduler:
*
{@code fromFuture} does not operate by default on a particular {@code Scheduler}.
*
* * @param future * the source {@code Future} * @param timeout * the maximum time to wait before calling {@code get} * @param unit * the {@link TimeUnit} of the {@code timeout} argument * @param * the type of object that the {@code Future} returns, and also the type of item to be emitted by * the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code future} or {@code unit} is {@code null} * @see ReactiveX operators documentation: From * @see #fromCompletionStage(CompletionStage) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe fromFuture(@NonNull Future future, long timeout, @NonNull TimeUnit unit) { Objects.requireNonNull(future, "future is null"); Objects.requireNonNull(unit, "unit is null"); return RxJavaPlugins.onAssembly(new MaybeFromFuture<>(future, timeout, unit)); } /** * Wraps an {@link ObservableSource} into a {@code Maybe} and emits the very first item * or completes if the source is empty. *

* *

*
Scheduler:
*
{@code fromObservable} does not operate by default on a particular {@link Scheduler}.
*
* @param the target type * @param source the {@code ObservableSource} to convert from * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe fromObservable(@NonNull ObservableSource source) { Objects.requireNonNull(source, "source is null"); return RxJavaPlugins.onAssembly(new ObservableElementAtMaybe<>(source, 0L)); } /** * Wraps a {@link Publisher} into a {@code Maybe} and emits the very first item * or completes if the source is empty. *

* *

*
Backpressure:
*
The operator consumes the given {@code Publisher} in an unbounded manner * (requesting {@link Long#MAX_VALUE}) but cancels it after one item received.
*
Scheduler:
*
{@code fromPublisher} does not operate by default on a particular {@link Scheduler}.
*
* @param the target type * @param source the {@code Publisher} to convert from * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) public static <@NonNull T> Maybe fromPublisher(@NonNull Publisher source) { Objects.requireNonNull(source, "source is null"); return RxJavaPlugins.onAssembly(new FlowableElementAtMaybePublisher<>(source, 0L)); } /** * Returns a {@code Maybe} instance that runs the given {@link Runnable} for each {@link MaybeObserver} and * emits either its unchecked exception or simply completes. *

* *

* If the code to be wrapped needs to throw a checked or more broader {@link Throwable} exception, that * exception has to be converted to an unchecked exception by the wrapped code itself. Alternatively, * use the {@link #fromAction(Action)} method which allows the wrapped code to throw any {@code Throwable} * exception and will signal it to observers as-is. *

*
Scheduler:
*
{@code fromRunnable} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the {@code Runnable} throws an exception, the respective {@code Throwable} is * delivered to the downstream via {@link MaybeObserver#onError(Throwable)}, * except when the downstream has disposed this {@code Maybe} source. * In this latter case, the {@code Throwable} is delivered to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} as an {@link io.reactivex.rxjava3.exceptions.UndeliverableException UndeliverableException}. *
*
* @param the target type * @param run the {@code Runnable} to run for each {@code MaybeObserver} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code run} is {@code null} * @see #fromAction(Action) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe fromRunnable(@NonNull Runnable run) { Objects.requireNonNull(run, "run is null"); return RxJavaPlugins.onAssembly(new MaybeFromRunnable<>(run)); } /** * Returns a {@code Maybe} that invokes the given {@link Supplier} for each individual {@link MaybeObserver} that * subscribes and emits the resulting non-{@code null} item via {@code onSuccess} while * considering a {@code null} result from the {@code Supplier} as indication for valueless completion * via {@code onComplete}. *

* This operator allows you to defer the execution of the given {@code Supplier} until a {@code MaybeObserver} * subscribes to the returned {@code Maybe}. In other terms, this source operator evaluates the given * {@code Supplier} "lazily". *

* *

* Note that the {@code null} handling of this operator differs from the similar source operators in the other * {@link io.reactivex.rxjava3.core base reactive classes}. Those operators signal a {@link NullPointerException} if the value returned by their * {@code Supplier} is {@code null} while this {@code fromSupplier} considers it to indicate the * returned {@code Maybe} is empty. *

*
Scheduler:
*
{@code fromSupplier} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
Any non-fatal exception thrown by {@link Supplier#get()} will be forwarded to {@code onError}, * except if the {@code MaybeObserver} disposed the subscription in the meantime. In this latter case, * the exception is forwarded to the global error handler via * {@link io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable)} wrapped into a * {@link io.reactivex.rxjava3.exceptions.UndeliverableException UndeliverableException}. * Fatal exceptions are rethrown and usually will end up in the executing thread's * {@link java.lang.Thread.UncaughtExceptionHandler#uncaughtException(Thread, Throwable)} handler.
*
* * @param supplier * a {@code Supplier} instance whose execution should be deferred and performed for each individual * {@code MaybeObserver} that subscribes to the returned {@code Maybe}. * @param * the type of the item emitted by the {@code Maybe}. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code supplier} is {@code null} * @see #defer(Supplier) * @see #fromCallable(Callable) * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static Maybe<@NonNull T> fromSupplier(@NonNull Supplier supplier) { Objects.requireNonNull(supplier, "supplier is null"); return RxJavaPlugins.onAssembly(new MaybeFromSupplier<>(supplier)); } /** * Returns a {@code Maybe} that emits a specified item. *

* *

* To convert any object into a {@code Maybe} that emits that object, pass that object into the * {@code just} method. *

*
Scheduler:
*
{@code just} does not operate by default on a particular {@link Scheduler}.
*
* * @param item * the item to emit * @param * the type of that item * @return the new {@code Maybe} instance * @throws NullPointerException if {@code item} is {@code null} * @see ReactiveX operators documentation: Just */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe just(T item) { Objects.requireNonNull(item, "item is null"); return RxJavaPlugins.onAssembly(new MaybeJust<>(item)); } /** * Merges an {@link Iterable} sequence of {@link MaybeSource} instances into a single {@link Flowable} sequence, * running all {@code MaybeSource}s at once. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code MaybeSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code MaybeSource}s are disposed. * If more than one {@code MaybeSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(Iterable)} to merge sources and terminate only when all source {@code MaybeSource}s * have completed or failed with an error. *
*
* @param the common and resulting value type * @param sources the {@code Iterable} sequence of {@code MaybeSource} sources * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see #mergeDelayError(Iterable) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable merge(@NonNull Iterable<@NonNull ? extends MaybeSource> sources) { return Flowable.fromIterable(sources).flatMapMaybe(Functions.identity(), false, Integer.MAX_VALUE); } /** * Merges a {@link Publisher} sequence of {@link MaybeSource} instances into a single {@link Flowable} sequence, * running all {@code MaybeSource}s at once. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code MaybeSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code MaybeSource}s are disposed. * If more than one {@code MaybeSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(Publisher)} to merge sources and terminate only when all source {@code MaybeSource}s * have completed or failed with an error. *
*
* @param the common and resulting value type * @param sources the {@code Flowable} sequence of {@code MaybeSource} sources * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see #mergeDelayError(Publisher) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable merge(@NonNull Publisher<@NonNull ? extends MaybeSource> sources) { return merge(sources, Integer.MAX_VALUE); } /** * Merges a {@link Publisher} sequence of {@link MaybeSource} instances into a single {@link Flowable} sequence, * running at most maxConcurrency {@code MaybeSource}s at once. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code MaybeSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code MaybeSource}s are disposed. * If more than one {@code MaybeSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(Publisher, int)} to merge sources and terminate only when all source {@code MaybeSource}s * have completed or failed with an error. *
*
* @param the common and resulting value type * @param sources the {@code Flowable} sequence of {@code MaybeSource} sources * @param maxConcurrency the maximum number of concurrently running {@code MaybeSource}s * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @see #mergeDelayError(Publisher, int) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge(@NonNull Publisher<@NonNull ? extends MaybeSource> sources, int maxConcurrency) { Objects.requireNonNull(sources, "sources is null"); ObjectHelper.verifyPositive(maxConcurrency, "maxConcurrency"); return RxJavaPlugins.onAssembly(new FlowableFlatMapMaybePublisher<>(sources, Functions.identity(), false, maxConcurrency)); } /** * Flattens a {@link MaybeSource} that emits a {@code MaybeSource} into a single {@code MaybeSource} that emits the item * emitted by the nested {@code MaybeSource}, without any transformation. *

* *

*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
The resulting {@code Maybe} emits the outer source's or the inner {@code MaybeSource}'s {@link Throwable} as is. * Unlike the other {@code merge()} operators, this operator won't and can't produce a {@link CompositeException} because there is * only one possibility for the outer or the inner {@code MaybeSource} to emit an {@code onError} signal. * Therefore, there is no need for a {@code mergeDelayError(MaybeSource>)} operator. *
*
* * @param the value type of the sources and the output * @param source * a {@code MaybeSource} that emits a {@code MaybeSource} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source} is {@code null} * @see ReactiveX operators documentation: Merge */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings({ "unchecked", "rawtypes" }) public static <@NonNull T> Maybe merge(@NonNull MaybeSource> source) { Objects.requireNonNull(source, "source is null"); return RxJavaPlugins.onAssembly(new MaybeFlatten(source, Functions.identity())); } /** * Flattens two {@link MaybeSource}s into a single {@link Flowable}, without any transformation. *

* *

* You can combine items emitted by multiple {@code MaybeSource}s so that they appear as a single {@code Flowable}, by * using the {@code merge} method. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code MaybeSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code MaybeSource}s are disposed. * If more than one {@code MaybeSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(MaybeSource, MaybeSource)} to merge sources and terminate only when all source {@code MaybeSource}s * have completed or failed with an error. *
*
* * @param the common value type * @param source1 * a {@code MaybeSource} to be merged * @param source2 * a {@code MaybeSource} to be merged * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} * @see ReactiveX operators documentation: Merge * @see #mergeDelayError(MaybeSource, MaybeSource) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge( @NonNull MaybeSource source1, @NonNull MaybeSource source2 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); return mergeArray(source1, source2); } /** * Flattens three {@link MaybeSource}s into a single {@link Flowable}, without any transformation. *

* *

* You can combine items emitted by multiple {@code MaybeSource}s so that they appear as a single {@code Flowable}, by using * the {@code merge} method. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code MaybeSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code MaybeSource}s are disposed. * If more than one {@code MaybeSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(MaybeSource, MaybeSource, MaybeSource)} to merge sources and terminate only when all source {@code MaybeSource}s * have completed or failed with an error. *
*
* * @param the common value type * @param source1 * a {@code MaybeSource} to be merged * @param source2 * a {@code MaybeSource} to be merged * @param source3 * a {@code MaybeSource} to be merged * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1}, {@code source2} or {@code source3} is {@code null} * @see ReactiveX operators documentation: Merge * @see #mergeDelayError(MaybeSource, MaybeSource, MaybeSource) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); return mergeArray(source1, source2, source3); } /** * Flattens four {@link MaybeSource}s into a single {@link Flowable}, without any transformation. *

* *

* You can combine items emitted by multiple {@code MaybeSource}s so that they appear as a single {@code Flowable}, by using * the {@code merge} method. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code merge} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code MaybeSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code MaybeSource}s are disposed. * If more than one {@code MaybeSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeDelayError(MaybeSource, MaybeSource, MaybeSource, MaybeSource)} to merge sources and terminate only when all source {@code MaybeSource}s * have completed or failed with an error. *
*
* * @param the common value type * @param source1 * a {@code MaybeSource} to be merged * @param source2 * a {@code MaybeSource} to be merged * @param source3 * a {@code MaybeSource} to be merged * @param source4 * a {@code MaybeSource} to be merged * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3} or {@code source4} is {@code null} * @see ReactiveX operators documentation: Merge * @see #mergeDelayError(MaybeSource, MaybeSource, MaybeSource, MaybeSource) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable merge( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4 ) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); return mergeArray(source1, source2, source3, source4); } /** * Merges an array of {@link MaybeSource} instances into a single {@link Flowable} sequence, * running all {@code MaybeSource}s at once. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeArray} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If any of the source {@code MaybeSource}s signal a {@link Throwable} via {@code onError}, the resulting * {@code Flowable} terminates with that {@code Throwable} and all other source {@code MaybeSource}s are disposed. * If more than one {@code MaybeSource} signals an error, the resulting {@code Flowable} may terminate with the * first one's error or, depending on the concurrency of the sources, may terminate with a * {@link CompositeException} containing two or more of the various error signals. * {@code Throwable}s that didn't make into the composite will be sent (individually) to the global error handler via * {@link RxJavaPlugins#onError(Throwable)} method as {@link UndeliverableException} errors. Similarly, {@code Throwable}s * signaled by source(s) after the returned {@code Flowable} has been cancelled or terminated with a * (composite) error will be sent to the same global error handler. * Use {@link #mergeArrayDelayError(MaybeSource...)} to merge sources and terminate only when all source {@code MaybeSource}s * have completed or failed with an error. *
*
* @param the common and resulting value type * @param sources the array sequence of {@code MaybeSource} sources * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see #mergeArrayDelayError(MaybeSource...) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T> Flowable mergeArray(MaybeSource... sources) { Objects.requireNonNull(sources, "sources is null"); if (sources.length == 0) { return Flowable.empty(); } if (sources.length == 1) { @SuppressWarnings("unchecked") MaybeSource source = (MaybeSource)sources[0]; return RxJavaPlugins.onAssembly(new MaybeToFlowable<>(source)); } return RxJavaPlugins.onAssembly(new MaybeMergeArray<>(sources)); } /** * Flattens an array of {@link MaybeSource}s into one {@link Flowable}, in a way that allows a subscriber to receive all * successfully emitted items from each of the source {@code MaybeSource}s without being interrupted by an error * notification from one of them. *

* *

* This behaves like {@link #merge(Publisher)} except that if any of the merged {@code MaybeSource}s notify of an * error via {@link Subscriber#onError onError}, {@code mergeArrayDelayError} will refrain from propagating that * error notification until all of the merged {@code MaybeSource}s have finished emitting items. *

* Even if multiple merged {@code MaybeSource}s send {@code onError} notifications, {@code mergeArrayDelayError} will only * invoke the {@code onError} method of its subscribers once. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeArrayDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources * the array of {@code MaybeSource}s * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs @NonNull public static <@NonNull T> Flowable mergeArrayDelayError(@NonNull MaybeSource... sources) { Objects.requireNonNull(sources, "sources is null"); return Flowable.fromArray(sources).flatMapMaybe(Functions.identity(), true, Math.max(1, sources.length)); } /** * Flattens an {@link Iterable} sequence of {@link MaybeSource}s into one {@link Flowable}, in a way that allows a subscriber to receive all * successfully emitted items from each of the source {@code MaybeSource}s without being interrupted by an error * notification from one of them. *

* *

* This behaves like {@link #merge(Publisher)} except that if any of the merged {@code MaybeSource}s notify of an * error via {@link Subscriber#onError onError}, {@code mergeDelayError} will refrain from propagating that * error notification until all of the merged {@code MaybeSource}s have finished emitting items. *

* *

* Even if multiple merged {@code MaybeSource}s send {@code onError} notifications, {@code mergeDelayError} will only * invoke the {@code onError} method of its subscribers once. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources * the {@code Iterable} of {@code MaybeSource}s * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable mergeDelayError(@NonNull Iterable<@NonNull ? extends MaybeSource> sources) { return Flowable.fromIterable(sources).flatMapMaybe(Functions.identity(), true, Integer.MAX_VALUE); } /** * Flattens a {@link Publisher} that emits {@link MaybeSource}s into one {@link Flowable}, in a way that allows a subscriber to * receive all successfully emitted items from all of the source {@code MaybeSource}s without being interrupted by * an error notification from one of them or even the main {@code Publisher}. *

* *

* This behaves like {@link #merge(Publisher)} except that if any of the merged {@code MaybeSource}s notify of an * error via {@link Subscriber#onError onError}, {@code mergeDelayError} will refrain from propagating that * error notification until all of the merged {@code MaybeSource}s and the main {@code Publisher} have finished emitting items. *

* Even if multiple merged {@code MaybeSource}s send {@code onError} notifications, {@code mergeDelayError} will only * invoke the {@code onError} method of its subscribers once. *

*
Backpressure:
*
The operator honors backpressure from downstream. The outer {@code Publisher} is consumed * in unbounded mode (i.e., no backpressure is applied to it).
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param sources * a {@code Publisher} that emits {@code MaybeSource}s * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Flowable mergeDelayError(@NonNull Publisher<@NonNull ? extends MaybeSource> sources) { return mergeDelayError(sources, Integer.MAX_VALUE); } /** * Flattens a {@link Publisher} that emits {@link MaybeSource}s into one {@link Flowable}, in a way that allows a subscriber to * receive all successfully emitted items from all of the source {@code MaybeSource}s without being interrupted by * an error notification from one of them or even the main {@code Publisher} as well as limiting the total number of active {@code MaybeSource}s. *

* *

* This behaves like {@link #merge(Publisher, int)} except that if any of the merged {@code MaybeSource}s notify of an * error via {@link Subscriber#onError onError}, {@code mergeDelayError} will refrain from propagating that * error notification until all of the merged {@code MaybeSource}s and the main {@code Publisher} have finished emitting items. *

* Even if multiple merged {@code MaybeSource}s send {@code onError} notifications, {@code mergeDelayError} will only * invoke the {@code onError} method of its subscribers once. *

*
Backpressure:
*
The operator honors backpressure from downstream. The outer {@code Publisher} is consumed * in unbounded mode (i.e., no backpressure is applied to it).
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.9 - experimental * @param the common element base type * @param sources * a {@code Publisher} that emits {@code MaybeSource}s * @param maxConcurrency the maximum number of active inner {@code MaybeSource}s to be merged at a time * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @throws IllegalArgumentException if {@code maxConcurrency} is non-positive * @see ReactiveX operators documentation: Merge * @since 2.2 */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError(@NonNull Publisher<@NonNull ? extends MaybeSource> sources, int maxConcurrency) { Objects.requireNonNull(sources, "sources is null"); ObjectHelper.verifyPositive(maxConcurrency, "maxConcurrency"); return RxJavaPlugins.onAssembly(new FlowableFlatMapMaybePublisher<>(sources, Functions.identity(), true, maxConcurrency)); } /** * Flattens two {@link MaybeSource}s into one {@link Flowable}, in a way that allows a subscriber to receive all * successfully emitted items from each of the source {@code MaybeSource}s without being interrupted by an error * notification from one of them. *

* *

* This behaves like {@link #merge(MaybeSource, MaybeSource)} except that if any of the merged {@code MaybeSource}s * notify of an error via {@link Subscriber#onError onError}, {@code mergeDelayError} will refrain from * propagating that error notification until all of the merged {@code MaybeSource}s have finished emitting items. *

* Even if both merged {@code MaybeSource}s send {@code onError} notifications, {@code mergeDelayError} will only * invoke the {@code onError} method of its subscribers once. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param source1 * a {@code MaybeSource} to be merged * @param source2 * a {@code MaybeSource} to be merged * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError(@NonNull MaybeSource source1, @NonNull MaybeSource source2) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); return mergeArrayDelayError(source1, source2); } /** * Flattens three {@link MaybeSource} into one {@link Flowable}, in a way that allows a subscriber to receive all * successfully emitted items from all of the source {@code MaybeSource}s without being interrupted by an error * notification from one of them. *

* *

* This behaves like {@link #merge(MaybeSource, MaybeSource, MaybeSource)} except that if any of the merged * {@code MaybeSource}s notify of an error via {@link Subscriber#onError onError}, {@code mergeDelayError} will refrain * from propagating that error notification until all of the merged {@code MaybeSource}s have finished emitting * items. *

* Even if multiple merged {@code MaybeSource}s send {@code onError} notifications, {@code mergeDelayError} will only * invoke the {@code onError} method of its subscribers once. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param source1 * a {@code MaybeSource} to be merged * @param source2 * a {@code MaybeSource} to be merged * @param source3 * a {@code MaybeSource} to be merged * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1}, {@code source2} or {@code source3} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError(@NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); return mergeArrayDelayError(source1, source2, source3); } /** * Flattens four {@link MaybeSource}s into one {@link Flowable}, in a way that allows a subscriber to receive all * successfully emitted items from all of the source {@code MaybeSource}s without being interrupted by an error * notification from one of them. *

* *

* This behaves like {@link #merge(MaybeSource, MaybeSource, MaybeSource, MaybeSource)} except that if any of * the merged {@code MaybeSource}s notify of an error via {@link Subscriber#onError onError}, {@code mergeDelayError} * will refrain from propagating that error notification until all of the merged {@code MaybeSource}s have finished * emitting items. *

* Even if multiple merged {@code MaybeSource}s send {@code onError} notifications, {@code mergeDelayError} will only * invoke the {@code onError} method of its subscribers once. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeDelayError} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element base type * @param source1 * a {@code MaybeSource} to be merged * @param source2 * a {@code MaybeSource} to be merged * @param source3 * a {@code MaybeSource} to be merged * @param source4 * a {@code MaybeSource} to be merged * @return the new {@code Flowable} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3} or {@code source4} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable mergeDelayError( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); return mergeArrayDelayError(source1, source2, source3, source4); } /** * Returns a {@code Maybe} that never sends any items or notifications to a {@link MaybeObserver}. *

* *

* This {@code Maybe} is useful primarily for testing purposes. *

*
Scheduler:
*
{@code never} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of items (not) emitted by the {@code Maybe} * @return the shared {@code Maybe} instance * @see ReactiveX operators documentation: Never */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @SuppressWarnings("unchecked") @NonNull public static <@NonNull T> Maybe never() { return RxJavaPlugins.onAssembly((Maybe)MaybeNever.INSTANCE); } /** * Returns a {@link Single} that emits a {@link Boolean} value that indicates whether two {@link MaybeSource} sequences are the * same by comparing the items emitted by each {@code MaybeSource} pairwise. *

* *

*
Scheduler:
*
{@code sequenceEqual} does not operate by default on a particular {@link Scheduler}.
*
* * @param source1 * the first {@code MaybeSource} to compare * @param source2 * the second {@code MaybeSource} to compare * @param * the type of items emitted by each {@code MaybeSource} * @return the new {@code Single} instance * @throws NullPointerException if {@code source1} or {@code source2} is {@code null} * @see ReactiveX operators documentation: SequenceEqual */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Single sequenceEqual(@NonNull MaybeSource source1, @NonNull MaybeSource source2) { return sequenceEqual(source1, source2, ObjectHelper.equalsPredicate()); } /** * Returns a {@link Single} that emits a {@link Boolean} value that indicates whether two {@link MaybeSource}s are the * same by comparing the items emitted by each {@code MaybeSource} pairwise based on the results of a specified * equality function. *

* *

*
Scheduler:
*
{@code sequenceEqual} does not operate by default on a particular {@link Scheduler}.
*
* * @param source1 * the first {@code MaybeSource} to compare * @param source2 * the second {@code MaybeSource} to compare * @param isEqual * a function used to compare items emitted by each {@code MaybeSource} * @param * the type of items emitted by each {@code MaybeSource} * @return the new {@code Single} instance * @throws NullPointerException if {@code source1}, {@code source2} or {@code isEqual} is {@code null} * @see ReactiveX operators documentation: SequenceEqual */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Single sequenceEqual(@NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull BiPredicate isEqual) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(isEqual, "isEqual is null"); return RxJavaPlugins.onAssembly(new MaybeEqualSingle<>(source1, source2, isEqual)); } /** * Switches between {@link MaybeSource}s emitted by the source {@link Publisher} whenever * a new {@code MaybeSource} is emitted, disposing the previously running {@code MaybeSource}, * exposing the success items as a {@link Flowable} sequence. *

* *

*
Backpressure:
*
The {@code sources} {@code Publisher} is consumed in an unbounded manner (requesting {@link Long#MAX_VALUE}). * The returned {@code Flowable} respects the backpressure from the downstream.
*
Scheduler:
*
{@code switchOnNext} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
The returned sequence fails with the first error signaled by the {@code sources} {@code Publisher} * or the currently running {@code MaybeSource}, disposing the rest. Late errors are * forwarded to the global error handler via {@link RxJavaPlugins#onError(Throwable)}.
*
* @param the element type of the {@code MaybeSource}s * @param sources the {@code Publisher} sequence of inner {@code MaybeSource}s to switch between * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 * @see #switchOnNextDelayError(Publisher) * @see ReactiveX operators documentation: Switch */ @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable switchOnNext(@NonNull Publisher<@NonNull ? extends MaybeSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new FlowableSwitchMapMaybePublisher<>(sources, Functions.identity(), false)); } /** * Switches between {@link MaybeSource}s emitted by the source {@link Publisher} whenever * a new {@code MaybeSource} is emitted, disposing the previously running {@code MaybeSource}, * exposing the success items as a {@link Flowable} sequence and delaying all errors from * all of them until all terminate. *

* *

*
Backpressure:
*
The {@code sources} {@code Publisher} is consumed in an unbounded manner (requesting {@link Long#MAX_VALUE}). * The returned {@code Flowable} respects the backpressure from the downstream.
*
Scheduler:
*
{@code switchOnNextDelayError} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
The returned {@code Flowable} collects all errors emitted by either the {@code sources} * {@code Publisher} or any inner {@code MaybeSource} and emits them as a {@link CompositeException} * when all sources terminate. If only one source ever failed, its error is emitted as-is at the end.
*
* @param the element type of the {@code MaybeSource}s * @param sources the {@code Publisher} sequence of inner {@code MaybeSource}s to switch between * @return the new {@code Flowable} instance * @throws NullPointerException if {@code sources} is {@code null} * @since 3.0.0 * @see #switchOnNext(Publisher) * @see ReactiveX operators documentation: Switch */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Flowable switchOnNextDelayError(@NonNull Publisher<@NonNull ? extends MaybeSource> sources) { Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new FlowableSwitchMapMaybePublisher<>(sources, Functions.identity(), true)); } /** * Returns a {@code Maybe} that emits {@code 0L} after a specified delay. *

* *

*
Scheduler:
*
{@code timer} operates by default on the {@code computation} {@link Scheduler}.
*
* * @param delay * the initial delay before emitting a single {@code 0L} * @param unit * time units to use for {@code delay} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} is {@code null} * @see ReactiveX operators documentation: Timer */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public static Maybe timer(long delay, @NonNull TimeUnit unit) { return timer(delay, unit, Schedulers.computation()); } /** * Returns a {@code Maybe} that emits {@code 0L} after a specified delay on a specified {@link Scheduler}. *

* *

*
Scheduler:
*
You specify which {@code Scheduler} this operator will use.
*
* * @param delay * the initial delay before emitting a single 0L * @param unit * time units to use for {@code delay} * @param scheduler * the {@code Scheduler} to use for scheduling the item * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @see ReactiveX operators documentation: Timer */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public static Maybe timer(long delay, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new MaybeTimer(Math.max(0L, delay), unit, scheduler)); } /** * Advanced use only: creates a {@code Maybe} instance without * any safeguards by using a callback that is called with a {@link MaybeObserver}. *

* *

*
Scheduler:
*
{@code unsafeCreate} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param onSubscribe the function that is called with the subscribing {@code MaybeObserver} * @return the new {@code Maybe} instance * @throws IllegalArgumentException if {@code onSubscribe} is a {@code Maybe} * @throws NullPointerException if {@code onSubscribe} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe unsafeCreate(@NonNull MaybeSource onSubscribe) { if (onSubscribe instanceof Maybe) { throw new IllegalArgumentException("unsafeCreate(Maybe) should be upgraded"); } Objects.requireNonNull(onSubscribe, "onSubscribe is null"); return RxJavaPlugins.onAssembly(new MaybeUnsafeCreate<>(onSubscribe)); } /** * Constructs a {@code Maybe} that creates a dependent resource object which is disposed of when the * generated {@link MaybeSource} terminates or the downstream calls dispose(). *

* *

*
Scheduler:
*
{@code using} does not operate by default on a particular {@link Scheduler}.
*
* * @param the element type of the generated {@code MaybeSource} * @param the type of the resource associated with the output sequence * @param resourceSupplier * the factory function to create a resource object that depends on the {@code Maybe} * @param sourceSupplier * the factory function to create a {@code MaybeSource} * @param resourceCleanup * the function that will dispose of the resource * @return the new {@code Maybe} instance * @throws NullPointerException if {@code resourceSupplier}, {@code sourceSupplier} or {@code resourceCleanup} is {@code null} * @see ReactiveX operators documentation: Using */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T, @NonNull D> Maybe using(@NonNull Supplier resourceSupplier, @NonNull Function> sourceSupplier, @NonNull Consumer resourceCleanup) { return using(resourceSupplier, sourceSupplier, resourceCleanup, true); } /** * Constructs a {@code Maybe} that creates a dependent resource object which is disposed first ({code eager == true}) * when the generated {@link MaybeSource} terminates or the downstream disposes; or after ({code eager == false}). *

* *

* Eager disposal is particularly appropriate for a synchronous {@code Maybe} that reuses resources. {@code disposeAction} will * only be called once per subscription. *

*
Scheduler:
*
{@code using} does not operate by default on a particular {@link Scheduler}.
*
* * @param the element type of the generated {@code MaybeSource} * @param the type of the resource associated with the output sequence * @param resourceSupplier * the factory function to create a resource object that depends on the {@code Maybe} * @param sourceSupplier * the factory function to create a {@code MaybeSource} * @param resourceCleanup * the function that will dispose of the resource * @param eager * If {@code true} then resource disposal will happen either on a {@code dispose()} call before the upstream is disposed * or just before the emission of a terminal event ({@code onSuccess}, {@code onComplete} or {@code onError}). * If {@code false} the resource disposal will happen either on a {@code dispose()} call after the upstream is disposed * or just after the emission of a terminal event ({@code onSuccess}, {@code onComplete} or {@code onError}). * @return the new {@code Maybe} instance * @throws NullPointerException if {@code resourceSupplier}, {@code sourceSupplier} or {@code resourceCleanup} is {@code null} * @see ReactiveX operators documentation: Using */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T, @NonNull D> Maybe using(@NonNull Supplier resourceSupplier, @NonNull Function> sourceSupplier, @NonNull Consumer resourceCleanup, boolean eager) { Objects.requireNonNull(resourceSupplier, "resourceSupplier is null"); Objects.requireNonNull(sourceSupplier, "sourceSupplier is null"); Objects.requireNonNull(resourceCleanup, "resourceCleanup is null"); return RxJavaPlugins.onAssembly(new MaybeUsing(resourceSupplier, sourceSupplier, resourceCleanup, eager)); } /** * Wraps a {@link MaybeSource} instance into a new {@code Maybe} instance if not already a {@code Maybe} * instance. *

* *

*
Scheduler:
*
{@code wrap} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type * @param source the source to wrap * @return the new wrapped or cast {@code Maybe} instance * @throws NullPointerException if {@code source} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T> Maybe wrap(@NonNull MaybeSource source) { if (source instanceof Maybe) { return RxJavaPlugins.onAssembly((Maybe)source); } Objects.requireNonNull(source, "source is null"); return RxJavaPlugins.onAssembly(new MaybeUnsafeCreate<>(source)); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * items emitted, in sequence, by an {@link Iterable} of other {@link MaybeSource}s. *

* *

* Note on method signature: since Java doesn't allow creating a generic array with {@code new T[]}, the * implementation of this operator has to create an {@code Object[]} instead. Unfortunately, a * {@code Function} passed to the method would trigger a {@link ClassCastException}. *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common value type * @param the zipped result type * @param sources * an {@code Iterable} of source {@code MaybeSource}s * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code zipper} or {@code sources} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T, @NonNull R> Maybe zip(@NonNull Iterable<@NonNull ? extends MaybeSource> sources, @NonNull Function zipper) { Objects.requireNonNull(zipper, "zipper is null"); Objects.requireNonNull(sources, "sources is null"); return RxJavaPlugins.onAssembly(new MaybeZipIterable<>(sources, zipper)); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * two items emitted, in sequence, by two other {@link MaybeSource}s. *

* *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the first source * @param the value type of the second source * @param the zipped result type * @param source1 * the first source {@code MaybeSource} * @param source2 * a second source {@code MaybeSource} * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results * in an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source1}, {@code source2} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull R> Maybe zip( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull BiFunction zipper) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * three items emitted, in sequence, by three other {@link MaybeSource}s. *

* *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the first source * @param the value type of the second source * @param the value type of the third source * @param the zipped result type * @param source1 * the first source {@code MaybeSource} * @param source2 * a second source {@code MaybeSource} * @param source3 * a third source {@code MaybeSource} * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull R> Maybe zip( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull Function3 zipper) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * four items emitted, in sequence, by four other {@link MaybeSource}s. *

* *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the first source * @param the value type of the second source * @param the value type of the third source * @param the value type of the fourth source * @param the zipped result type * @param source1 * the first source {@code MaybeSource} * @param source2 * a second source {@code MaybeSource} * @param source3 * a third source {@code MaybeSource} * @param source4 * a fourth source {@code MaybeSource} * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, * {@code source4} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull R> Maybe zip( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4, @NonNull Function4 zipper) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * five items emitted, in sequence, by five other {@link MaybeSource}s. *

* *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the first source * @param the value type of the second source * @param the value type of the third source * @param the value type of the fourth source * @param the value type of the fifth source * @param the zipped result type * @param source1 * the first source {@code MaybeSource} * @param source2 * a second source {@code MaybeSource} * @param source3 * a third source {@code MaybeSource} * @param source4 * a fourth source {@code MaybeSource} * @param source5 * a fifth source {@code MaybeSource} * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, * {@code source4}, {@code source5} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull R> Maybe zip( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4, @NonNull MaybeSource source5, @NonNull Function5 zipper) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * six items emitted, in sequence, by six other {@link MaybeSource}s. *

* *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the first source * @param the value type of the second source * @param the value type of the third source * @param the value type of the fourth source * @param the value type of the fifth source * @param the value type of the sixth source * @param the zipped result type * @param source1 * the first source {@code MaybeSource} * @param source2 * a second source {@code MaybeSource} * @param source3 * a third source {@code MaybeSource} * @param source4 * a fourth source {@code MaybeSource} * @param source5 * a fifth source {@code MaybeSource} * @param source6 * a sixth source {@code MaybeSource} * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, * {@code source4}, {@code source5}, {@code source6} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull T6, @NonNull R> Maybe zip( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4, @NonNull MaybeSource source5, @NonNull MaybeSource source6, @NonNull Function6 zipper) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(source6, "source6 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * seven items emitted, in sequence, by seven other {@link MaybeSource}s. *

* *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the first source * @param the value type of the second source * @param the value type of the third source * @param the value type of the fourth source * @param the value type of the fifth source * @param the value type of the sixth source * @param the value type of the seventh source * @param the zipped result type * @param source1 * the first source {@code MaybeSource} * @param source2 * a second source {@code MaybeSource} * @param source3 * a third source {@code MaybeSource} * @param source4 * a fourth source {@code MaybeSource} * @param source5 * a fifth source {@code MaybeSource} * @param source6 * a sixth source {@code MaybeSource} * @param source7 * a seventh source {@code MaybeSource} * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, * {@code source4}, {@code source5}, {@code source6}, * {@code source7} or {@code zipper} is {@code null} * @return the new {@code Maybe} instance * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull T6, @NonNull T7, @NonNull R> Maybe zip( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4, @NonNull MaybeSource source5, @NonNull MaybeSource source6, @NonNull MaybeSource source7, @NonNull Function7 zipper) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(source6, "source6 is null"); Objects.requireNonNull(source7, "source7 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6, source7); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * eight items emitted, in sequence, by eight other {@link MaybeSource}s. *

* *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the first source * @param the value type of the second source * @param the value type of the third source * @param the value type of the fourth source * @param the value type of the fifth source * @param the value type of the sixth source * @param the value type of the seventh source * @param the value type of the eighth source * @param the zipped result type * @param source1 * the first source {@code MaybeSource} * @param source2 * a second source {@code MaybeSource} * @param source3 * a third source {@code MaybeSource} * @param source4 * a fourth source {@code MaybeSource} * @param source5 * a fifth source {@code MaybeSource} * @param source6 * a sixth source {@code MaybeSource} * @param source7 * a seventh source {@code MaybeSource} * @param source8 * an eighth source {@code MaybeSource} * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, * {@code source4}, {@code source5}, {@code source6}, * {@code source7}, {@code source8} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull T6, @NonNull T7, @NonNull T8, @NonNull R> Maybe zip( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4, @NonNull MaybeSource source5, @NonNull MaybeSource source6, @NonNull MaybeSource source7, @NonNull MaybeSource source8, @NonNull Function8 zipper) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(source6, "source6 is null"); Objects.requireNonNull(source7, "source7 is null"); Objects.requireNonNull(source8, "source8 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6, source7, source8); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * nine items emitted, in sequence, by nine other {@link MaybeSource}s. *

* *

* This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zip} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the first source * @param the value type of the second source * @param the value type of the third source * @param the value type of the fourth source * @param the value type of the fifth source * @param the value type of the sixth source * @param the value type of the seventh source * @param the value type of the eighth source * @param the value type of the ninth source * @param the zipped result type * @param source1 * the first source {@code MaybeSource} * @param source2 * a second source {@code MaybeSource} * @param source3 * a third source {@code MaybeSource} * @param source4 * a fourth source {@code MaybeSource} * @param source5 * a fifth source {@code MaybeSource} * @param source6 * a sixth source {@code MaybeSource} * @param source7 * a seventh source {@code MaybeSource} * @param source8 * an eighth source {@code MaybeSource} * @param source9 * a ninth source {@code MaybeSource} * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code source1}, {@code source2}, {@code source3}, * {@code source4}, {@code source5}, {@code source6}, * {@code source7}, {@code source8}, {@code source9} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static <@NonNull T1, @NonNull T2, @NonNull T3, @NonNull T4, @NonNull T5, @NonNull T6, @NonNull T7, @NonNull T8, @NonNull T9, @NonNull R> Maybe zip( @NonNull MaybeSource source1, @NonNull MaybeSource source2, @NonNull MaybeSource source3, @NonNull MaybeSource source4, @NonNull MaybeSource source5, @NonNull MaybeSource source6, @NonNull MaybeSource source7, @NonNull MaybeSource source8, @NonNull MaybeSource source9, @NonNull Function9 zipper) { Objects.requireNonNull(source1, "source1 is null"); Objects.requireNonNull(source2, "source2 is null"); Objects.requireNonNull(source3, "source3 is null"); Objects.requireNonNull(source4, "source4 is null"); Objects.requireNonNull(source5, "source5 is null"); Objects.requireNonNull(source6, "source6 is null"); Objects.requireNonNull(source7, "source7 is null"); Objects.requireNonNull(source8, "source8 is null"); Objects.requireNonNull(source9, "source9 is null"); Objects.requireNonNull(zipper, "zipper is null"); return zipArray(Functions.toFunction(zipper), source1, source2, source3, source4, source5, source6, source7, source8, source9); } /** * Returns a {@code Maybe} that emits the results of a specified combiner function applied to combinations of * items emitted, in sequence, by an array of other {@link MaybeSource}s. *

* Note on method signature: since Java doesn't allow creating a generic array with {@code new T[]}, the * implementation of this operator has to create an {@code Object[]} instead. Unfortunately, a * {@code Function} passed to the method would trigger a {@link ClassCastException}. * *

* *

This operator terminates eagerly if any of the source {@code MaybeSource}s signal an {@code onError} or {@code onComplete}. This * also means it is possible some sources may not get subscribed to at all. *

*
Scheduler:
*
{@code zipArray} does not operate by default on a particular {@link Scheduler}.
*
* * @param the common element type * @param the result type * @param sources * an array of source {@code MaybeSource}s * @param zipper * a function that, when applied to an item emitted by each of the source {@code MaybeSource}s, results in * an item that will be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code sources} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @SafeVarargs public static <@NonNull T, @NonNull R> Maybe zipArray(@NonNull Function zipper, @NonNull MaybeSource... sources) { Objects.requireNonNull(sources, "sources is null"); if (sources.length == 0) { return empty(); } Objects.requireNonNull(zipper, "zipper is null"); return RxJavaPlugins.onAssembly(new MaybeZipArray<>(sources, zipper)); } // ------------------------------------------------------------------ // Instance methods // ------------------------------------------------------------------ /** * Mirrors the {@link MaybeSource} (current or provided) that first signals an event. *

* *

*
Scheduler:
*
{@code ambWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * a {@code MaybeSource} competing to react first. A subscription to this provided source will occur after * subscribing to the current source. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: Amb */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe ambWith(@NonNull MaybeSource other) { Objects.requireNonNull(other, "other is null"); return ambArray(this, other); } /** * Waits in a blocking fashion until the current {@code Maybe} signals a success value (which is returned), * {@code null} if completed or an exception (which is propagated). *

* *

*
Scheduler:
*
{@code blockingGet} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the source signals an error, the operator wraps a checked {@link Exception} * into {@link RuntimeException} and throws that. Otherwise, {@code RuntimeException}s and * {@link Error}s are rethrown as they are.
*
* @return the success value */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @Nullable public final T blockingGet() { BlockingMultiObserver observer = new BlockingMultiObserver<>(); subscribe(observer); return observer.blockingGet(); } /** * Waits in a blocking fashion until the current {@code Maybe} signals a success value (which is returned), * defaultValue if completed or an exception (which is propagated). *

* *

*
Scheduler:
*
{@code blockingGet} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the source signals an error, the operator wraps a checked {@link Exception} * into {@link RuntimeException} and throws that. Otherwise, {@code RuntimeException}s and * {@link Error}s are rethrown as they are.
*
* @param defaultValue the default item to return if this {@code Maybe} is empty * @return the success value * @throws NullPointerException if {@code defaultValue} is {@code null} */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final T blockingGet(@NonNull T defaultValue) { Objects.requireNonNull(defaultValue, "defaultValue is null"); BlockingMultiObserver observer = new BlockingMultiObserver<>(); subscribe(observer); return observer.blockingGet(defaultValue); } /** * Subscribes to the current {@code Maybe} and blocks the current thread until it terminates. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If the current {@code Maybe} signals an error, * the {@link Throwable} is routed to the global error handler via {@link RxJavaPlugins#onError(Throwable)}. * If the current thread is interrupted, an {@link InterruptedException} is routed to the same global error handler. *
*
* @since 3.0.0 * @see #blockingSubscribe(Consumer) * @see #blockingSubscribe(Consumer, Consumer) * @see #blockingSubscribe(Consumer, Consumer, Action) */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe() { blockingSubscribe(Functions.emptyConsumer(), Functions.ERROR_CONSUMER, Functions.EMPTY_ACTION); } /** * Subscribes to the current {@code Maybe} and calls given {@code onSuccess} callback on the current thread * when it completes normally. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If either the current {@code Maybe} signals an error or {@code onSuccess} throws, * the respective {@link Throwable} is routed to the global error handler via {@link RxJavaPlugins#onError(Throwable)}. * If the current thread is interrupted, an {@link InterruptedException} is routed to the same global error handler. *
*
* @param onSuccess the {@link Consumer} to call if the current {@code Maybe} succeeds * @throws NullPointerException if {@code onSuccess} is {@code null} * @since 3.0.0 * @see #blockingSubscribe(Consumer, Consumer) * @see #blockingSubscribe(Consumer, Consumer, Action) */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe(@NonNull Consumer onSuccess) { blockingSubscribe(onSuccess, Functions.ERROR_CONSUMER, Functions.EMPTY_ACTION); } /** * Subscribes to the current {@code Maybe} and calls the appropriate callback on the current thread * when it terminates. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If either {@code onSuccess} or {@code onError} throw, the {@link Throwable} is routed to the * global error handler via {@link RxJavaPlugins#onError(Throwable)}. * If the current thread is interrupted, the {@code onError} consumer is called with an {@link InterruptedException}. *
*
* @param onSuccess the {@link Consumer} to call if the current {@code Maybe} succeeds * @param onError the {@code Consumer} to call if the current {@code Maybe} signals an error * @throws NullPointerException if {@code onSuccess} or {@code onError} is {@code null} * @since 3.0.0 * @see #blockingSubscribe(Consumer, Consumer, Action) */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe(@NonNull Consumer onSuccess, @NonNull Consumer onError) { blockingSubscribe(onSuccess, onError, Functions.EMPTY_ACTION); } /** * Subscribes to the current {@code Maybe} and calls the appropriate callback on the current thread * when it terminates. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
If either {@code onSuccess}, {@code onError} or {@code onComplete} throw, the {@link Throwable} is routed to the * global error handler via {@link RxJavaPlugins#onError(Throwable)}. * If the current thread is interrupted, the {@code onError} consumer is called with an {@link InterruptedException}. *
*
* @param onSuccess the {@link Consumer} to call if the current {@code Maybe} succeeds * @param onError the {@code Consumer} to call if the current {@code Maybe} signals an error * @param onComplete the {@link Action} to call if the current {@code Maybe} completes without a value * @throws NullPointerException if {@code onSuccess}, {@code onError} or {@code onComplete} is {@code null} * @since 3.0.0 */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe(@NonNull Consumer onSuccess, @NonNull Consumer onError, @NonNull Action onComplete) { Objects.requireNonNull(onSuccess, "onSuccess is null"); Objects.requireNonNull(onError, "onError is null"); Objects.requireNonNull(onComplete, "onComplete is null"); BlockingMultiObserver observer = new BlockingMultiObserver<>(); subscribe(observer); observer.blockingConsume(onSuccess, onError, onComplete); } /** * Subscribes to the current {@code Maybe} and calls the appropriate {@link MaybeObserver} method on the current thread. *

* *

*
Scheduler:
*
{@code blockingSubscribe} does not operate by default on a particular {@link Scheduler}.
*
Error handling:
*
An {@code onError} signal is delivered to the {@link MaybeObserver#onError(Throwable)} method. * If any of the {@code MaybeObserver}'s methods throw, the {@link RuntimeException} is propagated to the caller of this method. * If the current thread is interrupted, an {@link InterruptedException} is delivered to {@code observer.onError}. *
*
* @param observer the {@code MaybeObserver} to call methods on the current thread * @throws NullPointerException if {@code observer} is {@code null} * @since 3.0.0 */ @SchedulerSupport(SchedulerSupport.NONE) public final void blockingSubscribe(@NonNull MaybeObserver observer) { Objects.requireNonNull(observer, "observer is null"); BlockingDisposableMultiObserver blockingObserver = new BlockingDisposableMultiObserver<>(); observer.onSubscribe(blockingObserver); subscribe(blockingObserver); blockingObserver.blockingConsume(observer); } /** * Returns a {@code Maybe} that subscribes to this {@code Maybe} lazily, caches its event * and replays it, to all the downstream subscribers. *

* *

* The operator subscribes only when the first downstream subscriber subscribes and maintains * a single subscription towards this {@code Maybe}. *

* Note: You sacrifice the ability to dispose the origin when you use the {@code cache}. *

*
Scheduler:
*
{@code cache} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Maybe} instance * @see ReactiveX operators documentation: Replay */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe cache() { return RxJavaPlugins.onAssembly(new MaybeCache<>(this)); } /** * Casts the success value of the current {@code Maybe} into the target type or signals a * {@link ClassCastException} if not compatible. *

* *

*
Scheduler:
*
{@code cast} does not operate by default on a particular {@link Scheduler}.
*
* @param the target type * @param clazz the type token to use for casting the success result from the current {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code clazz} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe cast(@NonNull Class clazz) { Objects.requireNonNull(clazz, "clazz is null"); return map(Functions.castFunction(clazz)); } /** * Transform a {@code Maybe} by applying a particular {@link MaybeTransformer} function to it. *

* *

* This method operates on the {@code Maybe} itself whereas {@link #lift} operates on the {@code Maybe}'s {@link MaybeObserver}s. *

* If the operator you are creating is designed to act on the individual item emitted by a {@code Maybe}, use * {@link #lift}. If your operator is designed to transform the current {@code Maybe} as a whole (for instance, by * applying a particular set of existing RxJava operators to it) use {@code compose}. *

*
Scheduler:
*
{@code compose} does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the {@code Maybe} returned by the transformer function * @param transformer the transformer function, not {@code null} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code transformer} is {@code null} * @see RxJava wiki: Implementing Your Own Operators */ @SuppressWarnings("unchecked") @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull R> Maybe compose(@NonNull MaybeTransformer transformer) { return wrap(((MaybeTransformer) Objects.requireNonNull(transformer, "transformer is null")).apply(this)); } /** * Returns a {@code Maybe} that is based on applying a specified function to the item emitted by the current {@code Maybe}, * where that function returns a {@link MaybeSource}. *

* *

* Note that flatMap and concatMap for {@code Maybe} is the same operation. *

*
Scheduler:
*
{@code concatMap} does not operate by default on a particular {@link Scheduler}.
*
* @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Maybe}, returns a {@code MaybeSource} * @return the new {@code Maybe} instance * @see ReactiveX operators documentation: FlatMap * @throws NullPointerException if {@code mapper} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe concatMap(@NonNull Function> mapper) { return flatMap(mapper); } /** * Returns a {@link Completable} that completes based on applying a specified function to the item emitted by the * current {@code Maybe}, where that function returns a {@code Completable}. *

* *

* This operator is an alias for {@link #flatMapCompletable(Function)}. *

*
Scheduler:
*
{@code concatMapCompletable} does not operate by default on a particular {@link Scheduler}.
*
* * @param mapper * a function that, when applied to the item emitted by the current {@code Maybe}, returns a * {@code Completable} * @return the new {@code Completable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Completable concatMapCompletable(@NonNull Function mapper) { return flatMapCompletable(mapper); } /** * Returns a {@code Maybe} based on applying a specified function to the item emitted by the * current {@code Maybe}, where that function returns a {@link Single}. * When this {@code Maybe} just completes the resulting {@code Maybe} completes as well. *

* *

* This operator is an alias for {@link #flatMapSingle(Function)}. *

*
Scheduler:
*
{@code concatMapSingle} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Maybe}, returns a * {@code Single} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe concatMapSingle(@NonNull Function> mapper) { return flatMapSingle(mapper); } /** * Returns a {@link Flowable} that emits the items emitted from the current {@code Maybe}, then the {@code other} {@link MaybeSource}, one after * the other, without interleaving them. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code concatWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * a {@code MaybeSource} to be concatenated after the current * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: Concat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Flowable concatWith(@NonNull MaybeSource other) { Objects.requireNonNull(other, "other is null"); return concat(this, other); } /** * Returns a {@link Single} that emits a {@link Boolean} that indicates whether the current {@code Maybe} emitted a * specified item. *

* *

*
Scheduler:
*
{@code contains} does not operate by default on a particular {@link Scheduler}.
*
* * @param item * the item to search for in the emissions from the current {@code Maybe}, not {@code null} * @return the new {@code Single} instance * @throws NullPointerException if {@code item} is {@code null} * @see ReactiveX operators documentation: Contains */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single contains(@NonNull Object item) { Objects.requireNonNull(item, "item is null"); return RxJavaPlugins.onAssembly(new MaybeContains<>(this, item)); } /** * Returns a {@link Single} that counts the total number of items emitted (0 or 1) by the current {@code Maybe} and emits * this count as a 64-bit {@link Long}. *

* *

*
Scheduler:
*
{@code count} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Single} instance * @see ReactiveX operators documentation: Count */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single count() { return RxJavaPlugins.onAssembly(new MaybeCount<>(this)); } /** * Returns a {@link Single} that emits the item emitted by the current {@code Maybe} or a specified default item * if the current {@code Maybe} is empty. *

* *

*
Scheduler:
*
{@code defaultIfEmpty} does not operate by default on a particular {@link Scheduler}.
*
* * @param defaultItem * the item to emit if the current {@code Maybe} emits no items * @return the new {@code Single} instance * @throws NullPointerException if {@code defaultItem} is {@code null} * @see ReactiveX operators documentation: DefaultIfEmpty */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single defaultIfEmpty(@NonNull T defaultItem) { Objects.requireNonNull(defaultItem, "defaultItem is null"); return RxJavaPlugins.onAssembly(new MaybeToSingle<>(this, defaultItem)); } /** * Maps the {@link Notification} success value of the current {@code Maybe} back into normal * {@code onSuccess}, {@code onError} or {@code onComplete} signals. *

* *

* The intended use of the {@code selector} function is to perform a * type-safe identity mapping (see example) on a source that is already of type * {@code Notification}. The Java language doesn't allow * limiting instance methods to a certain generic argument shape, therefore, * a function is used to ensure the conversion remains type safe. *

* Regular {@code onError} or {@code onComplete} signals from the current {@code Maybe} are passed along to the downstream. *

*
Scheduler:
*
{@code dematerialize} does not operate by default on a particular {@link Scheduler}.
*
*

* Example: *


     * Maybe.just(Notification.createOnNext(1))
     * .dematerialize(notification -> notification)
     * .test()
     * .assertResult(1);
     * 
* @param the result type * @param selector the function called with the success item and should * return a {@code Notification} instance. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code selector} is {@code null} * @since 3.0.0 * @see #materialize() */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe dematerialize(@NonNull Function> selector) { Objects.requireNonNull(selector, "selector is null"); return RxJavaPlugins.onAssembly(new MaybeDematerialize<>(this, selector)); } /** * Returns a {@code Maybe} that signals the events emitted by the current {@code Maybe} shifted forward in time by a * specified delay. * An error signal will not be delayed. *

* *

*
Scheduler:
*
This version of {@code delay} operates by default on the {@code computation} {@link Scheduler}.
*
* * @param time * the delay to shift the source by * @param unit * the {@link TimeUnit} in which {@code time} is defined * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} is {@code null} * @see ReactiveX operators documentation: Delay * @see #delay(long, TimeUnit, Scheduler, boolean) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public final Maybe delay(long time, @NonNull TimeUnit unit) { return delay(time, unit, Schedulers.computation(), false); } /** * Returns a {@code Maybe} that signals the events emitted by the current {@code Maybe} shifted forward in time by a * specified delay. *

* *

*
Scheduler:
*
This version of {@code delay} operates by default on the {@code computation} {@link Scheduler}.
*
* * @param time the delay to shift the source by * @param unit the {@link TimeUnit} in which {@code time} is defined * @param delayError if {@code true}, both success and error signals are delayed. if {@code false}, only success signals are delayed. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} is {@code null} * @see ReactiveX operators documentation: Delay * @see #delay(long, TimeUnit, Scheduler, boolean) * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public final Maybe delay(long time, @NonNull TimeUnit unit, boolean delayError) { return delay(time, unit, Schedulers.computation(), delayError); } /** * Returns a {@code Maybe} that signals the events emitted by the current {@code Maybe} shifted forward in time by a * specified delay. * An error signal will not be delayed. *

* *

*
Scheduler:
*
you specify the {@link Scheduler} where the non-blocking wait and emission happens
*
* * @param time the delay to shift the source by * @param unit the {@link TimeUnit} in which {@code time} is defined * @param scheduler the {@code Scheduler} to use for delaying * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @see ReactiveX operators documentation: Delay * @see #delay(long, TimeUnit, Scheduler, boolean) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) @NonNull public final Maybe delay(long time, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) { return delay(time, unit, scheduler, false); } /** * Returns a {@code Maybe} that signals the events emitted by the current {@code Maybe} shifted forward in time by a * specified delay running on the specified {@link Scheduler}. *

* *

*
Scheduler:
*
you specify which {@code Scheduler} this operator will use.
*
* * @param time * the delay to shift the source by * @param unit * the {@link TimeUnit} in which {@code time} is defined * @param scheduler * the {@code Scheduler} to use for delaying * @param delayError if {@code true}, both success and error signals are delayed. if {@code false}, only success signals are delayed. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @see ReactiveX operators documentation: Delay * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe delay(long time, @NonNull TimeUnit unit, @NonNull Scheduler scheduler, boolean delayError) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new MaybeDelay<>(this, Math.max(0L, time), unit, scheduler, delayError)); } /** * Delays the emission of this {@code Maybe} until the given {@link Publisher} signals an item or completes. *

* *

*
Backpressure:
*
The {@code delayIndicator} is consumed in an unbounded manner but is cancelled after * the first item it produces.
*
Scheduler:
*
This version of {@code delay} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the subscription delay value type (ignored) * @param delayIndicator * the {@code Publisher} that gets subscribed to when this {@code Maybe} signals an event and that * signal is emitted when the {@code Publisher} signals an item or completes * @return the new {@code Maybe} instance * @throws NullPointerException if {@code delayIndicator} is {@code null} * @see ReactiveX operators documentation: Delay */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) public final <@NonNull U> Maybe delay(@NonNull Publisher delayIndicator) { Objects.requireNonNull(delayIndicator, "delayIndicator is null"); return RxJavaPlugins.onAssembly(new MaybeDelayOtherPublisher<>(this, delayIndicator)); } /** * Returns a {@code Maybe} that delays the subscription to this {@code Maybe} * until the other {@link Publisher} emits an element or completes normally. *

* *

*
Backpressure:
*
The {@code Publisher} source is consumed in an unbounded fashion (without applying backpressure).
*
Scheduler:
*
This method does not operate by default on a particular {@link Scheduler}.
*
* * @param the value type of the other {@code Publisher}, irrelevant * @param subscriptionIndicator the other {@code Publisher} that should trigger the subscription * to this {@code Publisher}. * @throws NullPointerException if {@code subscriptionIndicator} is {@code null} * @return the new {@code Maybe} instance */ @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe delaySubscription(@NonNull Publisher subscriptionIndicator) { Objects.requireNonNull(subscriptionIndicator, "subscriptionIndicator is null"); return RxJavaPlugins.onAssembly(new MaybeDelaySubscriptionOtherPublisher<>(this, subscriptionIndicator)); } /** * Returns a {@code Maybe} that delays the subscription to the current {@code Maybe} by a given amount of time. *

* *

*
Scheduler:
*
This version of {@code delaySubscription} operates by default on the {@code computation} {@link Scheduler}.
*
* * @param time * the time to delay the subscription * @param unit * the time unit of {@code delay} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} is {@code null} * @see ReactiveX operators documentation: Delay * @see #delaySubscription(long, TimeUnit, Scheduler) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public final Maybe delaySubscription(long time, @NonNull TimeUnit unit) { return delaySubscription(time, unit, Schedulers.computation()); } /** * Returns a {@code Maybe} that delays the subscription to the current {@code Maybe} by a given amount of time, * both waiting and subscribing on a given {@link Scheduler}. *

* *

*
Scheduler:
*
You specify which {@code Scheduler} this operator will use.
*
* * @param time * the time to delay the subscription * @param unit * the time unit of {@code delay} * @param scheduler * the {@code Scheduler} on which the waiting and subscription will happen * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @see ReactiveX operators documentation: Delay */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) @NonNull public final Maybe delaySubscription(long time, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) { return delaySubscription(Flowable.timer(time, unit, scheduler)); } /** * Calls the specified {@link Consumer} with the success item after this item has been emitted to the downstream. *

Note that the {@code onAfterSuccess} action is shared between subscriptions and as such * should be thread-safe. *

* *

*
Scheduler:
*
{@code doAfterSuccess} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.0.1 - experimental * @param onAfterSuccess the {@code Consumer} that will be called after emitting an item from upstream to the downstream * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onAfterSuccess} is {@code null} * @since 2.1 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doAfterSuccess(@NonNull Consumer onAfterSuccess) { Objects.requireNonNull(onAfterSuccess, "onAfterSuccess is null"); return RxJavaPlugins.onAssembly(new MaybeDoAfterSuccess<>(this, onAfterSuccess)); } /** * Registers an {@link Action} to be called when this {@code Maybe} invokes either * {@link MaybeObserver#onComplete onSuccess}, * {@link MaybeObserver#onComplete onComplete} or {@link MaybeObserver#onError onError}. *

* *

*
Scheduler:
*
{@code doAfterTerminate} does not operate by default on a particular {@link Scheduler}.
*
* * @param onAfterTerminate * an {@code Action} to be invoked when the current {@code Maybe} finishes * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onAfterTerminate} is {@code null} * @see ReactiveX operators documentation: Do */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doAfterTerminate(@NonNull Action onAfterTerminate) { return RxJavaPlugins.onAssembly(new MaybePeek<>(this, Functions.emptyConsumer(), // onSubscribe Functions.emptyConsumer(), // onSuccess Functions.emptyConsumer(), // onError Functions.EMPTY_ACTION, // onComplete Objects.requireNonNull(onAfterTerminate, "onAfterTerminate is null"), Functions.EMPTY_ACTION // dispose )); } /** * Calls the specified action after this {@code Maybe} signals {@code onSuccess}, {@code onError} or {@code onComplete} or gets disposed by * the downstream. *

* *

* In case of a race between a terminal event and a dispose call, the provided {@code onFinally} action * is executed once per subscription. *

Note that the {@code onFinally} action is shared between subscriptions and as such * should be thread-safe. *

*
Scheduler:
*
{@code doFinally} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.0.1 - experimental * @param onFinally the action called when this {@code Maybe} terminates or gets disposed * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onFinally} is {@code null} * @since 2.1 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doFinally(@NonNull Action onFinally) { Objects.requireNonNull(onFinally, "onFinally is null"); return RxJavaPlugins.onAssembly(new MaybeDoFinally<>(this, onFinally)); } /** * Calls the shared {@link Action} if a {@link MaybeObserver} subscribed to the current {@code Maybe} * disposes the common {@link Disposable} it received via {@code onSubscribe}. *

* *

*
Scheduler:
*
{@code doOnDispose} does not operate by default on a particular {@link Scheduler}.
*
* @param onDispose the action called when the subscription is disposed * @throws NullPointerException if {@code onDispose} is {@code null} * @return the new {@code Maybe} instance */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doOnDispose(@NonNull Action onDispose) { return RxJavaPlugins.onAssembly(new MaybePeek<>(this, Functions.emptyConsumer(), // onSubscribe Functions.emptyConsumer(), // onSuccess Functions.emptyConsumer(), // onError Functions.EMPTY_ACTION, // onComplete Functions.EMPTY_ACTION, // (onSuccess | onError | onComplete) after Objects.requireNonNull(onDispose, "onDispose is null") )); } /** * Invokes an {@link Action} just before the current {@code Maybe} calls {@code onComplete}. *

* *

*
Scheduler:
*
{@code doOnComplete} does not operate by default on a particular {@link Scheduler}.
*
* * @param onComplete * the action to invoke when the current {@code Maybe} calls {@code onComplete} * @return the new {@code Maybe} with the side-effecting behavior applied * @throws NullPointerException if {@code onComplete} is {@code null} * @see ReactiveX operators documentation: Do */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doOnComplete(@NonNull Action onComplete) { return RxJavaPlugins.onAssembly(new MaybePeek<>(this, Functions.emptyConsumer(), // onSubscribe Functions.emptyConsumer(), // onSuccess Functions.emptyConsumer(), // onError Objects.requireNonNull(onComplete, "onComplete is null"), Functions.EMPTY_ACTION, // (onSuccess | onError | onComplete) Functions.EMPTY_ACTION // dispose )); } /** * Calls the shared {@link Consumer} with the error sent via {@code onError} for each * {@link MaybeObserver} that subscribes to the current {@code Maybe}. *

* *

*
Scheduler:
*
{@code doOnError} does not operate by default on a particular {@link Scheduler}.
*
* @param onError the consumer called with the success value of {@code onError} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onError} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doOnError(@NonNull Consumer onError) { return RxJavaPlugins.onAssembly(new MaybePeek<>(this, Functions.emptyConsumer(), // onSubscribe Functions.emptyConsumer(), // onSuccess Objects.requireNonNull(onError, "onError is null"), Functions.EMPTY_ACTION, // onComplete Functions.EMPTY_ACTION, // (onSuccess | onError | onComplete) Functions.EMPTY_ACTION // dispose )); } /** * Calls the given {@code onEvent} callback with the (success value, {@code null}) for an {@code onSuccess}, ({@code null}, throwable) for * an {@code onError} or ({@code null}, {@code null}) for an {@code onComplete} signal from this {@code Maybe} before delivering said * signal to the downstream. *

* *

* The exceptions thrown from the callback will override the event so the downstream receives the * error instead of the original signal. *

*
Scheduler:
*
{@code doOnEvent} does not operate by default on a particular {@link Scheduler}.
*
* @param onEvent the callback to call with the success value or the exception, whichever is not {@code null} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onEvent} is {@code null} */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe doOnEvent(@NonNull BiConsumer<@Nullable ? super T, @Nullable ? super Throwable> onEvent) { Objects.requireNonNull(onEvent, "onEvent is null"); return RxJavaPlugins.onAssembly(new MaybeDoOnEvent<>(this, onEvent)); } /** * Calls the appropriate {@code onXXX} method (shared between all {@link MaybeObserver}s) for the lifecycle events of * the sequence (subscription, disposal). *

* *

*
Scheduler:
*
{@code doOnLifecycle} does not operate by default on a particular {@link Scheduler}.
*
* * @param onSubscribe * a {@link Consumer} called with the {@link Disposable} sent via {@link MaybeObserver#onSubscribe(Disposable)} * @param onDispose * called when the downstream disposes the {@code Disposable} via {@code dispose()} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onSubscribe} or {@code onDispose} is {@code null} * @see ReactiveX operators documentation: Do * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe doOnLifecycle(@NonNull Consumer onSubscribe, @NonNull Action onDispose) { Objects.requireNonNull(onSubscribe, "onSubscribe is null"); Objects.requireNonNull(onDispose, "onDispose is null"); return RxJavaPlugins.onAssembly(new MaybeDoOnLifecycle<>(this, onSubscribe, onDispose)); } /** * Calls the shared {@link Consumer} with the {@link Disposable} sent through the {@code onSubscribe} for each * {@link MaybeObserver} that subscribes to the current {@code Maybe}. *

* *

*
Scheduler:
*
{@code doOnSubscribe} does not operate by default on a particular {@link Scheduler}.
*
* @param onSubscribe the {@code Consumer} called with the {@code Disposable} sent via {@code onSubscribe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onSubscribe} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doOnSubscribe(@NonNull Consumer onSubscribe) { return RxJavaPlugins.onAssembly(new MaybePeek<>(this, Objects.requireNonNull(onSubscribe, "onSubscribe is null"), Functions.emptyConsumer(), // onSuccess Functions.emptyConsumer(), // onError Functions.EMPTY_ACTION, // onComplete Functions.EMPTY_ACTION, // (onSuccess | onError | onComplete) Functions.EMPTY_ACTION // dispose )); } /** * Returns a {@code Maybe} instance that calls the given onTerminate callback * just before this {@code Maybe} completes normally or with an exception. *

* *

* This differs from {@code doAfterTerminate} in that this happens before the {@code onComplete} or * {@code onError} notification. *

*
Scheduler:
*
{@code doOnTerminate} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.2.7 - experimental * @param onTerminate the action to invoke when the consumer calls {@code onComplete} or {@code onError} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onTerminate} is {@code null} * @see ReactiveX operators documentation: Do * @see #doOnTerminate(Action) * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doOnTerminate(@NonNull Action onTerminate) { Objects.requireNonNull(onTerminate, "onTerminate is null"); return RxJavaPlugins.onAssembly(new MaybeDoOnTerminate<>(this, onTerminate)); } /** * Calls the shared {@link Consumer} with the success value sent via {@code onSuccess} for each * {@link MaybeObserver} that subscribes to the current {@code Maybe}. *

* *

*
Scheduler:
*
{@code doOnSuccess} does not operate by default on a particular {@link Scheduler}.
*
* @param onSuccess the {@code Consumer} called with the success value of the upstream * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onSuccess} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe doOnSuccess(@NonNull Consumer onSuccess) { return RxJavaPlugins.onAssembly(new MaybePeek<>(this, Functions.emptyConsumer(), // onSubscribe Objects.requireNonNull(onSuccess, "onSuccess is null"), Functions.emptyConsumer(), // onError Functions.EMPTY_ACTION, // onComplete Functions.EMPTY_ACTION, // (onSuccess | onError | onComplete) Functions.EMPTY_ACTION // dispose )); } /** * Filters the success item of the {@code Maybe} via a predicate function and emitting it if the predicate * returns {@code true}, completing otherwise. *

* *

*
Scheduler:
*
{@code filter} does not operate by default on a particular {@link Scheduler}.
*
* * @param predicate * a function that evaluates the item emitted by the current {@code Maybe}, returning {@code true} * if it passes the filter * @return the new {@code Maybe} instance * @throws NullPointerException if {@code predicate} is {@code null} * @see ReactiveX operators documentation: Filter */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe filter(@NonNull Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return RxJavaPlugins.onAssembly(new MaybeFilter<>(this, predicate)); } /** * Returns a {@code Maybe} that is based on applying a specified function to the item emitted by the current {@code Maybe}, * where that function returns a {@link MaybeSource}. *

* *

*
Scheduler:
*
{@code flatMap} does not operate by default on a particular {@link Scheduler}.
*
*

Note that flatMap and concatMap for {@code Maybe} is the same operation. * * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Maybe}, returns a {@code MaybeSource} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe flatMap(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlatten<>(this, mapper)); } /** * Maps the {@code onSuccess}, {@code onError} or {@code onComplete} signals of the current {@code Maybe} into a {@link MaybeSource} and emits that * {@code MaybeSource}'s signals. *

* *

*
Scheduler:
*
{@code flatMap} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the result type * @param onSuccessMapper * a function that returns a {@code MaybeSource} to merge for the {@code onSuccess} item emitted by this {@code Maybe} * @param onErrorMapper * a function that returns a {@code MaybeSource} to merge for an {@code onError} notification from this {@code Maybe} * @param onCompleteSupplier * a function that returns a {@code MaybeSource} to merge for an {@code onComplete} notification this {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code onSuccessMapper}, {@code onErrorMapper} or {@code onCompleteSupplier} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe flatMap( @NonNull Function> onSuccessMapper, @NonNull Function> onErrorMapper, @NonNull Supplier> onCompleteSupplier) { Objects.requireNonNull(onSuccessMapper, "onSuccessMapper is null"); Objects.requireNonNull(onErrorMapper, "onErrorMapper is null"); Objects.requireNonNull(onCompleteSupplier, "onCompleteSupplier is null"); return RxJavaPlugins.onAssembly(new MaybeFlatMapNotification<>(this, onSuccessMapper, onErrorMapper, onCompleteSupplier)); } /** * Returns a {@code Maybe} that emits the results of a specified function to the pair of values emitted by the * current {@code Maybe} and a specified mapped {@link MaybeSource}. *

* *

*
Scheduler:
*
{@code flatMap} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of items emitted by the {@code MaybeSource} returned by the {@code mapper} function * @param * the type of items emitted by the resulting {@code Maybe} * @param mapper * a function that returns a {@code MaybeSource} for the item emitted by the current {@code Maybe} * @param combiner * a function that combines one item emitted by each of the source and collection {@code MaybeSource} and * returns an item to be emitted by the resulting {@code MaybeSource} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code mapper} or {@code combiner} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U, @NonNull R> Maybe flatMap(@NonNull Function> mapper, @NonNull BiFunction combiner) { Objects.requireNonNull(mapper, "mapper is null"); Objects.requireNonNull(combiner, "combiner is null"); return RxJavaPlugins.onAssembly(new MaybeFlatMapBiSelector<>(this, mapper, combiner)); } /** * Maps the success value of the current {@code Maybe} into an {@link Iterable} and emits its items as a * {@link Flowable} sequence. *

* *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code flattenAsFlowable} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of item emitted by the inner {@code Iterable} * @param mapper * a function that returns an {@code Iterable} sequence of values for when given an item emitted by the * current {@code Maybe} * @return the new {@code Flowable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @see #flattenStreamAsFlowable(Function) */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Flowable flattenAsFlowable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlatMapIterableFlowable<>(this, mapper)); } /** * Maps the success value of the current {@code Maybe} into an {@link Iterable} and emits its items as an * {@link Observable} sequence. *

* *

*
Scheduler:
*
{@code flattenAsObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of item emitted by the resulting {@code Iterable} * @param mapper * a function that returns an {@code Iterable} sequence of values for when given an item emitted by the * current {@code Maybe} * @return the new {@code Observable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Observable flattenAsObservable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlatMapIterableObservable<>(this, mapper)); } /** * Returns an {@link Observable} that is based on applying a specified function to the item emitted by the current {@code Maybe}, * where that function returns an {@link ObservableSource}. *

* *

*
Scheduler:
*
{@code flatMapObservable} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Maybe}, returns an {@code ObservableSource} * @return the new {@code Observable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Observable flatMapObservable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlatMapObservable<>(this, mapper)); } /** * Returns a {@link Flowable} that emits items based on applying a specified function to the item emitted by the * current {@code Maybe}, where that function returns a {@link Publisher}. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the downstream backpressure.
*
Scheduler:
*
{@code flatMapPublisher} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Maybe}, returns a * {@code Flowable} * @return the new {@code Flowable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Flowable flatMapPublisher(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlatMapPublisher<>(this, mapper)); } /** * Returns a {@code Maybe} based on applying a specified function to the item emitted by the * current {@code Maybe}, where that function returns a {@link Single}. * When this {@code Maybe} just completes the resulting {@code Maybe} completes as well. *

* *

*
Scheduler:
*
{@code flatMapSingle} does not operate by default on a particular {@link Scheduler}.
*
* *

History: 2.0.2 - experimental * @param the result value type * @param mapper * a function that, when applied to the item emitted by the current {@code Maybe}, returns a * {@code Single} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap * @since 2.1 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe flatMapSingle(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlatMapSingle<>(this, mapper)); } /** * Returns a {@link Completable} that completes based on applying a specified function to the item emitted by the * current {@code Maybe}, where that function returns a {@code Completable}. *

* *

*
Scheduler:
*
{@code flatMapCompletable} does not operate by default on a particular {@link Scheduler}.
*
* * @param mapper * a function that, when applied to the item emitted by the current {@code Maybe}, returns a * {@code Completable} * @return the new {@code Completable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: FlatMap */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Completable flatMapCompletable(@NonNull Function mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlatMapCompletable<>(this, mapper)); } /** * Hides the identity of this {@code Maybe} and its {@link Disposable}. *

* *

Allows preventing certain identity-based * optimizations (fusion). *

*
Scheduler:
*
{@code hide} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Maybe} instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe hide() { return RxJavaPlugins.onAssembly(new MaybeHide<>(this)); } /** * Returns a {@link Completable} that ignores the item emitted by the current {@code Maybe} and only calls {@code onComplete} or {@code onError}. *

* *

*
Scheduler:
*
{@code ignoreElement} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Completable} instance * @see ReactiveX operators documentation: IgnoreElements */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Completable ignoreElement() { return RxJavaPlugins.onAssembly(new MaybeIgnoreElementCompletable<>(this)); } /** * Returns a {@link Single} that emits {@code true} if the current {@code Maybe} is empty, otherwise {@code false}. *

* *

*
Scheduler:
*
{@code isEmpty} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Single} instance * @see ReactiveX operators documentation: Contains */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single isEmpty() { return RxJavaPlugins.onAssembly(new MaybeIsEmptySingle<>(this)); } /** * This method requires advanced knowledge about building operators, please consider * other standard composition methods first; * Returns a {@code Maybe} which, when subscribed to, invokes the {@link MaybeOperator#apply(MaybeObserver) apply(MaybeObserver)} method * of the provided {@link MaybeOperator} for each individual downstream {@code Maybe} and allows the * insertion of a custom operator by accessing the downstream's {@link MaybeObserver} during this subscription phase * and providing a new {@code MaybeObserver}, containing the custom operator's intended business logic, that will be * used in the subscription process going further upstream. *

* *

* Generally, such a new {@code MaybeObserver} will wrap the downstream's {@code MaybeObserver} and forwards the * {@code onSuccess}, {@code onError} and {@code onComplete} events from the upstream directly or according to the * emission pattern the custom operator's business logic requires. In addition, such operator can intercept the * flow control calls of {@code dispose} and {@code isDisposed} that would have traveled upstream and perform * additional actions depending on the same business logic requirements. *

* Example: *


     * // Step 1: Create the consumer type that will be returned by the MaybeOperator.apply():
     * 
     * public final class CustomMaybeObserver<T> implements MaybeObserver<T>, Disposable {
     *
     *     // The downstream's MaybeObserver that will receive the onXXX events
     *     final MaybeObserver<? super String> downstream;
     *
     *     // The connection to the upstream source that will call this class' onXXX methods
     *     Disposable upstream;
     *
     *     // The constructor takes the downstream subscriber and usually any other parameters
     *     public CustomMaybeObserver(MaybeObserver<? super String> downstream) {
     *         this.downstream = downstream;
     *     }
     *
     *     // In the subscription phase, the upstream sends a Disposable to this class
     *     // and subsequently this class has to send a Disposable to the downstream.
     *     // Note that relaying the upstream's Disposable directly is not allowed in RxJava
     *     @Override
     *     public void onSubscribe(Disposable d) {
     *         if (upstream != null) {
     *             d.dispose();
     *         } else {
     *             upstream = d;
     *             downstream.onSubscribe(this);
     *         }
     *     }
     *
     *     // The upstream calls this with the next item and the implementation's
     *     // responsibility is to emit an item to the downstream based on the intended
     *     // business logic, or if it can't do so for the particular item,
     *     // request more from the upstream
     *     @Override
     *     public void onSuccess(T item) {
     *         String str = item.toString();
     *         if (str.length() < 2) {
     *             downstream.onSuccess(str);
     *         } else {
     *             // Maybe is expected to produce one of the onXXX events only
     *             downstream.onComplete();
     *         }
     *     }
     *
     *     // Some operators may handle the upstream's error while others
     *     // could just forward it to the downstream.
     *     @Override
     *     public void onError(Throwable throwable) {
     *         downstream.onError(throwable);
     *     }
     *
     *     // When the upstream completes, usually the downstream should complete as well.
     *     @Override
     *     public void onComplete() {
     *         downstream.onComplete();
     *     }
     *
     *     // Some operators may use their own resources which should be cleaned up if
     *     // the downstream disposes the flow before it completed. Operators without
     *     // resources can simply forward the dispose to the upstream.
     *     // In some cases, a disposed flag may be set by this method so that other parts
     *     // of this class may detect the dispose and stop sending events
     *     // to the downstream.
     *     @Override
     *     public void dispose() {
     *         upstream.dispose();
     *     }
     *
     *     // Some operators may simply forward the call to the upstream while others
     *     // can return the disposed flag set in dispose().
     *     @Override
     *     public boolean isDisposed() {
     *         return upstream.isDisposed();
     *     }
     * }
     *
     * // Step 2: Create a class that implements the MaybeOperator interface and
     * //         returns the custom consumer type from above in its apply() method.
     * //         Such class may define additional parameters to be submitted to
     * //         the custom consumer type.
     *
     * final class CustomMaybeOperator<T> implements MaybeOperator<String> {
     *     @Override
     *     public MaybeObserver<? super String> apply(MaybeObserver<? super T> upstream) {
     *         return new CustomMaybeObserver<T>(upstream);
     *     }
     * }
     *
     * // Step 3: Apply the custom operator via lift() in a flow by creating an instance of it
     * //         or reusing an existing one.
     *
     * Maybe.just(5)
     * .lift(new CustomMaybeOperator<Integer>())
     * .test()
     * .assertResult("5");
     *
     * Maybe.just(15)
     * .lift(new CustomMaybeOperator<Integer>())
     * .test()
     * .assertResult();
     * 
*

* Creating custom operators can be complicated and it is recommended one consults the * RxJava wiki: Writing operators page about * the tools, requirements, rules, considerations and pitfalls of implementing them. *

* Note that implementing custom operators via this {@code lift()} method adds slightly more overhead by requiring * an additional allocation and indirection per assembled flows. Instead, extending the abstract {@code Maybe} * class and creating a {@link MaybeTransformer} with it is recommended. *

* Note also that it is not possible to stop the subscription phase in {@code lift()} as the {@code apply()} method * requires a non-{@code null} {@code MaybeObserver} instance to be returned, which is then unconditionally subscribed to * the current {@code Maybe}. For example, if the operator decided there is no reason to subscribe to the * upstream source because of some optimization possibility or a failure to prepare the operator, it still has to * return a {@code MaybeObserver} that should immediately dispose the upstream's {@link Disposable} in its * {@code onSubscribe} method. Again, using a {@code MaybeTransformer} and extending the {@code Maybe} is * a better option as {@link #subscribeActual} can decide to not subscribe to its upstream after all. *

*
Scheduler:
*
{@code lift} does not operate by default on a particular {@link Scheduler}, however, the * {@code MaybeOperator} may use a {@code Scheduler} to support its own asynchronous behavior.
*
* * @param the output value type * @param lift the {@code MaybeOperator} that receives the downstream's {@code MaybeObserver} and should return * a {@code MaybeObserver} with custom behavior to be used as the consumer for the current * {@code Maybe}. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code lift} is {@code null} * @see RxJava wiki: Writing operators * @see #compose(MaybeTransformer) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe lift(@NonNull MaybeOperator lift) { Objects.requireNonNull(lift, "lift is null"); return RxJavaPlugins.onAssembly(new MaybeLift<>(this, lift)); } /** * Returns a {@code Maybe} that applies a specified function to the item emitted by the current {@code Maybe} and * emits the result of this function application. *

* *

*
Scheduler:
*
{@code map} does not operate by default on a particular {@link Scheduler}.
*
* * @param the result value type * @param mapper * a function to apply to the item emitted by the {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code mapper} is {@code null} * @see ReactiveX operators documentation: Map */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull R> Maybe map(@NonNull Function mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeMap<>(this, mapper)); } /** * Maps the signal types of this {@code Maybe} into a {@link Notification} of the same kind * and emits it as a {@link Single}'s {@code onSuccess} value to downstream. *

* *

*
Scheduler:
*
{@code materialize} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.2.4 - experimental * @return the new {@code Single} instance * @since 3.0.0 * @see Single#dematerialize(Function) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single> materialize() { return RxJavaPlugins.onAssembly(new MaybeMaterialize<>(this)); } /** * Flattens this {@code Maybe} and another {@link MaybeSource} into a single {@link Flowable}, without any transformation. *

* *

* You can combine items emitted by multiple {@code Maybe}s so that they appear as a single {@code Flowable}, by * using the {@code mergeWith} method. *

*
Backpressure:
*
The operator honors backpressure from downstream.
*
Scheduler:
*
{@code mergeWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * a {@code MaybeSource} to be merged * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: Merge */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Flowable mergeWith(@NonNull MaybeSource other) { Objects.requireNonNull(other, "other is null"); return merge(this, other); } /** * Wraps a {@code Maybe} to emit its item (or notify of its error) on a specified {@link Scheduler}, * asynchronously. *

* *

*
Scheduler:
*
you specify which {@code Scheduler} this operator will use.
*
* * @param scheduler * the {@code Scheduler} to notify subscribers on * @return the new {@code Maybe} instance that its subscribers are notified on the specified * {@code Scheduler} * @throws NullPointerException if {@code scheduler} is {@code null} * @see ReactiveX operators documentation: ObserveOn * @see RxJava Threading Examples * @see #subscribeOn */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe observeOn(@NonNull Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new MaybeObserveOn<>(this, scheduler)); } /** * Filters the items emitted by the current {@code Maybe}, only emitting its success value if that * is an instance of the supplied {@link Class}. *

* *

*
Scheduler:
*
{@code ofType} does not operate by default on a particular {@link Scheduler}.
*
* * @param the output type * @param clazz * the class type to filter the items emitted by the current {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code clazz} is {@code null} * @see ReactiveX operators documentation: Filter */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe ofType(@NonNull Class clazz) { Objects.requireNonNull(clazz, "clazz is null"); return filter(Functions.isInstanceOf(clazz)).cast(clazz); } /** * Calls the specified converter function during assembly time and returns its resulting value. *

* *

* This allows fluent conversion to any other type. *

*
Scheduler:
*
{@code to} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.7 - experimental * @param the resulting object type * @param converter the function that receives the current {@code Maybe} instance and returns a value * @return the converted value * @throws NullPointerException if {@code converter} is {@code null} * @since 2.2 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) public final R to(@NonNull MaybeConverter converter) { return Objects.requireNonNull(converter, "converter is null").apply(this); } /** * Converts this {@code Maybe} into a backpressure-aware {@link Flowable} instance composing cancellation * through. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream.
*
Scheduler:
*
{@code toFlowable} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Flowable} instance */ @SuppressWarnings("unchecked") @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable toFlowable() { if (this instanceof FuseToFlowable) { return ((FuseToFlowable)this).fuseToFlowable(); } return RxJavaPlugins.onAssembly(new MaybeToFlowable<>(this)); } /** * Returns a {@link Future} representing the single value emitted by the current {@code Maybe} * or {@code null} if the current {@code Maybe} is empty. *

* *

* Cancelling the {@code Future} will cancel the subscription to the current {@code Maybe}. *

*
Scheduler:
*
{@code toFuture} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Future} instance * @see ReactiveX documentation: To * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Future toFuture() { return subscribeWith(new FutureMultiObserver<>()); } /** * Converts this {@code Maybe} into an {@link Observable} instance composing disposal * through. *

* *

*
Scheduler:
*
{@code toObservable} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Observable} instance */ @SuppressWarnings("unchecked") @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Observable toObservable() { if (this instanceof FuseToObservable) { return ((FuseToObservable)this).fuseToObservable(); } return RxJavaPlugins.onAssembly(new MaybeToObservable<>(this)); } /** * Converts this {@code Maybe} into a {@link Single} instance composing disposal * through and turning an empty {@code Maybe} into a signal of {@link NoSuchElementException}. *

* *

*
Scheduler:
*
{@code toSingle} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Single} instance * @see #defaultIfEmpty(Object) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Single toSingle() { return RxJavaPlugins.onAssembly(new MaybeToSingle<>(this, null)); } /** * Returns a {@code Maybe} instance that if this {@code Maybe} emits an error, it will emit an {@code onComplete} * and swallow the throwable. *

* *

*
Scheduler:
*
{@code onErrorComplete} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Maybe} instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe onErrorComplete() { return onErrorComplete(Functions.alwaysTrue()); } /** * Returns a {@code Maybe} instance that if this {@code Maybe} emits an error and the predicate returns * {@code true}, it will emit an {@code onComplete} and swallow the throwable. *

* *

*
Scheduler:
*
{@code onErrorComplete} does not operate by default on a particular {@link Scheduler}.
*
* @param predicate the predicate to call when an {@link Throwable} is emitted which should return {@code true} * if the {@code Throwable} should be swallowed and replaced with an {@code onComplete}. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code predicate} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe onErrorComplete(@NonNull Predicate predicate) { Objects.requireNonNull(predicate, "predicate is null"); return RxJavaPlugins.onAssembly(new MaybeOnErrorComplete<>(this, predicate)); } /** * Resumes the flow with the given {@link MaybeSource} when the current {@code Maybe} fails instead of * signaling the error via {@code onError}. *

* *

* You can use this to prevent errors from propagating or to supply fallback data should errors be * encountered. *

*
Scheduler:
*
{@code onErrorResumeWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param fallback * the next {@code MaybeSource} that will take over if the current {@code Maybe} encounters * an error * @return the new {@code Maybe} instance * @throws NullPointerException if {@code fallback} is {@code null} * @see ReactiveX operators documentation: Catch */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe onErrorResumeWith(@NonNull MaybeSource fallback) { Objects.requireNonNull(fallback, "fallback is null"); return onErrorResumeNext(Functions.justFunction(fallback)); } /** * Resumes the flow with a {@link MaybeSource} returned for the failure {@link Throwable} of the current {@code Maybe} by a * function instead of signaling the error via {@code onError}. *

* *

* You can use this to prevent errors from propagating or to supply fallback data should errors be * encountered. *

*
Scheduler:
*
{@code onErrorResumeNext} does not operate by default on a particular {@link Scheduler}.
*
* * @param fallbackSupplier * a function that returns a {@code MaybeSource} that will take over if the current {@code Maybe} encounters * an error * @return the new {@code Maybe} instance * @throws NullPointerException if {@code fallbackSupplier} is {@code null} * @see ReactiveX operators documentation: Catch */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe onErrorResumeNext(@NonNull Function> fallbackSupplier) { Objects.requireNonNull(fallbackSupplier, "fallbackSupplier is null"); return RxJavaPlugins.onAssembly(new MaybeOnErrorNext<>(this, fallbackSupplier)); } /** * Ends the flow with a success item returned by a function for the {@link Throwable} error signaled by the current * {@code Maybe} instead of signaling the error via {@code onError}. *

* *

* You can use this to prevent errors from propagating or to supply fallback data should errors be * encountered. *

*
Scheduler:
*
{@code onErrorReturn} does not operate by default on a particular {@link Scheduler}.
*
* * @param itemSupplier * a function that returns a single value that will be emitted as success value * the current {@code Maybe} signals an {@code onError} event * @return the new {@code Maybe} instance * @throws NullPointerException if {@code itemSupplier} is {@code null} * @see ReactiveX operators documentation: Catch */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe onErrorReturn(@NonNull Function itemSupplier) { Objects.requireNonNull(itemSupplier, "itemSupplier is null"); return RxJavaPlugins.onAssembly(new MaybeOnErrorReturn<>(this, itemSupplier)); } /** * Ends the flow with the given success item when the current {@code Maybe} fails instead of signaling the error via {@code onError}. *

* *

* You can use this to prevent errors from propagating or to supply fallback data should errors be * encountered. *

*
Scheduler:
*
{@code onErrorReturnItem} does not operate by default on a particular {@link Scheduler}.
*
* * @param item * the value that is emitted as {@code onSuccess} in case the current {@code Maybe} signals an {@code onError} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code item} is {@code null} * @see ReactiveX operators documentation: Catch */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe onErrorReturnItem(@NonNull T item) { Objects.requireNonNull(item, "item is null"); return onErrorReturn(Functions.justFunction(item)); } /** * Nulls out references to the upstream producer and downstream {@link MaybeObserver} if * the sequence is terminated or downstream calls {@code dispose()}. *

* *

*
Scheduler:
*
{@code onTerminateDetach} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code Maybe} instance * the sequence is terminated or downstream calls {@code dispose()} */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe onTerminateDetach() { return RxJavaPlugins.onAssembly(new MaybeDetach<>(this)); } /** * Returns a {@link Flowable} that repeats the sequence of items emitted by the current {@code Maybe} indefinitely. *

* *

*
Backpressure:
*
The operator honors downstream backpressure.
*
Scheduler:
*
{@code repeat} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Flowable} instance * @see ReactiveX operators documentation: Repeat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable repeat() { return repeat(Long.MAX_VALUE); } /** * Returns a {@link Flowable} that repeats the sequence of items emitted by the current {@code Maybe} at most * {@code count} times. *

* *

*
Backpressure:
*
This operator honors downstream backpressure.
*
Scheduler:
*
{@code repeat} does not operate by default on a particular {@link Scheduler}.
*
* * @param times * the number of times the current {@code Maybe} items are repeated, a count of 0 will yield an empty * sequence * @return the new {@code Flowable} instance * @throws IllegalArgumentException * if {@code times} is negative * @see ReactiveX operators documentation: Repeat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable repeat(long times) { return toFlowable().repeat(times); } /** * Returns a {@link Flowable} that repeats the sequence of items emitted by the current {@code Maybe} until * the provided stop function returns {@code true}. *

* *

*
Backpressure:
*
This operator honors downstream backpressure.
*
Scheduler:
*
{@code repeatUntil} does not operate by default on a particular {@link Scheduler}.
*
* * @param stop * a boolean supplier that is called when the current {@code Flowable} completes and unless it returns * {@code false}, the current {@code Flowable} is resubscribed * @return the new {@code Flowable} instance * @throws NullPointerException * if {@code stop} is {@code null} * @see ReactiveX operators documentation: Repeat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable repeatUntil(@NonNull BooleanSupplier stop) { return toFlowable().repeatUntil(stop); } /** * Returns a {@link Flowable} that emits the same values as the current {@code Maybe} with the exception of an * {@code onComplete}. An {@code onComplete} notification from the source will result in the emission of * a {@code void} item to the {@code Flowable} provided as an argument to the {@code notificationHandler} * function. If that {@link Publisher} calls {@code onComplete} or {@code onError} then {@code repeatWhen} will * call {@code onComplete} or {@code onError} on the child observer. Otherwise, this operator will * resubscribe to the current {@code Maybe}. *

* *

*
Backpressure:
*
The operator honors downstream backpressure and expects the source {@code Publisher} to honor backpressure as well. * If this expectation is violated, the operator may throw an {@link IllegalStateException}.
*
Scheduler:
*
{@code repeatWhen} does not operate by default on a particular {@link Scheduler}.
*
* * @param handler * receives a {@code Publisher} of notifications with which a user can complete or error, aborting the repeat. * @return the new {@code Flowable} instance * @throws NullPointerException if {@code handler} is {@code null} * @see ReactiveX operators documentation: Repeat */ @BackpressureSupport(BackpressureKind.FULL) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Flowable repeatWhen(@NonNull Function, @NonNull ? extends Publisher<@NonNull ?>> handler) { return toFlowable().repeatWhen(handler); } /** * Returns a {@code Maybe} that mirrors the current {@code Maybe}, resubscribing to it if it calls {@code onError} * (infinite retry count). *

* *

* If the current {@code Maybe} calls {@link MaybeObserver#onError}, this operator will resubscribe to the current * {@code Maybe} rather than propagating the {@code onError} call. *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@code Maybe} instance * @see ReactiveX operators documentation: Retry */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe retry() { return retry(Long.MAX_VALUE, Functions.alwaysTrue()); } /** * Returns a {@code Maybe} that mirrors the current {@code Maybe}, resubscribing to it if it calls {@code onError} * and the predicate returns {@code true} for that specific exception and retry count. *

* *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* * @param predicate * the predicate that determines if a resubscription may happen in case of a specific exception * and retry count * @return the new {@code Maybe} instance * @throws NullPointerException if {@code predicate} is {@code null} * @see #retry() * @see ReactiveX operators documentation: Retry */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe retry(@NonNull BiPredicate predicate) { return toFlowable().retry(predicate).singleElement(); } /** * Returns a {@code Maybe} that mirrors the current {@code Maybe}, resubscribing to it if it calls {@code onError} * up to a specified number of retries. *

* *

* If the current {@code Maybe} calls {@link MaybeObserver#onError}, this operator will resubscribe to the current * {@code Maybe} for a maximum of {@code count} resubscriptions rather than propagating the * {@code onError} call. *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* * @param times * the number of times to resubscribe if the current {@code Maybe} fails * @return the new {@code Maybe} instance * @throws IllegalArgumentException if {@code times} is negative * @see ReactiveX operators documentation: Retry */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe retry(long times) { return retry(times, Functions.alwaysTrue()); } /** * Retries at most {@code times} or until the predicate returns {@code false}, whichever happens first. *

* *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* @param times the number of times to resubscribe if the current {@code Maybe} fails * @param predicate the predicate called with the failure {@link Throwable} and should return {@code true} to trigger a retry. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code predicate} is {@code null} * @throws IllegalArgumentException if {@code times} is negative */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe retry(long times, @NonNull Predicate predicate) { return toFlowable().retry(times, predicate).singleElement(); } /** * Retries the current {@code Maybe} if it fails and the predicate returns {@code true}. *

* *

*
Scheduler:
*
{@code retry} does not operate by default on a particular {@link Scheduler}.
*
* * @param predicate the predicate that receives the failure {@link Throwable} and should return {@code true} to trigger a retry. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code predicate} is {@code null} */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe retry(@NonNull Predicate predicate) { return retry(Long.MAX_VALUE, predicate); } /** * Retries until the given stop function returns {@code true}. *

* *

*
Scheduler:
*
{@code retryUntil} does not operate by default on a particular {@link Scheduler}.
*
* @param stop the function that should return {@code true} to stop retrying * @return the new {@code Maybe} instance * @throws NullPointerException if {@code stop} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe retryUntil(@NonNull BooleanSupplier stop) { Objects.requireNonNull(stop, "stop is null"); return retry(Long.MAX_VALUE, Functions.predicateReverseFor(stop)); } /** * Returns a {@code Maybe} that emits the same values as the current {@code Maybe} with the exception of an * {@code onError}. An {@code onError} notification from the source will result in the emission of a * {@link Throwable} item to the {@link Flowable} provided as an argument to the {@code notificationHandler} * function. If the returned {@link Publisher} calls {@code onComplete} or {@code onError} then {@code retry} will call * {@code onComplete} or {@code onError} on the child subscription. Otherwise, this operator will * resubscribe to the current {@code Maybe}. *

* *

* Example: * * This retries 3 times, each time incrementing the number of seconds it waits. * *


     *  Maybe.create((MaybeEmitter<? super String> s) -> {
     *      System.out.println("subscribing");
     *      s.onError(new RuntimeException("always fails"));
     *  }, BackpressureStrategy.BUFFER).retryWhen(attempts -> {
     *      return attempts.zipWith(Publisher.range(1, 3), (n, i) -> i).flatMap(i -> {
     *          System.out.println("delay retry by " + i + " second(s)");
     *          return Flowable.timer(i, TimeUnit.SECONDS);
     *      });
     *  }).blockingForEach(System.out::println);
     * 
* * Output is: * *
 {@code
     * subscribing
     * delay retry by 1 second(s)
     * subscribing
     * delay retry by 2 second(s)
     * subscribing
     * delay retry by 3 second(s)
     * subscribing
     * } 
*

* Note that the inner {@code Publisher} returned by the handler function should signal * either {@code onNext}, {@code onError} or {@code onComplete} in response to the received * {@code Throwable} to indicate the operator should retry or terminate. If the upstream to * the operator is asynchronous, signalling {@code onNext} followed by {@code onComplete} immediately may * result in the sequence to be completed immediately. Similarly, if this inner * {@code Publisher} signals {@code onError} or {@code onComplete} while the upstream is * active, the sequence is terminated with the same signal immediately. *

* The following example demonstrates how to retry an asynchronous source with a delay: *


     * Maybe.timer(1, TimeUnit.SECONDS)
     *     .doOnSubscribe(s -> System.out.println("subscribing"))
     *     .map(v -> { throw new RuntimeException(); })
     *     .retryWhen(errors -> {
     *         AtomicInteger counter = new AtomicInteger();
     *         return errors
     *                   .takeWhile(e -> counter.getAndIncrement() != 3)
     *                   .flatMap(e -> {
     *                       System.out.println("delay retry by " + counter.get() + " second(s)");
     *                       return Flowable.timer(counter.get(), TimeUnit.SECONDS);
     *                   });
     *     })
     *     .blockingGet();
     * 
*
*
Scheduler:
*
{@code retryWhen} does not operate by default on a particular {@link Scheduler}.
*
* * @param handler * receives a {@code Publisher} of notifications with which a user can complete or error, aborting the * retry * @return the new {@code Maybe} instance * @throws NullPointerException if {@code handler} is {@code null} * @see ReactiveX operators documentation: Retry */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Maybe retryWhen( @NonNull Function, @NonNull ? extends Publisher<@NonNull ?>> handler) { return toFlowable().retryWhen(handler).singleElement(); } /** * Wraps the given {@link MaybeObserver}, catches any {@link RuntimeException}s thrown by its * {@link MaybeObserver#onSubscribe(Disposable)}, {@link MaybeObserver#onSuccess(Object)}, * {@link MaybeObserver#onError(Throwable)} or {@link MaybeObserver#onComplete()} methods * and routes those to the global error handler via {@link RxJavaPlugins#onError(Throwable)}. *

* By default, the {@code Maybe} protocol forbids the {@code onXXX} methods to throw, but some * {@code MaybeObserver} implementation may do it anyway, causing undefined behavior in the * upstream. This method and the underlying safe wrapper ensures such misbehaving consumers don't * disrupt the protocol. *

*
Scheduler:
*
{@code safeSubscribe} does not operate by default on a particular {@link Scheduler}.
*
* @param observer the potentially misbehaving {@code MaybeObserver} * @throws NullPointerException if {@code observer} is {@code null} * @see #subscribe(Consumer,Consumer, Action) * @since 3.0.0 */ @SchedulerSupport(SchedulerSupport.NONE) public final void safeSubscribe(@NonNull MaybeObserver observer) { Objects.requireNonNull(observer, "observer is null"); subscribe(new SafeMaybeObserver<>(observer)); } /** * Returns a {@link Flowable} which first runs the other {@link CompletableSource} * then the current {@code Maybe} if the other completed normally. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code CompletableSource} to run first * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) public final Flowable startWith(@NonNull CompletableSource other) { Objects.requireNonNull(other, "other is null"); return Flowable.concat(Completable.wrap(other).toFlowable(), toFlowable()); } /** * Returns a {@link Flowable} which first runs the other {@link SingleSource} * then the current {@code Maybe} if the other succeeded normally. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code SingleSource} to run first * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) public final Flowable startWith(@NonNull SingleSource other) { Objects.requireNonNull(other, "other is null"); return Flowable.concat(Single.wrap(other).toFlowable(), toFlowable()); } /** * Returns a {@link Flowable} which first runs the other {@link MaybeSource} * then the current {@code Maybe} if the other succeeded or completed normally. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer.
*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code MaybeSource} to run first * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) public final Flowable startWith(@NonNull MaybeSource other) { Objects.requireNonNull(other, "other is null"); return Flowable.concat(Maybe.wrap(other).toFlowable(), toFlowable()); } /** * Returns an {@link Observable} which first delivers the events * of the other {@link ObservableSource} then runs the current {@code Maybe}. *

* *

*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code ObservableSource} to run first * @return the new {@code Observable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Observable startWith(@NonNull ObservableSource other) { Objects.requireNonNull(other, "other is null"); return Observable.wrap(other).concatWith(this.toObservable()); } /** * Returns a {@link Flowable} which first delivers the events * of the other {@link Publisher} then runs the current {@code Maybe}. *

* *

*
Backpressure:
*
The returned {@code Flowable} honors the backpressure of the downstream consumer * and expects the other {@code Publisher} to honor it as well.
*
Scheduler:
*
{@code startWith} does not operate by default on a particular {@link Scheduler}.
*
* @param other the other {@code Publisher} to run first * @return the new {@code Flowable} instance * @throws NullPointerException if {@code other} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @BackpressureSupport(BackpressureKind.FULL) @SchedulerSupport(SchedulerSupport.NONE) public final Flowable startWith(@NonNull Publisher other) { Objects.requireNonNull(other, "other is null"); return toFlowable().startWith(other); } /** * Subscribes to a {@code Maybe} and ignores {@code onSuccess} and {@code onComplete} emissions. *

* If the {@code Maybe} emits an error, it is wrapped into an * {@link io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException} * and routed to the {@link RxJavaPlugins#onError(Throwable)} handler. *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @return the new {@link Disposable} instance that can be used for disposing the subscription at any time * @see ReactiveX operators documentation: Subscribe * @see #subscribe(Consumer, Consumer, Action, DisposableContainer) */ @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Disposable subscribe() { return subscribe(Functions.emptyConsumer(), Functions.ON_ERROR_MISSING, Functions.EMPTY_ACTION); } /** * Subscribes to a {@code Maybe} and provides a callback to handle the items it emits. *

* If the {@code Maybe} emits an error, it is wrapped into an * {@link io.reactivex.rxjava3.exceptions.OnErrorNotImplementedException OnErrorNotImplementedException} * and routed to the {@link RxJavaPlugins#onError(Throwable)} handler. *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @param onSuccess * the {@code Consumer} you have designed to accept a success value from the {@code Maybe} * @return the new {@link Disposable} instance that can be used for disposing the subscription at any time * @throws NullPointerException * if {@code onSuccess} is {@code null} * @see ReactiveX operators documentation: Subscribe * @see #subscribe(Consumer, Consumer, Action, DisposableContainer) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Disposable subscribe(@NonNull Consumer onSuccess) { return subscribe(onSuccess, Functions.ON_ERROR_MISSING, Functions.EMPTY_ACTION); } /** * Subscribes to a {@code Maybe} and provides callbacks to handle the items it emits and any error * notification it issues. *
*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @param onSuccess * the {@code Consumer} you have designed to accept a success value from the {@code Maybe} * @param onError * the {@code Consumer} you have designed to accept any error notification from the * {@code Maybe} * @return the new {@link Disposable} instance that can be used for disposing the subscription at any time * @throws NullPointerException * if {@code onSuccess} is {@code null}, or * if {@code onError} is {@code null} * @see ReactiveX operators documentation: Subscribe * @see #subscribe(Consumer, Consumer, Action, DisposableContainer) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Disposable subscribe(@NonNull Consumer onSuccess, @NonNull Consumer onError) { return subscribe(onSuccess, onError, Functions.EMPTY_ACTION); } /** * Subscribes to a {@code Maybe} and provides callbacks to handle the items it emits and any error or * completion notification it issues. *
*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* * @param onSuccess * the {@code Consumer} you have designed to accept a success value from the {@code Maybe} * @param onError * the {@code Consumer} you have designed to accept any error notification from the * {@code Maybe} * @param onComplete * the {@link Action} you have designed to accept a completion notification from the * {@code Maybe} * @return the new {@link Disposable} instance that can be used for disposing the subscription at any time * @throws NullPointerException * if {@code onSuccess}, {@code onError} or * {@code onComplete} is {@code null} * @see ReactiveX operators documentation: Subscribe * @see #subscribe(Consumer, Consumer, Action, DisposableContainer) */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Disposable subscribe(@NonNull Consumer onSuccess, @NonNull Consumer onError, @NonNull Action onComplete) { Objects.requireNonNull(onSuccess, "onSuccess is null"); Objects.requireNonNull(onError, "onError is null"); Objects.requireNonNull(onComplete, "onComplete is null"); return subscribeWith(new MaybeCallbackObserver<>(onSuccess, onError, onComplete)); } /** * Wraps the given onXXX callbacks into a {@link Disposable} {@link MaybeObserver}, * adds it to the given {@link DisposableContainer} and ensures, that if the upstream * terminates or this particular {@code Disposable} is disposed, the {@code MaybeObserver} is removed * from the given composite. *

* The {@code MaybeObserver} will be removed after the callback for the terminal event has been invoked. *

*
Scheduler:
*
{@code subscribe} does not operate by default on a particular {@link Scheduler}.
*
* @param onSuccess the callback for upstream items * @param onError the callback for an upstream error * @param onComplete the callback for an upstream completion without any value or error * @param container the {@code DisposableContainer} (such as {@link CompositeDisposable}) to add and remove the * created {@code Disposable} {@code MaybeObserver} * @return the {@code Disposable} that allows disposing the particular subscription. * @throws NullPointerException * if {@code onSuccess}, {@code onError}, * {@code onComplete} or {@code container} is {@code null} * @since 3.1.0 */ @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final Disposable subscribe( @NonNull Consumer onSuccess, @NonNull Consumer onError, @NonNull Action onComplete, @NonNull DisposableContainer container) { Objects.requireNonNull(onSuccess, "onSuccess is null"); Objects.requireNonNull(onError, "onError is null"); Objects.requireNonNull(onComplete, "onComplete is null"); Objects.requireNonNull(container, "container is null"); DisposableAutoReleaseMultiObserver observer = new DisposableAutoReleaseMultiObserver<>( container, onSuccess, onError, onComplete); container.add(observer); subscribe(observer); return observer; } @SchedulerSupport(SchedulerSupport.NONE) @Override public final void subscribe(@NonNull MaybeObserver observer) { Objects.requireNonNull(observer, "observer is null"); observer = RxJavaPlugins.onSubscribe(this, observer); Objects.requireNonNull(observer, "The RxJavaPlugins.onSubscribe hook returned a null MaybeObserver. Please check the handler provided to RxJavaPlugins.setOnMaybeSubscribe for invalid null returns. Further reading: https://github.com/ReactiveX/RxJava/wiki/Plugins"); try { subscribeActual(observer); } catch (NullPointerException ex) { throw ex; } catch (Throwable ex) { Exceptions.throwIfFatal(ex); NullPointerException npe = new NullPointerException("subscribeActual failed"); npe.initCause(ex); throw npe; } } /** * Implement this method in subclasses to handle the incoming {@link MaybeObserver}s. *

There is no need to call any of the plugin hooks on the current {@code Maybe} instance or * the {@code MaybeObserver}; all hooks and basic safeguards have been * applied by {@link #subscribe(MaybeObserver)} before this method gets called. * @param observer the {@code MaybeObserver} to handle, not {@code null} */ protected abstract void subscribeActual(@NonNull MaybeObserver observer); /** * Asynchronously subscribes subscribers to this {@code Maybe} on the specified {@link Scheduler}. *

* *

*
Scheduler:
*
you specify which {@code Scheduler} this operator will use.
*
* * @param scheduler * the {@code Scheduler} to perform subscription actions on * @return the new {@code Maybe} instance * @throws NullPointerException if {@code scheduler} is {@code null} * @see ReactiveX operators documentation: SubscribeOn * @see RxJava Threading Examples * @see #observeOn */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe subscribeOn(@NonNull Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new MaybeSubscribeOn<>(this, scheduler)); } /** * Subscribes a given {@link MaybeObserver} (subclass) to this {@code Maybe} and returns the given * {@code MaybeObserver} as is. *

Usage example: *


     * Maybe<Integer> source = Maybe.just(1);
     * CompositeDisposable composite = new CompositeDisposable();
     *
     * DisposableMaybeObserver<Integer> ds = new DisposableMaybeObserver<>() {
     *     // ...
     * };
     *
     * composite.add(source.subscribeWith(ds));
     * 
*
*
Scheduler:
*
{@code subscribeWith} does not operate by default on a particular {@link Scheduler}.
*
* @param the type of the {@code MaybeObserver} to use and return * @param observer the {@code MaybeObserver} (subclass) to use and return, not {@code null} * @return the input {@code observer} * @throws NullPointerException if {@code observer} is {@code null} */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull E extends MaybeObserver> E subscribeWith(E observer) { subscribe(observer); return observer; } /** * Returns a {@code Maybe} that emits the items emitted by the current {@code Maybe} or the items of an alternate * {@link MaybeSource} if the current {@code Maybe} is empty. *

* *

*
Scheduler:
*
{@code switchIfEmpty} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * the alternate {@code MaybeSource} to subscribe to if the main does not emit any items * @return the new {@code Maybe} instance * @throws NullPointerException if {@code other} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Maybe switchIfEmpty(@NonNull MaybeSource other) { Objects.requireNonNull(other, "other is null"); return RxJavaPlugins.onAssembly(new MaybeSwitchIfEmpty<>(this, other)); } /** * Returns a {@link Single} that emits the items emitted by the current {@code Maybe} or the item of an alternate * {@link SingleSource} if the current {@code Maybe} is empty. *

* *

*
Scheduler:
*
{@code switchIfEmpty} does not operate by default on a particular {@link Scheduler}.
*
*

History: 2.1.4 - experimental * @param other * the alternate {@code SingleSource} to subscribe to if the main does not emit any items * @return the new {@code Single} instance * @throws NullPointerException if {@code other} is {@code null} * @since 2.2 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final Single switchIfEmpty(@NonNull SingleSource other) { Objects.requireNonNull(other, "other is null"); return RxJavaPlugins.onAssembly(new MaybeSwitchIfEmptySingle<>(this, other)); } /** * Returns a {@code Maybe} that emits the items emitted by the current {@code Maybe} until a second {@link MaybeSource} * emits an item. *

* *

*
Scheduler:
*
{@code takeUntil} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * the {@code MaybeSource} whose first emitted item will cause {@code takeUntil} to stop emitting items * from the current {@code Maybe} * @param * the type of items emitted by {@code other} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: TakeUntil */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe takeUntil(@NonNull MaybeSource other) { Objects.requireNonNull(other, "other is null"); return RxJavaPlugins.onAssembly(new MaybeTakeUntilMaybe<>(this, other)); } /** * Returns a {@code Maybe} that emits the item emitted by the current {@code Maybe} until a second {@link Publisher} * emits an item. *

* *

*
Backpressure:
*
The {@code Publisher} is consumed in an unbounded fashion and is cancelled after the first item * emitted.
*
Scheduler:
*
{@code takeUntil} does not operate by default on a particular {@link Scheduler}.
*
* * @param other * the {@code Publisher} whose first emitted item will cause {@code takeUntil} to stop emitting items * from the source {@code Publisher} * @param * the type of items emitted by {@code other} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code other} is {@code null} * @see ReactiveX operators documentation: TakeUntil */ @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe takeUntil(@NonNull Publisher other) { Objects.requireNonNull(other, "other is null"); return RxJavaPlugins.onAssembly(new MaybeTakeUntilPublisher<>(this, other)); } /** * Measures the time (in milliseconds) between the subscription and success item emission * of the current {@code Maybe} and signals it as a tuple ({@link Timed}) * success value. *

* *

* If the current {@code Maybe} is empty or fails, the resulting {@code Maybe} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link Single#timeInterval()}. *

*
Scheduler:
*
{@code timeInterval} uses the {@code computation} {@link Scheduler} * for determining the current time upon subscription and upon receiving the * success item from the current {@code Maybe}.
*
* @return the new {@code Maybe} instance * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Maybe> timeInterval() { return timeInterval(TimeUnit.MILLISECONDS, Schedulers.computation()); } /** * Measures the time (in milliseconds) between the subscription and success item emission * of the current {@code Maybe} and signals it as a tuple ({@link Timed}) * success value. *

* *

* If the current {@code Maybe} is empty or fails, the resulting {@code Maybe} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link Single#timeInterval(Scheduler)}. *

*
Scheduler:
*
{@code timeInterval} uses the provided {@link Scheduler} * for determining the current time upon subscription and upon receiving the * success item from the current {@code Maybe}.
*
* @param scheduler the {@code Scheduler} used for providing the current time * @return the new {@code Maybe} instance * @throws NullPointerException if {@code scheduler} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe> timeInterval(@NonNull Scheduler scheduler) { return timeInterval(TimeUnit.MILLISECONDS, scheduler); } /** * Measures the time between the subscription and success item emission * of the current {@code Maybe} and signals it as a tuple ({@link Timed}) * success value. *

* *

* If the current {@code Maybe} is empty or fails, the resulting {@code Maybe} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link Single#timeInterval(TimeUnit)}. *

*
Scheduler:
*
{@code timeInterval} uses the {@code computation} {@link Scheduler} * for determining the current time upon subscription and upon receiving the * success item from the current {@code Maybe}.
*
* @param unit the time unit for measurement * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Maybe> timeInterval(@NonNull TimeUnit unit) { return timeInterval(unit, Schedulers.computation()); } /** * Measures the time between the subscription and success item emission * of the current {@code Maybe} and signals it as a tuple ({@link Timed}) * success value. *

* *

* If the current {@code Maybe} is empty or fails, the resulting {@code Maybe} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link Single#timeInterval(TimeUnit, Scheduler)}. *

*
Scheduler:
*
{@code timeInterval} uses the provided {@link Scheduler} * for determining the current time upon subscription and upon receiving the * success item from the current {@code Maybe}.
*
* @param unit the time unit for measurement * @param scheduler the {@code Scheduler} used for providing the current time * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe> timeInterval(@NonNull TimeUnit unit, @NonNull Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new MaybeTimeInterval<>(this, unit, scheduler, true)); } /** * Combines the success value from the current {@code Maybe} with the current time (in milliseconds) of * its reception, using the {@code computation} {@link Scheduler} as time source, * then signals them as a {@link Timed} instance. *

* *

* If the current {@code Maybe} is empty or fails, the resulting {@code Maybe} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link Single#timestamp()}. *

*
Scheduler:
*
{@code timestamp} uses the {@code computation} {@code Scheduler} * for determining the current time upon receiving the * success item from the current {@code Maybe}.
*
* @return the new {@code Maybe} instance * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Maybe> timestamp() { return timestamp(TimeUnit.MILLISECONDS, Schedulers.computation()); } /** * Combines the success value from the current {@code Maybe} with the current time (in milliseconds) of * its reception, using the given {@link Scheduler} as time source, * then signals them as a {@link Timed} instance. *

* *

* If the current {@code Maybe} is empty or fails, the resulting {@code Maybe} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link Single#timestamp(Scheduler)}. *

*
Scheduler:
*
{@code timestamp} uses the provided {@code Scheduler} * for determining the current time upon receiving the * success item from the current {@code Maybe}.
*
* @param scheduler the {@code Scheduler} used for providing the current time * @return the new {@code Maybe} instance * @throws NullPointerException if {@code scheduler} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe> timestamp(@NonNull Scheduler scheduler) { return timestamp(TimeUnit.MILLISECONDS, scheduler); } /** * Combines the success value from the current {@code Maybe} with the current time of * its reception, using the {@code computation} {@link Scheduler} as time source, * then signals it as a {@link Timed} instance. *

* *

* If the current {@code Maybe} is empty or fails, the resulting {@code Maybe} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link Single#timestamp(TimeUnit)}. *

*
Scheduler:
*
{@code timestamp} uses the {@code computation} {@code Scheduler}, * for determining the current time upon receiving the * success item from the current {@code Maybe}.
*
* @param unit the time unit for measurement * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Maybe> timestamp(@NonNull TimeUnit unit) { return timestamp(unit, Schedulers.computation()); } /** * Combines the success value from the current {@code Maybe} with the current time of * its reception, using the given {@link Scheduler} as time source, * then signals it as a {@link Timed} instance. *

* *

* If the current {@code Maybe} is empty or fails, the resulting {@code Maybe} will * pass along the signals to the downstream. To measure the time to termination, * use {@link #materialize()} and apply {@link Single#timestamp(TimeUnit, Scheduler)}. *

*
Scheduler:
*
{@code timestamp} uses the provided {@code Scheduler}, * which is used for determining the current time upon receiving the * success item from the current {@code Maybe}.
*
* @param unit the time unit for measurement * @param scheduler the {@code Scheduler} used for providing the current time * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} * @since 3.0.0 */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe> timestamp(@NonNull TimeUnit unit, @NonNull Scheduler scheduler) { Objects.requireNonNull(unit, "unit is null"); Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new MaybeTimeInterval<>(this, unit, scheduler, false)); } /** * Returns a {@code Maybe} that mirrors the current {@code Maybe} but applies a timeout policy for each emitted * item. If the next item isn't emitted within the specified timeout duration starting from its predecessor, * the resulting {@code Maybe} terminates and notifies {@link MaybeObserver}s of a {@link TimeoutException}. *

* *

*
Scheduler:
*
This version of {@code timeout} operates by default on the {@code computation} {@link Scheduler}.
*
* * @param timeout * maximum duration between emitted items before a timeout occurs * @param unit * the unit of time that applies to the {@code timeout} argument. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} is {@code null} * @see ReactiveX operators documentation: Timeout */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.COMPUTATION) @NonNull public final Maybe timeout(long timeout, @NonNull TimeUnit unit) { return timeout(timeout, unit, Schedulers.computation()); } /** * Returns a {@code Maybe} that mirrors the current {@code Maybe} but applies a timeout policy for each emitted * item. If the next item isn't emitted within the specified timeout duration starting from its predecessor, * the current {@code Maybe} is disposed and resulting {@code Maybe} begins instead to mirror a fallback {@link MaybeSource}. *

* *

*
Scheduler:
*
This version of {@code timeout} operates by default on the {@code computation} {@link Scheduler}.
*
* * @param timeout * maximum duration between items before a timeout occurs * @param unit * the unit of time that applies to the {@code timeout} argument * @param fallback * the fallback {@code MaybeSource} to use in case of a timeout * @return the new {@code Maybe} instance * @throws NullPointerException if {@code unit} or {@code fallback} is {@code null} * @see ReactiveX operators documentation: Timeout */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.COMPUTATION) public final Maybe timeout(long timeout, @NonNull TimeUnit unit, @NonNull MaybeSource fallback) { Objects.requireNonNull(fallback, "fallback is null"); return timeout(timeout, unit, Schedulers.computation(), fallback); } /** * Returns a {@code Maybe} that mirrors the current {@code Maybe} but applies a timeout policy for each emitted * item using a specified {@link Scheduler}. If the next item isn't emitted within the specified timeout duration * starting from its predecessor, the current {@code Maybe} is disposed and resulting {@code Maybe} begins instead * to mirror a fallback {@link MaybeSource}. *

* *

*
Scheduler:
*
You specify which {@code Scheduler} this operator will use.
*
* * @param timeout * maximum duration between items before a timeout occurs * @param unit * the unit of time that applies to the {@code timeout} argument * @param fallback * the {@code MaybeSource} to use as the fallback in case of a timeout * @param scheduler * the {@code Scheduler} to run the timeout timers on * @return the new {@code Maybe} instance * @throws NullPointerException if {@code fallback}, {@code unit} or {@code scheduler} is {@code null} * @see ReactiveX operators documentation: Timeout */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe timeout(long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler, @NonNull MaybeSource fallback) { Objects.requireNonNull(fallback, "fallback is null"); return timeout(timer(timeout, unit, scheduler), fallback); } /** * Returns a {@code Maybe} that mirrors the current {@code Maybe} but applies a timeout policy for each emitted * item, where this policy is governed on a specified {@link Scheduler}. If the next item isn't emitted within the * specified timeout duration starting from its predecessor, the resulting {@code Maybe} terminates and * notifies {@link MaybeObserver}s of a {@link TimeoutException}. *

* *

*
Scheduler:
*
You specify which {@code Scheduler} this operator will use.
*
* * @param timeout * maximum duration between items before a timeout occurs * @param unit * the unit of time that applies to the {@code timeout} argument * @param scheduler * the {@code Scheduler} to run the timeout timers on * @return the new {@code Maybe} instance * @see ReactiveX operators documentation: Timeout * @throws NullPointerException if {@code unit} or {@code scheduler} is {@code null} */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.CUSTOM) @NonNull public final Maybe timeout(long timeout, @NonNull TimeUnit unit, @NonNull Scheduler scheduler) { return timeout(timer(timeout, unit, scheduler)); } /** * If the current {@code Maybe} didn't signal an event before the {@code timeoutIndicator} {@link MaybeSource} signals, a * {@link TimeoutException} is signaled instead. *

* *

*
Scheduler:
*
{@code timeout} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the * @param timeoutIndicator the {@code MaybeSource} that indicates the timeout by signaling {@code onSuccess} * or {@code onComplete}. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code timeoutIndicator} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe timeout(@NonNull MaybeSource timeoutIndicator) { Objects.requireNonNull(timeoutIndicator, "timeoutIndicator is null"); return RxJavaPlugins.onAssembly(new MaybeTimeoutMaybe<>(this, timeoutIndicator, null)); } /** * If the current {@code Maybe} didn't signal an event before the {@code timeoutIndicator} {@link MaybeSource} signals, * the current {@code Maybe} is disposed and the {@code fallback} {@code MaybeSource} subscribed to * as a continuation. *

* *

*
Scheduler:
*
{@code timeout} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the * @param timeoutIndicator the {@code MaybeSource} that indicates the timeout by signaling {@code onSuccess} * or {@code onComplete}. * @param fallback the {@code MaybeSource} that is subscribed to if the current {@code Maybe} times out * @return the new {@code Maybe} instance * @throws NullPointerException if {@code timeoutIndicator} or {@code fallback} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe timeout(@NonNull MaybeSource timeoutIndicator, @NonNull MaybeSource fallback) { Objects.requireNonNull(timeoutIndicator, "timeoutIndicator is null"); Objects.requireNonNull(fallback, "fallback is null"); return RxJavaPlugins.onAssembly(new MaybeTimeoutMaybe<>(this, timeoutIndicator, fallback)); } /** * If the current {@code Maybe} source didn't signal an event before the {@code timeoutIndicator} {@link Publisher} signals, a * {@link TimeoutException} is signaled instead. *

* *

*
Backpressure:
*
The {@code timeoutIndicator} {@code Publisher} is consumed in an unbounded manner and * is cancelled after its first item.
*
Scheduler:
*
{@code timeout} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the * @param timeoutIndicator the {@code Publisher} that indicates the timeout by signaling {@code onSuccess} * or {@code onComplete}. * @return the new {@code Maybe} instance * @throws NullPointerException if {@code timeoutIndicator} is {@code null} */ @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe timeout(@NonNull Publisher timeoutIndicator) { Objects.requireNonNull(timeoutIndicator, "timeoutIndicator is null"); return RxJavaPlugins.onAssembly(new MaybeTimeoutPublisher<>(this, timeoutIndicator, null)); } /** * If the current {@code Maybe} didn't signal an event before the {@code timeoutIndicator} {@link Publisher} signals, * the current {@code Maybe} is disposed and the {@code fallback} {@link MaybeSource} subscribed to * as a continuation. *

* *

*
Backpressure:
*
The {@code timeoutIndicator} {@code Publisher} is consumed in an unbounded manner and * is cancelled after its first item.
*
Scheduler:
*
{@code timeout} does not operate by default on a particular {@link Scheduler}.
*
* @param the value type of the * @param timeoutIndicator the {@code MaybeSource} that indicates the timeout by signaling {@code onSuccess} * or {@code onComplete} * @param fallback the {@code MaybeSource} that is subscribed to if the current {@code Maybe} times out * @return the new {@code Maybe} instance * @throws NullPointerException if {@code timeoutIndicator} or {@code fallback} is {@code null} */ @BackpressureSupport(BackpressureKind.UNBOUNDED_IN) @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U> Maybe timeout(@NonNull Publisher timeoutIndicator, @NonNull MaybeSource fallback) { Objects.requireNonNull(timeoutIndicator, "timeoutIndicator is null"); Objects.requireNonNull(fallback, "fallback is null"); return RxJavaPlugins.onAssembly(new MaybeTimeoutPublisher<>(this, timeoutIndicator, fallback)); } /** * Returns a {@code Maybe} which makes sure when a {@link MaybeObserver} disposes the {@link Disposable}, * that call is propagated up on the specified {@link Scheduler}. *

* *

*
Scheduler:
*
{@code unsubscribeOn} calls {@code dispose()} of the upstream on the {@code Scheduler} you specify.
*
* @param scheduler the target scheduler where to execute the disposal * @return the new {@code Maybe} instance * @throws NullPointerException if {@code scheduler} is {@code null} */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.CUSTOM) public final Maybe unsubscribeOn(@NonNull Scheduler scheduler) { Objects.requireNonNull(scheduler, "scheduler is null"); return RxJavaPlugins.onAssembly(new MaybeUnsubscribeOn<>(this, scheduler)); } /** * Waits until this and the other {@link MaybeSource} signal a success value then applies the given {@link BiFunction} * to those values and emits the {@code BiFunction}'s resulting value to downstream. * * * *

If either this or the other {@code MaybeSource} is empty or signals an error, the resulting {@code Maybe} will * terminate immediately and dispose the other source. * *

*
Scheduler:
*
{@code zipWith} does not operate by default on a particular {@link Scheduler}.
*
* * @param * the type of items emitted by the {@code other} {@code MaybeSource} * @param * the type of items emitted by the resulting {@code Maybe} * @param other * the other {@code MaybeSource} * @param zipper * a function that combines the pairs of items from the two {@code MaybeSource}s to generate the items to * be emitted by the resulting {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code other} or {@code zipper} is {@code null} * @see ReactiveX operators documentation: Zip */ @CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public final <@NonNull U, @NonNull R> Maybe zipWith(@NonNull MaybeSource other, @NonNull BiFunction zipper) { Objects.requireNonNull(other, "other is null"); return zip(this, other, zipper); } // ------------------------------------------------------------------ // Test helper // ------------------------------------------------------------------ /** * Creates a {@link TestObserver} and subscribes * it to this {@code Maybe}. *
*
Scheduler:
*
{@code test} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code TestObserver} instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final TestObserver test() { TestObserver to = new TestObserver<>(); subscribe(to); return to; } /** * Creates a {@link TestObserver} optionally in cancelled state, then subscribes it to this {@code Maybe}. *
*
Scheduler:
*
{@code test} does not operate by default on a particular {@link Scheduler}.
*
* @param dispose if {@code true}, the {@code TestObserver} will be disposed before subscribing to this * {@code Maybe}. * @return the new {@code TestObserver} instance */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final TestObserver test(boolean dispose) { TestObserver to = new TestObserver<>(); if (dispose) { to.dispose(); } subscribe(to); return to; } // ------------------------------------------------------------------------- // JDK 8 Support // ------------------------------------------------------------------------- /** * Converts the existing value of the provided optional into a {@link #just(Object)} * or an empty optional into an {@link #empty()} {@code Maybe} instance. *

* *

* Note that the operator takes an already instantiated optional reference and does not * by any means create this original optional. If the optional is to be created per * consumer upon subscription, use {@link #defer(Supplier)} around {@code fromOptional}: *


     * Maybe.defer(() -> Maybe.fromOptional(createOptional()));
     * 
*
*
Scheduler:
*
{@code fromOptional} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the optional value * @param optional the optional value to convert into a {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code optional} is {@code null} * @since 3.0.0 * @see #just(Object) * @see #empty() */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Maybe<@NonNull T> fromOptional(@NonNull Optional optional) { Objects.requireNonNull(optional, "optional is null"); return optional.map(Maybe::just).orElseGet(Maybe::empty); } /** * Signals the completion value or error of the given (hot) {@link CompletionStage}-based asynchronous calculation. *

* *

* Note that the operator takes an already instantiated, running or terminated {@code CompletionStage}. * If the {@code CompletionStage} is to be created per consumer upon subscription, use {@link #defer(Supplier)} * around {@code fromCompletionStage}: *


     * Maybe.defer(() -> Maybe.fromCompletionStage(createCompletionStage()));
     * 
*

* If the {@code CompletionStage} completes with {@code null}, the resulting {@code Maybe} is completed via {@code onComplete}. *

* Canceling the flow can't cancel the execution of the {@code CompletionStage} because {@code CompletionStage} * itself doesn't support cancellation. Instead, the operator detaches from the {@code CompletionStage}. *

*
Scheduler:
*
{@code fromCompletionStage} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the {@code CompletionStage} * @param stage the {@code CompletionStage} to convert to {@code Maybe} and signal its terminal value or error * @return the new {@code Maybe} instance * @throws NullPointerException if {@code stage} is {@code null} * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public static <@NonNull T> Maybe<@NonNull T> fromCompletionStage(@NonNull CompletionStage stage) { Objects.requireNonNull(stage, "stage is null"); return RxJavaPlugins.onAssembly(new MaybeFromCompletionStage<>(stage)); } /** * Maps the upstream success value into an {@link Optional} and emits the contained item if not empty. *

* * *

*
Scheduler:
*
{@code mapOptional} does not operate by default on a particular {@link Scheduler}.
*
* @param the non-{@code null} output type * @param mapper the function that receives the upstream success item and should return a non-empty {@code Optional} * to emit as the success output or an empty {@code Optional} to complete the {@code Maybe} * @return the new {@code Maybe} instance * @throws NullPointerException if {@code mapper} is {@code null} * @since 3.0.0 * @see #map(Function) * @see #filter(Predicate) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull R> Maybe mapOptional(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeMapOptional<>(this, mapper)); } /** * Signals the upstream success item (or a {@link NoSuchElementException} if the upstream is empty) via * a {@link CompletionStage}. *

* *

* The upstream can be canceled by converting the resulting {@code CompletionStage} into * {@link CompletableFuture} via {@link CompletionStage#toCompletableFuture()} and * calling {@link CompletableFuture#cancel(boolean)} on it. * The upstream will be also cancelled if the resulting {@code CompletionStage} is converted to and * completed manually by {@link CompletableFuture#complete(Object)} or {@link CompletableFuture#completeExceptionally(Throwable)}. *

* {@code CompletionStage}s don't have a notion of emptiness and allow {@code null}s, therefore, one can either use * {@link #toCompletionStage(Object)} with {@code null} or turn the upstream into a sequence of {@link Optional}s and * default to {@link Optional#empty()}: *


     * CompletionStage<Optional<T>> stage = source.map(Optional::of).toCompletionStage(Optional.empty());
     * 
*
*
Scheduler:
*
{@code toCompletionStage} does not operate by default on a particular {@link Scheduler}.
*
* @return the new {@code CompletionStage} instance * @since 3.0.0 * @see #toCompletionStage(Object) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final CompletionStage toCompletionStage() { return subscribeWith(new CompletionStageConsumer<>(false, null)); } /** * Signals the upstream success item (or the default item if the upstream is empty) via * a {@link CompletionStage}. *

* *

* The upstream can be canceled by converting the resulting {@code CompletionStage} into * {@link CompletableFuture} via {@link CompletionStage#toCompletableFuture()} and * calling {@link CompletableFuture#cancel(boolean)} on it. * The upstream will be also cancelled if the resulting {@code CompletionStage} is converted to and * completed manually by {@link CompletableFuture#complete(Object)} or {@link CompletableFuture#completeExceptionally(Throwable)}. *

* {@code CompletionStage}s don't have a notion of emptiness and allow {@code null}s, therefore, one can either use * a {@code defaultItem} of {@code null} or turn the flow into a sequence of {@link Optional}s and default to {@link Optional#empty()}: *


     * CompletionStage<Optional<T>> stage = source.map(Optional::of).toCompletionStage(Optional.empty());
     * 
*
*
Scheduler:
*
{@code toCompletionStage} does not operate by default on a particular {@link Scheduler}.
*
* @param defaultItem the item to signal if the upstream is empty * @return the new {@code CompletionStage} instance * @since 3.0.0 */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final CompletionStage toCompletionStage(@Nullable T defaultItem) { return subscribeWith(new CompletionStageConsumer<>(true, defaultItem)); } /** * Maps the upstream succecss value into a Java {@link Stream} and emits its * items to the downstream consumer as a {@link Flowable}. *

* *

* The operator closes the {@code Stream} upon cancellation and when it terminates. The exceptions raised when * closing a {@code Stream} are routed to the global error handler ({@link RxJavaPlugins#onError(Throwable)}. * If a {@code Stream} should not be closed, turn it into an {@link Iterable} and use {@link #flattenAsFlowable(Function)}: *


     * source.flattenAsFlowable(item -> createStream(item)::iterator);
     * 
*

* Primitive streams are not supported and items have to be boxed manually (e.g., via {@link IntStream#boxed()}): *


     * source.flattenStreamAsFlowable(item -> IntStream.rangeClosed(1, 10).boxed());
     * 
*

* {@code Stream} does not support concurrent usage so creating and/or consuming the same instance multiple times * from multiple threads can lead to undefined behavior. *

*
Backpressure:
*
The operator honors backpressure from downstream and iterates the given {@code Stream} * on demand (i.e., when requested).
*
Scheduler:
*
{@code flattenStreamAsFlowable} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the {@code Stream} and the output {@code Flowable} * @param mapper the function that receives the upstream success item and should * return a {@code Stream} of values to emit. * @return the new {@code Flowable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @since 3.0.0 * @see #flattenAsFlowable(Function) * @see #flattenStreamAsObservable(Function) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @BackpressureSupport(BackpressureKind.FULL) @NonNull public final <@NonNull R> Flowable flattenStreamAsFlowable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlattenStreamAsFlowable<>(this, mapper)); } /** * Maps the upstream succecss value into a Java {@link Stream} and emits its * items to the downstream consumer as an {@link Observable}. * *

* The operator closes the {@code Stream} upon cancellation and when it terminates. The exceptions raised when * closing a {@code Stream} are routed to the global error handler ({@link RxJavaPlugins#onError(Throwable)}. * If a {@code Stream} should not be closed, turn it into an {@link Iterable} and use {@link #flattenAsObservable(Function)}: *


     * source.flattenAsObservable(item -> createStream(item)::iterator);
     * 
*

* Primitive streams are not supported and items have to be boxed manually (e.g., via {@link IntStream#boxed()}): *


     * source.flattenStreamAsObservable(item -> IntStream.rangeClosed(1, 10).boxed());
     * 
*

* {@code Stream} does not support concurrent usage so creating and/or consuming the same instance multiple times * from multiple threads can lead to undefined behavior. *

*
Scheduler:
*
{@code flattenStreamAsObservable} does not operate by default on a particular {@link Scheduler}.
*
* @param the element type of the {@code Stream} and the output {@code Observable} * @param mapper the function that receives the upstream success item and should * return a {@code Stream} of values to emit. * @return the new {@code Observable} instance * @throws NullPointerException if {@code mapper} is {@code null} * @since 3.0.0 * @see #flattenAsObservable(Function) * @see #flattenStreamAsFlowable(Function) */ @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) @NonNull public final <@NonNull R> Observable flattenStreamAsObservable(@NonNull Function> mapper) { Objects.requireNonNull(mapper, "mapper is null"); return RxJavaPlugins.onAssembly(new MaybeFlattenStreamAsObservable<>(this, mapper)); } }