Class LazyAggregateRoot<T>
- Type Parameters:
T- The type of the aggregate's value.
- All Implemented Interfaces:
AggregateRoot<T>, Entity<T>
AggregateRoot that defers deserialization and event application until the
entity value is explicitly accessed.
This implementation is typically used as a historical previous reference in ImmutableAggregateRoot
when the aggregate's Aggregate.cachingDepth() has been exceeded. In
such cases, only the checkpointed state is retained in memory, and the full entity state is reconstructed by
replaying events from the most recent checkpoint.
LazyAggregateRoot is read-only and will throw UnsupportedOperationException for any attempt to
apply updates or modify the state. It is solely intended for retrieving past states in an efficient and
memory-conscious way.
Event replay occurs on demand via the get() method, which reconstructs the aggregate state by re-applying
all events from the last known checkpoint until the desired sequence number or event id is reached.
- See Also:
-
Field Summary
Fields inherited from interface Entity
AGGREGATE_ID_METADATA_KEY, AGGREGATE_SN_METADATA_KEY, AGGREGATE_TYPE_METADATA_KEY, applying, loading -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCollection<?> aliases()Retrieves a collection of aliases associated with this entity.Applies the given message to the entity.assertLegal(Object update) Verifies that the provided update is legal given the current state of the aggregate.commit()Commits the current state of the entity, persisting any changes made to it.Collection<? extends Entity<?>> entities()Retrieves child entities of this entity.static <T> LazyAggregateRoot<T> from(ImmutableAggregateRoot<T> delegate) get()Retrieves the current instance of the entity.id()Retrieves the unique identifier of the entity.Retrieves the name of the property that serves as the unique identifier for the entity.Retrieves the identifier of the last event applied to the entity hierarchy.Retrieves the index of the last event applied to this entity hierarchy.previous()Retrieves the previous version of this entity.longRetrieves the sequence number of the current entity.Retrieves the timestamp of the entity.type()Retrieves the type of the entity.update(UnaryOperator<T> function) Updates the current entity's value using the specified unary operator and returns a new entity containing the updated value.withEventIndex(Long index, String messageId) Updates the event index and message ID of the entity and returns the updated version of the entity.withSequenceNumber(long sequenceNumber) Returns an updated entity based on this with the specified sequence number.Sets the type of the entity to the specified class and returns the updated entity.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface AggregateRoot
parentMethods inherited from interface Entity
allEntities, ancestors, ancestorValue, apply, apply, apply, apply, apply, assertAndApply, assertAndApply, assertAndApply, assertAndApply, associations, depth, dissociations, getEntity, highestEventIndex, ifPresent, isEmpty, isPresent, isRoot, mapIfPresent, playBackToCondition, playBackToEvent, possibleTargets, relationships, root, rootAnnotation
-
Constructor Details
-
LazyAggregateRoot
-
-
Method Details
-
from
-
withEventIndex
Description copied from interface:EntityUpdates the event index and message ID of the entity and returns the updated version of the entity.- Specified by:
withEventIndexin interfaceAggregateRoot<T>- Specified by:
withEventIndexin interfaceEntity<T>- Parameters:
index- the event index to set for the root entitymessageId- the message ID associated with the event- Returns:
- the updated entity corresponding to the current entity's ID and type, or null if no such entity can be found
-
sequenceNumber
public long sequenceNumber()Description copied from interface:EntityRetrieves the sequence number of the current entity.- Specified by:
sequenceNumberin interfaceAggregateRoot<T>- Specified by:
sequenceNumberin interfaceEntity<T>- Returns:
- the sequence number of the root entity
-
timestamp
-
previous
Description copied from interface:EntityRetrieves the previous version of this entity. -
get
-
getLastCheckpoint
-
aliases
Description copied from interface:EntityRetrieves a collection of aliases associated with this entity. -
entities
-
id
-
type
-
idProperty
Description copied from interface:EntityRetrieves the name of the property that serves as the unique identifier for the entity.- Specified by:
idPropertyin interfaceEntity<T>- Returns:
- the name of the identification property as a string
-
lastEventId
Description copied from interface:EntityRetrieves the identifier of the last event applied to the entity hierarchy.- Specified by:
lastEventIdin interfaceAggregateRoot<T>- Specified by:
lastEventIdin interfaceEntity<T>- Returns:
- the identifier of the last event as a string, or null if no events have been recorded in the entity's aggregate
-
lastEventIndex
Description copied from interface:EntityRetrieves the index of the last event applied to this entity hierarchy.- Specified by:
lastEventIndexin interfaceAggregateRoot<T>- Specified by:
lastEventIndexin interfaceEntity<T>- Returns:
- the index of the last event as a
Long, or null if no events have been recorded for the entity's aggregate
-
update
Description copied from interface:EntityUpdates the current entity's value using the specified unary operator and returns a new entity containing the updated value. -
apply
Description copied from interface:EntityApplies the given message to the entity. -
commit
Description copied from interface:EntityCommits the current state of the entity, persisting any changes made to it. This method ensures that the modifications are saved. Typically, it is unnecessary to invoke this manually as it is automatically invoked after the current handler or consumer batch has completed. -
withType
Description copied from interface:EntitySets the type of the entity to the specified class and returns the updated entity. -
withSequenceNumber
Description copied from interface:EntityReturns an updated entity based on this with the specified sequence number.- Specified by:
withSequenceNumberin interfaceAggregateRoot<T>- Specified by:
withSequenceNumberin interfaceEntity<T>- Parameters:
sequenceNumber- the sequence number to assign to the entity- Returns:
- the updated entity with the specified sequence number, or null if the entity could not be found in the entity hierarchy
-
assertLegal
Description copied from interface:EntityVerifies that the provided update is legal given the current state of the aggregate. If so, the entity is returned; otherwise, it throws an appropriate exception.- Specified by:
assertLegalin interfaceEntity<T>- Type Parameters:
E- the type of exception expected if the update is not legal- Parameters:
update- the update to be validated for compliance with the required rules- Returns:
- the entity if the update is legal
- Throws:
E- if the update fails to meet legal requirements
-