Class SideEffectFreeEntity<T>

java.lang.Object
io.fluxcapacitor.javaclient.modeling.SideEffectFreeEntity<T>
Type Parameters:
T - the type of the entity's value
All Implemented Interfaces:
Entity<T>

public class SideEffectFreeEntity<T> extends Object implements Entity<T>
A functional, non-persistent Entity wrapper that enables safe state mutation and validation without producing side effects such as event publication or snapshot creation.

SideEffectFreeEntity allows invocation of methods like apply(Message), update(UnaryOperator), and assertLegal(Object), making it suitable for scenarios where domain logic or legality checks are needed without committing the resulting changes.

This entity is returned from AggregateRepository.asEntity(Object) or FluxCapacitor.asEntity(Object) to allow ad-hoc interaction with domain objects outside of the lifecycle of an active aggregate. It is particularly useful for:

  • Performing validations with @AssertLegal
  • Executing @Apply handlers to derive a new state
  • Analyzing projected changes without actually modifying persisted state
Internally, it enforces Entity.isLoading() to remain true during apply() and update() invocations to suppress unintended side effects like event publication or committing. However, the returned state reflects the result of applying the logic, allowing chaining or inspection.

Unlike NoOpEntity, which disables all state changes entirely, SideEffectFreeEntity evaluates them fully but simply discards any side effects. The commit() operation is a no-op.

See Also:
  • Constructor Details

    • SideEffectFreeEntity

      public SideEffectFreeEntity(Entity<T> delegate)
  • Method Details

    • apply

      public SideEffectFreeEntity<T> apply(Message eventMessage)
      Description copied from interface: Entity
      Applies the given message to the entity.
      Specified by:
      apply in interface Entity<T>
      Parameters:
      eventMessage - the message representing the event to be applied
      Returns:
      the updated or newly created Entity of type T
    • update

      public SideEffectFreeEntity<T> update(UnaryOperator<T> function)
      Description copied from interface: Entity
      Updates the current entity's value using the specified unary operator and returns a new entity containing the updated value.
      Specified by:
      update in interface Entity<T>
      Parameters:
      function - the unary operator to apply to the current entity's value
      Returns:
      a new entity containing the updated value after applying the function
    • commit

      public Entity<T> commit()
      Description copied from interface: Entity
      Commits 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.
      Specified by:
      commit in interface Entity<T>
      Returns:
      The updated entity after the commit operation is successfully completed.
    • assertLegal

      public <E extends Exception> Entity<T> assertLegal(Object update) throws E
      Description copied from interface: Entity
      Verifies 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:
      assertLegal in interface Entity<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
    • parent

      public Entity<?> parent()
      Description copied from interface: Entity
      Retrieves the parent entity of the current entity.
      Specified by:
      parent in interface Entity<T>
      Returns:
      the parent entity of the current entity, or null if this entity does not have a parent
    • aliases

      public Collection<?> aliases()
      Description copied from interface: Entity
      Retrieves a collection of aliases associated with this entity.
      Specified by:
      aliases in interface Entity<T>
      Returns:
      a collection containing aliases of this entity, or an empty collection if no aliases are present
    • previous

      public SideEffectFreeEntity<T> previous()
      Description copied from interface: Entity
      Retrieves the previous version of this entity.
      Specified by:
      previous in interface Entity<T>
      Returns:
      the previous state of the entity, or null if this is the first known version
    • id

      public Object id()
      Description copied from interface: Entity
      Retrieves the unique identifier of the entity.
      Specified by:
      id in interface Entity<T>
      Returns:
      the unique identifier of the entity, or null if this entity has not been initialized yet
    • type

      public Class<T> type()
      Description copied from interface: Entity
      Retrieves the type of the entity.
      Specified by:
      type in interface Entity<T>
      Returns:
      the class type of the entity, or null if the type has not been defined
    • withType

      public Entity<T> withType(Class<T> type)
      Description copied from interface: Entity
      Sets the type of the entity to the specified class and returns the updated entity.
      Specified by:
      withType in interface Entity<T>
      Parameters:
      type - the class representing the type to be set for the entity
      Returns:
      the updated entity with the specified type
    • get

      public T get()
      Description copied from interface: Entity
      Retrieves the current instance of the entity.
      Specified by:
      get in interface Entity<T>
      Returns:
      the current instance of the entity or null if not initialized
    • idProperty

      public String idProperty()
      Description copied from interface: Entity
      Retrieves the name of the property that serves as the unique identifier for the entity.
      Specified by:
      idProperty in interface Entity<T>
      Returns:
      the name of the identification property as a string