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 "Segment.java".  Description: 
010"Represents an HL7 message segment, which is a unit of data that contains multiple fields.
011@author Bryan Tripp (bryan_tripp@sourceforge.net)" 
012
013The Initial Developer of the Original Code is University Health Network. Copyright (C) 
0142001.  All Rights Reserved. 
015
016Contributor(s): ______________________________________. 
017
018Alternatively, the contents of this file may be used under the terms of the 
019GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
020applicable instead of those above.  If you wish to allow use of your version of this 
021file only under the terms of the GPL and not to allow others to use your version 
022of this file under the MPL, indicate your decision by deleting  the provisions above 
023and replace  them with the notice and other provisions required by the GPL License.  
024If you do not delete the provisions above, a recipient may use your version of 
025this file under either the MPL or the GPL. 
026
027 */
028package ca.uhn.hl7v2.model;
029
030import ca.uhn.hl7v2.HL7Exception;
031
032/**
033 * Represents an HL7 message segment, which is a unit of data that contains multiple fields.
034 * @author Bryan Tripp (bryan_tripp@sourceforge.net)
035 */
036public interface Segment extends Structure {
037
038    /**
039     * Encodes this message using the parser returned by {@link Message#getParser() }
040     *
041     * @return the string-encoded segment
042     * @throws HL7Exception if errors occurred during encoding
043     */
044    public String encode() throws HL7Exception;
045
046    /**
047     * Returns the array of Fields at the specified index.  The array will be of length 1 for
048     * non-repeating fields, and >1 for repeating fields.  Fields are numbered from 1.
049     *
050     * @param number field number (starting at 1)
051     * @return the array of Fields at the specified field number
052     * @throws HL7Exception if field index is out of range.
053     */
054    public Type[] getField(int number) throws HL7Exception;
055
056    /**
057     * Returns a specific repetition of field at the specified index.  If there exist
058     * fewer repetitions than are required, the number of repetitions can be increased
059     * by specifying the lowest repetition that does not yet exist.  For example
060     * if there are two repetitions but three are needed, the third can be created
061     * and accessed using the following code: <br>
062     * <code>Type t = getField(x, 2);</code>
063     * @param number the field number (starting at 1)
064     * @param rep the repetition number (starting at 0)
065     * @return field at the specified field number and repetition
066     * @throws HL7Exception if field index is out of range, or if the specified
067     *    repetition is more than 1 greater than the highest index of existing repetitions.
068     *    NOTE: to facilitate local extensions, no exception is thrown if
069     *    rep > max cardinality
070     */
071    public Type getField(int number, int rep) throws HL7Exception;
072
073    /**
074     * Returns the maximum length of the field at the given index, in characters.
075     * @param number field number starting at 1
076     * @return maximum length of the field
077     * @throws HL7Exception if field index is out of range.
078     */
079    public int getLength(int number) throws HL7Exception;
080
081    /**
082     * Returns the maximum number of repetitions of this field that are allowed.
083     * The current cardinality can be obtained by checking the length
084     * of the array returned by getLength(n).
085     * @param number field number starting at 1
086     * @return maximum number of repetitions of this field
087     * @throws HL7Exception if field index is out of range.
088     */
089    public int getMaxCardinality(int number) throws HL7Exception;
090
091    /**
092     * Returns the names of the fields in this segment.
093     * @return array of names
094     *
095     * @since 1.0 - Note that if user defined types are being used, there is a possibility that some entries may be null. All official hapi structures will have all entries populated, but older user defined structures may not have populated all values, since this feature did not exist prior to release 1.0.
096     */
097    public String[] getNames();
098
099    /**
100     * Returns true if the field at the given index is required, false otherwise.
101     *
102     * @param number field number starting at 1
103     * @return true if the field is required
104     * @throws HL7Exception if field index is out of range.
105     */
106    public boolean isRequired(int number) throws HL7Exception;
107
108
109    /**
110     * Returns the number of fields defined by this segment (repeating
111     * fields are not counted multiple times).
112     * @return number of fields
113     */
114    public int numFields();
115
116
117    /**
118     * Parses the string into this segment using the parser returned by {@link Message#getParser() }
119     * @param string encoded segment
120     * @throws HL7Exception if errors occurred during parsing
121     */
122    public void parse(String string) throws HL7Exception;
123
124    
125}