ca.uhn.hl7v2.model
Class AbstractSegment

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

public abstract class AbstractSegment
extends java.lang.Object
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:
Serialized Form

Constructor Summary
AbstractSegment(Group parent, ModelClassFactory factory)
          Calls the abstract init() method to create the fields in this segment.
 
Method Summary
protected  void add(java.lang.Class c, boolean required, int maxReps, int length, java.lang.Object[] constructorArgs)
          Adds a field to the segment.
 Type[] getField(int number)
          Returns an array of Field objects at the specified location in the segment.
 Type getField(int number, int rep)
          Returns a specific repetition of field at the specified index.
 int getLength(int number)
          Returns the maximum length of the field at the given index, in characters - fields are numbered from 1.
 int getMaxCardinality(int number)
          Returns the number of repetitions of this field that are allowed.
 Message getMessage()
          Returns the Message to which this segment belongs.
 java.lang.String getName()
          Returns the class name (excluding package).
 Group getParent()
          Returns the parent group within which this structure exists (may be root message group).
 boolean isRequired(int number)
          Returns true if the given field is required in this segment - fields are numbered from 1.
static void main(java.lang.String[] args)
           
 int numFields()
          Returns the number of fields defined by this segment (repeating fields are not counted multiple times).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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 Detail

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
Throws:
HL7Exception - if field index is out of range.

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
rep - the repetition number (starting at 0)
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.

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
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
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
Throws:
HL7Exception - if field index is out of range.

getMessage

public Message getMessage()
Returns the Message to which this segment belongs.

Specified by:
getMessage in interface Structure

getParent

public Group getParent()
Description copied from interface: Structure
Returns the parent group within which this structure exists (may be root message group).

Specified by:
getParent in interface Structure

add

protected void add(java.lang.Class c,
                   boolean required,
                   int maxReps,
                   int length,
                   java.lang.Object[] constructorArgs)
            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 data for this field - this should inherit from Type
required - whether a value for this field is required in order for the segment to be valid
maxReps - the maximum number of repetitions - 0 implies that there is no limit
length - the maximum length of each repetition of the field (in characters)
constructorArgs - 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)
Throws:
HL7Exception - if the given class does not inherit from Type or if it can not be instantiated.

main

public static void main(java.lang.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

getName

public java.lang.String getName()
Returns the class name (excluding package).

Specified by:
getName in interface Structure
See Also:
Structure.getName()