001    package ca.uhn.hl7v2.model;
002    
003    import java.io.Serializable;
004    import java.util.ArrayList;
005    
006    /**
007     * A set of "extra" components (sub-components) that are not a standard part 
008     * of a field (component) but have been added at runtime.  The purpose is to allow 
009     * processing of locally-defined extensions to datatypes without the need for a 
010     * custom message definition.  
011     * Extra components are not treated uniformly with standard components (e.g. 
012     * they are not accessible through methods like Primitive.getValue() and 
013     * Composite.getComponent()).  To do so would blur the distinction between 
014     * primitive and composite types (i.e. leaf and non-leaf nodes), which seems 
015     * nice and polymorphic for a moment but actually isn't helpful.  
016     * Furthermore, the auto-generated classes do not define accessors to extra 
017     * components, because they are meant to encourage and enforce use of the standard 
018     * message structure -- stepping outside the standard structure must be 
019     * deliberate. 
020     * Note that a uniformity of access to standard and extra components is provided
021     * by Terser.   
022     * @author Bryan Tripp
023     */
024    public class ExtraComponents implements Serializable {
025        
026        private static final long serialVersionUID = -2614683870975956395L;
027        
028        private ArrayList comps;
029        private Message message;
030        
031        /** Creates a new instance of ExtraComponents */
032        public ExtraComponents(Message message) {
033            this.comps = new ArrayList();
034            this.message = message; 
035        }
036        
037        /** Returns the number of existing extra components */
038        public int numComponents() {
039            return comps.size();
040        }
041        
042        /** Returns the number of existing reps of a given extra component */
043        /*public int numReps(int comp) {
044            return ((ArrayList) this.comps.get(comp)).size();
045        }*/
046        
047        /** 
048         * Returns the component at the given location, creating it 
049         * and all preceeding components if necessary.  
050         * @param comp the extra component number starting at 0 (i.e. 0 is the first 
051         *      extra component)
052         */
053        public Varies getComponent(int comp) {
054            ensureComponentAndPredecessorsExist(comp);
055            return (Varies) this.comps.get(comp);
056        }
057        
058        /**
059         * Checks that the component at the given location exists, and that 
060         * all preceding components exist, creating any missing ones.  
061         */
062        private void ensureComponentAndPredecessorsExist(int comp) {
063            for (int i = this.comps.size(); i <= comp; i++) {
064                this.comps.add(new Varies(message));
065            }
066            /*ArrayList reps = (ArrayList) this.comps.get(comp);
067            for (int j = reps.size(); j <= rep; j++) {
068                addRep(comp, j);
069            }*/
070        }
071    
072    
073            /**
074             * Clears all extra components
075             */
076            void clear() {
077                    comps.clear();
078            }
079    
080    
081        /*private void addComp(int comp) {
082        }*/
083        
084        /*private void addRep(int comp, int rep) {
085            ArrayList l = (ArrayList) this.comps.get(comp);
086            l.add(rep, new Varies());
087        }*/
088    }