View Javadoc

1   /**
2    *
3    * The contents of this file are subject to the Mozilla Public License Version 1.1
4    * (the "License"); you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at http://www.mozilla.org/MPL
6    * Software distributed under the License is distributed on an "AS IS" basis,
7    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
8    * specific language governing rights and limitations under the License.
9    *
10   * The Initial Developer of the Original Code is University Health Network. Copyright (C)
11   * 2001.  All Rights Reserved.
12   *
13   * Alternatively, the contents of this file may be used under the terms of the
14   * GNU General Public License (the  "GPL"), in which case the provisions of the GPL are
15   * applicable instead of those above.  If you wish to allow use of your version of this
16   * file only under the terms of the GPL and not to allow others to use your version
17   * of this file under the MPL, indicate your decision by deleting  the provisions above
18   * and replace  them with the notice and other provisions required by the GPL License.
19   * If you do not delete the provisions above, a recipient may use your version of
20   * this file under either the MPL or the GPL.
21   */
22  package ca.uhn.hl7v2.testpanel.util.compare;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import ca.uhn.hl7v2.HL7Exception;
28  import ca.uhn.hl7v2.model.Group;
29  import ca.uhn.hl7v2.model.Segment;
30  import ca.uhn.hl7v2.model.Structure;
31  
32  public class GroupComparison extends StructureComparison {
33      //~ Instance fields ------------------------------------------------------------------------------------------------
34  
35      private ArrayList<SegmentComparison> myFlattened;
36      private Boolean mySame;
37      private List<StructureComparison> myGroupComparisons;
38  
39      //~ Constructors ---------------------------------------------------------------------------------------------------
40  
41      public GroupComparison(List<StructureComparison> theStructureComparison) {
42          myGroupComparisons = theStructureComparison;
43      }
44  
45      public GroupComparison(Group theExpected, Group theActual) {
46          assert (theExpected != null) || (theActual != null);
47          assert (theExpected == null) || (theActual == null);
48  
49          myGroupComparisons = new ArrayList<StructureComparison>();
50  
51          if (theExpected != null) {
52              addStructures(theExpected, true);
53          }
54  
55          if (theActual != null) {
56              addStructures(theActual, false);
57          }
58      }
59  
60      //~ Methods --------------------------------------------------------------------------------------------------------
61  
62      private void addStructures(Group theGroup, boolean theIsGroupExpected)
63                          throws Error {
64          for (String nextName : theGroup.getNames()) {
65              Structure[] allReps;
66  
67              try {
68                  allReps = theGroup.getAll(nextName);
69              } catch (HL7Exception e) {
70                  throw new Error("Unknown name: " + nextName, e);
71              }
72  
73              for (Structure nextStructure : allReps) {
74                  if (nextStructure instanceof Group) {
75                      if (theIsGroupExpected) {
76                          myGroupComparisons.add(new GroupComparison((Group) nextStructure, null));
77                      } else {
78                          myGroupComparisons.add(new GroupComparison(null, (Group) nextStructure));
79                      }
80                  } else {
81                      if (theIsGroupExpected) {
82                          myGroupComparisons.add(new SegmentComparison(
83                                                                       nextStructure.getName(),
84                                                                       (Segment) nextStructure,
85                                                                       null));
86                      } else {
87                          myGroupComparisons.add(new SegmentComparison(
88                                                                       nextStructure.getName(),
89                                                                       null,
90                                                                       (Segment) nextStructure));
91                      }
92                  }
93              }
94          }
95      }
96  
97      @Override
98      public List<SegmentComparison> flattenMessage() {
99          if (myFlattened == null) {
100             myFlattened = new ArrayList<SegmentComparison>();
101 
102             for (StructureComparison structureComparison : myGroupComparisons) {
103                 myFlattened.addAll(structureComparison.flattenMessage());
104             }
105         }
106 
107         return myFlattened;
108     }
109 
110     public List<StructureComparison> getGroupComparisons() {
111         return myGroupComparisons;
112     }
113 
114     public boolean isSame() {
115         if (mySame == null) {
116             mySame = true;
117 
118             for (SegmentComparison next : flattenMessage()) {
119                 if (next.getActualSegment() != null) {
120                     mySame = false;
121 
122                     break;
123                 } else if (next.getExpectSegment() != null) {
124                     mySame = false;
125 
126                     break;
127                 } else {
128                     for (FieldComparison nextField : next.getFieldComparisons()) {
129                         if (! nextField.isSame()) {
130                             mySame = false;
131 
132                             break;
133                         }
134                     }
135                 }
136             }
137         }
138 
139         return mySame;
140     }
141 }