org.reactfx.AccumulativeEventStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of reactfx Show documentation
Show all versions of reactfx Show documentation
Reactive event streams for JavaFX
package org.reactfx;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
class AccumulativeEventStream extends SuspendableEventStreamBase {
private final Function super T, ? extends A> initialTransformation;
private final BiFunction super A, ? super T, ? extends A> accumulation;
private final Function super A, List> deconstruction;
private boolean hasValue = false;
private A accum = null;
AccumulativeEventStream(
EventStream source,
Function super T, ? extends A> initialTransformation,
BiFunction super A, ? super T, ? extends A> accumulation,
Function super A, List> 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