View Javadoc

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