View Javadoc

1   package ca.uhn.hl7v2.util;
2   
3   import ca.uhn.hl7v2.model.DataTypeException;
4   import ca.uhn.hl7v2.HL7Exception;
5   import ca.uhn.hl7v2.model.*;
6   
7   /**
8    * Tools for copying data recurvisely from one message element into another.  Currently only Types are 
9    * supported.  
10   * @author Bryan Tripp
11   */
12  public class DeepCopy {
13      
14      /**
15       * Copies data from the "from" Type into the "to" Type.  Either Type may be 
16       * a Primitive, Composite, or Varies.  If a Varies is provided, the operation is 
17       * performed on the result of calling its getData() method.  A Primitive may be 
18       * copied into a Composite, in which case the value is copied into the first 
19       * component of the Composite.  A Composite may be copied into a Primitive, 
20       * in which case the first component is copied.  Given Composites with different 
21       * numbers of components, the first components are copied, up to the length 
22       * of the smaller one.
23       *
24       * @param from type to copy from
25       * @param to type to copy to
26       * @throws DataTypeException if the types are not compatible
27       */
28      public static void copy(Type from, Type to) throws DataTypeException {
29          for (int i = 1; i <= Terser.numComponents(from); i++) {
30              for (int j = 1; j <= Terser.numSubComponents(from, i); j++) {
31                  String val = Terser.getPrimitive(from, i, j).getValue();
32                  Terser.getPrimitive(to, i, j).setValue(val);
33              }
34          }
35      }
36  
37      
38      /**
39       * Copies contents from the source segment to the destination segment.  This 
40       * method calls copy(Type, Type) on each repetition of each field (see additional 
41       * behavioural description there).  An attempt is made to copy each repetition of 
42       * each field in the source segment, regardless of whether the corresponding 
43       * destination field is repeating or even exists.
44       *
45       * @param from the segment from which data are copied 
46       * @param to the segment into which data are copied
47       * @throws HL7Exception if an error occurred while copying
48       */
49      public static void copy(Segment from, Segment to) throws HL7Exception {
50          int n = from.numFields();
51          for (int i = 1; i <= n; i++) {
52              Type[] reps = from.getField(i);
53              for (int j = 0; j < reps.length; j++) {
54                  copy(reps[j], to.getField(i, j));
55              }
56          }
57      }
58  }