com.github.rahulsom.grooves.api.OnSpec Maven / Gradle / Ivy
package com.github.rahulsom.grooves.api;
import com.github.rahulsom.grooves.api.events.BaseEvent;
import com.github.rahulsom.grooves.api.snapshots.Snapshot;
import com.github.rahulsom.grooves.queries.QuerySupport;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Date;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static io.reactivex.Flowable.fromPublisher;
public class OnSpec<
AggregateIdT,
AggregateT extends AggregateType,
EventIdT,
EventT extends BaseEvent,
SnapshotIdT,
SnapshotT extends Snapshot> {
private AggregateT aggregate;
private Consumer entityConsumer;
private Supplier timestampSupplier;
private Supplier positionSupplier;
private static final Logger log = LoggerFactory.getLogger(OnSpec.class);
/**
* Applies an event to an aggregate. This involves checking if any important fields are
* missing and populating them based on the suppliers.
*
* @param type of event
* @param event The event to be applied
*
* @return The event after persisting
*/
@NotNull public T apply(@NotNull T event) {
event.setAggregate(aggregate);
if (event.getPosition() == null) {
event.setPosition(positionSupplier.get());
}
if (event.getTimestamp() == null) {
event.setTimestamp(timestampSupplier.get());
}
entityConsumer.accept(event);
return event;
}
/**
* Computes and persists a snapshot based on a QueryUtil on the aggregate that this
* OnSpec applies on
*
* @param The type of Query to be executed
* @param query The Query Util to compute the snapshot
* @param beforePersist Code to execute before persisting the snapshot.
*
* @return The snapshot after persisting
*/
@NotNull
public > SnapshotT snapshotWith(
@NotNull QueryT query,
@NotNull Consumer beforePersist) {
SnapshotT snapshotT = fromPublisher(query.computeSnapshot(aggregate, Long.MAX_VALUE))
.blockingFirst();
beforePersist.accept(snapshotT);
entityConsumer.accept(snapshotT);
log.info("Persisted {}", snapshotT);
return snapshotT;
}
/**
* Computes and persists a snapshot based on a QueryUtil on the aggregate that this
* OnSpec applies on.
*
* @param The type of Query to be executed
* @param query The Query Util to compute the snapshot
*
* @return The snapshot after persisting
*/
@NotNull
public > SnapshotT snapshotWith(@NotNull QueryT query) {
return snapshotWith(query, snapshotT -> {
});
}
public AggregateT getAggregate() {
return aggregate;
}
public void setAggregate(@NotNull AggregateT aggregate) {
this.aggregate = aggregate;
}
public void setEntityConsumer(@NotNull Consumer entityConsumer) {
this.entityConsumer = entityConsumer;
}
public void setTimestampSupplier(@NotNull Supplier timestampSupplier) {
this.timestampSupplier = timestampSupplier;
}
public void setPositionSupplier(@NotNull Supplier positionSupplier) {
this.positionSupplier = positionSupplier;
}
} © 2015 - 2025 Weber Informatics LLC | Privacy Policy