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.Collections;
26  import java.util.List;
27  
28  import ca.uhn.hl7v2.HL7Exception;
29  import ca.uhn.hl7v2.model.Group;
30  import ca.uhn.hl7v2.model.Segment;
31  import ca.uhn.hl7v2.model.Structure;
32  
33  public class ExtraGroup extends StructureComparison {
34      //~ Instance fields ------------------------------------------------------------------------------------------------
35  
36      private Group myGroup;
37      private boolean myMessage1;
38  
39      //~ Constructors ---------------------------------------------------------------------------------------------------
40  
41      public ExtraGroup(Group theGroup, boolean theMessage1) {
42          myGroup = theGroup;
43          myMessage1 = theMessage1;
44      }
45  
46      //~ Methods --------------------------------------------------------------------------------------------------------
47  
48      private List<SegmentComparison> flatten(Structure theStructure) {
49          if (theStructure instanceof Segment) {
50              if (myMessage1) {
51                  return Collections.singletonList((SegmentComparison) new SegmentComparison(
52                                                                                             theStructure.getName(),
53                                                                                             (Segment) theStructure,
54                                                                                             null));
55              } else {
56                  return Collections.singletonList((SegmentComparison) new SegmentComparison(
57                                                                                             theStructure.getName(),
58                                                                                             null,
59                                                                                             (Segment) theStructure));
60              }
61          }
62  
63          ArrayList<SegmentComparison> retVal = new ArrayList<SegmentComparison>();
64          Group group = (Group) theStructure;
65  
66          for (String nextName : group.getNames()) {
67              try {
68                  for (Structure nextRep : group.getAll(nextName)) {
69                      retVal.addAll(flatten(nextRep));
70                  }
71              } catch (HL7Exception e) {
72                  throw new Error(e);
73              }
74          }
75  
76          return retVal;
77      }
78  
79      @Override
80      public List<SegmentComparison> flattenMessage() {
81          return flatten(myGroup);
82      }
83  
84      public Group getGroup() {
85          return myGroup;
86      }
87  
88      public boolean isMessage1() {
89          return myMessage1;
90      }
91  }