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

org.reactfx.BiEventStream Maven / Gradle / Ivy

package org.reactfx;

import static org.reactfx.util.Tuples.*;

import java.util.Optional;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Consumer;

import javafx.application.Platform;
import javafx.concurrent.Task;

import org.reactfx.util.Either;
import org.reactfx.util.Tuple2;
import org.reactfx.util.Tuple3;

public interface BiEventStream extends EventStream> {

    Subscription subscribe(BiConsumer subscriber);

    @Override
    default Subscription subscribe(Consumer> subscriber) {
        return subscribe((a, b) -> subscriber.accept(t(a, b)));
    }

    default Subscription watch(
            BiConsumer subscriber,
            Consumer monitor) {
        return monitor(monitor).and(subscribe(subscriber));
    }

    default Subscription feedTo2(BiEventSink sink) {
        return subscribe((a, b) -> sink.push(a, b));
    }

    default BiEventStream hook(
            BiConsumer sideEffect) {
        return new SideEffectBiStream<>(this, sideEffect);
    }

    default BiEventStream filter(
            BiPredicate predicate) {
        return new FilterBiStream<>(this, predicate);
    }

    @Override
    default BiEventStream distinct() {
        return new DistinctBiStream<>(this);
    }

    default  EventStream map(
            BiFunction f) {
        return new MappedBiStream<>(this, f);
    }

    default  BiEventStream mapToBi(BiFunction> f) {
        return new MappedBiToBiStream<>(this, f);
    }

    default  TriEventStream mapToTri(BiFunction> f) {
        return new MappedBiToTriStream<>(this, f);
    }

    /**
     * @deprecated See deprecation comment at {@link EitherEventStream}.
     */
    @Deprecated
    default  EitherEventStream split(
            BiFunction> f) {
        return new MappedToEitherBiStream<>(this, f);
    }

    default  CompletionStageStream mapToCompletionStage(
            BiFunction> f) {
        return new MappedToCompletionStageBiStream<>(this, f);
    }

    default  TaskStream mapToTask(
            BiFunction> f) {
        return new MappedToTaskBiStream<>(this, f);
    }

    default  EventStream filterMap(
            BiPredicate predicate,
            BiFunction f) {
        return new FilterMapBiStream<>(this, predicate, f);
    }

    default  EventStream filterMap(BiFunction> f) {
        return filterMap(t -> f.apply(t._1, t._2));
    }

    default  EventStream flatMap(BiFunction> f) {
        return flatMap(t -> f.apply(t._1, t._2));
    }

    /**
     * @deprecated Since 1.2.1. Renamed to {@link #filterMap(BiFunction)}.
     */
    @Deprecated
    default  EventStream flatMapOpt(BiFunction> f) {
        return flatMapOpt(t -> f.apply(t._1, t._2));
    }

    @Override
    default BiEventStream emitOn(EventStream impulse) {
        return new EmitOnBiStream<>(this, impulse);
    }

    @Override
    default BiEventStream emitOnEach(EventStream impulse) {
        return new EmitOnEachBiStream<>(this, impulse);
    }

    default  TriEventStream emitAllOnEach(EventStream impulse) {
        return new EmitAll3OnEachStream<>(this, impulse);
    }

    @Override
    default BiEventStream repeatOn(EventStream impulse) {
        return new RepeatOnBiStream<>(this, impulse);
    }

    @Deprecated
    @Override
    default InterceptableBiEventStream interceptable() {
        if(this instanceof InterceptableBiEventStream) {
            return (InterceptableBiEventStream) this;
        } else {
            return new InterceptableBiEventStreamImpl(this);
        }
    }

    @Override
    default BiEventStream threadBridge(
            Executor sourceThreadExecutor,
            Executor targetThreadExecutor) {
        return new BiThreadBridge(this, sourceThreadExecutor, targetThreadExecutor);
    }

    @Override
    default BiEventStream threadBridgeFromFx(Executor targetThreadExecutor) {
        return threadBridge(Platform::runLater, targetThreadExecutor);
    }

    @Override
    default BiEventStream threadBridgeToFx(Executor sourceThreadExecutor) {
        return threadBridge(sourceThreadExecutor, Platform::runLater);
    }

    @Override
    default BiEventStream guardedBy(Guardian... guardians) {
        return new GuardedBiStream<>(this, guardians);
    }
}