
com.opencredo.concursus.mapping.events.methods.dispatching.DispatchingEventReplayer Maven / Gradle / Ivy
The newest version!
package com.opencredo.concursus.mapping.events.methods.dispatching;
import com.opencredo.concursus.domain.events.Event;
import com.opencredo.concursus.domain.events.sourcing.EventReplayer;
import com.opencredo.concursus.mapping.events.methods.reflection.dispatching.MultiTypeEventDispatcher;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* Wraps an {@link EventReplayer} and dispatches replayed events to suitable handlers.
* @param The type of the event-emitter interface that the handlers must implement.
*/
public final class DispatchingEventReplayer {
static DispatchingEventReplayer dispatching(Comparator causalOrderComparator, MultiTypeEventDispatcher mapper, EventReplayer eventReplayer) {
return new DispatchingEventReplayer<>(causalOrderComparator, mapper, eventReplayer);
}
private final Comparator causalOrderComparator;
private final MultiTypeEventDispatcher dispatcher;
private final EventReplayer eventReplayer;
private DispatchingEventReplayer(Comparator causalOrderComparator, MultiTypeEventDispatcher dispatcher, EventReplayer eventReplayer) {
this.causalOrderComparator = causalOrderComparator;
this.dispatcher = dispatcher;
this.eventReplayer = eventReplayer;
}
/**
* Set the order to descending.
* @return A {@link DispatchingEventReplayer} replaying events in time-descending order.
*/
public DispatchingEventReplayer inDescendingOrder() {
return new DispatchingEventReplayer<>(causalOrderComparator, dispatcher, eventReplayer.inDescendingOrder());
}
/**
* Set the order to descending, sorted by the supplied {@link Comparator}.
* @param comparator The {@link Comparator} comparator to use to sort events.
* @return A {@link DispatchingEventReplayer} replaying events in time-descending order.
*/
public DispatchingEventReplayer inDescendingOrder(Comparator comparator) {
return new DispatchingEventReplayer<>(causalOrderComparator, dispatcher, eventReplayer.inDescendingOrder(comparator));
}
/**
* Set the order to descending.
* @return A {@link DispatchingEventReplayer} replaying events in time-descending causal order.
*/
public DispatchingEventReplayer inDescendingCausalOrder() {
return inDescendingOrder(causalOrderComparator);
}
/**
* Set the order to ascending.
* @return A {@link DispatchingEventReplayer} replaying events in time-ascending order.
*/
public DispatchingEventReplayer inAscendingOrder() {
return new DispatchingEventReplayer<>(causalOrderComparator, dispatcher, eventReplayer.inAscendingOrder());
}
/**
* Set the order to ascending, sorted by the supplied {@link Comparator}.
* @param comparator The {@link Comparator} comparator to use to sort events.
* @return A {@link DispatchingEventReplayer} replaying events in time-ascending order.
*/
public DispatchingEventReplayer inAscendingOrder(Comparator comparator) {
return new DispatchingEventReplayer<>(causalOrderComparator, dispatcher, eventReplayer.inAscendingOrder(comparator));
}
/**
* Set the order to ascending.
* @return A {@link DispatchingEventReplayer} replaying events in time-ascending causal order.
*/
public DispatchingEventReplayer inAscendingCausalOrder() {
return inAscendingOrder(causalOrderComparator);
}
/**
* Apply a filter to the events
* @param predicate The {@link Predicate} to use to filter events.
* @return A {@link DispatchingEventReplayer} replaying events which match the filter.
*/
public DispatchingEventReplayer filter(Predicate predicate) {
return new DispatchingEventReplayer<>(causalOrderComparator, dispatcher, eventReplayer.filter(predicate));
}
/**
* Replay only the first {@link Event} in the sequence.
* @param handler The event handler to replay the event to.
*/
public void replayFirst(T handler) {
eventReplayer.replayFirst(DispatchingEventOutChannel.binding(dispatcher, handler));
}
/**
* Replay all of the {@link Event}s in the sequence.
* @param handler The event handler to replay the events to.
*/
public void replayAll(T handler) {
eventReplayer.replayAll(DispatchingEventOutChannel.binding(dispatcher, handler));
}
/**
* Collect only the first {@link Event} in the sequence.
* @param collector The collector to collect the event with.
* @param The type of value returned by the collector.
* @return The collected value, or {@link Optional}::empty if the sequence is empty.
*/
public Optional collectFirst(Function, T> collector) {
return eventReplayer.collectFirst(caller -> DispatchingEventOutChannel.binding(dispatcher, collector.apply(caller)));
}
/**
* Collect all of the {@link Event}s in the sequence.
* @param collector The collector to collect the events with.
* @param The type of value returned by the collector.
* @return The collected values.
*/
public List collectAll(Function, T> collector) {
return eventReplayer.collectAll(caller -> DispatchingEventOutChannel.binding(dispatcher, collector.apply(caller)));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy