001package ca.uhn.hl7v2.conf.spec.message;
002
003import java.util.ArrayList;
004import java.util.Arrays;
005import java.util.Iterator;
006import java.util.List;
007
008import ca.uhn.hl7v2.conf.ProfileException;
009
010/**
011 * An abstraction of SegGroup and MessageProfile (both are containers for segment specs).  
012 * @author Bryan Tripp
013 */
014public class AbstractSegmentContainer implements Iterable<ProfileStructure> {
015    
016    private String description;
017    private String reference;
018    private String impNote;    
019    private List<ProfileStructure> children = new ArrayList<ProfileStructure>();
020    
021    /** Utility field used by bound properties. */
022    private java.beans.PropertyChangeSupport propertyChangeSupport =  new java.beans.PropertyChangeSupport(this);
023    
024    /** Utility field used by constrained properties. */
025    private java.beans.VetoableChangeSupport vetoableChangeSupport =  new java.beans.VetoableChangeSupport(this);
026    
027    /** Creates a new instance of AbstractSegmentContainer */
028    public AbstractSegmentContainer() {
029    }
030    
031    public List<ProfileStructure> getChildrenAsList() {
032        return (children);
033    }
034    
035    /** Adds a PropertyChangeListener to the listener list.
036     * @param l The listener to add.
037     */
038    public void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
039        propertyChangeSupport.addPropertyChangeListener(l);
040    }
041    
042    /** Removes a PropertyChangeListener from the listener list.
043     * @param l The listener to remove.
044     */
045    public void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
046        propertyChangeSupport.removePropertyChangeListener(l);
047    }
048    
049    /** Adds a VetoableChangeListener to the listener list.
050     * @param l The listener to add.
051     */
052    public void addVetoableChangeListener(java.beans.VetoableChangeListener l) {
053        vetoableChangeSupport.addVetoableChangeListener(l);
054    }
055    
056    /** Removes a VetoableChangeListener from the listener list.
057     * @param l The listener to remove.
058     */
059    public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) {
060        vetoableChangeSupport.removeVetoableChangeListener(l);
061    }
062    
063    /** Getter for property description.
064     * @return Value of property description.
065     */
066    public String getDescription() {
067        return this.description;
068    }
069    
070    /** Setter for property description.
071     * @param description New value of property description.
072     *
073     * @throws ProfileException
074     */
075    public void setDescription(String description) throws ProfileException {
076        String oldDescription = this.description;
077        try {
078            vetoableChangeSupport.fireVetoableChange("description", oldDescription, description);
079        } catch (Exception e) {
080            throw new ProfileException(null, e);
081        }
082        this.description = description;
083        propertyChangeSupport.firePropertyChange("description", oldDescription, description);
084    }
085    
086    /** Getter for property reference.
087     * @return Value of property reference.
088     */
089    public String getReference() {
090        return this.reference;
091    }
092    
093    /** Setter for property reference.
094     * @param reference New value of property reference.
095     *
096     * @throws ProfileException
097     */
098    public void setReference(String reference) throws ProfileException {
099        String oldReference = this.reference;
100        try {
101            vetoableChangeSupport.fireVetoableChange("reference", oldReference, reference);
102        } catch (Exception e) {
103            throw new ProfileException(null, e);
104        }
105        this.reference = reference;
106        propertyChangeSupport.firePropertyChange("reference", oldReference, reference);
107    }
108    
109    /** Getter for property impNote.
110     * @return Value of property impNote.
111     */
112    public String getImpNote() {
113        return this.impNote;
114    }
115    
116    /** Setter for property impNote.
117     * @param impNote New value of property impNote.
118     *
119     * @throws ProfileException
120     */
121    public void setImpNote(String impNote) throws ProfileException {
122        String oldImpNote = this.impNote;
123        try {
124            vetoableChangeSupport.fireVetoableChange("impNote", oldImpNote, impNote);
125        } catch (Exception e) {
126            throw new ProfileException(null, e);
127        }
128        this.impNote = impNote;
129        propertyChangeSupport.firePropertyChange("impNote", oldImpNote, impNote);
130    }
131        
132    
133    /** Indexed getter for property structure (index starts at 1 following HL7 convention).
134     * @param index Index of the property (starts at 1 following HL7 convention).
135     * @return Value of the property at <CODE>index</CODE>.
136     */
137    public ProfileStructure getChild(int index) {
138        return this.children.get(index - 1);
139    }
140    
141    /** Indexed setter for property structure.  Lengthens child list if necessary.  
142     * @param index Index of the property (starts at 1 following HL7 convention).
143     * @param structure New value of the property at <CODE>index</CODE>.
144     *
145     * @throws ProfileException
146     */
147    public void setChild(int index, ProfileStructure structure) throws ProfileException {
148        index--;
149        while (children.size() <= index) {
150                children.add(null);
151        }
152        ProfileStructure oldStructure = this.children.get(index);
153        this.children.set(index, structure);
154        try {
155            vetoableChangeSupport.fireVetoableChange("structure", null, null );
156        }
157        catch(java.beans.PropertyVetoException vetoException ) {
158            this.children.set(index, oldStructure);
159            throw new ProfileException(null, vetoException);
160        }
161        propertyChangeSupport.firePropertyChange("structure", null, null );
162    }
163    
164    /** Returns the number of children */
165    public int getChildren() {
166        return this.children.size();
167    }
168    
169        public Iterator<ProfileStructure> iterator() {
170                return (this.children).iterator();
171        }
172    
173}