com.github.rahulsom.grooves.api.EventsDsl 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 org.jetbrains.annotations.NotNull;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* DSL to simplify writing code with Events.
*
* @param The type of {@link AggregateT}'s id
* @param The Aggregate on which this operates
* @param The type of {@link EventT}'s id
* @param The type of event on which this operates
*
* @author Rahul Somasunderam
*/
public class EventsDsl<
AggregateIdT,
AggregateT extends AggregateType,
EventIdT,
EventT extends BaseEvent> {
private static AtomicLong defaultPositionSupplier = new AtomicLong();
protected static AtomicLong getDefaultPositionSupplier() {
return defaultPositionSupplier;
}
/**
* Allows executing a consumer with some context to setup events.
*
* @param The type of {@link SnapshotT}'s id
* @param The type of Snapshot Generated
* @param aggregate The aggregate on which the consumer must operate
* @param entityConsumer A Consumer that decides what happens when apply is called on an
* entity
* @param positionSupplier A supplier which offers the default position number for an event
* when it is not provided
* @param timestampSupplier A supplier that provides the date for an event if it isn't set
* @param closure The block of code to execute with the aggregate
*
* @return The aggregate after all the code has been executed
*/
@NotNull
public
> AggregateT on(
@NotNull AggregateT aggregate,
@NotNull Consumer entityConsumer,
@NotNull Supplier positionSupplier,
@NotNull Supplier timestampSupplier,
@NotNull Consumer> closure) {
OnSpec spec =
new OnSpec<>();
spec.setAggregate(aggregate);
spec.setEntityConsumer(entityConsumer);
spec.setTimestampSupplier(timestampSupplier);
spec.setPositionSupplier(positionSupplier);
closure.accept(spec);
return aggregate;
}
@NotNull
public > AggregateT on(
@NotNull AggregateT aggregate,
@NotNull Consumer entityConsumer,
@NotNull Supplier positionSupplier,
@NotNull Consumer> closure) {
return on(aggregate, entityConsumer, positionSupplier, Date::new,
closure);
}
@NotNull
public > AggregateT on(
@NotNull AggregateT aggregate,
@NotNull Consumer entityConsumer,
@NotNull Consumer> closure) {
return on(aggregate, entityConsumer, () -> defaultPositionSupplier.incrementAndGet(),
Date::new, closure);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy