io.reactivex.internal.operators.flowable.FlowableInternalHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava Show documentation
Show all versions of rxjava Show documentation
Reactive Extensions for Java
/**
* 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.internal.operators.flowable;
import java.util.List;
import java.util.concurrent.*;
import org.reactivestreams.*;
import io.reactivex.*;
import io.reactivex.flowables.ConnectableFlowable;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.*;
/**
* Helper utility class to support Flowable with inner classes.
*/
public final class FlowableInternalHelper {
/** Utility class. */
private FlowableInternalHelper() {
throw new IllegalStateException("No instances!");
}
static final class SimpleGenerator implements BiFunction, S> {
final Consumer> consumer;
SimpleGenerator(Consumer> consumer) {
this.consumer = consumer;
}
@Override
public S apply(S t1, Emitter t2) throws Exception {
consumer.accept(t2);
return t1;
}
}
public static BiFunction, S> simpleGenerator(Consumer> consumer) {
return new SimpleGenerator(consumer);
}
static final class SimpleBiGenerator implements BiFunction, S> {
final BiConsumer> consumer;
SimpleBiGenerator(BiConsumer> consumer) {
this.consumer = consumer;
}
@Override
public S apply(S t1, Emitter t2) throws Exception {
consumer.accept(t1, t2);
return t1;
}
}
public static BiFunction, S> simpleBiGenerator(BiConsumer> consumer) {
return new SimpleBiGenerator(consumer);
}
static final class ItemDelayFunction implements Function> {
final Function> itemDelay;
ItemDelayFunction(Function> itemDelay) {
this.itemDelay = itemDelay;
}
@Override
public Publisher apply(final T v) throws Exception {
Publisher p = ObjectHelper.requireNonNull(itemDelay.apply(v), "The itemDelay returned a null Publisher");
return new FlowableTakePublisher(p, 1).map(Functions.justFunction(v)).defaultIfEmpty(v);
}
}
public static Function> itemDelay(final Function> itemDelay) {
return new ItemDelayFunction(itemDelay);
}
static final class SubscriberOnNext implements Consumer {
final Subscriber subscriber;
SubscriberOnNext(Subscriber subscriber) {
this.subscriber = subscriber;
}
@Override
public void accept(T v) throws Exception {
subscriber.onNext(v);
}
}
static final class SubscriberOnError implements Consumer {
final Subscriber subscriber;
SubscriberOnError(Subscriber subscriber) {
this.subscriber = subscriber;
}
@Override
public void accept(Throwable v) throws Exception {
subscriber.onError(v);
}
}
static final class SubscriberOnComplete implements Action {
final Subscriber subscriber;
SubscriberOnComplete(Subscriber subscriber) {
this.subscriber = subscriber;
}
@Override
public void run() throws Exception {
subscriber.onComplete();
}
}
public static Consumer subscriberOnNext(Subscriber subscriber) {
return new SubscriberOnNext(subscriber);
}
public static Consumer subscriberOnError(Subscriber subscriber) {
return new SubscriberOnError(subscriber);
}
public static Action subscriberOnComplete(Subscriber subscriber) {
return new SubscriberOnComplete(subscriber);
}
static final class FlatMapWithCombinerInner implements Function {
private final BiFunction combiner;
private final T t;
FlatMapWithCombinerInner(BiFunction combiner, T t) {
this.combiner = combiner;
this.t = t;
}
@Override
public R apply(U w) throws Exception {
return combiner.apply(t, w);
}
}
static final class FlatMapWithCombinerOuter implements Function> {
private final BiFunction combiner;
private final Function> mapper;
FlatMapWithCombinerOuter(BiFunction combiner,
Function> mapper) {
this.combiner = combiner;
this.mapper = mapper;
}
@Override
public Publisher apply(final T t) throws Exception {
@SuppressWarnings("unchecked")
Publisher u = (Publisher)ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null Publisher");
return new FlowableMapPublisher(u, new FlatMapWithCombinerInner(combiner, t));
}
}
public static Function> flatMapWithCombiner(
final Function> mapper,
final BiFunction combiner) {
return new FlatMapWithCombinerOuter(combiner, mapper);
}
static final class FlatMapIntoIterable implements Function> {
private final Function> mapper;
FlatMapIntoIterable(Function> mapper) {
this.mapper = mapper;
}
@Override
public Publisher apply(T t) throws Exception {
return new FlowableFromIterable(ObjectHelper.requireNonNull(mapper.apply(t), "The mapper returned a null Iterable"));
}
}
public static Function> flatMapIntoIterable(final Function> mapper) {
return new FlatMapIntoIterable(mapper);
}
public static Callable> replayCallable(final Flowable parent) {
return new ReplayCallable(parent);
}
public static Callable> replayCallable(final Flowable parent, final int bufferSize) {
return new BufferedReplayCallable(parent, bufferSize);
}
public static Callable> replayCallable(final Flowable parent, final int bufferSize, final long time, final TimeUnit unit, final Scheduler scheduler) {
return new BufferedTimedReplay(parent, bufferSize, time, unit, scheduler);
}
public static Callable> replayCallable(final Flowable parent, final long time, final TimeUnit unit, final Scheduler scheduler) {
return new TimedReplay(parent, time, unit, scheduler);
}
public static Function, Publisher> replayFunction(final Function, ? extends Publisher> selector, final Scheduler scheduler) {
return new ReplayFunction(selector, scheduler);
}
public enum RequestMax implements Consumer {
INSTANCE;
@Override
public void accept(Subscription t) throws Exception {
t.request(Long.MAX_VALUE);
}
}
static final class ZipIterableFunction
implements Function>, Publisher> {
private final Function zipper;
ZipIterableFunction(Function zipper) {
this.zipper = zipper;
}
@Override
public Publisher apply(List> list) {
return Flowable.zipIterable(list, zipper, false, Flowable.bufferSize());
}
}
public static Function>, Publisher> zipIterable(final Function zipper) {
return new ZipIterableFunction(zipper);
}
static final class ReplayCallable implements Callable> {
private final Flowable parent;
ReplayCallable(Flowable parent) {
this.parent = parent;
}
@Override
public ConnectableFlowable call() {
return parent.replay();
}
}
static final class BufferedReplayCallable implements Callable> {
private final Flowable parent;
private final int bufferSize;
BufferedReplayCallable(Flowable parent, int bufferSize) {
this.parent = parent;
this.bufferSize = bufferSize;
}
@Override
public ConnectableFlowable call() {
return parent.replay(bufferSize);
}
}
static final class BufferedTimedReplay implements Callable> {
private final Flowable parent;
private final int bufferSize;
private final long time;
private final TimeUnit unit;
private final Scheduler scheduler;
BufferedTimedReplay(Flowable parent, int bufferSize, long time, TimeUnit unit, Scheduler scheduler) {
this.parent = parent;
this.bufferSize = bufferSize;
this.time = time;
this.unit = unit;
this.scheduler = scheduler;
}
@Override
public ConnectableFlowable call() {
return parent.replay(bufferSize, time, unit, scheduler);
}
}
static final class TimedReplay implements Callable> {
private final Flowable parent;
private final long time;
private final TimeUnit unit;
private final Scheduler scheduler;
TimedReplay(Flowable parent, long time, TimeUnit unit, Scheduler scheduler) {
this.parent = parent;
this.time = time;
this.unit = unit;
this.scheduler = scheduler;
}
@Override
public ConnectableFlowable call() {
return parent.replay(time, unit, scheduler);
}
}
static final class ReplayFunction implements Function, Publisher> {
private final Function, ? extends Publisher> selector;
private final Scheduler scheduler;
ReplayFunction(Function, ? extends Publisher> selector, Scheduler scheduler) {
this.selector = selector;
this.scheduler = scheduler;
}
@Override
public Publisher apply(Flowable t) throws Exception {
Publisher p = ObjectHelper.requireNonNull(selector.apply(t), "The selector returned a null Publisher");
return Flowable.fromPublisher(p).observeOn(scheduler);
}
}
}