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  7712
 public class StructureDefinition implements IStructureDefinition {
 40  
 
 41  
     private HashSet<String> myAllChildrenNames;
 42  
     private HashSet<String> myAllFirstLeafNames;
 43  3986
     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  3986
     public StructureDefinition() {
 64  3986
     }
 65  
 
 66  
 
 67  
     /**
 68  
      * Setter
 69  
      */
 70  
     void addChild(StructureDefinition theChild) {
 71  3792
         myChildren.add(theChild);
 72  3792
     }
 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  5200
         if (myAllFirstLeafNames == null) {
 109  1421
             myAllFirstLeafNames = new HashSet<String>();
 110  
             
 111  1421
             boolean hasChoice = false;
 112  1421
             for (IStructureDefinition next : myChildren) {
 113  875
                 myAllFirstLeafNames.addAll(next.getAllPossibleFirstChildren());
 114  
                 
 115  875
                 if (next.isChoiceElement()) {
 116  42
                         hasChoice = true;
 117  42
                         continue;
 118  833
                 } else if (hasChoice) {
 119  7
                         break;
 120  
                 }
 121  
                 
 122  826
                 if (next.isRequired()) {
 123  600
                     break;
 124  
                 }
 125  226
             }
 126  
 
 127  1421
             myAllFirstLeafNames.add(getName());
 128  
         }
 129  
 
 130  5200
         return myAllFirstLeafNames;
 131  
     }
 132  
 
 133  
 
 134  
     /**
 135  
      * {@inheritDoc }
 136  
      */
 137  
     public ArrayList<StructureDefinition> getChildren() {
 138  2512
         return myChildren;
 139  
     }
 140  
 
 141  
 
 142  
     /**
 143  
      * {@inheritDoc }
 144  
      */
 145  
     public IStructureDefinition getFirstChild() {
 146  1565
         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  8647
         return myName;
 174  
     }
 175  
 
 176  
 
 177  
     /**
 178  
      * {@inheritDoc}
 179  
      */
 180  
     public String getNameAsItAppearsInParent() {
 181  3757
         return myNameAsItAppearsInParent;
 182  
     }
 183  
 
 184  
 
 185  
     /**
 186  
      * {@inheritDoc }
 187  
      */
 188  
     public Set<String> getNamesOfAllPossibleFollowingLeaves() {
 189  5315
         if (myNamesOfAllPossibleFollowingLeaves != null) {
 190  2800
             return myNamesOfAllPossibleFollowingLeaves;
 191  
         }
 192  
 
 193  2515
         myNamesOfAllPossibleFollowingLeaves = new HashSet<String>();
 194  
 
 195  2515
         IStructureDefinition nextLeaf = getNextLeaf();
 196  2515
         if (nextLeaf != null) {
 197  2358
             myNamesOfAllPossibleFollowingLeaves.add(nextLeaf.getName());
 198  2358
             myNamesOfAllPossibleFollowingLeaves.addAll(nextLeaf.getNamesOfAllPossibleFollowingLeaves());
 199  
         }
 200  
 
 201  2515
         IStructureDefinition parent = myParent;
 202  7842
         while (parent != null) {
 203  5327
             if (parent.isRepeating()) {
 204  2078
                 myNamesOfAllPossibleFollowingLeaves.addAll(parent.getAllPossibleFirstChildren());
 205  
             }
 206  5327
             parent = parent.getParent();
 207  
         }
 208  
 
 209  2515
         return myNamesOfAllPossibleFollowingLeaves;
 210  
 
 211  
     }
 212  
 
 213  
 
 214  
     /**
 215  
      * {@inheritDoc }
 216  
      */
 217  
     public IStructureDefinition getNextLeaf() {
 218  5444
         return myNextLeaf;
 219  
     }
 220  
 
 221  
 
 222  
     /**
 223  
      * {@inheritDoc }
 224  
      */
 225  
     public IStructureDefinition getNextSibling() {
 226  2842
         if (myNextSibling != null) {
 227  1657
             return myNextSibling;
 228  
         }
 229  
 
 230  1185
         if (isFinalChildOfParent()) {
 231  0
             throw new IllegalStateException("Final child");
 232  
         }
 233  
 
 234  1185
         myNextSibling = myParent.getChildren().get(myPosition + 1);
 235  1185
         return myNextSibling;
 236  
     }
 237  
 
 238  
 
 239  
     /**
 240  
      * {@inheritDoc }
 241  
      */
 242  
     public IStructureDefinition getParent() {
 243  5477
         return myParent;
 244  
     }
 245  
 
 246  
 
 247  
     /**
 248  
      * {@inheritDoc }
 249  
      */
 250  
     public int getPosition() {
 251  73
         return myPosition;
 252  
     }
 253  
 
 254  
 
 255  
     /**
 256  
      * {@inheritDoc }
 257  
      */
 258  
     public boolean hasChildren() {
 259  7857
         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  4483
         if (myIsFinalChildOfParent != null) {
 277  3142
             return myIsFinalChildOfParent;
 278  
         }
 279  1341
         myIsFinalChildOfParent = myParent == null || (myPosition == (myParent.getChildren().size() - 1));
 280  1341
         return myIsFinalChildOfParent;
 281  
     }
 282  
 
 283  
 
 284  
     /**
 285  
      * {@inheritDoc }
 286  
      */
 287  
     public boolean isRepeating() {
 288  8900
         return myIsRepeating;
 289  
     }
 290  
 
 291  
 
 292  
     /**
 293  
      * {@inheritDoc }
 294  
      */
 295  
     public boolean isRequired() {
 296  826
         return myIsRequired;
 297  
     }
 298  
 
 299  
 
 300  
     /**
 301  
      * {@inheritDoc }
 302  
      */
 303  
     public boolean isSegment() {
 304  11325
         return myIsSegment;
 305  
     }
 306  
 
 307  
 
 308  
     /**
 309  
      * Setter
 310  
      */
 311  
     void setName(String theName) {
 312  3986
         myName = theName;
 313  3986
     }
 314  
 
 315  
 
 316  
     /**
 317  
      * Setter
 318  
      */
 319  
     void setNameAsItAppearsInParent(String theName) {
 320  3792
         myNameAsItAppearsInParent = theName;
 321  3792
     }
 322  
 
 323  
 
 324  
     /**
 325  
      * Setter
 326  
      */
 327  
     void setNextLeaf(IStructureDefinition theNextLeaf) {
 328  3033
         myNextLeaf = theNextLeaf;
 329  3033
     }
 330  
 
 331  
 
 332  
     /**
 333  
      * Setter
 334  
      */
 335  
     void setParent(IStructureDefinition theParent) {
 336  3792
         myParent = theParent;
 337  3792
     }
 338  
 
 339  
 
 340  
     /**
 341  
      * Setter
 342  
      */
 343  
     void setPosition(int thePosition) {
 344  3792
         myPosition = thePosition;
 345  3792
     }
 346  
 
 347  
 
 348  
     /**
 349  
      * Setter
 350  
      */
 351  
     void setRepeating(boolean theIsRepeating) {
 352  3792
         myIsRepeating = theIsRepeating;
 353  3792
     }
 354  
 
 355  
 
 356  
     /**
 357  
      * Setter
 358  
      */
 359  
     void setRequired(boolean theIsRequired) {
 360  3792
         myIsRequired = theIsRequired;
 361  3792
     }
 362  
 
 363  
 
 364  
     /**
 365  
      * Setter
 366  
      */
 367  
     void setSegment(boolean theIsSegment) {
 368  3986
         myIsSegment = theIsSegment;
 369  3986
     }
 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  3792
                 myChoiceElement = theChoiceElement;
 387  3792
         }
 388  
 
 389  
 
 390  
         /**
 391  
          * @see ca.uhn.hl7v2.model.Group#isChoiceElement(String)
 392  
          */
 393  
         public boolean isChoiceElement() {
 394  875
                 return myChoiceElement;
 395  
         }
 396  
 
 397  
 }