Record Class Channel<T,R>

java.lang.Object
java.lang.Record
org.nanonative.nano.helper.event.model.Channel<T,R>

public record Channel<T,R>(int id, String name, Class<T> payload, Class<R> response) extends Record
Publish/subscribe channel used to route events between components.
  • Constructor Details

    • Channel

      public Channel(int id, String name, Class<T> payload, Class<R> response)
      Creates an instance of a Channel record class.
      Parameters:
      id - the value for the id record component
      name - the value for the name record component
      payload - the value for the payload record component
      response - the value for the response record component
  • Method Details

    • registerChannelId

      public static Channel<Void,Void> registerChannelId(String name)
      Registers a new Channel with a given name if it does not already exist. If the Channel payload already exists, it returns the existing Channel.
      Parameters:
      name - The name of the Channel payload to register.
      Returns:
      The Channel of the newly registered event payload, or the Channel of the existing event payload if it already exists. Returns null if the input is null or empty.
    • registerChannelId

      public static <C> Channel<C,Void> registerChannelId(String name, Class<C> payload)
      Registers a new Channel with a given name if it does not already exist. If the Channel payload already exists, it returns the existing Channel.
      Parameters:
      name - The name of the Channel payload to register.
      payload - The class type of the payload for the Channel.
      Returns:
      The Channel of the newly registered event payload, or the Channel of the existing event payload if it already exists. Returns null if the input is null or empty.
    • registerChannelId

      public static <C, R> Channel<C,R> registerChannelId(String name, Class<C> type, Class<R> response)
      Registers a new Channel with a given name if it does not already exist. If the Channel payload already exists, it returns the existing Channel.
      Parameters:
      name - The name of the Channel payload to register.
      response - The class type of the response for the Channel.
      Returns:
      The Channel of the newly registered event payload, or the Channel of the existing event payload if it already exists. Returns null if the input is null or empty.
    • channelOf

      public static Optional<Channel<?,?>> channelOf(String name)
      Attempts to find the Channel based on its name. This method is primarily used for debugging or startup purposes and is not optimized for performance.
      Parameters:
      name - The name of the Channel.
      Returns:
      An Optional containing the Channel of the Event payload if found, or empty if not found
    • channelOf

      public static Channel<?,?> channelOf(int id)
      Retrieves the name of an event Channel given its id.
      Parameters:
      id - for the Channel.
      Returns:
      The name of the Channel associated with the given id, or null if not found.
    • isChannelIdAvailable

      public static boolean isChannelIdAvailable(int id)
      Checks if a Channel with the given id exists.
      Parameters:
      id - The id of the Channel to check.
      Returns:
      true if a Channel with the given id exists, false otherwise.
    • toString

      public String toString()
      Performs the toString operation.
      Specified by:
      toString in class Record
      Returns:
      the result
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • id

      public int id()
      Returns the value of the id record component.
      Returns:
      the value of the id record component
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • payload

      public Class<T> payload()
      Returns the value of the payload record component.
      Returns:
      the value of the payload record component
    • response

      public Class<R> response()
      Returns the value of the response record component.
      Returns:
      the value of the response record component