- All Implemented Interfaces:
Serializable
Message
is composed of the following required and optional fields:
- A required message
Type
: used by the communication protocol. - A required status code: indicates successful or error completion.
- An optional set of labels: used by the communication protocol.
- An optional text value: used to send a unicode string such as XML content.
- An optional bytes value: used to send an array of bytes such as a PDF file.
- An optional object value: used to send a
Serializable
object. - An optional set of properties: used to send a set of name-value pairs.
Message
is not synchronized. If multiple
threads access a Message
instance concurrently, and at least one of
the threads modifies the Message
, it should be synchronized
externally. This is typically accomplished by synchronizing on some object
that naturally encapsulates the Message
.- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMessage()
Creates a newMessage
instance.Message
(byte[] bytes) Creates a newMessage
with supplied bytes.Message
(Message.Type type) Creates a newMessage
with supplied type.Message
(Serializable object) Creates a newMessage
with supplied object.Creates a newMessage
with supplied text. -
Method Summary
Modifier and TypeMethodDescriptionfinal void
clear()
Resets all the fields of this Message to their default value.final void
Removes all defined labels from this Message.final void
Removes all defined properties from this Message.final void
fromBuffer
(ByteBuffer buffer) Decodes all the fields of this Message from suppliedByteBuffer
.final BigDecimal
getBigDecimal
(String name) Returns the big decimal value of the property indexed byname
.final BigInteger
getBigInteger
(String name) Returns the big integer value of the property indexed byname
.final Boolean
getBoolean
(String name) Returns the boolean value of the property indexed byname
.final Byte
Returns the byte value of the property indexed byname
.final byte[]
getBytes()
Returns an array of bytes containing the data of this Message.final byte[]
Returns the bytes value of the property indexed byname
.final Date
Returns the date value of the property indexed byname
.final Double
Returns the double value of the property indexed byname
.final Float
Returns the float value of the property indexed byname
.final Integer
getInteger
(String name) Returns the integer value of the property indexed byname
.final String
getLabel
(Message.Label key) Returns the value of the label indexed bykey
.final Set<Message.Label>
Returns an unmodifiable set with all defined label keys.final Long
Returns the long value of the property indexed byname
.final <T extends Serializable>
TReturns the object containing the data of this Message.final <T extends Serializable>
TReturns the object value of the property indexed byname
.Returns an unmodifiable set with all defined property names and values.Returns an unmodifiable set with all defined property names.final PropertyType
getPropertyType
(String name) Returns the type of the value of the property indexed byname
.final Short
Returns the short value of the property indexed byname
.final int
Returns the status of this Message.final String
getText()
Returns a string containing the data of this Message.final String
Returns the string value the property indexed byname
.final Message.Type
getType()
Returns the type of this Message.final boolean
hasLabel
(Message.Label key) Returnstrue
if the label indexed bykey
is defined.final boolean
hasProperty
(String name) Returnstrue
if the property indexed byname
is defined.final void
putBigDecimal
(String name, BigDecimal value) Changes the big decimal value of the property indexed byname
.final void
putBigInteger
(String name, BigInteger value) Changes the big integer value of the property indexed byname
.final void
putBoolean
(String name, Boolean value) Changes the boolean value of the property indexed byname
.final void
Changes the byte value of the property indexed byname
.final void
Changes the bytes value of the property indexed byname
.final void
Changes the date value of the property indexed byname
.final void
Changes the double value of the property indexed byname
.final void
Changes the float value of the property indexed byname
.final void
putInteger
(String name, Integer value) Changes the integer value of the property indexed byname
.final void
putLabel
(Message.Label key, String value) Changes the value of the label indexed bykey
.final void
Changes the long value of the property indexed byname
.final void
putObject
(String name, Serializable value) Changes the object value of the property indexed byname
.final void
putProperties
(Message message) Copies all the properties of a message to this Message.final void
Changes the short value of the property indexed byname
.final void
Changes the string value of the property indexed byname
.final void
removeLabel
(Message.Label key) Removes the label indexed by thekey
.final void
removeProperty
(String name) Removes the property indexed byname
.final void
setBytes
(byte[] bytes) Changes the array of bytes containing the data of this Message.final void
setObject
(Serializable object) Changes the object containing the data of this Message.final void
setStatus
(int status) Changes the status of this Message.final void
Changes the string containing the data of this Message.final void
setType
(Message.Type type) Changes the type of this Message.final ByteBuffer
toBuffer
(int compressionThreshold) Encodes all the fields of this Message into aByteBuffer
.toString()
Returns a string representation of this Message.static Message
valueOf
(Serializable data) Returns aMessage
built using the supplied objectdata
.
-
Field Details
-
SUCCESS
public static final int SUCCESSCompletion status indicating success.- See Also:
-
ERROR
public static final int ERRORCompletion status indicating error.- See Also:
-
-
Constructor Details
-
Message
public Message()Creates a newMessage
instance. -
Message
Creates a newMessage
with supplied type.- Parameters:
type
- the initial message type (can be null).
-
Message
Creates a newMessage
with supplied text.- Parameters:
text
- the initial text data (can be null).
-
Message
public Message(byte[] bytes) Creates a newMessage
with supplied bytes.- Parameters:
bytes
- the initial bytes data (can be null).
-
Message
Creates a newMessage
with supplied object.- Parameters:
object
- the initial object data (can be null).
-
-
Method Details
-
clear
public final void clear()Resets all the fields of this Message to their default value. -
getType
Returns the type of this Message.- Returns:
- the type of this Message.
-
setType
Changes the type of this Message.- Parameters:
type
- the new type of this Message.- Throws:
NullPointerException
- iftype
isnull
.
-
getStatus
public final int getStatus()Returns the status of this Message.- Returns:
- the status of the Message.
-
setStatus
public final void setStatus(int status) Changes the status of this Message.- Parameters:
status
- the new status of the Message.
-
getLabelKeys
Returns an unmodifiable set with all defined label keys.- Returns:
- an unmodifiable set with all defined label keys.
-
hasLabel
Returnstrue
if the label indexed bykey
is defined.- Parameters:
key
- the index of the label.- Returns:
true
if the label indexed bykey
is defined.- Throws:
NullPointerException
- ifkey
isnull
.
-
getLabel
Returns the value of the label indexed bykey
.- Parameters:
key
- the index of the label.- Returns:
- a string or
null
if the label is not defined. - Throws:
NullPointerException
- ifkey
isnull
.
-
putLabel
Changes the value of the label indexed bykey
. Ifvalue
isnull
the label is removed.- Parameters:
key
- the index of the label.value
- the new value of the label (can be null).- Throws:
NullPointerException
- ifkey
isnull
.
-
removeLabel
Removes the label indexed by thekey
.- Parameters:
key
- the index of the label.- Throws:
NullPointerException
- ifkey
isnull
.
-
clearLabels
public final void clearLabels()Removes all defined labels from this Message. -
getPropertyNames
Returns an unmodifiable set with all defined property names.- Returns:
- an unmodifiable set with all defined property names.
-
getPropertyEntries
Returns an unmodifiable set with all defined property names and values.- Returns:
- an unmodifiable set with all defined property names and values.
-
getPropertyType
Returns the type of the value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the type of the value of the property indexed by
name
. - Throws:
NullPointerException
- ifname
isnull
.IllegalArgumentException
- if the property value is invalid.
-
hasProperty
Returnstrue
if the property indexed byname
is defined.- Parameters:
name
- the name of the property.- Returns:
true
if the property indexed by name is defined.- Throws:
NullPointerException
- ifname
isnull
.
-
putProperties
Copies all the properties of a message to this Message.- Parameters:
message
- the message whose properties will be copied.- Throws:
NullPointerException
- ifmessage
isnull
.
-
removeProperty
Removes the property indexed byname
.- Parameters:
name
- the name of the property.- Throws:
NullPointerException
- ifname
isnull
.
-
clearProperties
public final void clearProperties()Removes all defined properties from this Message. -
getBigDecimal
Returns the big decimal value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the big decimal value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.NumberFormatException
- if string value of property is invalid.ClassCastException
- if the value is not aBigDecimal
.
-
putBigDecimal
Changes the big decimal value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new big decimal value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getBigInteger
Returns the big integer value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the big integer value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.NumberFormatException
- if string value of property is invalid.ClassCastException
- if the value is not aBigInteger
.
-
putBigInteger
Changes the big integer value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new big integer value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getBoolean
Returns the boolean value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the boolean value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.ClassCastException
- if the value is not aBoolean
.
-
putBoolean
Changes the boolean value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new boolean value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getByte
Returns the byte value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the byte value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.NumberFormatException
- if string value of property is invalid.ClassCastException
- if the value is not aByte
.
-
putByte
Changes the byte value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new byte value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getBytes
public final byte[] getBytes()Returns an array of bytes containing the data of this Message.Warning: to reduce memory consumption, this method returns a reference to (not a copy of) the mutable object stored in this Message. If the returned object is later modified, the Message object will be modified.
- Returns:
- the byte array data or
null
if not defined.
-
setBytes
public final void setBytes(byte[] bytes) Changes the array of bytes containing the data of this Message.Warning: to reduce memory consumption, this Message stores a reference to (not a copy of) the supplied mutable object. If the supplied object is later modified, the Message object will be modified.
- Parameters:
bytes
- the new bytes data of the Message (can be null).
-
getBytes
Returns the bytes value of the property indexed byname
.Warning: to reduce memory consumption, this method returns a reference to (not a copy of) the mutable object stored in this Message. If the returned object is later modified, the Message object will be modified.
- Parameters:
name
- the name of the property.- Returns:
- the bytes value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.ClassCastException
- if the value is not abyte[]
.
-
putBytes
Changes the bytes value of the property indexed byname
. Ifvalue
isnull
the property is removed.Warning: to reduce memory consumption, this Message stores a reference to (not a copy of) the supplied mutable object. If the supplied object is later modified, the Message object will be modified.
- Parameters:
name
- the name of the property.value
- the new bytes value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getDate
Returns the date value of the property indexed byname
.Warning: to reduce memory consumption, this method returns a reference to (not a copy of) the mutable object stored in this Message. If the returned object is later modified, the Message object will be modified.
- Parameters:
name
- the name of the property.- Returns:
- the date value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.ClassCastException
- if the value is not aDate
.
-
putDate
Changes the date value of the property indexed byname
. Ifvalue
isnull
the property is removed.Warning: to reduce memory consumption, this Message stores a reference to (not a copy of) the supplied mutable object. If the supplied object is later modified, the Message object will be modified.
- Parameters:
name
- the name of the property.value
- the new date value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getDouble
Returns the double value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the double value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.NumberFormatException
- if string value of property is invalid.ClassCastException
- if the value is not aDouble
.
-
putDouble
Changes the double value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new double value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getFloat
Returns the float value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the float value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.NumberFormatException
- if string value of property is invalid.ClassCastException
- if the value is not aFloat
.
-
putFloat
Changes the float value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new float value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getInteger
Returns the integer value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the integer value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.NumberFormatException
- if string value of property is invalid.ClassCastException
- if the value is not anInteger
.
-
putInteger
Changes the integer value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new integer value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getLong
Returns the long value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the long value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.NumberFormatException
- if string value of property is invalid.ClassCastException
- if the value is not anLong
.
-
putLong
Changes the long value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new long value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getObject
Returns the object containing the data of this Message.Warning: to reduce memory consumption, this method returns a reference to (not a copy of) the mutable object stored in this Message. If the returned object is later modified, the Message object will be modified.
- Type Parameters:
T
- the type of the returned object.- Parameters:
type
- the class type of the required object.- Returns:
- the required object or
null
if not defined. - Throws:
NullPointerException
- iftype
isnull
.ClassCastException
- if object is not an instance oftype
.
-
setObject
Changes the object containing the data of this Message.Warning: to reduce memory consumption, this Message stores a reference to (not a copy of) the supplied mutable object. If the supplied object is later modified, the Message object will be modified.
- Parameters:
object
- the new object data of the Message (can be null).
-
getObject
Returns the object value of the property indexed byname
.Warning: to reduce memory consumption, this method returns a reference to (not a copy of) the mutable object stored in this Message. If the returned object is later modified, the Message object will be modified.
- Type Parameters:
T
- the type of the returned object.- Parameters:
name
- the name of the property.type
- the class of the returned object.- Returns:
- the typed object value or
null
if not defined. - Throws:
NullPointerException
- if an argument isnull
.ClassCastException
- if object is not an instance oftype
.
-
putObject
Changes the object value of the property indexed byname
. Ifvalue
isnull
the property is removed.Warning: to reduce memory consumption, this Message stores a reference to (not a copy of) the supplied mutable object. If the supplied object is later modified, the Message object will be modified.
- Parameters:
name
- the name of the property.value
- the new object value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getShort
Returns the short value of the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the short value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.NumberFormatException
- if string value of property is invalid.ClassCastException
- if the value is not anShort
.
-
putShort
Changes the short value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new short value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
getText
Returns a string containing the data of this Message.- Returns:
- the text data or
null
if not defined.
-
setText
Changes the string containing the data of this Message.- Parameters:
text
- the new text data of the Message (can be null).
-
getText
Returns the string value the property indexed byname
.- Parameters:
name
- the name of the property.- Returns:
- the string value or
null
if not defined. - Throws:
NullPointerException
- ifname
isnull
.ClassCastException
- if the value is not aString
.
-
putText
Changes the string value of the property indexed byname
. Ifvalue
isnull
the property is removed.- Parameters:
name
- the name of the property.value
- the new string value of the property (can be null).- Throws:
NullPointerException
- ifname
isnull
.
-
toString
Returns a string representation of this Message. -
toBuffer
Encodes all the fields of this Message into aByteBuffer
. The encoded message is compressed if its size is greater or equal to the specified compression threshold. No compression occurs if the size of the encoded message is less than 100 bytes.- Parameters:
compressionThreshold
- the minimum size in bytes that a encoded message must have before it is compressed.- Returns:
- a byte buffer with the encoded Message.
- Throws:
Exception
- if an encoding error occurs.
-
fromBuffer
Decodes all the fields of this Message from suppliedByteBuffer
.- Parameters:
buffer
- a byte buffer with an encoded Message.- Throws:
NullPointerException
- ifbuffer
isnull
.BufferUnderflowException
- ifbuffer
is missing bytes.Exception
- if another decoding error occurs.
-
valueOf
Returns aMessage
built using the supplied objectdata
. Ifdata
isnull
, a newMessage
without data is returned. Ifdata
is itself aMessage
, it is returned directly without creating a newMessage
. Otherwisedata
is used to create a newMessage
, in which casedata
must be either aString
, aSerializable
or abyte[]
.- Parameters:
data
- the data used to build aMessage
(can be null).- Returns:
- a
Message
built using the supplied objectdata
. - Throws:
IllegalArgumentException
- if the type ofdata
is invalid.
-