001package org.hl7.fhir.r4.model; 002 003import static org.apache.commons.lang3.StringUtils.defaultString; 004import static org.apache.commons.lang3.StringUtils.isBlank; 005import static org.apache.commons.lang3.StringUtils.isNotBlank; 006 007import java.math.BigDecimal; 008import java.util.UUID; 009 010import org.apache.commons.lang3.ObjectUtils; 011import org.apache.commons.lang3.StringUtils; 012import org.apache.commons.lang3.Validate; 013import org.apache.commons.lang3.builder.HashCodeBuilder; 014import org.hl7.fhir.instance.model.api.IBaseResource; 015import org.hl7.fhir.instance.model.api.IIdType; 016import org.hl7.fhir.instance.model.api.IPrimitiveType; 017 018/* 019 Copyright (c) 2011+, HL7, Inc. 020 All rights reserved. 021 022 Redistribution and use in source and binary forms, with or without modification, 023 are permitted provided that the following conditions are met: 024 025 * Redistributions of source code must retain the above copyright notice, this 026 list of conditions and the following disclaimer. 027 * Redistributions in binary form must reproduce the above copyright notice, 028 this list of conditions and the following disclaimer in the documentation 029 and/or other materials provided with the distribution. 030 * Neither the name of HL7 nor the names of its contributors may be used to 031 endorse or promote products derived from this software without specific 032 prior written permission. 033 034 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 035 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 036 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 037 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 038 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 039 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 040 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 041 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 043 POSSIBILITY OF SUCH DAMAGE. 044 045*/ 046 047/* 048Copyright (c) 2011+, HL7, Inc. 049All rights reserved. 050 051Redistribution and use in source and binary forms, with or without modification, 052are permitted provided that the following conditions are met: 053 054 * Redistributions of source code must retain the above copyright notice, this 055 list of conditions and the following disclaimer. 056 * Redistributions in binary form must reproduce the above copyright notice, 057 this list of conditions and the following disclaimer in the documentation 058 and/or other materials provided with the distribution. 059 * Neither the name of HL7 nor the names of its contributors may be used to 060 endorse or promote products derived from this software without specific 061 prior written permission. 062 063THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 064ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 065WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 066IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 067INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 068NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 069PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 070WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 071ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 072POSSIBILITY OF SUCH DAMAGE. 073 074*/ 075 076import ca.uhn.fhir.model.api.annotation.DatatypeDef; 077 078/** 079 * This class represents the logical identity for a resource, or as much of that 080 * identity is known. In FHIR, every resource must have a "logical ID" which is 081 * defined by the FHIR specification as: 082 * <p> 083 * <code> 084 * Any combination of upper or lower case ASCII letters ('A'..'Z', and 'a'..'z', numerals ('0'..'9'), '-' and '.', with a length limit of 64 characters. (This might be an integer, an un-prefixed OID, UUID or any other identifier pattern that meets these constraints.) 085 * </code> 086 * </p> 087 * <p> 088 * This class contains that logical ID, and can optionally also contain a 089 * relative or absolute URL representing the resource identity. For example, the 090 * following are all valid values for IdType, and all might represent the same 091 * resource: 092 * </p> 093 * <ul> 094 * <li><code>123</code> (just a resource's ID)</li> 095 * <li><code>Patient/123</code> (a relative identity)</li> 096 * <li><code>http://example.com/Patient/123 (an absolute identity)</code></li> 097 * <li> 098 * <code>http://example.com/Patient/123/_history/1 (an absolute identity with a version id)</code> 099 * </li> 100 * <li> 101 * <code>Patient/123/_history/1 (a relative identity with a version id)</code> 102 * </li> 103 * </ul> 104 * <p> 105 * Note that the 64 character limit applies only to the ID portion ("123" in the 106 * examples above). 107 * </p> 108 * <p> 109 * In most situations, you only need to populate the resource's ID (e.g. 110 * <code>123</code>) in resources you are constructing and the encoder will 111 * infer the rest from the context in which the object is being used. On the 112 * other hand, the parser will always try to populate the complete absolute 113 * identity on objects it creates as a convenience. 114 * </p> 115 * <p> 116 * Regex for ID: [a-z0-9\-\.]{1,36} 117 * </p> 118 */ 119@DatatypeDef(name = "id", profileOf = StringType.class) 120public final class IdType extends UriType implements IPrimitiveType<String>, IIdType { 121 public static final String URN_PREFIX = "urn:"; 122 123 /** 124 * This is the maximum length for the ID 125 */ 126 public static final int MAX_LENGTH = 64; // maximum length 127 128 private static final long serialVersionUID = 2L; 129 private String myBaseUrl; 130 private boolean myHaveComponentParts; 131 private String myResourceType; 132 private String myUnqualifiedId; 133 private String myUnqualifiedVersionId; 134 135 /** 136 * Create a new empty ID 137 */ 138 public IdType() { 139 super(); 140 } 141 142 /** 143 * Create a new ID, using a BigDecimal input. Uses 144 * {@link BigDecimal#toPlainString()} to generate the string representation. 145 */ 146 public IdType(BigDecimal thePid) { 147 if (thePid != null) { 148 setValue(toPlainStringWithNpeThrowIfNeeded(thePid)); 149 } else { 150 setValue(null); 151 } 152 } 153 154 /** 155 * Create a new ID using a long 156 */ 157 public IdType(long theId) { 158 setValue(Long.toString(theId)); 159 } 160 161 /** 162 * Create a new ID using a string. This String may contain a simple ID (e.g. 163 * "1234") or it may contain a complete URL 164 * (http://example.com/fhir/Patient/1234). 165 * <p> 166 * <p> 167 * <b>Description</b>: A whole number in the range 0 to 2^64-1 (optionally 168 * represented in hex), a uuid, an oid, or any other combination of lowercase 169 * letters, numerals, "-" and ".", with a length limit of 36 characters. 170 * </p> 171 * <p> 172 * regex: [a-z0-9\-\.]{1,36} 173 * </p> 174 */ 175 public IdType(String theValue) { 176 setValue(theValue); 177 } 178 179 /** 180 * Constructor 181 * 182 * @param theResourceType The resource type (e.g. "Patient") 183 * @param theIdPart The ID (e.g. "123") 184 */ 185 public IdType(String theResourceType, BigDecimal theIdPart) { 186 this(theResourceType, toPlainStringWithNpeThrowIfNeeded(theIdPart)); 187 } 188 189 /** 190 * Constructor 191 * 192 * @param theResourceType The resource type (e.g. "Patient") 193 * @param theIdPart The ID (e.g. "123") 194 */ 195 public IdType(String theResourceType, Long theIdPart) { 196 this(theResourceType, toPlainStringWithNpeThrowIfNeeded(theIdPart)); 197 } 198 199 /** 200 * Constructor 201 * 202 * @param theResourceType The resource type (e.g. "Patient") 203 * @param theId The ID (e.g. "123") 204 */ 205 public IdType(String theResourceType, String theId) { 206 this(theResourceType, theId, null); 207 } 208 209 /** 210 * Constructor 211 * 212 * @param theResourceType The resource type (e.g. "Patient") 213 * @param theId The ID (e.g. "123") 214 * @param theVersionId The version ID ("e.g. "456") 215 */ 216 public IdType(String theResourceType, String theId, String theVersionId) { 217 this(null, theResourceType, theId, theVersionId); 218 } 219 220 /** 221 * Constructor 222 * 223 * @param theBaseUrl The server base URL (e.g. "http://example.com/fhir") 224 * @param theResourceType The resource type (e.g. "Patient") 225 * @param theId The ID (e.g. "123") 226 * @param theVersionId The version ID ("e.g. "456") 227 */ 228 public IdType(String theBaseUrl, String theResourceType, String theId, String theVersionId) { 229 myBaseUrl = theBaseUrl; 230 myResourceType = theResourceType; 231 myUnqualifiedId = theId; 232 myUnqualifiedVersionId = StringUtils.defaultIfBlank(theVersionId, null); 233 myHaveComponentParts = true; 234 if (isBlank(myBaseUrl) && isBlank(myResourceType) && isBlank(myUnqualifiedId) && isBlank(myUnqualifiedVersionId)) { 235 myHaveComponentParts = false; 236 } 237 } 238 239 /** 240 * Creates an ID based on a given URL 241 */ 242 public IdType(UriType theUrl) { 243 setValue(theUrl.getValueAsString()); 244 } 245 246 public void applyTo(IBaseResource theResouce) { 247 if (theResouce == null) { 248 throw new NullPointerException("theResource can not be null"); 249 } else { 250 theResouce.setId(new IdType(getValue())); 251 } 252 } 253 254 @Override 255 public IdType copy() { 256 IdType ret = new IdType(getValue()); 257 copyValues(ret); 258 return ret; 259 } 260 261 @Override 262 public boolean equals(Object theArg0) { 263 if (!(theArg0 instanceof IdType)) { 264 return false; 265 } 266 return StringUtils.equals(getValueAsString(), ((IdType) theArg0).getValueAsString()); 267 } 268 269 /** 270 * Returns true if this IdType matches the given IdType in terms of resource 271 * type and ID, but ignores the URL base 272 */ 273 @SuppressWarnings("deprecation") 274 public boolean equalsIgnoreBase(IdType theId) { 275 if (theId == null) { 276 return false; 277 } 278 if (theId.isEmpty()) { 279 return isEmpty(); 280 } 281 return ObjectUtils.equals(getResourceType(), theId.getResourceType()) 282 && ObjectUtils.equals(getIdPart(), theId.getIdPart()) 283 && ObjectUtils.equals(getVersionIdPart(), theId.getVersionIdPart()); 284 } 285 286 public String fhirType() { 287 return "id"; 288 } 289 290 /** 291 * Returns the portion of this resource ID which corresponds to the server base 292 * URL. For example given the resource ID 293 * <code>http://example.com/fhir/Patient/123</code> the base URL would be 294 * <code>http://example.com/fhir</code>. 295 * <p> 296 * This method may return null if the ID contains no base (e.g. "Patient/123") 297 * </p> 298 */ 299 @Override 300 public String getBaseUrl() { 301 return myBaseUrl; 302 } 303 304 /** 305 * Returns only the logical ID part of this ID. For example, given the ID 306 * "http://example,.com/fhir/Patient/123/_history/456", this method would return 307 * "123". 308 */ 309 @Override 310 public String getIdPart() { 311 return myUnqualifiedId; 312 } 313 314 /** 315 * Returns the unqualified portion of this ID as a big decimal, or 316 * <code>null</code> if the value is null 317 * 318 * @throws NumberFormatException If the value is not a valid BigDecimal 319 */ 320 public BigDecimal getIdPartAsBigDecimal() { 321 String val = getIdPart(); 322 if (isBlank(val)) { 323 return null; 324 } 325 return new BigDecimal(val); 326 } 327 328 /** 329 * Returns the unqualified portion of this ID as a {@link Long}, or 330 * <code>null</code> if the value is null 331 * 332 * @throws NumberFormatException If the value is not a valid Long 333 */ 334 @Override 335 public Long getIdPartAsLong() { 336 String val = getIdPart(); 337 if (isBlank(val)) { 338 return null; 339 } 340 return Long.parseLong(val); 341 } 342 343 @Override 344 public String getResourceType() { 345 return myResourceType; 346 } 347 348 /** 349 * Returns the value of this ID. Note that this value may be a fully qualified 350 * URL, a relative/partial URL, or a simple ID. Use {@link #getIdPart()} to get 351 * just the ID portion. 352 * 353 * @see #getIdPart() 354 */ 355 @Override 356 public String getValue() { 357 String retVal = super.getValue(); 358 if (retVal == null && myHaveComponentParts) { 359 360 if (isLocal() || isUrn()) { 361 return myUnqualifiedId; 362 } 363 364 StringBuilder b = new StringBuilder(); 365 if (isNotBlank(myBaseUrl)) { 366 b.append(myBaseUrl); 367 if (myBaseUrl.charAt(myBaseUrl.length() - 1) != '/') { 368 b.append('/'); 369 } 370 } 371 372 if (isNotBlank(myResourceType)) { 373 b.append(myResourceType); 374 } 375 376 if (b.length() > 0 && isNotBlank(myUnqualifiedId)) { 377 b.append('/'); 378 } 379 380 if (isNotBlank(myUnqualifiedId)) { 381 b.append(myUnqualifiedId); 382 } else if (isNotBlank(myUnqualifiedVersionId)) { 383 b.append('/'); 384 } 385 386 if (isNotBlank(myUnqualifiedVersionId)) { 387 b.append('/'); 388 b.append("_history"); 389 b.append('/'); 390 b.append(myUnqualifiedVersionId); 391 } 392 retVal = b.toString(); 393 super.setValue(retVal); 394 } 395 return retVal; 396 } 397 398 /** 399 * Set the value 400 * <p> 401 * <p> 402 * <b>Description</b>: A whole number in the range 0 to 2^64-1 (optionally 403 * represented in hex), a uuid, an oid, or any other combination of lowercase 404 * letters, numerals, "-" and ".", with a length limit of 36 characters. 405 * </p> 406 * <p> 407 * regex: [a-z0-9\-\.]{1,36} 408 * </p> 409 */ 410 @Override 411 public IdType setValue(String theValue) { 412 // TODO: add validation 413 super.setValue(theValue); 414 myHaveComponentParts = false; 415 416 if (StringUtils.isBlank(theValue)) { 417 myBaseUrl = null; 418 super.setValue(null); 419 myUnqualifiedId = null; 420 myUnqualifiedVersionId = null; 421 myResourceType = null; 422 } else if (theValue.charAt(0) == '#' && theValue.length() > 1) { 423 super.setValue(theValue); 424 myBaseUrl = null; 425 myUnqualifiedId = theValue; 426 myUnqualifiedVersionId = null; 427 myResourceType = null; 428 myHaveComponentParts = true; 429 } else if (theValue.startsWith(URN_PREFIX)) { 430 myBaseUrl = null; 431 myUnqualifiedId = theValue; 432 myUnqualifiedVersionId = null; 433 myResourceType = null; 434 myHaveComponentParts = true; 435 } else { 436 int vidIndex = theValue.indexOf("/_history/"); 437 int idIndex; 438 if (vidIndex != -1) { 439 myUnqualifiedVersionId = theValue.substring(vidIndex + "/_history/".length()); 440 idIndex = theValue.lastIndexOf('/', vidIndex - 1); 441 myUnqualifiedId = theValue.substring(idIndex + 1, vidIndex); 442 } else { 443 idIndex = theValue.lastIndexOf('/'); 444 myUnqualifiedId = theValue.substring(idIndex + 1); 445 myUnqualifiedVersionId = null; 446 } 447 448 myBaseUrl = null; 449 if (idIndex <= 0) { 450 myResourceType = null; 451 } else { 452 int typeIndex = theValue.lastIndexOf('/', idIndex - 1); 453 if (typeIndex == -1) { 454 myResourceType = theValue.substring(0, idIndex); 455 } else { 456 if (typeIndex > 0 && '/' == theValue.charAt(typeIndex - 1)) { 457 typeIndex = theValue.indexOf('/', typeIndex + 1); 458 } 459 if (typeIndex >= idIndex) { 460 // e.g. http://example.org/foo 461 // 'foo' was the id but we're making that the resource type. Nullify the id part 462 // because we don't have an id. 463 // Also set null value to the super.setValue() and enable myHaveComponentParts 464 // so it forces getValue() to properly 465 // recreate the url 466 myResourceType = myUnqualifiedId; 467 myUnqualifiedId = null; 468 super.setValue(null); 469 myHaveComponentParts = true; 470 } else { 471 myResourceType = theValue.substring(typeIndex + 1, idIndex); 472 } 473 474 if (typeIndex > 4) { 475 myBaseUrl = theValue.substring(0, typeIndex); 476 } 477 478 } 479 } 480 481 } 482 return this; 483 } 484 485 @Override 486 public String getValueAsString() { 487 return getValue(); 488 } 489 490 @Override 491 public String asStringValue() { 492 return getValue(); 493 } 494 495 /** 496 * Set the value 497 * <p> 498 * <p> 499 * <b>Description</b>: A whole number in the range 0 to 2^64-1 (optionally 500 * represented in hex), a uuid, an oid, or any other combination of lowercase 501 * letters, numerals, "-" and ".", with a length limit of 36 characters. 502 * </p> 503 * <p> 504 * regex: [a-z0-9\-\.]{1,36} 505 * </p> 506 */ 507 @Override 508 public void setValueAsString(String theValue) { 509 setValue(theValue); 510 } 511 512 @Override 513 public String getVersionIdPart() { 514 return myUnqualifiedVersionId; 515 } 516 517 public Long getVersionIdPartAsLong() { 518 if (!hasVersionIdPart()) { 519 return null; 520 } else { 521 return Long.parseLong(getVersionIdPart()); 522 } 523 } 524 525 /** 526 * Returns true if this ID has a base url 527 * 528 * @see #getBaseUrl() 529 */ 530 public boolean hasBaseUrl() { 531 return isNotBlank(myBaseUrl); 532 } 533 534 @Override 535 public boolean hasIdPart() { 536 return isNotBlank(getIdPart()); 537 } 538 539 @Override 540 public boolean hasResourceType() { 541 return isNotBlank(myResourceType); 542 } 543 544 @Override 545 public boolean hasVersionIdPart() { 546 return isNotBlank(getVersionIdPart()); 547 } 548 549 @Override 550 public int hashCode() { 551 HashCodeBuilder b = new HashCodeBuilder(); 552 b.append(getValueAsString()); 553 return b.toHashCode(); 554 } 555 556 /** 557 * Returns <code>true</code> if this ID contains an absolute URL (in other 558 * words, a URL starting with "http://" or "https://" 559 */ 560 @Override 561 public boolean isAbsolute() { 562 if (StringUtils.isBlank(getValue())) { 563 return false; 564 } 565 return isUrlAbsolute(getValue()); 566 } 567 568 @Override 569 public boolean isEmpty() { 570 return isBlank(getValue()); 571 } 572 573 @Override 574 public boolean isIdPartValid() { 575 String id = getIdPart(); 576 if (StringUtils.isBlank(id)) { 577 return false; 578 } 579 if (id.length() > 64) { 580 return false; 581 } 582 for (int i = 0; i < id.length(); i++) { 583 char nextChar = id.charAt(i); 584 if (nextChar >= 'a' && nextChar <= 'z') { 585 continue; 586 } 587 if (nextChar >= 'A' && nextChar <= 'Z') { 588 continue; 589 } 590 if (nextChar >= '0' && nextChar <= '9') { 591 continue; 592 } 593 if (nextChar == '-' || nextChar == '.') { 594 continue; 595 } 596 return false; 597 } 598 return true; 599 } 600 601 /** 602 * Returns <code>true</code> if the unqualified ID is a valid {@link Long} value 603 * (in other words, it consists only of digits) 604 */ 605 @Override 606 public boolean isIdPartValidLong() { 607 return isValidLong(getIdPart()); 608 } 609 610 /** 611 * Returns <code>true</code> if the ID is a local reference (in other words, it 612 * begins with the '#' character) 613 */ 614 @Override 615 public boolean isLocal() { 616 return defaultString(myUnqualifiedId).startsWith("#"); 617 } 618 619 public boolean isUrn() { 620 return defaultString(myUnqualifiedId).startsWith(URN_PREFIX); 621 } 622 623 @Override 624 public boolean isVersionIdPartValidLong() { 625 return isValidLong(getVersionIdPart()); 626 } 627 628 @Override 629 public IIdType setParts(String theBaseUrl, String theResourceType, String theIdPart, String theVersionIdPart) { 630 if (isNotBlank(theVersionIdPart)) { 631 Validate.notBlank(theResourceType, 632 "If theVersionIdPart is populated, theResourceType and theIdPart must be populated"); 633 Validate.notBlank(theIdPart, "If theVersionIdPart is populated, theResourceType and theIdPart must be populated"); 634 } 635 if (isNotBlank(theBaseUrl) && isNotBlank(theIdPart)) { 636 Validate.notBlank(theResourceType, 637 "If theBaseUrl is populated and theIdPart is populated, theResourceType must be populated"); 638 } 639 640 setValue(null); 641 642 myBaseUrl = theBaseUrl; 643 myResourceType = theResourceType; 644 myUnqualifiedId = theIdPart; 645 myUnqualifiedVersionId = StringUtils.defaultIfBlank(theVersionIdPart, null); 646 myHaveComponentParts = true; 647 648 return this; 649 } 650 651 @Override 652 public String toString() { 653 return getValue(); 654 } 655 656 /** 657 * Returns a new IdType containing this IdType's values but with no server base 658 * URL if one is present in this IdType. For example, if this IdType contains 659 * the ID "http://foo/Patient/1", this method will return a new IdType 660 * containing ID "Patient/1". 661 */ 662 @Override 663 public IdType toUnqualified() { 664 if (isLocal() || isUrn()) { 665 return new IdType(getValueAsString()); 666 } 667 return new IdType(getResourceType(), getIdPart(), getVersionIdPart()); 668 } 669 670 @Override 671 public IdType toUnqualifiedVersionless() { 672 if (isLocal() || isUrn()) { 673 return new IdType(getValueAsString()); 674 } 675 return new IdType(getResourceType(), getIdPart()); 676 } 677 678 @Override 679 public IdType toVersionless() { 680 if (isLocal() || isUrn()) { 681 return new IdType(getValueAsString()); 682 } 683 return new IdType(getBaseUrl(), getResourceType(), getIdPart(), null); 684 } 685 686 @Override 687 public IdType withResourceType(String theResourceName) { 688 if (isLocal() || isUrn()) { 689 return new IdType(getValueAsString()); 690 } 691 return new IdType(theResourceName, getIdPart(), getVersionIdPart()); 692 } 693 694 /** 695 * Returns a view of this ID as a fully qualified URL, given a server base and 696 * resource name (which will only be used if the ID does not already contain 697 * those respective parts). Essentially, because IdType can contain either a 698 * complete URL or a partial one (or even jut a simple ID), this method may be 699 * used to translate into a complete URL. 700 * 701 * @param theServerBase The server base (e.g. "http://example.com/fhir") 702 * @param theResourceType The resource name (e.g. "Patient") 703 * @return A fully qualified URL for this ID (e.g. 704 * "http://example.com/fhir/Patient/1") 705 */ 706 @Override 707 public IdType withServerBase(String theServerBase, String theResourceType) { 708 if (isLocal() || isUrn()) { 709 return new IdType(getValueAsString()); 710 } 711 return new IdType(theServerBase, theResourceType, getIdPart(), getVersionIdPart()); 712 } 713 714 /** 715 * Creates a new instance of this ID which is identical, but refers to the 716 * specific version of this resource ID noted by theVersion. 717 * 718 * @param theVersion The actual version string, e.g. "1". If theVersion is blank 719 * or null, returns the same as {@link #toVersionless()}} 720 * @return A new instance of IdType which is identical, but refers to the 721 * specific version of this resource ID noted by theVersion. 722 */ 723 @Override 724 public IdType withVersion(String theVersion) { 725 if (isBlank(theVersion)) { 726 return toVersionless(); 727 } 728 729 if (isLocal() || isUrn()) { 730 return new IdType(getValueAsString()); 731 } 732 733 String existingValue = getValue(); 734 735 int i = existingValue.indexOf("_history"); 736 String value; 737 if (i > 1) { 738 value = existingValue.substring(0, i - 1); 739 } else { 740 value = existingValue; 741 } 742 743 return new IdType(value + '/' + "_history" + '/' + theVersion); 744 } 745 746 private static boolean isUrlAbsolute(String theValue) { 747 String value = theValue.toLowerCase(); 748 return value.startsWith("http://") || value.startsWith("https://"); 749 } 750 751 private static boolean isValidLong(String id) { 752 if (StringUtils.isBlank(id)) { 753 return false; 754 } 755 for (int i = 0; i < id.length(); i++) { 756 if (Character.isDigit(id.charAt(i)) == false) { 757 return false; 758 } 759 } 760 return true; 761 } 762 763 /** 764 * Construct a new ID with with form "urn:uuid:[UUID]" where [UUID] is a new, 765 * randomly created UUID generated by {@link UUID#randomUUID()} 766 */ 767 public static IdType newRandomUuid() { 768 return new IdType("urn:uuid:" + UUID.randomUUID().toString()); 769 } 770 771 /** 772 * Retrieves the ID from the given resource instance 773 */ 774 public static IdType of(IBaseResource theResouce) { 775 if (theResouce == null) { 776 throw new NullPointerException("theResource can not be null"); 777 } else { 778 IIdType retVal = theResouce.getIdElement(); 779 if (retVal == null) { 780 return null; 781 } else if (retVal instanceof IdType) { 782 return (IdType) retVal; 783 } else { 784 return new IdType(retVal.getValue()); 785 } 786 } 787 } 788 789 private static String toPlainStringWithNpeThrowIfNeeded(BigDecimal theIdPart) { 790 if (theIdPart == null) { 791 throw new NullPointerException("BigDecimal ID can not be null"); 792 } 793 return theIdPart.toPlainString(); 794 } 795 796 private static String toPlainStringWithNpeThrowIfNeeded(Long theIdPart) { 797 if (theIdPart == null) { 798 throw new NullPointerException("Long ID can not be null"); 799 } 800 return theIdPart.toString(); 801 } 802 803}