
com.github.lstephen.ai.search.action.SequencedAction Maven / Gradle / Ivy
package com.github.lstephen.ai.search.action;
import java.util.Arrays;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
/**
*
* @author lstephen
*/
public final class SequencedAction implements Action {
private final ImmutableList> actions;
private SequencedAction(Iterable> actions) {
this.actions = ImmutableList.copyOf(actions);
}
@Override
public S apply(S initial) {
AtomicReference state = new AtomicReference<>(initial);
actions.stream().forEach((a) -> state.getAndUpdate(a::apply));
return state.get();
}
@SafeVarargs
@SuppressWarnings("varargs")
public static SequencedAction create(Action... as) {
return new SequencedAction<>(Arrays.asList(as));
}
public static Stream> allPairs(Collection extends Action> actions) {
return merged(actions, actions);
}
public static Stream> merged(
Collection extends Action> firsts,
Collection extends Action> seconds) {
return merged(firsts.stream(), seconds);
}
public static Stream> merged(
Stream extends Action> firsts,
Collection extends Action> seconds) {
return firsts.flatMap((f) -> seconds.stream().map((s) -> SequencedAction.create(f, s)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy