Class Event<C,R>

java.lang.Object
java.util.AbstractMap<Object,Object>
java.util.HashMap<Object,Object>
berlin.yuna.typemap.model.TypeMap
org.nanonative.nano.helper.event.model.Event<C,R>
All Implemented Interfaces:
berlin.yuna.typemap.model.TypeInfo<berlin.yuna.typemap.model.TypeMap>, berlin.yuna.typemap.model.TypeMapI<berlin.yuna.typemap.model.TypeMap>, Serializable, Cloneable, Map<Object,Object>

public class Event<C,R> extends berlin.yuna.typemap.model.TypeMap
Event container transporting payloads and metadata.
See Also:
  • Field Details

    • channel

      protected final transient Channel<C,R> channel
    • context

      protected final Context context
    • responseListener

      protected transient Consumer<R> responseListener
    • payload

      protected transient Supplier<C> payload
    • payloadRaw

      protected transient C payloadRaw
    • response

      protected transient R response
    • error

      protected Throwable error
    • isAcknowledged

      protected final AtomicBoolean isAcknowledged
  • Constructor Details

    • Event

      public Event(Context context, Channel<C,R> channel)
      Constructs an instance of the Event class with specified payload, context, payload, and response listener. This event object can be used to trigger specific actions or responses based on the event payload and payload.
      Parameters:
      context - The Context in which the event is created and processed. It provides environmental data and configurations.
      channel - The Channel associated with this event, defining the type of payload and response.
  • Method Details

    • channel

      public Channel<C,R> channel()
      Returns the name of the Channel.
      Returns:
      the name of the Channel.
    • channel

      public <A, B> Optional<Event<A,B>> channel(Channel<A,B> channel)
      Returns the event as an Optional of type Event. This method allows for safe retrieval of the event, ensuring that it matches the specified channel.
      Parameters:
      channel - The channel to match against the event's channel.
      Returns:
      An Optional containing the event if it matches the channel, otherwise an empty Optional.
    • async

      public Event<C,R> async(boolean async)
      Sets the event to asynchronous mode, allowing the response to be handled by a listener.
      Returns:
      self for chaining
    • async

      public Event<C,R> async(Consumer<R> responseListener)
      Sets the event to asynchronous mode, allowing the response to be handled by a listener.
      Parameters:
      responseListener - A consumer that handles the response of the event processing. It can be used to execute actions based on the event's outcome or data.
      Returns:
      self for chaining
    • payload

      public Event<C,R> payload(Supplier<C> payload)
      Sets the payload of the event.
      Parameters:
      payload - The data or object that is associated with this event. This can be any relevant information that needs to be passed along with the event.
      Returns:
      self for chaining
    • payloadAck

      public C payloadAck()
      Acknowledges the event and returns the resolved payload.

      Equivalent to acknowledge().payload().

      Returns:
      the payload value
    • payloadOpt

      public Optional<C> payloadOpt()
      Returns the resolved payload if present.
      Returns:
      optional payload
    • payloadAckOpt

      public Optional<C> payloadAckOpt()
      Acknowledges the event and returns the payload if present.

      If the payload exists, respond(Object) is invoked with null to mark acknowledgement and the payload is returned.

      Returns:
      optional payload after acknowledgement
    • payloadAsync

      public Event<C,R> payloadAsync(Consumer<C> consumer)
      Resolves the payload asynchronously on the Context executor and passes it to the consumer. Does nothing if no payload has been set.
      Parameters:
      consumer - callback receiving the resolved payload
      Returns:
      this event for chaining
    • payloadAckAsync

      public Event<C,R> payloadAckAsync(Consumer<C> consumer)
      Acknowledges first, then resolves the payload asynchronously and passes it to the consumer.
      Parameters:
      consumer - callback receiving the resolved payload
      Returns:
      this event for chaining
    • payload

      public C payload()
      Returns the payload, resolving the supplier on first access and caching the result.

      If the resolved value is an Event, this instance records containsEvent=true and sets a parentEvent link on the nested event to support acknowledgement and error propagation.

      Returns:
      the payload value; may be null if no supplier/value were provided
    • isAcknowledged

      public boolean isAcknowledged()
      Indicates whether the event has been acknowledged via respond(Object) or acknowledge().
      Returns:
      true if acknowledged; otherwise false
    • acknowledge

      public Event<C,R> acknowledge()
      Marks the event as acknowledged without providing a response body.

      Shorthand for respond(null).

      Returns:
      this event for chaining
    • isBroadcast

      public boolean isBroadcast()
      Returns whether this event is marked as broadcast.

      Broadcast is a lightweight flag stored in the event map; routing components may use it to fan out delivery.

      Returns:
      true if broadcast; otherwise false
    • broadcast

      public Event<C,R> broadcast(boolean broadcast)
      Sets the broadcast flag.
      Parameters:
      broadcast - whether the event should be treated as broadcast
      Returns:
      this event for chaining
    • context

      public Context context()
      Returns the context associated with this event.
      Returns:
      the context
    • isAsync

      public boolean isAsync()
      Returns true if a response listener is installed (i.e., the event is asynchronous).
      Returns:
      true when async; otherwise false
    • containsEvent

      public boolean containsEvent()
      Returns true if the resolved payload is itself an Event.
      Returns:
      whether the payload contains a nested event
    • containsEvent

      public Event<C,R> containsEvent(boolean containsEvent)
      Sets the internal flag indicating that the payload contains a nested event.
      Parameters:
      containsEvent - true if payload is an event
      Returns:
      this event for chaining
    • listener

      public Consumer<R> listener()
      Returns the currently configured response listener, if any.
      Returns:
      the listener or null if synchronous
    • respond

      public Event<C,R> respond(R response)
      Completes the event with a response and marks it as acknowledged.

      If a listener is present, it is invoked with the response. Acknowledgement is also propagated to a parent or nested event when applicable.

      Parameters:
      response - the response value (maybe null)
      Returns:
      this event for chaining
    • response

      public R response()
      Returns the response value set via respond(Object).
      Returns:
      the response; may be null if none
    • responseOpt

      public Optional<R> responseOpt()
      Returns the response as an Optional.
      Returns:
      optional response
    • responseOpt

      public <T> Optional<T> responseOpt(Class<T> type)
      Returns the response as an Optional only if it is compatible with type.
      Type Parameters:
      T - desired response view
      Parameters:
      type - target class to check against the channel's response type
      Returns:
      optional typed response when assignable; otherwise empty
    • peek

      public Event<C,R> peek(Consumer<Event<C,R>> peek)
      Invokes the given consumer with this event and returns this instance.

      Useful for debugging or fluent side-effects without breaking the chain.

      Parameters:
      peek - consumer observing this event
      Returns:
      this event for chaining
    • putR

      public Event<C,R> putR(Object key, Object value)
      Puts the key/value into the underlying map and returns this for fluent chaining.
      Parameters:
      key - map key
      value - map value
      Returns:
      this event for chaining
    • filter

      public berlin.yuna.typemap.model.Type<Event<C,R>> filter(Predicate<Event<C,R>> predicate)
      Applies a predicate to this event and returns a Type wrapper containing either this event (when the predicate passes) or null.

      The Type class integrates nicely with the project’s TypeMap usage.

      Parameters:
      predicate - filter to evaluate
      Returns:
      a Type containing this event or null
    • error

      public Throwable error()
      Returns the most relevant error for this event.

      If a direct error has been set via error(Throwable), it is returned. Otherwise, the method attempts to retrieve an error from the parent event or a nested payload event (if present).

      Returns:
      the error or null if none found
    • error

      public Event<C,R> error(Throwable error)
      Attaches an error to this event.
      Parameters:
      error - the throwable to associate
      Returns:
      this event for chaining
    • send

      public Event<C,R> send()
      Sends the event to the Nano instance for processing.
      Returns:
      self for chaining
    • sendR

      public Context sendR()
      Sends the event to the Nano instance for processing.
      Returns:
      Context for chaining
    • toString

      public String toString()
      Returns a JSON-like representation with selected fields for diagnostics.
      Overrides:
      toString in class AbstractMap<Object,Object>
    • equals

      public boolean equals(Object o)
      Two events are equal when their underlying map content is equal and they share the same channel, context, listener, and payload supplier.
      Specified by:
      equals in interface Map<C,R>
      Overrides:
      equals in class AbstractMap<Object,Object>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Map<C,R>
      Overrides:
      hashCode in class AbstractMap<Object,Object>