Class AbstractSegment

java.lang.Object
ca.uhn.hl7v2.model.AbstractStructure
ca.uhn.hl7v2.model.AbstractSegment
All Implemented Interfaces:
Segment, Structure, Visitable, Serializable
Direct Known Subclasses:
GenericSegment

public abstract class AbstractSegment extends AbstractStructure implements Segment

Provides common functionality needed by implementers of the Segment interface.

Implementing classes should define all the fields for the segment they represent in their constructor. The add() method is useful for this purpose.

For example the constructor for an MSA segment might contain the following code:
this.add(new ID(), true, 2, null);
this.add(new ST(), true, 20, null);
...

Author:
Bryan Tripp (bryan_tripp@sourceforge.net)
See Also:
  • Constructor Details

    • AbstractSegment

      public AbstractSegment(Group parent, ModelClassFactory factory)
      Calls the abstract init() method to create the fields in this segment.
      Parameters:
      parent - parent group
      factory - all implementors need a model class factory to find datatype classes, so we include it as an arg here to emphasize that fact ... AbstractSegment doesn't actually use it though
  • Method Details

    • accept

      public boolean accept(MessageVisitor visitor, Location location) throws HL7Exception
      Iterates over the contained fields and calls the visitor for each of them.
      Specified by:
      accept in interface Visitable
      Parameters:
      visitor - MessageVisitor instance to be called back.
      location - location of the group
      Returns:
      true if visiting shall continue, false if not
      Throws:
      HL7Exception
    • provideLocation

      public Location provideLocation(Location location, int index, int repetition)
      Description copied from interface: Visitable
      Reusing the location of this object's parent, this method returns its own location within the message.
      Specified by:
      provideLocation in interface Visitable
      Parameters:
      location - the parents location
      index - index of this object
      repetition - repetition of this object
      Returns:
      this objects location
    • getField

      public Type[] getField(int number) throws HL7Exception
      Returns an array of Field objects at the specified location in the segment. In the case of non-repeating fields the array will be of length one. Fields are numbered from 1.
      Specified by:
      getField in interface Segment
      Parameters:
      number - field number (starting at 1)
      Returns:
      the array of Fields at the specified field number
      Throws:
      HL7Exception - if field index is out of range.
    • isEmpty

      public boolean isEmpty() throws HL7Exception
      Description copied from interface: Visitable
      Returns true if this Visitable has actual content
      Specified by:
      isEmpty in interface Visitable
      Returns:
      true if Visitable is empty
      Throws:
      HL7Exception - if an error occurred while determining emptiness
      See Also:
    • getTypedField

      protected <T extends Type> T[] getTypedField(int number, T[] array)
      Returns an array of a specific type class
    • getReps

      protected int getReps(int number)
    • getField

      public Type getField(int number, int rep) throws HL7Exception
      Returns a specific repetition of field at the specified index. If there exist fewer repetitions than are required, the number of repetitions can be increased by specifying the lowest repetition that does not yet exist. For example if there are two repetitions but three are needed, the third can be created and accessed using the following code:
      Type t = getField(x, 3);
      Specified by:
      getField in interface Segment
      Parameters:
      number - the field number (starting at 1)
      rep - the repetition number (starting at 0)
      Returns:
      field at the specified field number and repetition
      Throws:
      HL7Exception - if field index is out of range, if the specified repetition is greater than the maximum allowed, or if the specified repetition is more than 1 greater than the existing # of repetitions.
    • getTypedField

      protected <T extends Type> T getTypedField(int number, int rep)
      Returns a specific repetition of field with concrete type at the specified index
    • createNewTypeWithoutReflection

      protected Type createNewTypeWithoutReflection(int field)

      Attempts to create an instance of a field type without using reflection.

      Note that the default implementation just returns null, and it is not neccesary to override this method to provide any particular behaviour. When a new field instance is needed within a segment, this method is tried first, and if it returns null, reflection is used instead. Implementations of this method is auto-generated by the source generator module.

      Parameters:
      field - Field number - Note that this is zero indexed!
      Returns:
      Returns a newly instantiated type, or null if not possible
    • isRequired

      public boolean isRequired(int number) throws HL7Exception
      Returns true if the given field is required in this segment - fields are numbered from 1.
      Specified by:
      isRequired in interface Segment
      Parameters:
      number - field number starting at 1
      Returns:
      true if the field is required
      Throws:
      HL7Exception - if field index is out of range.
    • getLength

      public int getLength(int number) throws HL7Exception
      Returns the maximum length of the field at the given index, in characters - fields are numbered from 1.
      Specified by:
      getLength in interface Segment
      Parameters:
      number - field number starting at 1
      Returns:
      maximum length of the field
      Throws:
      HL7Exception - if field index is out of range.
    • getMaxCardinality

      public int getMaxCardinality(int number) throws HL7Exception
      Returns the number of repetitions of this field that are allowed.
      Specified by:
      getMaxCardinality in interface Segment
      Parameters:
      number - field number starting at 1
      Returns:
      maximum number of repetitions of this field
      Throws:
      HL7Exception - if field index is out of range.
    • add

      protected void add(Class<? extends Type> c, boolean required, int maxReps, int length, Object[] constructorArgs) throws HL7Exception
      Throws:
      HL7Exception
    • add

      protected void add(Class<? extends Type> c, boolean required, int maxReps, int length, Object[] constructorArgs, String name) throws HL7Exception
      Adds a field to the segment. The field is initially empty (zero repetitions). The field number is sequential depending on previous add() calls. Implementing classes should use the add() method in their constructor in order to define fields in their segment.
      Parameters:
      c - the class of the datatype for the field - this should inherit from Type
      required - whether a value for the field is required in order for the segment to be valid
      maxReps - The maximum number of repetitions for the field. Note that 0 implies that there is no limit, and 1 implies that the field may not repeat.
      length - the maximum length of each repetition of the field (in characters)
      constructorArgs - This parameter provides an array of objects that will be used as constructor arguments if new instances of this class are created (use null for zero-arg constructor). To determine the appropriate value for this parameter, consult the javadoc for the specific datatype class passed to the first argument of this method, and provide an array which satisfies the requirements of its constructor. For example, most datatypes take a single Message argument in their constructor. In that case, the appropriate value for this argument is as follows: new Object[]{ getMessage() }
      name - A textual description of the name of the field
      Throws:
      HL7Exception
    • main

      public static void main(String[] args)
    • numFields

      public int numFields()
      Returns the number of fields defined by this segment (repeating fields are not counted multiple times).
      Specified by:
      numFields in interface Segment
      Returns:
      number of fields
    • getName

      public String getName()
      Returns the class name (excluding package).
      Specified by:
      getName in interface Structure
      Returns:
      name of this structure
      See Also:
    • getNames

      public String[] getNames()
      Returns the names of the fields in this segment.
      Specified by:
      getNames in interface Segment
      Returns:
      array of names
    • parse

      public void parse(String string) throws HL7Exception
      Parses the string into this segment using the parser returned by Message.getParser()

      Note that this method will not currently work to parse an MSH segment if the encoding characters are not already set. This limitation should be resolved in a future version

      Specified by:
      parse in interface Segment
      Parameters:
      string - encoded segment
      Throws:
      HL7Exception - if errors occurred during parsing
    • encode

      public String encode() throws HL7Exception
      Encodes this message using the parser returned by Message.getParser()
      Specified by:
      encode in interface Segment
      Returns:
      the string-encoded segment
      Throws:
      HL7Exception - if errors occurred during encoding
    • removeRepetition

      public Type removeRepetition(int fieldNum, int index) throws HL7Exception
      Removes a repetition of a given field by name. For example, if a PID segment contains 10 repetitions a "Patient Identifier List" field and "Patient Identifier List" is supplied with an index of 2, then this call would remove the 3rd repetition.
      Returns:
      The removed structure
      Throws:
      HL7Exception - if the named Structure is not part of this Group.
    • insertRepetition

      public Type insertRepetition(int fieldNum, int index) throws HL7Exception
      Inserts a repetition of a given Field into repetitions of that field by name.
      Returns:
      The newly created and inserted field
      Throws:
      HL7Exception - if the named Structure is not part of this Group.
    • clear

      public void clear()
      Clears all data from this segment