Class Message
- 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
Serializableobject. - 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 newMessageinstance.Message(byte[] bytes) Creates a newMessagewith supplied bytes.Message(Message.Type type) Creates a newMessagewith supplied type.Message(Serializable object) Creates a newMessagewith supplied object.Creates a newMessagewith supplied text. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclear()Resets all the fields of this Message to their default value.final voidRemoves all defined labels from this Message.final voidRemoves all defined properties from this Message.final voidfromBuffer(ByteBuffer buffer) Decodes all the fields of this Message from suppliedByteBuffer.final BigDecimalgetBigDecimal(String name) Returns the big decimal value of the property indexed byname.final BigIntegergetBigInteger(String name) Returns the big integer value of the property indexed byname.final BooleangetBoolean(String name) Returns the boolean value of the property indexed byname.final ByteReturns 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 DateReturns the date value of the property indexed byname.final DoubleReturns the double value of the property indexed byname.final FloatReturns the float value of the property indexed byname.final IntegergetInteger(String name) Returns the integer value of the property indexed byname.final StringgetLabel(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 LongReturns 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 PropertyTypegetPropertyType(String name) Returns the type of the value of the property indexed byname.final ShortReturns the short value of the property indexed byname.final intReturns the status of this Message.final StringgetText()Returns a string containing the data of this Message.final StringReturns the string value the property indexed byname.final Message.TypegetType()Returns the type of this Message.final booleanhasLabel(Message.Label key) Returnstrueif the label indexed bykeyis defined.final booleanhasProperty(String name) Returnstrueif the property indexed bynameis defined.final voidputBigDecimal(String name, BigDecimal value) Changes the big decimal value of the property indexed byname.final voidputBigInteger(String name, BigInteger value) Changes the big integer value of the property indexed byname.final voidputBoolean(String name, Boolean value) Changes the boolean value of the property indexed byname.final voidChanges the byte value of the property indexed byname.final voidChanges the bytes value of the property indexed byname.final voidChanges the date value of the property indexed byname.final voidChanges the double value of the property indexed byname.final voidChanges the float value of the property indexed byname.final voidputInteger(String name, Integer value) Changes the integer value of the property indexed byname.final voidputLabel(Message.Label key, String value) Changes the value of the label indexed bykey.final voidChanges the long value of the property indexed byname.final voidputObject(String name, Serializable value) Changes the object value of the property indexed byname.final voidputProperties(Message message) Copies all the properties of a message to this Message.final voidChanges the short value of the property indexed byname.final voidChanges the string value of the property indexed byname.final voidremoveLabel(Message.Label key) Removes the label indexed by thekey.final voidremoveProperty(String name) Removes the property indexed byname.final voidsetBytes(byte[] bytes) Changes the array of bytes containing the data of this Message.final voidsetObject(Serializable object) Changes the object containing the data of this Message.final voidsetStatus(int status) Changes the status of this Message.final voidChanges the string containing the data of this Message.final voidsetType(Message.Type type) Changes the type of this Message.final ByteBuffertoBuffer(int compressionThreshold) Encodes all the fields of this Message into aByteBuffer.toString()Returns a string representation of this Message.static MessagevalueOf(Serializable data) Returns aMessagebuilt 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 newMessageinstance. -
Message
Creates a newMessagewith supplied type.- Parameters:
type- the initial message type (can be null).
-
Message
Creates a newMessagewith supplied text.- Parameters:
text- the initial text data (can be null).
-
Message
public Message(byte[] bytes) Creates a newMessagewith supplied bytes.- Parameters:
bytes- the initial bytes data (can be null).
-
Message
Creates a newMessagewith 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- iftypeisnull.
-
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
Returnstrueif the label indexed bykeyis defined.- Parameters:
key- the index of the label.- Returns:
trueif the label indexed bykeyis defined.- Throws:
NullPointerException- ifkeyisnull.
-
getLabel
Returns the value of the label indexed bykey.- Parameters:
key- the index of the label.- Returns:
- a string or
nullif the label is not defined. - Throws:
NullPointerException- ifkeyisnull.
-
putLabel
Changes the value of the label indexed bykey. Ifvalueisnullthe label is removed.- Parameters:
key- the index of the label.value- the new value of the label (can be null).- Throws:
NullPointerException- ifkeyisnull.
-
removeLabel
Removes the label indexed by thekey.- Parameters:
key- the index of the label.- Throws:
NullPointerException- ifkeyisnull.
-
clearLabels
public final void clearLabels()Removes all defined labels from this Message. -
getPropertyNames
-
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- ifnameisnull.IllegalArgumentException- if the property value is invalid.
-
hasProperty
Returnstrueif the property indexed bynameis defined.- Parameters:
name- the name of the property.- Returns:
trueif the property indexed by name is defined.- Throws:
NullPointerException- ifnameisnull.
-
putProperties
Copies all the properties of a message to this Message.- Parameters:
message- the message whose properties will be copied.- Throws:
NullPointerException- ifmessageisnull.
-
removeProperty
Removes the property indexed byname.- Parameters:
name- the name of the property.- Throws:
NullPointerException- ifnameisnull.
-
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
nullif not defined. - Throws:
NullPointerException- ifnameisnull.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. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new big decimal value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
getBigInteger
Returns the big integer value of the property indexed byname.- Parameters:
name- the name of the property.- Returns:
- the big integer value or
nullif not defined. - Throws:
NullPointerException- ifnameisnull.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. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new big integer value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
getBoolean
Returns the boolean value of the property indexed byname.- Parameters:
name- the name of the property.- Returns:
- the boolean value or
nullif not defined. - Throws:
NullPointerException- ifnameisnull.ClassCastException- if the value is not aBoolean.
-
putBoolean
Changes the boolean value of the property indexed byname. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new boolean value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
getByte
Returns the byte value of the property indexed byname.- Parameters:
name- the name of the property.- Returns:
- the byte value or
nullif not defined. - Throws:
NullPointerException- ifnameisnull.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. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new byte value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
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
nullif 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
nullif not defined. - Throws:
NullPointerException- ifnameisnull.ClassCastException- if the value is not abyte[].
-
putBytes
Changes the bytes value of the property indexed byname. Ifvalueisnullthe 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- ifnameisnull.
-
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
nullif not defined. - Throws:
NullPointerException- ifnameisnull.ClassCastException- if the value is not aDate.
-
putDate
Changes the date value of the property indexed byname. Ifvalueisnullthe 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- ifnameisnull.
-
getDouble
Returns the double value of the property indexed byname.- Parameters:
name- the name of the property.- Returns:
- the double value or
nullif not defined. - Throws:
NullPointerException- ifnameisnull.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. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new double value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
getFloat
Returns the float value of the property indexed byname.- Parameters:
name- the name of the property.- Returns:
- the float value or
nullif not defined. - Throws:
NullPointerException- ifnameisnull.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. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new float value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
getInteger
Returns the integer value of the property indexed byname.- Parameters:
name- the name of the property.- Returns:
- the integer value or
nullif not defined. - Throws:
NullPointerException- ifnameisnull.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. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new integer value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
getLong
Returns the long value of the property indexed byname.- Parameters:
name- the name of the property.- Returns:
- the long value or
nullif not defined. - Throws:
NullPointerException- ifnameisnull.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. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new long value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
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
nullif not defined. - Throws:
NullPointerException- iftypeisnull.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
nullif 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. Ifvalueisnullthe 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- ifnameisnull.
-
getShort
Returns the short value of the property indexed byname.- Parameters:
name- the name of the property.- Returns:
- the short value or
nullif not defined. - Throws:
NullPointerException- ifnameisnull.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. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new short value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
getText
Returns a string containing the data of this Message.- Returns:
- the text data or
nullif 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
nullif not defined. - Throws:
NullPointerException- ifnameisnull.ClassCastException- if the value is not aString.
-
putText
Changes the string value of the property indexed byname. Ifvalueisnullthe property is removed.- Parameters:
name- the name of the property.value- the new string value of the property (can be null).- Throws:
NullPointerException- ifnameisnull.
-
toString
-
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- ifbufferisnull.BufferUnderflowException- ifbufferis missing bytes.Exception- if another decoding error occurs.
-
valueOf
Returns aMessagebuilt using the supplied objectdata. Ifdataisnull, a newMessagewithout data is returned. Ifdatais itself aMessage, it is returned directly without creating a newMessage. Otherwisedatais used to create a newMessage, in which casedatamust be either aString, aSerializableor abyte[].- Parameters:
data- the data used to build aMessage(can be null).- Returns:
- a
Messagebuilt using the supplied objectdata. - Throws:
IllegalArgumentException- if the type ofdatais invalid.
-