jio.JsArrayExp Maven / Gradle / Ivy
The newest version!
package jio;
import static java.util.Objects.requireNonNull;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import jsonvalues.JsArray;
import jsonvalues.JsValue;
/**
* Represents a sequence of effects that produce JSON values ({@link JsValue}) and combines them into a JSON array. This
* abstraction allows for the evaluation of these effects either in parallel or sequentially. If any of the effects
* fail, the entire expression fails.
*/
public abstract sealed class JsArrayExp extends Exp permits JsArrayExpPar, JsArrayExpSeq {
final List> list;
JsArrayExp(List> list,
Function> debugger
) {
super(debugger);
this.list = list;
}
/**
* Creates a JsArray expression that evaluates the effects sequentially, one after the other. If any effect fails, the
* entire expression fails, and subsequent effects are not evaluated.
*
* @param effects the effects to be evaluated sequentially
* @return a JsArrayExp for sequential evaluation
*/
@SafeVarargs
public static JsArrayExp seq(final IO... effects) {
var list = new ArrayList>();
for (var other : requireNonNull(effects)) {
list.add(requireNonNull(other));
}
return new JsArrayExpSeq(list,
null);
}
/**
* Creates a JsArray expression that evaluates all the effects in parallel. If any effect fails, the entire expression
* fails.
*
* @param effects the effects to be evaluated in parallel
* @return a JsArrayExp for parallel evaluation
*/
@SafeVarargs
public static JsArrayExp par(final IO... effects) {
var list = new ArrayList>();
for (var other : requireNonNull(effects)) {
list.add(requireNonNull(other));
}
return new JsArrayExpPar(list,
null);
}
List> debugJsArray(List> exps,
EventBuilder eventBuilder
) {
return IntStream.range(0,
exps.size())
.mapToObj(i -> DebuggerHelper.debugIO(exps.get(i),
String.format("%s[%s]",
eventBuilder.exp,
i
),
eventBuilder.context
)
)
.collect(Collectors.toList());
}
@Override
public abstract JsArrayExp retryEach(final Predicate super Throwable> predicate,
final RetryPolicy policy
);
@Override
public abstract JsArrayExp debugEach(final EventBuilder messageBuilder
);
@Override
public abstract JsArrayExp debugEach(final String context);
@Override
public JsArrayExp retryEach(RetryPolicy policy) {
return retryEach(_ -> true,
policy);
}
}