java.lang.Object
com.reallifedeveloper.common.application.eventstore.EventStore

public final class EventStore extends Object
An EventStore saves DomainEvents in a database as StoredEvents.
Author:
RealLifeDeveloper
  • Constructor Details

    • EventStore

      public EventStore(ObjectSerializer<String> serializer, StoredEventRepository repository)
      Creates a new EventStore with the given serializer and repository.
      Parameters:
      serializer - the DomainEventSerializer to use to serialize and deserialize DomainEvents
      repository - the StoredEventRepository to use to work with persisted StoredEvents
  • Method Details

    • add

      public StoredEvent add(DomainEvent event)
      Adds a new StoredEvent representing the given DomainEvent to the event store.
      Parameters:
      event - the DomainEvent to add
      Returns:
      the saved StoredEvent representing event
    • allEventsSince

      public List<StoredEvent> allEventsSince(long storedEventId)
      Gives all StoredEvents with IDs greater than storedEventId, i.e., all events that occurred after the event with the given ID.
      Parameters:
      storedEventId - return all events with IDs greater than this
      Returns:
      a list of StoredEvents with IDs greater than or equal to firstStoredEventId
    • allEventsBetween

      public List<StoredEvent> allEventsBetween(long firstStoredEventId, long lastStoredEventId)
      Gives all StoredEvents with IDs greater than or equal to firstStoredEventId and less than or equals to lastStoredEventId, i.e., all events that occurred between the events with the given IDs, inclusive.
      Parameters:
      firstStoredEventId - ID of the first StoredEvent to retrieve
      lastStoredEventId - ID of the last StoredEvent to retrieve
      Returns:
      a list of all StoredEvents with IDs between firstStoredEventId and lastStoredEventId, inclusive
    • toDomainEvent

      public <T extends DomainEvent> T toDomainEvent(StoredEvent storedEvent)
      Converts a StoredEvent back to its original DomainEvent.

      This is only guaranteed to work if the same kind of EventStore, using the same type of DomainEventSerializer, was used to add the DomainEvent.

      Type Parameters:
      T - the type of DomainEvent to return
      Parameters:
      storedEvent - the StoredEvent to convert
      Returns:
      the original DomainEvent represented by storedEvent
      Throws:
      IllegalArgumentException - if storedEvent is null
      IllegalStateException - if loading of the class T failed
    • lastStoredEventId

      public long lastStoredEventId()
      Gives the ID of the most recently added StoredEvents.
      Returns:
      the ID of the most recently added StoredEvent