Class EventStore
java.lang.Object
com.reallifedeveloper.common.application.eventstore.EventStore
- Author:
- RealLifeDeveloper
-
Constructor Summary
ConstructorsConstructorDescriptionEventStore
(ObjectSerializer<String> serializer, StoredEventRepository repository) Creates a newEventStore
with the given serializer and repository. -
Method Summary
Modifier and TypeMethodDescriptionadd
(DomainEvent event) Adds a newStoredEvent
representing the givenDomainEvent
to the event store.allEventsBetween
(long firstStoredEventId, long lastStoredEventId) Gives allStoredEvents
with IDs greater than or equal tofirstStoredEventId
and less than or equals tolastStoredEventId
, i.e., all events that occurred between the events with the given IDs, inclusive.allEventsSince
(long storedEventId) Gives allStoredEvents
with IDs greater thanstoredEventId
, i.e., all events that occurred after the event with the given ID.long
Gives the ID of the most recently addedStoredEvents
.<T extends DomainEvent>
TtoDomainEvent
(StoredEvent storedEvent) Converts aStoredEvent
back to its originalDomainEvent
.
-
Constructor Details
-
EventStore
Creates a newEventStore
with the given serializer and repository.- Parameters:
serializer
- theDomainEventSerializer
to use to serialize and deserializeDomainEvents
repository
- theStoredEventRepository
to use to work with persistedStoredEvents
-
-
Method Details
-
add
Adds a newStoredEvent
representing the givenDomainEvent
to the event store.- Parameters:
event
- theDomainEvent
to add- Returns:
- the saved
StoredEvent
representingevent
-
allEventsSince
Gives allStoredEvents
with IDs greater thanstoredEventId
, 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 tofirstStoredEventId
-
allEventsBetween
Gives allStoredEvents
with IDs greater than or equal tofirstStoredEventId
and less than or equals tolastStoredEventId
, i.e., all events that occurred between the events with the given IDs, inclusive.- Parameters:
firstStoredEventId
- ID of the firstStoredEvent
to retrievelastStoredEventId
- ID of the lastStoredEvent
to retrieve- Returns:
- a list of all
StoredEvents
with IDs betweenfirstStoredEventId
andlastStoredEventId
, inclusive
-
toDomainEvent
Converts aStoredEvent
back to its originalDomainEvent
.This is only guaranteed to work if the same kind of
EventStore
, using the same type ofDomainEventSerializer
, was used to add theDomainEvent
.- Type Parameters:
T
- the type ofDomainEvent
to return- Parameters:
storedEvent
- theStoredEvent
to convert- Returns:
- the original
DomainEvent
represented bystoredEvent
- Throws:
IllegalArgumentException
- ifstoredEvent
isnull
IllegalStateException
- if loading of the classT
failed
-
lastStoredEventId
public long lastStoredEventId()Gives the ID of the most recently addedStoredEvents
.- Returns:
- the ID of the most recently added
StoredEvent
-