Coverage Report - ca.uhn.hl7v2.parser.StructureDefinition
 
Classes in this File Line Coverage Branch Coverage Complexity
StructureDefinition
76%
73/95
60%
29/48
1.812
 
 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 "StructureDefinition.java".  Description: 
 10  
 "A definition element" 
 11  
 
 12  
 The Initial Developer of the Original Code is University Health Network. Copyright (C) 
 13  
 2001.  All Rights Reserved. 
 14  
 
 15  
 Contributor(s): ______________________________________. 
 16  
 
 17  
 Alternatively, the contents of this file may be used under the terms of the 
 18  
 GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
 19  
 applicable instead of those above.  If you wish to allow use of your version of this 
 20  
 file only under the terms of the GPL and not to allow others to use your version 
 21  
 of this file under the MPL, indicate your decision by deleting  the provisions above 
 22  
 and replace  them with the notice and other provisions required by the GPL License.  
 23  
 If you do not delete the provisions above, a recipient may use your version of 
 24  
 this file under either the MPL or the GPL. 
 25  
 
 26  
  */
 27  
 package ca.uhn.hl7v2.parser;
 28  
 
 29  
 import java.util.ArrayList;
 30  
 import java.util.HashSet;
 31  
 import java.util.Set;
 32  
 
 33  
 /**
 34  
  * Defines
 35  
  * 
 36  
  * @author James
 37  
  * 
 38  
  */
 39  46004
 public class StructureDefinition implements IStructureDefinition {
 40  
 
 41  
     private HashSet<String> myAllChildrenNames;
 42  
     private HashSet<String> myAllFirstLeafNames;
 43  26856
     private ArrayList<StructureDefinition> myChildren = new ArrayList<StructureDefinition>();
 44  
     private IStructureDefinition myFirstSibling;
 45  
     private boolean myFirstSiblingIsSet;
 46  
     private Boolean myIsFinalChildOfParent;
 47  
     private boolean myIsRepeating;
 48  
     private boolean myIsRequired;
 49  
     private boolean myIsSegment;
 50  
     private String myName;
 51  
     private String myNameAsItAppearsInParent;
 52  
     private Set<String> myNamesOfAllPossibleFollowingLeaves;
 53  
     private IStructureDefinition myNextLeaf;
 54  
     private IStructureDefinition myNextSibling;
 55  
     private IStructureDefinition myParent;
 56  
     private int myPosition;
 57  
         private boolean myChoiceElement;
 58  
 
 59  
 
 60  
     /**
 61  
      * Constructor
 62  
      */
 63  26856
     public StructureDefinition() {
 64  26856
     }
 65  
 
 66  
 
 67  
     /**
 68  
      * Setter
 69  
      */
 70  
     void addChild(StructureDefinition theChild) {
 71  25453
         myChildren.add(theChild);
 72  25453
     }
 73  
 
 74  
 
 75  
     /**
 76  
      * {@inheritDoc }
 77  
      */
 78  
     @Override
 79  
     public boolean equals(Object theObj) {
 80  0
         if (theObj == null || !(theObj instanceof StructureDefinition)) {
 81  0
             return false;
 82  
         }
 83  0
         StructureDefinition o = (StructureDefinition) theObj;
 84  0
         return o.myName.equals(myName) && o.myPosition == myPosition;
 85  
     }
 86  
 
 87  
 
 88  
     /**
 89  
      * {@inheritDoc }
 90  
      */
 91  
     public HashSet<String> getAllChildNames() {
 92  0
         if (myAllChildrenNames == null) {
 93  0
             myAllChildrenNames = new HashSet<String>();
 94  0
             for (IStructureDefinition next : myChildren) {
 95  0
                 myAllChildrenNames.add(next.getName());
 96  0
                 myAllChildrenNames.addAll(next.getAllChildNames());
 97  0
             }
 98  
         }
 99  
 
 100  0
         return myAllChildrenNames;
 101  
     }
 102  
 
 103  
 
 104  
     /**
 105  
      * {@inheritDoc }
 106  
      */
 107  
     public HashSet<String> getAllPossibleFirstChildren() {
 108  30904
         if (myAllFirstLeafNames == null) {
 109  9261
             myAllFirstLeafNames = new HashSet<String>();
 110  
             
 111  9261
             boolean hasChoice = false;
 112  9261
             for (IStructureDefinition next : myChildren) {
 113  5662
                 myAllFirstLeafNames.addAll(next.getAllPossibleFirstChildren());
 114  
                 
 115  5662
                 if (next.isChoiceElement()) {
 116  210
                         hasChoice = true;
 117  210
                         continue;
 118  5452
                 } else if (hasChoice) {
 119  35
                         break;
 120  
                 }
 121  
                 
 122  5417
                 if (next.isRequired()) {
 123  3944
                     break;
 124  
                 }
 125  1473
             }
 126  
 
 127  9261
             myAllFirstLeafNames.add(getName());
 128  
         }
 129  
 
 130  30904
         return myAllFirstLeafNames;
 131  
     }
 132  
 
 133  
 
 134  
     /**
 135  
      * {@inheritDoc }
 136  
      */
 137  
     public ArrayList<StructureDefinition> getChildren() {
 138  15100
         return myChildren;
 139  
     }
 140  
 
 141  
 
 142  
     /**
 143  
      * {@inheritDoc }
 144  
      */
 145  
     public IStructureDefinition getFirstChild() {
 146  8676
         return myChildren.get(0);
 147  
     }
 148  
 
 149  
 
 150  
     /**
 151  
      * {@inheritDoc }
 152  
      */
 153  
     public IStructureDefinition getFirstSibling() {
 154  0
         if (!myFirstSiblingIsSet) {
 155  0
             if (myParent == null) {
 156  0
                 myFirstSibling = null;
 157  0
             } else if (myParent.getChildren().get(0) == this) {
 158  0
                 myFirstSibling = null;
 159  
             } else {
 160  0
                 myFirstSibling = myParent.getChildren().get(0);
 161  
             }
 162  0
             myFirstSiblingIsSet = true;
 163  
         }
 164  
 
 165  0
         return myFirstSibling;
 166  
     }
 167  
 
 168  
 
 169  
     /**
 170  
      * {@inheritDoc }
 171  
      */
 172  
     public String getName() {
 173  50531
         return myName;
 174  
     }
 175  
 
 176  
 
 177  
     /**
 178  
      * {@inheritDoc}
 179  
      */
 180  
     public String getNameAsItAppearsInParent() {
 181  21345
         return myNameAsItAppearsInParent;
 182  
     }
 183  
 
 184  
 
 185  
     /**
 186  
      * {@inheritDoc }
 187  
      */
 188  
     public Set<String> getNamesOfAllPossibleFollowingLeaves() {
 189  30546
         if (myNamesOfAllPossibleFollowingLeaves != null) {
 190  15369
             return myNamesOfAllPossibleFollowingLeaves;
 191  
         }
 192  
 
 193  15177
         myNamesOfAllPossibleFollowingLeaves = new HashSet<String>();
 194  
 
 195  15177
         IStructureDefinition nextLeaf = getNextLeaf();
 196  15177
         if (nextLeaf != null) {
 197  14199
             myNamesOfAllPossibleFollowingLeaves.add(nextLeaf.getName());
 198  14199
             myNamesOfAllPossibleFollowingLeaves.addAll(nextLeaf.getNamesOfAllPossibleFollowingLeaves());
 199  
         }
 200  
 
 201  15176
         IStructureDefinition parent = myParent;
 202  47875
         while (parent != null) {
 203  32699
             if (parent.isRepeating()) {
 204  12813
                 myNamesOfAllPossibleFollowingLeaves.addAll(parent.getAllPossibleFirstChildren());
 205  
             }
 206  32699
             parent = parent.getParent();
 207  
         }
 208  
 
 209  15176
         return myNamesOfAllPossibleFollowingLeaves;
 210  
 
 211  
     }
 212  
 
 213  
 
 214  
     /**
 215  
      * {@inheritDoc }
 216  
      */
 217  
     public IStructureDefinition getNextLeaf() {
 218  31369
         return myNextLeaf;
 219  
     }
 220  
 
 221  
 
 222  
     /**
 223  
      * {@inheritDoc }
 224  
      */
 225  
     public IStructureDefinition getNextSibling() {
 226  15540
         if (myNextSibling != null) {
 227  8440
             return myNextSibling;
 228  
         }
 229  
 
 230  7100
         if (isFinalChildOfParent()) {
 231  0
             throw new IllegalStateException("Final child");
 232  
         }
 233  
 
 234  7100
         myNextSibling = myParent.getChildren().get(myPosition + 1);
 235  7100
         return myNextSibling;
 236  
     }
 237  
 
 238  
 
 239  
     /**
 240  
      * {@inheritDoc }
 241  
      */
 242  
     public IStructureDefinition getParent() {
 243  33582
         return myParent;
 244  
     }
 245  
 
 246  
 
 247  
     /**
 248  
      * {@inheritDoc }
 249  
      */
 250  
     public int getPosition() {
 251  365
         return myPosition;
 252  
     }
 253  
 
 254  
 
 255  
     /**
 256  
      * {@inheritDoc }
 257  
      */
 258  
     public boolean hasChildren() {
 259  43182
         return !myChildren.isEmpty();
 260  
     }
 261  
 
 262  
 
 263  
     /**
 264  
      * {@inheritDoc }
 265  
      */
 266  
     @Override
 267  
     public int hashCode() {
 268  0
         return 17 * myName.hashCode() * myPosition;
 269  
     }
 270  
 
 271  
 
 272  
     /**
 273  
      * {@inheritDoc }
 274  
      */
 275  
     public boolean isFinalChildOfParent() {
 276  25241
         if (myIsFinalChildOfParent != null) {
 277  17160
             return myIsFinalChildOfParent;
 278  
         }
 279  8081
         myIsFinalChildOfParent = myParent == null || (myPosition == (myParent.getChildren().size() - 1));
 280  8081
         return myIsFinalChildOfParent;
 281  
     }
 282  
 
 283  
 
 284  
     /**
 285  
      * {@inheritDoc }
 286  
      */
 287  
     public boolean isRepeating() {
 288  52461
         return myIsRepeating;
 289  
     }
 290  
 
 291  
 
 292  
     /**
 293  
      * {@inheritDoc }
 294  
      */
 295  
     public boolean isRequired() {
 296  5417
         return myIsRequired;
 297  
     }
 298  
 
 299  
 
 300  
     /**
 301  
      * {@inheritDoc }
 302  
      */
 303  
     public boolean isSegment() {
 304  62488
         return myIsSegment;
 305  
     }
 306  
 
 307  
 
 308  
     /**
 309  
      * Setter
 310  
      */
 311  
     void setName(String theName) {
 312  26856
         myName = theName;
 313  26856
     }
 314  
 
 315  
 
 316  
     /**
 317  
      * Setter
 318  
      */
 319  
     void setNameAsItAppearsInParent(String theName) {
 320  25453
         myNameAsItAppearsInParent = theName;
 321  25453
     }
 322  
 
 323  
 
 324  
     /**
 325  
      * Setter
 326  
      */
 327  
     void setNextLeaf(IStructureDefinition theNextLeaf) {
 328  20420
         myNextLeaf = theNextLeaf;
 329  20420
     }
 330  
 
 331  
 
 332  
     /**
 333  
      * Setter
 334  
      */
 335  
     void setParent(IStructureDefinition theParent) {
 336  25453
         myParent = theParent;
 337  25453
     }
 338  
 
 339  
 
 340  
     /**
 341  
      * Setter
 342  
      */
 343  
     void setPosition(int thePosition) {
 344  25453
         myPosition = thePosition;
 345  25453
     }
 346  
 
 347  
 
 348  
     /**
 349  
      * Setter
 350  
      */
 351  
     void setRepeating(boolean theIsRepeating) {
 352  25453
         myIsRepeating = theIsRepeating;
 353  25453
     }
 354  
 
 355  
 
 356  
     /**
 357  
      * Setter
 358  
      */
 359  
     void setRequired(boolean theIsRequired) {
 360  25453
         myIsRequired = theIsRequired;
 361  25453
     }
 362  
 
 363  
 
 364  
     /**
 365  
      * Setter
 366  
      */
 367  
     void setSegment(boolean theIsSegment) {
 368  26856
         myIsSegment = theIsSegment;
 369  26856
     }
 370  
 
 371  
 
 372  
     /**
 373  
      * {@inheritDoc }
 374  
      */
 375  
     @Override
 376  
     public String toString() {
 377  0
         return "StructureDefinition[" + getName() + "]";
 378  
     }
 379  
 
 380  
 
 381  
         /**
 382  
      * @param theChoiceElement true if the definition of this structure is a choice
 383  
          * @see ca.uhn.hl7v2.model.Group#isChoiceElement(String)
 384  
      */
 385  
         public void setChoiceElement(boolean theChoiceElement) {
 386  25453
                 myChoiceElement = theChoiceElement;
 387  25453
         }
 388  
 
 389  
 
 390  
         /**
 391  
          * @see ca.uhn.hl7v2.model.Group#isChoiceElement(String)
 392  
          */
 393  
         public boolean isChoiceElement() {
 394  5662
                 return myChoiceElement;
 395  
         }
 396  
 
 397  
 }