dk.cloudcreate.essentials.components.eventsourced.aggregates.decider.StateEvolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eventsourced-aggregates Show documentation
Show all versions of eventsourced-aggregates Show documentation
This library focuses on providing different flavors of eventsourced aggregrates
/*
* Copyright 2021-2024 the original author or authors.
*
* 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
*
* https://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 dk.cloudcreate.essentials.components.eventsourced.aggregates.decider;
import java.util.stream.Stream;
import static dk.cloudcreate.essentials.shared.FailFast.requireNonNull;
/**
* A {@link Decider} or {@link View} related interface, which can apply EVENT
<(s) to a aggregate/projection/view STATE
instance
*
* @param The type of Events that can be applied in the {@link #applyEvent(Object, Object)}
* @param The type of aggregate/projection/view STATE
that {@link #applyEvent(Object, Object)} supports
*/
@FunctionalInterface
public interface StateEvolver {
/**
* Apply the EVENT
to the aggregate/projection/view STATE
instance
* Note: This method is called evolve
in the decider pattern
*
* @param event the EVENT
to be applied / projected onto the current aggregate/projection/view STATE
* @param state the current STATE
of the aggregate/projection/view
* @return the new aggregate/projection/view STATE
(after the EVENT
has been applied / projected onto the current aggregate/projection/view STATE
)
*/
STATE applyEvent(EVENT event, STATE state);
/**
* Perform a left-fold over the eventStream
using the initialState
as the initial state
*
* @param stateEvolver the state evolver (that applies events to the state)
* @param initialState the initial state provided to the state evolver
* @param eventStream the stream of Events supplied one by one (in-order) to the state evolver
* @param The type of Events that can be applied in the {@link #applyEvent(Object, Object)}
* @param The type of aggregate/projection/view STATE
that {@link #applyEvent(Object, Object)} supports
* @return the initial state with all events applied to it
*/
static STATE applyEvents(StateEvolver stateEvolver,
STATE initialState,
Stream eventStream) {
requireNonNull(initialState, "No initialState provided");
return requireNonNull(eventStream, "No eventStream provided")
.reduce(initialState,
(state, event) -> stateEvolver.applyEvent(event, state),
(deltaState, deltaState2) -> deltaState2);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy