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

org.reactfx.AccumulativeEventStream Maven / Gradle / Ivy

There is a newer version: 2.0-M5
Show newest version
package org.reactfx;

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

class AccumulativeEventStream extends SuspendableEventStreamBase {
    private final Function initialTransformation;
    private final BiFunction accumulation;
    private final Function> deconstruction;

    private boolean hasValue = false;
    private A accum = null;

    AccumulativeEventStream(
            EventStream source,
            Function initialTransformation,
            BiFunction accumulation,
            Function> deconstruction) {
        super(source);
        this.initialTransformation = initialTransformation;
        this.accumulation = accumulation;
        this.deconstruction = deconstruction;
    }

    @Override
    protected void handleEventWhenSuspended(T event) {
        if(hasValue) {
            accum = accumulation.apply(accum, event);
        } else {
            accum = initialTransformation.apply(event);
            hasValue = true;
        }
    }

    @Override
    protected void onResume() {
        if(hasValue) {
            List toEmit = deconstruction.apply(accum);
            reset();
            for(T t: toEmit) {
                emit(t);
            }
        }
    }

    @Override
    protected void reset() {
        hasValue = false;
        accum = null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy