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

org.reactfx.AccumulatingStream Maven / Gradle / Ivy

There is a newer version: 1.11
Show newest version
package org.reactfx;

import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * See {@link EventStream#accumulate(BiFunction, Function)}
 */
class AccumulatingStream extends EventStreamBase {
    private final EventStream input;
    private final Function initialTransformation;
    private final BiFunction reduction;

    private boolean hasEvent = false;
    private U event = null;

    public AccumulatingStream(
            EventStream input,
            Function initial,
            BiFunction reduction) {

        this.input = input;
        this.initialTransformation = initial;
        this.reduction = reduction;
    }

    @Override
    protected final Subscription observeInputs() {
        return input.subscribe(i -> {
            event = hasEvent
                    ? reduction.apply(event, i)
                    : initialTransformation.apply(i);
            hasEvent = true;
            emit(event);
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy