| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 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 | |
|
| 35 | |
|
| 36 | |
|
| 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 | |
|
| 62 | |
|
| 63 | 26856 | public StructureDefinition() { |
| 64 | 26856 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
void addChild(StructureDefinition theChild) { |
| 71 | 25453 | myChildren.add(theChild); |
| 72 | 25453 | } |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 136 | |
|
| 137 | |
public ArrayList<StructureDefinition> getChildren() { |
| 138 | 15100 | return myChildren; |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
public IStructureDefinition getFirstChild() { |
| 146 | 8676 | return myChildren.get(0); |
| 147 | |
} |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 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 | |
|
| 171 | |
|
| 172 | |
public String getName() { |
| 173 | 50531 | return myName; |
| 174 | |
} |
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
public String getNameAsItAppearsInParent() { |
| 181 | 21345 | return myNameAsItAppearsInParent; |
| 182 | |
} |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 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 | |
|
| 216 | |
|
| 217 | |
public IStructureDefinition getNextLeaf() { |
| 218 | 31369 | return myNextLeaf; |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 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 | |
|
| 241 | |
|
| 242 | |
public IStructureDefinition getParent() { |
| 243 | 33582 | return myParent; |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
public int getPosition() { |
| 251 | 365 | return myPosition; |
| 252 | |
} |
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
public boolean hasChildren() { |
| 259 | 43182 | return !myChildren.isEmpty(); |
| 260 | |
} |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
@Override |
| 267 | |
public int hashCode() { |
| 268 | 0 | return 17 * myName.hashCode() * myPosition; |
| 269 | |
} |
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 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 | |
|
| 286 | |
|
| 287 | |
public boolean isRepeating() { |
| 288 | 52461 | return myIsRepeating; |
| 289 | |
} |
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | |
|
| 294 | |
|
| 295 | |
public boolean isRequired() { |
| 296 | 5417 | return myIsRequired; |
| 297 | |
} |
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
public boolean isSegment() { |
| 304 | 62488 | return myIsSegment; |
| 305 | |
} |
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
void setName(String theName) { |
| 312 | 26856 | myName = theName; |
| 313 | 26856 | } |
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
void setNameAsItAppearsInParent(String theName) { |
| 320 | 25453 | myNameAsItAppearsInParent = theName; |
| 321 | 25453 | } |
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
|
| 327 | |
void setNextLeaf(IStructureDefinition theNextLeaf) { |
| 328 | 20420 | myNextLeaf = theNextLeaf; |
| 329 | 20420 | } |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
void setParent(IStructureDefinition theParent) { |
| 336 | 25453 | myParent = theParent; |
| 337 | 25453 | } |
| 338 | |
|
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
void setPosition(int thePosition) { |
| 344 | 25453 | myPosition = thePosition; |
| 345 | 25453 | } |
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
void setRepeating(boolean theIsRepeating) { |
| 352 | 25453 | myIsRepeating = theIsRepeating; |
| 353 | 25453 | } |
| 354 | |
|
| 355 | |
|
| 356 | |
|
| 357 | |
|
| 358 | |
|
| 359 | |
void setRequired(boolean theIsRequired) { |
| 360 | 25453 | myIsRequired = theIsRequired; |
| 361 | 25453 | } |
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
|
| 366 | |
|
| 367 | |
void setSegment(boolean theIsSegment) { |
| 368 | 26856 | myIsSegment = theIsSegment; |
| 369 | 26856 | } |
| 370 | |
|
| 371 | |
|
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
@Override |
| 376 | |
public String toString() { |
| 377 | 0 | return "StructureDefinition[" + getName() + "]"; |
| 378 | |
} |
| 379 | |
|
| 380 | |
|
| 381 | |
|
| 382 | |
|
| 383 | |
|
| 384 | |
|
| 385 | |
public void setChoiceElement(boolean theChoiceElement) { |
| 386 | 25453 | myChoiceElement = theChoiceElement; |
| 387 | 25453 | } |
| 388 | |
|
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
public boolean isChoiceElement() { |
| 394 | 5662 | return myChoiceElement; |
| 395 | |
} |
| 396 | |
|
| 397 | |
} |