java.lang.Object
dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.classic.state.AggregateState<ID,EVENT_TYPE>
Type Parameters:
ID - the aggregate id type
EVENT_TYPE - the type of event

public abstract class AggregateState<ID,EVENT_TYPE extends Event<ID>> extends Object
Base class for the state object associated with AggregateRootWithState.
When this is combined with the AggregateRootWithState when the AggregateRootWithState will contain the command methods and the AggregateState contains the state fields and the EventHandler annotated methods.
  • Constructor Details

    • AggregateState

      public AggregateState()
  • Method Details

    • rehydrate

      public void rehydrate(Stream<EVENT_TYPE> previousEvents)
    • getEventOrderOfLastAppliedEvent

      public dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.types.EventOrder getEventOrderOfLastAppliedEvent()
    • getEventOrderOfLastRehydratedEvent

      public dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.types.EventOrder getEventOrderOfLastRehydratedEvent()
    • apply

      protected final void apply(EVENT_TYPE event)
      Apply a new non persisted/uncommitted Event to this aggregate instance.
      If it is the very FIRST Event that is being applied then Event.aggregateId() MUST return the ID of the aggregate the event relates to
      Every consecutive Event applied will have its Event.aggregateId(Object) method called IF it doesn't already have a value. I.e. you can be lazy and skip setting the aggregate id on the Event if you don't want to.

      The AggregateRoot automatically keeps track of the Event.eventOrder() value and will set it for you and ensure that it's growing consecutively.

      Parameters:
      event - the event to apply
    • aggregateId

      public ID aggregateId()
    • hasBeenRehydrated

      public boolean hasBeenRehydrated()
      Has rehydrate(Stream) been used
    • isRehydrating

      protected final boolean isRehydrating()
      Is the event being supplied to applyEventToTheAggregate(Event) a historic event
    • applyEventToTheAggregate

      protected void applyEventToTheAggregate(Event<ID> event)
      Apply the event to the aggregate instance to reflect the event as a state change to the aggregate
      The default implementation will automatically call any (private) methods annotated with EventHandler
      Parameters:
      event - the event to apply to the aggregate
      See Also:
    • eventOrderOfLastAppliedEvent

      public final dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.types.EventOrder eventOrderOfLastAppliedEvent()
      Get the of the last {}@link Event that was applied to the AggregateRoot (either using rehydrate(Stream) or using apply(Event)
      Returns:
      the event order of the last applied Event or EventOrder.NO_EVENTS_PREVIOUSLY_PERSISTED in case no events has ever been applied to the aggregate
    • uncommittedChanges

      public List<EVENT_TYPE> uncommittedChanges()
      The the events that have been applied to this aggregate instance but not yet persisted to the underlying EventStore
    • markChangesAsCommitted

      public void markChangesAsCommitted()
      Resets the uncommittedChanges() - effectively marking them as having been persisted and committed to the underlying EventStore