Class MessageIterator

java.lang.Object
ca.uhn.hl7v2.parser.MessageIterator
All Implemented Interfaces:
Iterator<Structure>

public class MessageIterator extends Object implements Iterator<Structure>
Iterates over all defined nodes (ie segments, groups) in a message, regardless of whether they have been instantiated previously. This is a tricky process, because the number of nodes is infinite, due to infinitely repeating segments and groups. See next() for details on how this is handled. This implementation assumes that the first segment in each group is present (as per HL7 rules). Specifically, when looking for a segment location, an empty group that has a spot for the segment will be overlooked if there is anything else before that spot. This may result in surprising (but sensible) behaviour if a message is missing the first segment in a group.
Author:
Bryan Tripp
  • Constructor Details

  • Method Details

    • hasNext

      public boolean hasNext()
      Returns true if another object exists in the iteration sequence.
      Specified by:
      hasNext in interface Iterator<Structure>
    • next

      public Structure next()

      Returns the next node in the message. Sometimes the next node is ambiguous. For example at the end of a repeating group, the next node may be the first segment in the next repetition of the group, or the next sibling, or an undeclared segment locally added to the group's end. Cases like this are disambiguated using getDirection(), which returns the name of the structure that we are "iterating towards". Usually we are "iterating towards" a segment of a certain name because we have a segment string that we would like to parse into that node. Here are the rules:

      1. If at a group, next means first child.
      2. If at a non-repeating segment, next means next "position"
      3. If at a repeating segment: if segment name matches direction then next means next rep, otherwise next means next "position".
      4. If at a segment within a group (not at the end of the group), next "position" means next sibling
      5. If at the end of a group: If name of group or any of its "first decendents" matches direction, then next position means next rep of group. Otherwise if direction matches name of next sibling of the group, or any of its first descendents, next position means next sibling of the group. Otherwise, next means a new segment added to the group (with a name that matches "direction").
      6. "First descendents" means first child, or first child of the first child, or first child of the first child of the first child, etc.
      Specified by:
      next in interface Iterator<Structure>
    • remove

      public void remove()
      Not supported
      Specified by:
      remove in interface Iterator<Structure>
    • getDirection

      public String getDirection()
    • setDirection

      public void setDirection(String direction)
    • getNextIndexWithinParent

      Must be called after next()