1 /** 2 The contents of this file are subject to the Mozilla Public License Version 1.1 3 (the "License"); you may not use this file except in compliance with the License. 4 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 5 Software distributed under the License is distributed on an "AS IS" basis, 6 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 7 specific language governing rights and limitations under the License. 8 9 The Original Code is "FiniteList.java". Description: 10 "Holds a group of repetitions for a given Profile and exercises cardinality constraints" 11 12 The Initial Developer of the Original Code is University Health Network. Copyright (C) 13 2001. All Rights Reserved. 14 15 Contributor(s): James Agnew 16 Paul Brohman 17 Mitch Delachevrotiere 18 Shawn Dyck 19 Cory Metcalf 20 21 Alternatively, the contents of this file may be used under the terms of the 22 GNU General Public License (the "GPL"), in which case the provisions of the GPL are 23 applicable instead of those above. If you wish to allow use of your version of this 24 file only under the terms of the GPL and not to allow others to use your version 25 of this file under the MPL, indicate your decision by deleting the provisions above 26 and replace them with the notice and other provisions required by the GPL License. 27 If you do not delete the provisions above, a recipient may use your version of 28 this file under either the MPL or the GPL. 29 30 */ 31 package ca.uhn.hl7v2.conf.classes.abs; 32 33 import ca.uhn.hl7v2.conf.classes.exceptions.*; 34 import java.lang.reflect.*; 35 import java.util.ArrayList; 36 37 /** Holds a group of repetitions for a given Profile and exercises cardinality constraints 38 * @author <table><tr>James Agnew</tr> 39 * <tr>Paul Brohman</tr> 40 * <tr>Mitch Delachevrotiere</tr> 41 * <tr>Shawn Dyck</tr> 42 * <tr>Cory Metcalf</tr></table> 43 */ 44 public class FiniteList { 45 46 private final ArrayList<Repeatable> reps; // Stores the reps 47 private final int maxReps; // The maximum allowable number of reps 48 // private int minReps; // The minimum allowable number of reps 49 private final Class<? extends Repeatable> repType; // The type of repetition being stored here 50 private final Object underlyingObject; // The underlying HAPI object 51 52 /** Constructor for FiniteList 53 * @param repType the Class which is repeating 54 * @param underlyingObject the underlying object that the extending class represents 55 */ 56 public FiniteList(Class<? extends Repeatable> repType, Object underlyingObject) { 57 this.repType = repType; 58 this.underlyingObject = underlyingObject; 59 60 Repeatable firstRep = createRep(0); 61 this.maxReps = firstRep.getMaxReps(); 62 // this.minReps = firstRep.getMinReps(); 63 64 reps = new ArrayList<>(); 65 reps.add(firstRep); 66 createNewReps(maxReps); 67 } 68 69 /** Creates a new repetition 70 * @param rep the number representing the number of repetitions 71 */ 72 private void createNewReps(int rep) { 73 while (reps.size() <= rep) 74 reps.add(createRep(reps.size())); 75 } 76 77 /** Creates the repition 78 * @param rep the number representing which repition 79 */ 80 private Repeatable createRep(int rep) { 81 try { 82 Constructor<?> theCon = repType.getConstructors()[0]; 83 return (Repeatable) theCon.newInstance(new Object[] { underlyingObject, rep}); 84 } catch (InvocationTargetException e) { 85 throw new ConformanceError("Error creating underlying repetition. This is a bug.\nError is: " + e.toString() + "\n" + e.getCause().toString()); 86 } catch (Exception e) { 87 throw new ConformanceError("Error creating underlying repetition. This is a bug. Error is: " + e.toString()); 88 } 89 } 90 91 /** 92 * Returns the desired repetition 93 * @param rep The desired repetition number. Note that in accordance with the HL7 standard 94 * @return The desired repetition 95 * @throws ConformanceException if repetition is not accessible 96 */ 97 public Repeatable getRep(int rep) throws ConfRepException { 98 if (rep < 1 || (maxReps != -1 && maxReps < rep)) 99 throw new ConfRepException(maxReps, rep); 100 101 rep--; // Decremented because HL7 standard wants 1-offset arrays 102 createNewReps(rep); // Create new reps if needed 103 104 return reps.get(rep); 105 } 106 107 }