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