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