com.github.rahulsom.grooves.queries.internal.JoinExecutor Maven / Gradle / Ivy
package com.github.rahulsom.grooves.queries.internal;
import com.github.rahulsom.grooves.api.AggregateType;
import com.github.rahulsom.grooves.api.events.*;
import com.github.rahulsom.grooves.api.snapshots.internal.BaseJoin;
import rx.Observable;
import java.util.List;
/**
* Executes a query as a Join.
*
* @param The Aggregate this join represents
* @param The type for the {@link EventT}'s id field
* @param The base type for events that apply to {@link AggregateT}
* @param The type for the join's id field
* @param The type for the id of the other aggregate that {@link AggregateT}
* joins to
* @param The type for the other aggregate that {@link AggregateT} joins to
* @param The type of Snapshot that is computed
* @param The type of the Join Event
* @param The type of the disjoin event
*
* @author Rahul Somasunderam
*/
public class JoinExecutor<
AggregateT extends AggregateType,
EventIdT,
EventT extends BaseEvent,
JoinedAggregateIdT,
JoinedAggregateT extends AggregateType,
SnapshotIdT,
SnapshotT extends BaseJoin,
JoinEventT extends JoinEvent,
DisjoinEventT extends DisjoinEvent>
extends
QueryExecutor {
private final Class classJoinE;
private final Class classDisjoinE;
public JoinExecutor(Class classJoinE, Class classDisjoinE) {
this.classJoinE = classJoinE;
this.classDisjoinE = classDisjoinE;
}
@Override
public Observable applyEvents(
BaseQuery query,
SnapshotT initialSnapshot,
Observable events,
List> deprecatesList,
List aggregates, AggregateT aggregate) {
// s -> snapshotObservable
return events.reduce(Observable.just(initialSnapshot), (s, event) -> s.flatMap(snapshot -> {
if (!query.shouldEventsBeApplied(snapshot)) {
return Observable.just(snapshot);
} else {
log.debug(" -> Applying Event: {}", event);
if (event instanceof Deprecates) {
return applyDeprecates((Deprecates) event,
query, aggregates, deprecatesList, aggregate);
} else if (event instanceof DeprecatedBy) {
return applyDeprecatedBy(
(DeprecatedBy) event,
initialSnapshot);
} else if (classJoinE.isAssignableFrom(event.getClass())) {
initialSnapshot.getJoinedIds().add(
((JoinEventT) event).getJoinAggregate().getId());
return Observable.just(initialSnapshot);
} else if (classDisjoinE.isAssignableFrom(event.getClass())) {
initialSnapshot.getJoinedIds().remove(
((DisjoinEventT) event).getJoinAggregate().getId());
return Observable.just(initialSnapshot);
} else {
return Observable.just(initialSnapshot);
}
}
})).flatMap(it -> it);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy