org.occurrent.application.composition.command.internal.SequentialFunctionComposer Maven / Gradle / Ivy
/*
* Copyright 2021 Johan Haleby
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.occurrent.application.composition.command.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
public class SequentialFunctionComposer {
private final List, List>> functions;
public SequentialFunctionComposer(List, List>> functions) {
this.functions = functions;
}
public Function, List> compose() {
return initialEvents -> {
SequentialFunctionComposer.State resultingState = functions.stream().collect(() -> new State<>(initialEvents),
(state, fn) -> {
List newEvents = fn.apply(state.allEvents());
state.addAll(newEvents);
}, (state1, state2) -> {
});
return resultingState.accumulatedEvents;
};
}
private static class State {
private final List initialEvents;
private final List accumulatedEvents;
private State(List initialEvents) {
this.initialEvents = initialEvents;
this.accumulatedEvents = new ArrayList<>();
}
private void addAll(List events) {
this.accumulatedEvents.addAll(events);
}
private List allEvents() {
List eventList = new ArrayList<>(initialEvents);
eventList.addAll(accumulatedEvents);
return Collections.unmodifiableList(eventList);
}
}
}