io.atleon.context.ContextActivatingAlo Maven / Gradle / Ivy
package io.atleon.context;
import io.atleon.core.AbstractDecoratingAlo;
import io.atleon.core.Alo;
import io.atleon.core.AloFactory;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Decorates {@link Alo} elements with {@link AloContext} activation and propagation
*
* @param
*/
public class ContextActivatingAlo extends AbstractDecoratingAlo {
private final AloContext context;
private ContextActivatingAlo(Alo delegate, AloContext context) {
super(delegate);
this.context = context;
}
public static ContextActivatingAlo create(Alo delegate) {
return new ContextActivatingAlo<>(delegate, AloContext.create());
}
@Override
public void runInContext(Runnable runnable) {
context.run(() -> delegate.runInContext(runnable));
}
@Override
public R supplyInContext(Supplier supplier) {
return context.supply(() -> delegate.supplyInContext(supplier));
}
@Override
public Alo map(Function super T, ? extends R> mapper) {
return new ContextActivatingAlo<>(context.supply(() -> delegate.map(mapper)), context);
}
@Override
public AloFactory> fanInPropagator(List extends Alo>> alos) {
AloContext mergedContext = AloContext.create();
for (Alo> alo : alos) {
doOnDelegator(alo, ContextActivatingAlo.class, it -> mergedContext.merge(it.context()));
}
return delegate.fanInPropagator(alos).withDecorator(alo -> new ContextActivatingAlo<>(alo, mergedContext));
}
@Override
public AloFactory propagator() {
return delegate.propagator().withDecorator(alo -> new ContextActivatingAlo<>(alo, context.copy()));
}
public AloContext context() {
return context;
}
}