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 /** Creates a new instance of ExtraComponents */ 034 public ExtraComponents(Message message) { 035 this.comps = new ArrayList<Varies>(); 036 this.message = message; 037 } 038 039 /** Returns the number of existing extra components */ 040 public int numComponents() { 041 return comps.size(); 042 } 043 044 /** Returns the number of existing reps of a given extra component */ 045 /*public int numReps(int comp) { 046 return ((ArrayList) this.comps.get(comp)).size(); 047 }*/ 048 049 /** 050 * Returns the component at the given location, creating it 051 * and all preceeding components if necessary. 052 * @param comp the extra component number starting at 0 (i.e. 0 is the first 053 * extra component) 054 */ 055 public Varies getComponent(int comp) { 056 ensureComponentAndPredecessorsExist(comp); 057 return this.comps.get(comp); 058 } 059 060 /** 061 * Checks that the component at the given location exists, and that 062 * all preceding components exist, creating any missing ones. 063 */ 064 private void ensureComponentAndPredecessorsExist(int comp) { 065 for (int i = this.comps.size(); i <= comp; i++) { 066 this.comps.add(new Varies(message)); 067 } 068 /*ArrayList reps = (ArrayList) this.comps.get(comp); 069 for (int j = reps.size(); j <= rep; j++) { 070 addRep(comp, j); 071 }*/ 072 } 073 074 075 /** 076 * Clears all extra components 077 */ 078 void clear() { 079 comps.clear(); 080 } 081 082 /** 083 * {@inheritDoc} 084 */ 085 @Override 086 public String toString() { 087 return "ExtraComponents" + comps; 088 } 089 090 091 /*private void addComp(int comp) { 092 }*/ 093 094 /*private void addRep(int comp, int rep) { 095 ArrayList l = (ArrayList) this.comps.get(comp); 096 l.add(rep, new Varies()); 097 }*/ 098}