Class WfMessageSegment

java.lang.Object
org.whiteflagprotocol.java.core.WfMessageSegment

public class WfMessageSegment extends Object
Whiteflag message segment class

This is a class representing a segment of a Whiteflag message, i.e. the message header or the message body. A message segment contains a number of WfMessageFields, depending on the segment and the type of the message. The fields in the segment must be ordered, without missing or overlapping bytes.

Since:
1.0
Whiteflag standard x-reference:
4.2 Message Header, 4.3 Message Body
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new Whiteflag message segment from an array of message fields, without copying the fields' values
    Constructs a new Whiteflag message segment from another message segment, also copying the fields' values
  • Method Summary

    Modifier and Type
    Method
    Description
    protected final WfMessageSegment
    Appends additional fields to this message segment if constructing complex message bodies
    final int
    Returns the bit length of this segment, excluding the last variable length field if not set
    final int
    bitLength(int fieldIndex)
    Returns the bit length up to and including the specified field, excluding the last variable length field if not set
    final int
    bitLength(String fieldName)
    Returns the bit length of this segment, excluding the last variable length field if not set
    final int
    Returns the byte length of this segment, excluding the last variable length field if not set
    int
    byteLength(int fieldIndex)
    Returns the byte length of this segment up to and including the specified field, excluding the last variable length field if not set
    final int
    byteLength(String fieldName)
    Returns the byte length of this segment, excluding the last variable length field if not set
    protected final void
    decode(WfBinaryBuffer buffer, int startBit, int fieldIndex)
    Decodes this message segment from the provided encoded message
    protected final void
    deserialize(String messageStr, int fieldIndex)
    Deserializes this message segment from the provided serialized message
    protected final WfBinaryBuffer
    Encodes this message segment
    final String
    get(int fieldIndex)
    Gets the value of the field specified by index
    final String
    get(String fieldname)
    Gets the value of the field specified by name
    protected final WfMessageField[]
    Gets all fields from this message segment
    protected final WfMessageField
    getField(int fieldIndex)
    Gets the field specified by index
    protected final WfMessageField
    getField(String fieldName)
    Gets the field specified by name
    protected final int
    getFieldIndex(String fieldName)
    Gets the index of the field specified by name
    Returns the field names of the message segment
    final int
    Gets the number of fields in this message segment
    final boolean
    Checks if all fields of this message segment contain valid data
    final boolean
    isValid(String fieldname)
    Checks if the specified field contains valid data
    final boolean
    isValid(String fieldname, String data)
    Checks if the provided data is valid for the specified field
    protected final String
    Serializes this message segment
    final Boolean
    set(int fieldIndex, String data)
    Sets the value of the field specified by its index in the message segment
    final Boolean
    set(String fieldname, String data)
    Sets the value of the specified field in the message segment
    final Boolean
    setAll(String[] data)
    Sets all field values of this segment with values from an array
    final Boolean
    setAll(String[] data, int startIndex)
    Sets all field values of this segment with values from an array
    final Boolean
    Sets all field values of this segment from a fieldname-to-value mapping
    Gets a fieldname-to-value mapping of this message segment
    final String
    Returns the message segment as a concatinated string of field values

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • WfMessageSegment

      public WfMessageSegment(WfMessageField[] fields)
      Constructs a new Whiteflag message segment from an array of message fields, without copying the fields' values
      Parameters:
      fields - an array of message fields
    • WfMessageSegment

      public WfMessageSegment(WfMessageSegment segment)
      Constructs a new Whiteflag message segment from another message segment, also copying the fields' values
      Parameters:
      segment - the message segment to create the new segment from
  • Method Details

    • toString

      public final String toString()
      Returns the message segment as a concatinated string of field values
      Overrides:
      toString in class Object
      Returns:
      serialized message segment
    • isValid

      public final boolean isValid()
      Checks if all fields of this message segment contain valid data
      Returns:
      TRUE if message segment contains valid data, else FALSE
    • isValid

      public final boolean isValid(String fieldname)
      Checks if the specified field contains valid data
      Parameters:
      fieldname - the name of the field
      Returns:
      TRUE if the field contains valid data, else FALSE
    • isValid

      public final boolean isValid(String fieldname, String data)
      Checks if the provided data is valid for the specified field
      Parameters:
      fieldname - the name of the field
      data - the value to be checked
      Returns:
      TRUE if the field contains valid data, else FALSE
    • bitLength

      public final int bitLength()
      Returns the bit length of this segment, excluding the last variable length field if not set
      Returns:
      the bit length of this segment
    • bitLength

      public final int bitLength(String fieldName)
      Returns the bit length of this segment, excluding the last variable length field if not set
      Parameters:
      fieldName - the name of the field up to which the segment length is calculated; negative index counts back from last field
      Returns:
      the bit length of this segment, or 0 if the field does not exist
    • bitLength

      public final int bitLength(int fieldIndex)
      Returns the bit length up to and including the specified field, excluding the last variable length field if not set
      Parameters:
      fieldIndex - the index of the field up to which the segment length is calculated; negative index counts back from last field
      Returns:
      the bit length of this segment up to and including the specified field, or 0 if the field does not exist
    • byteLength

      public final int byteLength()
      Returns the byte length of this segment, excluding the last variable length field if not set
      Returns:
      the byte length of this segment
    • byteLength

      public final int byteLength(String fieldName)
      Returns the byte length of this segment, excluding the last variable length field if not set
      Parameters:
      fieldName - the name of the field up to which the segment length is calculated; negative index counts back from last field
      Returns:
      the byte length of this segment, or 0 if the field does not exist
    • byteLength

      public int byteLength(int fieldIndex)
      Returns the byte length of this segment up to and including the specified field, excluding the last variable length field if not set
      Parameters:
      fieldIndex - the index of the field up to which the segment length is calculated; negative index counts back from last field
      Returns:
      the byte length of this segment, , or 0 if the field does not exist
    • getNoFields

      public final int getNoFields()
      Gets the number of fields in this message segment
      Returns:
      the number of message segment fields
    • getFieldNames

      public Set<String> getFieldNames()
      Returns the field names of the message segment
      Returns:
      a string set with all field names
    • get

      public final String get(String fieldname)
      Gets the value of the field specified by name
      Parameters:
      fieldname - the name of the requested field
      Returns:
      the field value, or NULL if field does not exist
    • get

      public final String get(int fieldIndex)
      Gets the value of the field specified by index
      Parameters:
      fieldIndex - the index of the requested field; negative index counts back from last field
      Returns:
      the field value, or NULL if field does not exist
    • toMap

      public final Map<String,String> toMap()
      Gets a fieldname-to-value mapping of this message segment
      Returns:
      a fieldname-to-value mapping
    • set

      public final Boolean set(String fieldname, String data)
      Sets the value of the specified field in the message segment
      Parameters:
      fieldname - the name of the field
      data - data to be set as the field value
      Returns:
      TRUE if field value is set, FALSE if field does not exits, isalready set, or data is invalid
    • set

      public final Boolean set(int fieldIndex, String data)
      Sets the value of the field specified by its index in the message segment
      Parameters:
      fieldIndex - the index of the requested field; negative index counts back from last field
      data - data to be set as the field value
      Returns:
      TRUE if the data was valid and the field value is set, else FALSE
    • setAll

      public final Boolean setAll(Map<String,String> map)
      Sets all field values of this segment from a fieldname-to-value mapping
      Parameters:
      map - a fieldname-to-value mapping
      Returns:
      TRUE if all field values in this segment were correctly set, else FALSE
    • setAll

      public final Boolean setAll(String[] data) throws WfCoreException
      Sets all field values of this segment with values from an array
      Parameters:
      data - array with the data to be set as the field values
      Returns:
      TRUE if the data was valid and all field values are set
      Throws:
      WfCoreException - if the provided data is invalid
      Since:
      1.1
    • setAll

      public final Boolean setAll(String[] data, int startIndex) throws WfCoreException
      Sets all field values of this segment with values from an array
      Parameters:
      data - array with the data to be set as the field values
      startIndex - starting position in the array
      Returns:
      TRUE if the data was valid and all field values are set
      Throws:
      WfCoreException - if the provided data is invalid
      Since:
      1.1
    • serialize

      protected final String serialize() throws WfCoreException
      Serializes this message segment
      Returns:
      the serialized message segment
      Throws:
      WfCoreException - if the segment cannot be serialized
    • deserialize

      protected final void deserialize(String messageStr, int fieldIndex) throws WfCoreException
      Deserializes this message segment from the provided serialized message
      Parameters:
      messageStr - the serialized message
      fieldIndex - the index of the next field to be deserialized
      Throws:
      WfCoreException - if the message cannot be deserialized
      Since:
      1.1
    • encode

      protected final WfBinaryBuffer encode() throws WfCoreException
      Encodes this message segment
      Returns:
      a binary buffer with the binary encoded message segment
      Throws:
      WfCoreException - if the message cannot be encoded
      Since:
      1.1
    • decode

      protected final void decode(WfBinaryBuffer buffer, int startBit, int fieldIndex) throws WfCoreException
      Decodes this message segment from the provided encoded message
      Parameters:
      buffer - the binary buffer with the binary encoded message
      startBit - the bit position where this segment starts in the encoded message
      fieldIndex - the index of the next field to be decoded
      Throws:
      WfCoreException - if the message cannot be decoded
      Since:
      1.1
    • append

      protected final WfMessageSegment append(WfMessageSegment segment)
      Appends additional fields to this message segment if constructing complex message bodies
      Parameters:
      segment - the segment to be added to the message segment
      Returns:
      this message segment
    • getField

      protected final WfMessageField getField(String fieldName)
      Gets the field specified by name
      Parameters:
      fieldName - the name of the requested field
      Returns:
      the requested message field, or NULL if it does not exist
    • getFieldIndex

      protected final int getFieldIndex(String fieldName)
      Gets the index of the field specified by name
      Parameters:
      fieldName - the name of the requested field
      Returns:
      the index of the requested field, or the number of fields if the specified field does not exist
    • getField

      protected final WfMessageField getField(int fieldIndex)
      Gets the field specified by index
      Parameters:
      fieldIndex - the index of the requested field; negative index counts back from last field
      Returns:
      the requested message field, or NULL if it does not exist
    • getAllFields

      protected final WfMessageField[] getAllFields()
      Gets all fields from this message segment
      Returns:
      an array with all message fields