Class ImmutableEntity<T>
- Type Parameters:
T- the type of the underlying domain object represented by this entity
- All Implemented Interfaces:
Entity<T>
- Direct Known Subclasses:
ImmutableAggregateRoot
Entity interface, representing a snapshot of a domain entity.
Unlike mutable entities, an ImmutableEntity is never changed in-place. Instead, updates such as event
applications or transformations return a new, updated instance, preserving immutability and making it suitable
for event-sourced state transitions, testing, and functional-style programming models.
This entity is typically wrapped by higher-level mutable structures such as ModifiableAggregateRoot,
which manage lifecycle and mutation tracking.
Key Features
- Supports event application via methods annotated with
@Apply - Supports recursive validation using
@AssertLegal - Automatically resolves child entities and aliases using
@Memberand@Aliasannotations - Preserves immutability by returning a new instance after each update
Common Usage
ImmutableEntity<Order> orderEntity = ImmutableEntity.<Order>builder()
.id("order123")
.type(Order.class)
.value(order)
.entityHelper(entityHelper)
.serializer(serializer)
.build();
// Apply an event and receive a new version of the entity
orderEntity = orderEntity.apply(eventMessage);
The update(UnaryOperator) method applies a transformation to the entity value and automatically
updates parent references when nested.
Event application is handled via the EntityHelper which dynamically locates appropriate handlers.
If no direct handler is found, the event is propagated recursively to nested child entities.
- 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 TypeMethodDescriptionApplies the given message to the entity.apply(DeserializingMessage message) Applies the given deserializing 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.protected Collection<?> protected Collection<? extends ImmutableEntity<?>> get()Retrieves the current instance 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.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 Entity
aliases, allEntities, ancestors, ancestorValue, apply, apply, apply, apply, assertAndApply, assertAndApply, assertAndApply, assertAndApply, associations, depth, dissociations, entities, getEntity, highestEventIndex, id, idProperty, ifPresent, isEmpty, isPresent, isRoot, lastEventId, lastEventIndex, mapIfPresent, parent, playBackToCondition, playBackToEvent, possibleTargets, previous, relationships, root, rootAnnotation, sequenceNumber, timestamp, withEventIndex, withSequenceNumber
-
Constructor Details
-
ImmutableEntity
public ImmutableEntity()
-
-
Method Details
-
type
-
withType
Description copied from interface:EntitySets the type of the entity to the specified class and returns the updated entity. -
get
-
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
-
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. -
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
-
apply
Description copied from interface:EntityApplies the given deserializing message to the entity. -
computeEntities
-
computeAliases
-