Package org.dellroad.hl7
Class HL7Message
- java.lang.Object
-
- org.dellroad.hl7.HL7Message
-
- All Implemented Interfaces:
Serializable
public class HL7Message extends Object implements Serializable
Represents an HL7 message.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static charSEGMENT_TERMINATORThe character that separates segments in an HL7 message.protected HL7SegmentListsegmentsThe segments in this message.
-
Constructor Summary
Constructors Constructor Description HL7Message()Convenience constructor.HL7Message(String msg)Parsing constructor.HL7Message(HL7Seps seps)Construct an empty HL7 message.HL7Message(MSHSegment msh)Construct a new HL7 message containing only the given MSH segment.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object obj)HL7SegmentfindSegment(String segname)Convenience method.HL7SegmentfindSegment(String segname, int segnum)Get the first occurrence of asegnamesegment in this message.Stringget(String name)Convenience method.Stringget(String name, int segnum, int repeat)Find a string value by HL7 name.HL7FieldgetField(String name)Convenience method.HL7FieldgetField(String name, int segnum)Find a field by HL7 name.MSHSegmentgetMSHSegment()Get the MSH segment of this message.HL7SegmentListgetSegments()Get all of the segments in this message.inthashCode()StringtoString()Convert this message into a string by concatenating the segments in string form, each terminated with a carriage return.StringtoString(HL7Seps seps)Format this message using the supplied separators instead of the ones defined by the MSH segment.
-
-
-
Field Detail
-
SEGMENT_TERMINATOR
public static final char SEGMENT_TERMINATOR
The character that separates segments in an HL7 message.- See Also:
- Constant Field Values
-
segments
protected final HL7SegmentList segments
The segments in this message. This list always contains at least one element, namely theMSHSegment.
-
-
Constructor Detail
-
HL7Message
public HL7Message(MSHSegment msh)
Construct a new HL7 message containing only the given MSH segment.- Parameters:
msh- MSH segment
-
HL7Message
public HL7Message(HL7Seps seps)
Construct an empty HL7 message.A MSH segment will be automatically added.
- Parameters:
seps- separator and escape characters to use for this message
-
HL7Message
public HL7Message()
Convenience constructor. Equivalent to:HL7Message(HL7Seps.DEFAULT)
-
HL7Message
public HL7Message(String msg) throws HL7ContentException
Parsing constructor. Constructs an HL7 message by parsing the given string. Segments must be separated with a carriage return character.- Parameters:
msg- string-encoded message- Throws:
HL7ContentException- if the string is invalid
-
-
Method Detail
-
getMSHSegment
public MSHSegment getMSHSegment()
Get the MSH segment of this message.- Returns:
- this message's MSH segment (not a copy; changes are reflected back)
-
getSegments
public HL7SegmentList getSegments()
Get all of the segments in this message.The returned list does not allow removing the first element (the MSH segment) or changing it to anything other than an
MSHSegment.- Returns:
- list of all message segments including MSH
-
findSegment
public HL7Segment findSegment(String segname, int segnum)
Get the first occurrence of asegnamesegment in this message.- Parameters:
segname- segment name, e.g., "PV1"segnum- starting segment index to start search; index zero refers to the MSH segment- Returns:
- first matching segment found, or null if not found
- Throws:
IllegalArgumentException- if segnum is negative
-
findSegment
public HL7Segment findSegment(String segname)
Convenience method. Equivalent to:findSegment(name, 0)- Parameters:
segname- segment name, e.g., "PV1"- Returns:
- first matching segment found, or null if not found
-
getField
public HL7Field getField(String name, int segnum)
Find a field by HL7 name. The name is of the form XYZ.N where "XYZ" is the segment name and "N" is the field number.- Parameters:
name- field name, e.g., "PV1.3"segnum- starting segment index (zero to start from MSH segment)- Returns:
- named HL7 field, or
nullif the named field doesn't exist in this message - Throws:
IllegalArgumentException- if name is not properly formattedIllegalArgumentException- if segnum is negative
-
getField
public HL7Field getField(String name)
Convenience method. Equivalent to:getField(name, 0)- Parameters:
name- field name, e.g., "PV1.3"- Returns:
- named HL7 field, or
nullif the named field doesn't exist in this message
-
get
public String get(String name, int segnum, int repeat)
Find a string value by HL7 name. The name is of the formXYZ.N[.M[.L]]whereXYZis the segment name,Nis the field number,Mis the optional component number, andLis the optional subcomponent number.If either of
MorLare not given, they are assumed to be1so an unambiguous string value can always be returned.- Parameters:
name- HL7 value name, e.g., "PV1.3", "MSH.9.1", "ZZZ.3.2.1"segnum- starting segment indexrepeat- repeat index (starting from zero)- Returns:
- named HL7 value, or
nullif the named value doesn't exist in this message - Throws:
IllegalArgumentException- if name is not properly formattedIllegalArgumentException- if "M" or "L" is negativeIllegalArgumentException- if segnum or repeat is negative
-
get
public String get(String name)
Convenience method. Equivalent to:get(name, 0, 0)- Parameters:
name- HL7 value name, e.g., "PV1.3", "MSH.9.1", "ZZZ.3.2.1"- Returns:
- named HL7 field, or
nullif the named field doesn't exist in this message
-
toString
public String toString()
Convert this message into a string by concatenating the segments in string form, each terminated with a carriage return.
-
toString
public String toString(HL7Seps seps)
Format this message using the supplied separators instead of the ones defined by the MSH segment. Ifsepsdoes not define an escape character, then characters that need to be escaped are silently elided.- Parameters:
seps- HL7 separator characters- Returns:
- string encoding of this message
-
-