Module concord_core

Record Class MessageTypeSerializer<T extends Message>

java.lang.Object
java.lang.Record
nl.andrewl.concord_core.msg.MessageTypeSerializer<T>
Type Parameters:
T - The type of message.
Record Components:
messageClass - The class of the message.
byteSizeFunction - A function that computes the byte size of the message.
reader - A reader that can read messages from an input stream.
writer - A writer that write messages from an input stream.

public record MessageTypeSerializer<T extends Message>(Class<T extends Message> messageClass, Function<T extends Message,Integer> byteSizeFunction, MessageReader<T extends Message> reader, MessageWriter<T extends Message> writer) extends Record
Record containing the components needed to read and write a given message.

Also contains methods for automatically generating message type implementations for standard record-based messages.

  • Field Details

  • Constructor Details

    • MessageTypeSerializer

      public MessageTypeSerializer(Class<T> messageClass, Function<T,Integer> byteSizeFunction, MessageReader<T> reader, MessageWriter<T> writer)
      Creates an instance of a MessageTypeSerializer record class.
      Parameters:
      messageClass - the value for the messageClass record component
      byteSizeFunction - the value for the byteSizeFunction record component
      reader - the value for the reader record component
      writer - the value for the writer record component
  • Method Details

    • get

      public static <T extends Message> MessageTypeSerializer<T> get(Class<T> messageClass)
      Gets the MessageTypeSerializer instance for a given message class, and generates a new implementation if none exists yet.
      Type Parameters:
      T - The type of the message.
      Parameters:
      messageClass - The class of the message to get a type for.
      Returns:
      The message type.
    • generateForRecord

      public static <T extends Message> MessageTypeSerializer<T> generateForRecord(Class<T> messageTypeClass)
      Generates a message type instance for a given class, using reflection to introspect the fields of the message.

      Note that this only works for record-based messages.

      Type Parameters:
      T - The type of the message.
      Parameters:
      messageTypeClass - The class of the message type.
      Returns:
      A message type instance.
    • generateByteSizeFunction

      private static <T extends Message> Function<T,Integer> generateByteSizeFunction(RecordComponent[] components)
      Generates a function implementation that counts the byte size of a message based on the message's record component types.
      Type Parameters:
      T - The message type.
      Parameters:
      components - The list of components that make up the message.
      Returns:
      A function that computes the byte size of a message of the given type.
    • generateReader

      private static <T extends Message> MessageReader<T> generateReader(Constructor<T> constructor)
      Generates a message reader for the given message constructor method. It will try to read objects from the input stream according to the parameters of the canonical constructor of a message record.
      Type Parameters:
      T - The message type.
      Parameters:
      constructor - The canonical constructor of the message record.
      Returns:
      A message reader for the given type.
    • generateWriter

      private static <T extends Message> MessageWriter<T> generateWriter(RecordComponent[] components)
      Generates a message writer for the given message record components.
      Type Parameters:
      T - The type of message.
      Parameters:
      components - The record components to write.
      Returns:
      The message writer for the given type.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • 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. All components in this record class are compared with Objects::equals(Object,Object).
      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.
    • messageClass

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

      public Function<T,Integer> byteSizeFunction()
      Returns the value of the byteSizeFunction record component.
      Returns:
      the value of the byteSizeFunction record component
    • reader

      public MessageReader<T> reader()
      Returns the value of the reader record component.
      Returns:
      the value of the reader record component
    • writer

      public MessageWriter<T> writer()
      Returns the value of the writer record component.
      Returns:
      the value of the writer record component