001/**
002 * The 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.
004 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
005 * Software distributed under the License is distributed on an "AS IS" basis,
006 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
007 * specific language governing rights and limitations under the License.
008 *
009 * The Original Code is "IStructureDefinition.java"
010 *
011 * The Initial Developer of the Original Code is University Health Network. Copyright (C)
012 * 2001.  All Rights Reserved.
013 *
014 * Contributor(s):
015 *
016 * Alternatively, the contents of this file may be used under the terms of the
017 * GNU General Public License (the  ?GPL?), in which case the provisions of the GPL are
018 * applicable instead of those above.  If you wish to allow use of your version of this
019 * file only under the terms of the GPL and not to allow others to use your version
020 * of this file under the MPL, indicate your decision by deleting  the provisions above
021 * and replace  them with the notice and other provisions required by the GPL License.
022 * If you do not delete the provisions above, a recipient may use your version of
023 * this file under either the MPL or the GPL.
024 *
025 */
026
027
028package ca.uhn.hl7v2.parser;
029
030import java.util.List;
031import java.util.Set;
032
033import ca.uhn.hl7v2.model.Group;
034import ca.uhn.hl7v2.model.Structure;
035
036/**
037 * Contains information about the composition of a given type of {@link Structure}.
038 * At runtime, parsers will use accessors provided by various structure types (messages, groups,
039 * segments) to determine the structure of a messages. Structure definitions are used
040 * to cache that information between parse calls.
041 */
042public interface IStructureDefinition {
043
044    /**
045     * @return Returns this structure's first sibling (in other words, its
046     *         parent's first child). Returns
047     *         <code>null<code> if this is the first sibling, or if this has no parent
048     */
049    IStructureDefinition getFirstSibling();
050
051    /**
052     * @return Returns the next leaf (segment) after this one, within the same
053     * group, only if one exists and this structure is also a leaf. Otherwise returns <code>null</code>.
054     */
055    IStructureDefinition getNextLeaf();
056
057    /**
058     * @return The name of the segment, as it is known to it's parent. This
059     * will differ from {{@link #getName()}} in the case of multiple segments
060     * with the same name in a group, e.g. the two PID segments in ADT_A17,
061     * where the second one it known as PID2 to it's parent. 
062     */
063    String getNameAsItAppearsInParent();
064    
065    /**
066     * @return Returns the name of this structure
067     */
068    String getName();
069
070    /**
071     * @return Returns true if this structure is a segment
072     */
073    boolean isSegment();
074
075    /**
076     * @return Returns true if this is a repeatable structure
077     */
078    boolean isRepeating();
079
080    /**
081     * @return Returns all children of this structure definition
082     */
083    List<StructureDefinition> getChildren();
084
085    /**
086     * @return Returns the index of the position of this structure
087     * within it's parent's children
088     */
089    int getPosition();
090
091    /**
092     * @return Returns the parent structure of this structure, if one exists.
093     * Otherwise, returns null.
094     */
095    IStructureDefinition getParent();
096
097    /**
098     * @return Returns true if this structure is the final child of it's parent.
099     */
100    boolean isFinalChildOfParent();
101
102    /**
103     * @return Returns this structure's next sibling within it's parent, if any.
104     */
105    IStructureDefinition getNextSibling();
106
107    /**
108     * @return Does this structure have children (i.e. is it not a segment)
109     */
110    boolean hasChildren();
111
112    /**
113     * Should only be called on a leaf node (segment). Returns the names
114     * of all valid children which may follow this one, at any level in the
115     * hierarchy (including as later siblings of parent structures to
116     * this one)
117     *
118     * @return the names of all valid children which may follow this one
119     */
120    Set<String> getNamesOfAllPossibleFollowingLeaves();
121
122    /**
123     * @return structure definition of first child of null if there is no child
124     */
125    IStructureDefinition getFirstChild();
126
127    /**
128     * Returns the names of any possible children that could be the first
129     * required child of this group.
130     *
131     * For instance, for the group below "ORC" and "OBR" would both be
132     * returned, as they are both potential first children of this group.
133     *
134     * Note that the name returned by {@link #getName() this.getName()}
135     * is also returned.
136     *
137     * <code>
138     *               ORDER_OBSERVATION
139     *    {
140     *    [ ORC ]
141     *    OBR
142     *    [ { NTE } ]
143     *    [ CTD ]
144     *                  OBSERVATION
145     *       {
146     *       [ OBX ]
147     *       [ { NTE } ]
148     *       }
149     *                  OBSERVATION
150     *    [ { FT1 } ]
151     *    [ { CTI } ]
152     *    }
153     *                  ORDER_OBSERVATION
154     *     </code>
155     *
156     *
157     * @return the names of any possible children that could be the first
158     * required child of this group
159     */
160    Set<String> getAllPossibleFirstChildren();
161
162    /**
163     * @return Returns the names of all children of this structure, including first elements within child groups
164     */
165    Set<String> getAllChildNames();
166
167    /**
168     * @return true if this element a choice element
169     *
170     * @see Group#isChoiceElement(String)
171     */
172    boolean isChoiceElement();
173    
174    /**
175     * @return true if this a required structure within it's parent
176     */
177    boolean isRequired();
178
179}