Class Message<T extends WritableIdentifier,C extends Message<T,C>>
- Type Parameters:
T
- Target identifier typeC
- Message type
- All Implemented Interfaces:
Serializable
,Immutable
Request
and Response
.
Messages have a target and a sequence number. Sequence numbers are expected to be assigned monotonically on a per-target basis, hence two targets can observe the same sequence number.
This class includes explicit versioning for forward- and backward- compatibility of serialization format. This is achieved by using the serialization proxy pattern. Subclasses are in complete control of what proxy is used to serialize a particular object on the wire. This class can serve as an explicit version marker, hence no further action is necessary in the deserialization path.
For the serialization path an explicit call from the user is required to select the appropriate serialization
version. This is done via toVersion(ABIVersion)
method, which should return a copy of this object with
the requested ABI version recorded and should return the appropriate serialization proxy.
This workflow allows least disturbance across ABI versions, as all messages not affected by a ABI version bump will remain working with the same serialization format for the new ABI version.
Note that this class specifies the Immutable
contract, which means that all subclasses must follow this API
contract.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static interface
Message.SerialForm<T extends WritableIdentifier,
C extends Message<T, C>> Externalizable proxy for use withMessage
subclasses. -
Method Summary
Modifier and TypeMethodDescriptionprotected @NonNull MoreObjects.ToStringHelper
addToStringAttributes
(@NonNull MoreObjects.ToStringHelper toStringHelper) Add attributes to the output oftoString()
.protected abstract @NonNull C
cloneAsVersion
(@NonNull ABIVersion targetVersion) Create a copy of this message which will serialize to a stream corresponding to the specified method.protected abstract @NonNull Message.SerialForm<T,
C> externalizableProxy
(@NonNull ABIVersion reqVersion) Instantiate a serialization proxy for this object for the target ABI version.final long
Get the logical sequence number.final @NonNull T
Get the target identifier for this message.final @NonNull ABIVersion
final String
toString()
final @NonNull C
toVersion
(@NonNull ABIVersion toVersion) Return a message which will end up being serialized in the specifiedABIVersion
.protected final Object
-
Method Details
-
getTarget
Get the target identifier for this message.- Returns:
- Target identifier
-
getSequence
public final long getSequence()Get the logical sequence number.- Returns:
- logical sequence number
-
getVersion
-
toVersion
Return a message which will end up being serialized in the specifiedABIVersion
.- Parameters:
toVersion
- RequestABIVersion
- Returns:
- A new message which will use ABIVersion as its serialization.
-
cloneAsVersion
Create a copy of this message which will serialize to a stream corresponding to the specified method. This method should be implemented by the concrete final message class and should invoke the equivalent ofMessage(Message, ABIVersion)
.- Parameters:
targetVersion
- target ABI version- Returns:
- A message with the specified serialization stream
- Throws:
IllegalArgumentException
- if this message does not support the target ABI
-
toString
-
addToStringAttributes
protected @NonNull MoreObjects.ToStringHelper addToStringAttributes(@NonNull MoreObjects.ToStringHelper toStringHelper) Add attributes to the output oftoString()
. Subclasses wanting to contribute additional information should override this method. Any null attributes will be omitted from the output.- Parameters:
toStringHelper
- aMoreObjects.ToStringHelper
instance- Returns:
- The
MoreObjects.ToStringHelper
passed in as argument - Throws:
NullPointerException
- if toStringHelper is null
-
externalizableProxy
protected abstract @NonNull Message.SerialForm<T,C> externalizableProxy(@NonNull ABIVersion reqVersion) Instantiate a serialization proxy for this object for the target ABI version. Implementations should return different objects for incompatibleABIVersion
s. This method should never fail, as any compatibility checks should have been done bycloneAsVersion(ABIVersion)
.- Parameters:
reqVersion
- Requested ABI version- Returns:
- Proxy for this object
-
writeReplace
-