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);
...
log
Constructor and Description |
---|
AbstractSegment(Group parent,
ModelClassFactory factory)
Calls the abstract init() method to create the fields in this segment.
|
Modifier and Type | Method and Description |
---|---|
boolean |
accept(MessageVisitor visitor,
Location location)
Iterates over the contained fields and calls the visitor for each
of them.
|
protected void |
add(Class<? extends Type> c,
boolean required,
int maxReps,
int length,
Object[] constructorArgs)
Deprecated.
|
protected void |
add(Class<? extends Type> c,
boolean required,
int maxReps,
int length,
Object[] constructorArgs,
String name)
Adds a field to the segment.
|
void |
clear()
Clears all data from this segment
|
protected Type |
createNewTypeWithoutReflection(int field)
Attempts to create an instance of a field type without using reflection.
|
String |
encode()
Encodes this message using the parser returned by
Message.getParser() |
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.
|
String |
getName()
Returns the class name (excluding package).
|
String[] |
getNames()
Returns the names of the fields in this segment.
|
protected int |
getReps(int number) |
protected <T extends Type> |
getTypedField(int number,
int rep)
Returns a specific repetition of field with concrete type at the specified index
|
protected <T extends Type> |
getTypedField(int number,
T[] array)
Returns an array of a specific type class
|
Type |
insertRepetition(int fieldNum,
int index)
Inserts a repetition of a given Field into repetitions of that field by
name.
|
boolean |
isEmpty()
Returns true if this Visitable has actual content
|
boolean |
isRequired(int number)
Returns true if the given field is required in this segment - fields are
numbered from 1.
|
static void |
main(String[] args) |
int |
numFields()
Returns the number of fields defined by this segment (repeating fields
are not counted multiple times).
|
void |
parse(String string)
Parses the string into this segment using the parser returned by
Message.getParser() |
Location |
provideLocation(Location location,
int index,
int repetition)
Reusing the location of this object's parent, this method returns its own
location within the message.
|
Type |
removeRepetition(int fieldNum,
int index)
Removes a repetition of a given field by name.
|
getMessage, getParent
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getMessage, getParent
public AbstractSegment(Group parent, ModelClassFactory factory)
parent
- parent groupfactory
- 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 thoughpublic boolean accept(MessageVisitor visitor, Location location) throws HL7Exception
accept
in interface Visitable
visitor
- MessageVisitor instance to be called back.location
- location of the groupHL7Exception
public Location provideLocation(Location location, int index, int repetition)
Visitable
provideLocation
in interface Visitable
location
- the parents locationindex
- index of this objectrepetition
- repetition of this objectpublic Type[] getField(int number) throws HL7Exception
getField
in interface Segment
number
- field number (starting at 1)HL7Exception
- if field index is out of range.public boolean isEmpty() throws HL7Exception
Visitable
isEmpty
in interface Visitable
HL7Exception
- if an error occurred while determining emptinessVisitable.isEmpty()
protected <T extends Type> T[] getTypedField(int number, T[] array)
protected int getReps(int number)
public Type getField(int number, int rep) throws HL7Exception
Type t = getField(x, 3);
getField
in interface Segment
number
- the field number (starting at 1)rep
- the repetition number (starting at 0)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.protected <T extends Type> T getTypedField(int number, int rep)
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.
field
- Field number - Note that this is zero indexed!null
if not
possiblepublic boolean isRequired(int number) throws HL7Exception
isRequired
in interface Segment
number
- field number starting at 1HL7Exception
- if field index is out of range.public int getLength(int number) throws HL7Exception
getLength
in interface Segment
number
- field number starting at 1HL7Exception
- if field index is out of range.public int getMaxCardinality(int number) throws HL7Exception
getMaxCardinality
in interface Segment
number
- field number starting at 1HL7Exception
- if field index is out of range.protected void add(Class<? extends Type> c, boolean required, int maxReps, int length, Object[] constructorArgs) throws HL7Exception
add(Class, boolean, int, int, Object[], String)
HL7Exception
protected void add(Class<? extends Type> c, boolean required, int maxReps, int length, Object[] constructorArgs, String name) throws HL7Exception
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 validmaxReps
- 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 fieldHL7Exception
- if the given class does not inherit from Type or if it can
not be instantiated.public int numFields()
public String getName()
getName
in interface Structure
Structure.getName()
public void parse(String string) throws HL7Exception
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
parse
in interface Segment
string
- encoded segmentHL7Exception
- if errors occurred during parsingpublic String encode() throws HL7Exception
Message.getParser()
encode
in interface Segment
HL7Exception
- if errors occurred during encodingpublic Type removeRepetition(int fieldNum, int index) throws HL7Exception
HL7Exception
- if the named Structure is not part of this Group.public Type insertRepetition(int fieldNum, int index) throws HL7Exception
HL7Exception
- if the named Structure is not part of this Group.public void clear()
Copyright © 2001–2017 University Health Network. All rights reserved.