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