Module concord_core
Package nl.andrewl.concord_core.msg
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 Summary
FieldsModifier and TypeFieldDescriptionThe field for thebyteSizeFunction
record component.private static final Map<Class<?>,
MessageTypeSerializer<?>> The field for themessageClass
record component.private final MessageReader<T>
The field for thereader
record component.private final MessageWriter<T>
The field for thewriter
record component. -
Constructor Summary
ConstructorsConstructorDescriptionMessageTypeSerializer
(Class<T> messageClass, Function<T, Integer> byteSizeFunction, MessageReader<T> reader, MessageWriter<T> writer) Creates an instance of aMessageTypeSerializer
record class. -
Method Summary
Modifier and TypeMethodDescriptionReturns the value of thebyteSizeFunction
record component.final boolean
Indicates whether some other object is "equal to" this one.generateByteSizeFunction
(RecordComponent[] components) Generates a function implementation that counts the byte size of a message based on the message's record component types.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.private static <T extends Message>
MessageReader<T>generateReader
(Constructor<T> constructor) Generates a message reader for the given message constructor method.private static <T extends Message>
MessageWriter<T>generateWriter
(RecordComponent[] components) Generates a message writer for the given message record components.static <T extends Message>
MessageTypeSerializer<T>Gets theMessageTypeSerializer
instance for a given message class, and generates a new implementation if none exists yet.final int
hashCode()
Returns a hash code value for this object.Returns the value of themessageClass
record component.reader()
Returns the value of thereader
record component.final String
toString()
Returns a string representation of this record class.writer()
Returns the value of thewriter
record component.
-
Field Details
-
messageClass
The field for themessageClass
record component. -
byteSizeFunction
The field for thebyteSizeFunction
record component. -
reader
The field for thereader
record component. -
writer
The field for thewriter
record component. -
generatedMessageTypes
-
-
Constructor Details
-
MessageTypeSerializer
public MessageTypeSerializer(Class<T> messageClass, Function<T, Integer> byteSizeFunction, MessageReader<T> reader, MessageWriter<T> writer) Creates an instance of aMessageTypeSerializer
record class.- Parameters:
messageClass
- the value for themessageClass
record componentbyteSizeFunction
- the value for thebyteSizeFunction
record componentreader
- the value for thereader
record componentwriter
- the value for thewriter
record component
-
-
Method Details
-
get
Gets theMessageTypeSerializer
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
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
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
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. -
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. -
equals
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 withObjects::equals(Object,Object)
. -
messageClass
Returns the value of themessageClass
record component.- Returns:
- the value of the
messageClass
record component
-
byteSizeFunction
Returns the value of thebyteSizeFunction
record component.- Returns:
- the value of the
byteSizeFunction
record component
-
reader
Returns the value of thereader
record component.- Returns:
- the value of the
reader
record component
-
writer
Returns the value of thewriter
record component.- Returns:
- the value of the
writer
record component
-