001/**
002The contents of this file are subject to the Mozilla Public License Version 1.1 
003(the "License"); you may not use this file except in compliance with the License. 
004You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
005Software distributed under the License is distributed on an "AS IS" basis, 
006WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
007specific language governing rights and limitations under the License. 
008
009The Original Code is "Group.java".  Description: 
010"An abstraction representing >1 message parts which may repeated together.
011  An implementation of Group should enforce contraints about on the contents of the group
012  and throw an exception if an attempt is made to add a Structure that the Group instance
013  does not recognize.
014  @author Bryan Tripp (bryan_tripp@sourceforge.net)" 
015
016The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0172001.  All Rights Reserved. 
018
019Contributor(s): ______________________________________. 
020
021Alternatively, the contents of this file may be used under the terms of the 
022GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
023applicable instead of those above.  If you wish to allow use of your version of this 
024file only under the terms of the GPL and not to allow others to use your version 
025of this file under the MPL, indicate your decision by deleting  the provisions above 
026and replace  them with the notice and other provisions required by the GPL License.  
027If you do not delete the provisions above, a recipient may use your version of 
028this file under either the MPL or the GPL. 
029
030*/
031
032package ca.uhn.hl7v2.model;
033
034import ca.uhn.hl7v2.HL7Exception;
035
036/**
037 * An abstraction representing >1 message parts which may repeated together.
038 * An implementation of Group should enforce contraints about on the contents of the group
039 * and throw an exception if an attempt is made to add a Structure that the Group instance
040 * does not recognize.
041 * @author Bryan Tripp (bryan_tripp@sourceforge.net)
042 */
043public interface Group extends Structure {
044
045  /**
046   * Returns an array of Structure objects by name.  For example, if the Group contains
047   * an MSH segment and "MSH" is supplied then this call would return a 1-element array 
048   * containing the MSH segment.  Multiple elements are returned when the segment or 
049   * group repeats.  The array may be empty if no repetitions have been accessed
050   * yet using the get(...) methods.
051   *
052   * @param name of the structure
053   * @return array of Structure objects
054   * @throws HL7Exception if the named Structure is not part of this Group. 
055   */
056  public Structure[] getAll(String name) throws HL7Exception;
057
058  /**
059   * Returns the named structure.  If this Structure is repeating then the first 
060   * repetition is returned.  Creates the Structure if necessary.
061   *
062   * @param name of the structure
063   * @return first (or only) structure object
064   * @throws HL7Exception if the named Structure is not part of this Group. 
065   */
066  public Structure get(String name) throws HL7Exception;
067  
068  /**
069   * Returns a particular repetition of the named Structure. If the given repetition
070   * number is one greater than the existing number of repetitions then a new  
071   * Structure is created.
072   *
073   * @param name name of the structure
074   * @param rep repetition (zero-based)
075   * @return particular repetition of the named structure
076   * @throws HL7Exception if the named Structure is not part of this group,
077   *    if the structure is not repeatable and the given rep is > 0,  
078   *    or if the given repetition number is more than one greater than the 
079   *    existing number of repetitions.  
080   */
081  public Structure get(String name, int rep) throws HL7Exception;
082  
083  /**
084   * Returns true if the named structure is required.
085   *
086   * @param name name of the structure nested in this group
087   * @return true if structure is required
088   * @throws HL7Exception if the named Structure is not part of this group
089   */
090  public boolean isRequired(String name) throws HL7Exception;
091  
092  /**
093   * Returns true if the named structure is repeating.
094   *
095   * @param name name of the structure nested in this group
096   * @return true if structure is repeating
097   * @throws HL7Exception if the named Structure is not part of this group
098   */
099  public boolean isRepeating(String name) throws HL7Exception;
100
101  /**
102   * Returns true if the named structure is a "choice element". 
103   * Some HL7 structures (e.g. ORM_O01 in v2.5) have groups that have
104   * several possible first segments. In these structures, one of these
105   * "choice elements" must be present, but not more than one.
106   *
107   * @param name name of the structure nested in this group
108   * @return true if structure is a choice element
109   * @throws HL7Exception if the named Structure is not part of this group
110   *
111   */
112  public boolean isChoiceElement(String name) throws HL7Exception;
113
114  /**
115   * Returns true if the named structure is a group.
116   *
117   * @param name name of the structure nested in this group
118   * @return true if structure is a choice element
119   * @throws HL7Exception if the named Structure is not part of this group
120   * @since 1.2
121   */
122  public boolean isGroup(String name) throws HL7Exception;
123  
124  /**
125   * Returns an ordered array of the names of the Structures in this 
126   * Group.  These names can be used to iterate through the group using 
127   * repeated calls to <code>get(name)</code>.
128   *
129   * @return an ordered array of the names of the Structures in this Group
130   */
131  public String[] getNames();
132  
133  /**
134   * Returns the Class of the Structure at the given name index.  
135   * @param name name of the structure nested in this group
136   * @return class of the structure or null if the class does not exist
137   */
138  public Class<? extends Structure> getClass(String name);
139  
140  /**
141   * Expands the group by introducing a new child Structure (i.e. 
142   * a new Segment or Group).  This method is used to support handling 
143   * of unexpected segments (e.g. Z-segments).  
144   * @param c class of the structure to insert (e.g. NTE.class) 
145   * @param required whether the child is required 
146   * @param repeating whether the child is repeating 
147   * @param index index at which to insert the new child
148   * @param name the child name (e.g. "NTE")
149   * @return the name used to index the structure (may be appended with a number if 
150   *        name already used)
151   */
152  //public String insert(Class c, boolean required, boolean repeating, int index, String name) throws HL7Exception;
153  
154    /**
155     * Expands the group definition to include a segment that is not 
156     * defined by HL7 to be part of this group (eg an unregistered Z segment). 
157     * The new segment is slotted at the end of the group.  Thenceforward if 
158     * such a segment is encountered it will be parsed into this location. 
159     * If the segment name is unrecognized a GenericSegment is used.  The 
160     * segment is defined as repeating and not required.
161     *
162     * @param name name of the segment
163     * @return the final name of the segment (may be renamed if a segment of this
164     * name already exists.
165     * @throws HL7Exception if the segment could not be added
166     */
167    public String addNonstandardSegment(String name) throws HL7Exception;
168
169    /**
170     * Expands the group definition to include a segment that is not 
171     * defined by HL7 to be part of this group (eg an unregistered Z segment).
172     *
173     * @param name name of the segment
174     * @param theIndex index (zero-based) at which to insert this segment
175     * @return the name used to index the structure (may be appended with a number if
176     *        name already used)
177     * @throws HL7Exception if the segment could not be added
178     */
179    public String addNonstandardSegment(String name, int theIndex) throws HL7Exception;
180
181}
182