View Javadoc
1   package ca.uhn.hl7v2.conf.spec.message;
2   
3   import java.util.ArrayList;
4   import java.util.Iterator;
5   import java.util.List;
6   
7   import ca.uhn.hl7v2.conf.ProfileException;
8   
9   /**
10   * An abstraction of SegGroup and MessageProfile (both are containers for segment specs).  
11   * @author Bryan Tripp
12   */
13  public class AbstractSegmentContainer implements Iterable<ProfileStructure> {
14      
15      private String description;
16      private String reference;
17      private String impNote;    
18      private final List<ProfileStructure> children = new ArrayList<>();
19      
20      /** Utility field used by bound properties. */
21      private final java.beans.PropertyChangeSupport propertyChangeSupport =  new java.beans.PropertyChangeSupport(this);
22      
23      /** Utility field used by constrained properties. */
24      private final java.beans.VetoableChangeSupport vetoableChangeSupport =  new java.beans.VetoableChangeSupport(this);
25      
26      /** Creates a new instance of AbstractSegmentContainer */
27      public AbstractSegmentContainer() {
28      }
29      
30      public List<ProfileStructure> getChildrenAsList() {
31      	return (children);
32      }
33      
34      /** Adds a PropertyChangeListener to the listener list.
35       * @param l The listener to add.
36       */
37      public void addPropertyChangeListener(java.beans.PropertyChangeListener l) {
38          propertyChangeSupport.addPropertyChangeListener(l);
39      }
40      
41      /** Removes a PropertyChangeListener from the listener list.
42       * @param l The listener to remove.
43       */
44      public void removePropertyChangeListener(java.beans.PropertyChangeListener l) {
45          propertyChangeSupport.removePropertyChangeListener(l);
46      }
47      
48      /** Adds a VetoableChangeListener to the listener list.
49       * @param l The listener to add.
50       */
51      public void addVetoableChangeListener(java.beans.VetoableChangeListener l) {
52          vetoableChangeSupport.addVetoableChangeListener(l);
53      }
54      
55      /** Removes a VetoableChangeListener from the listener list.
56       * @param l The listener to remove.
57       */
58      public void removeVetoableChangeListener(java.beans.VetoableChangeListener l) {
59          vetoableChangeSupport.removeVetoableChangeListener(l);
60      }
61      
62      /** Getter for property description.
63       * @return Value of property description.
64       */
65      public String getDescription() {
66          return this.description;
67      }
68      
69      /** Setter for property description.
70       * @param description New value of property description.
71       *
72       * @throws ProfileException
73       */
74      public void setDescription(String description) throws ProfileException {
75          String oldDescription = this.description;
76          try {
77              vetoableChangeSupport.fireVetoableChange("description", oldDescription, description);
78          } catch (Exception e) {
79              throw new ProfileException(null, e);
80          }
81          this.description = description;
82          propertyChangeSupport.firePropertyChange("description", oldDescription, description);
83      }
84      
85      /** Getter for property reference.
86       * @return Value of property reference.
87       */
88      public String getReference() {
89          return this.reference;
90      }
91      
92      /** Setter for property reference.
93       * @param reference New value of property reference.
94       *
95       * @throws ProfileException
96       */
97      public void setReference(String reference) throws ProfileException {
98          String oldReference = this.reference;
99          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 }