< prev index next >

src/java.base/share/classes/jdk/Version.java

Print this page
rev 14279 : [mq]: 8140281-deprecation-optional.get


 442         int min = Math.min(size, oSize);
 443         for (int i = 0; i < min; i++) {
 444             Integer val = version.get(i);
 445             Integer oVal = ob.version().get(i);
 446             if (val != oVal)
 447                 return val - oVal;
 448         }
 449         if (size != oSize)
 450             return size - oSize;
 451         return 0;
 452     }
 453 
 454     private int comparePre(Version ob) {
 455         Optional<String> oPre = ob.pre();
 456         if (!pre.isPresent()) {
 457             if (oPre.isPresent())
 458                 return 1;
 459         } else {
 460             if (!oPre.isPresent())
 461                 return -1;
 462             String val = pre.get();
 463             String oVal = oPre.get();
 464             if (val.matches("\\d+")) {
 465                 return (oVal.matches("\\d+")
 466                         ? (new BigInteger(val)).compareTo(new BigInteger(oVal))
 467                         : -1);
 468             } else {
 469                 return (oVal.matches("\\d+")
 470                         ? 1
 471                         : val.compareTo(oVal));
 472             }
 473         }
 474         return 0;
 475     }
 476 
 477     private int compareBuild(Version ob) {
 478         Optional<Integer> oBuild = ob.build();
 479         if (oBuild.isPresent()) {
 480             return (build.isPresent()
 481                    ? build.get().compareTo(oBuild.get())
 482                    : 1);
 483         } else if (build.isPresent()) {
 484             return -1;
 485         }
 486         return 0;
 487     }
 488 
 489     private int compareOpt(Version ob) {
 490         Optional<String> oOpt = ob.optional();
 491         if (!optional.isPresent()) {
 492             if (oOpt.isPresent())
 493                 return -1;
 494         } else {
 495             if (!oOpt.isPresent())
 496                 return 1;
 497             return optional.get().compareTo(oOpt.get());
 498         }
 499         return 0;
 500     }
 501 
 502     /**
 503      * Returns a string representation of this version.
 504      *
 505      * @return  The version string
 506      */
 507     @Override
 508     public String toString() {
 509         StringBuilder sb
 510             = new StringBuilder(version.stream()
 511                                 .map(Object::toString)
 512                                 .collect(Collectors.joining(".")));
 513         pre.ifPresent(v -> sb.append("-").append(v));
 514 
 515         if (build.isPresent()) {
 516             sb.append("+").append(build.get());
 517             if (optional.isPresent())
 518                 sb.append("-").append(optional.get());
 519         } else {
 520             if (optional.isPresent()) {
 521                 sb.append(pre.isPresent() ? "-" : "+-");
 522                 sb.append(optional.get());
 523             }
 524         }
 525 
 526         return sb.toString();
 527     }
 528 
 529     /**
 530      * Determines whether this {@code Version} is equal to another object.
 531      *
 532      * <p> Two {@code Version}s are equal if and only if they represent the
 533      * same version string.
 534      *
 535      * <p> This method satisfies the general contract of the {@link
 536      * Object#equals(Object) Object.equals} method. </p>
 537      *
 538      * @param  ob
 539      *         The object to which this {@code Version} is to be compared
 540      *
 541      * @return  {@code true} if, and only if, the given object is a {@code
 542      *          Version} that is identical to this {@code Version}




 442         int min = Math.min(size, oSize);
 443         for (int i = 0; i < min; i++) {
 444             Integer val = version.get(i);
 445             Integer oVal = ob.version().get(i);
 446             if (val != oVal)
 447                 return val - oVal;
 448         }
 449         if (size != oSize)
 450             return size - oSize;
 451         return 0;
 452     }
 453 
 454     private int comparePre(Version ob) {
 455         Optional<String> oPre = ob.pre();
 456         if (!pre.isPresent()) {
 457             if (oPre.isPresent())
 458                 return 1;
 459         } else {
 460             if (!oPre.isPresent())
 461                 return -1;
 462             String val = pre.getWhenPresent();
 463             String oVal = oPre.getWhenPresent();
 464             if (val.matches("\\d+")) {
 465                 return (oVal.matches("\\d+")
 466                         ? (new BigInteger(val)).compareTo(new BigInteger(oVal))
 467                         : -1);
 468             } else {
 469                 return (oVal.matches("\\d+")
 470                         ? 1
 471                         : val.compareTo(oVal));
 472             }
 473         }
 474         return 0;
 475     }
 476 
 477     private int compareBuild(Version ob) {
 478         Optional<Integer> oBuild = ob.build();
 479         if (oBuild.isPresent()) {
 480             return (build.isPresent()
 481                    ? build.getWhenPresent().compareTo(oBuild.getWhenPresent())
 482                    : 1);
 483         } else if (build.isPresent()) {
 484             return -1;
 485         }
 486         return 0;
 487     }
 488 
 489     private int compareOpt(Version ob) {
 490         Optional<String> oOpt = ob.optional();
 491         if (!optional.isPresent()) {
 492             if (oOpt.isPresent())
 493                 return -1;
 494         } else {
 495             if (!oOpt.isPresent())
 496                 return 1;
 497             return optional.getWhenPresent().compareTo(oOpt.getWhenPresent());
 498         }
 499         return 0;
 500     }
 501 
 502     /**
 503      * Returns a string representation of this version.
 504      *
 505      * @return  The version string
 506      */
 507     @Override
 508     public String toString() {
 509         StringBuilder sb
 510             = new StringBuilder(version.stream()
 511                                 .map(Object::toString)
 512                                 .collect(Collectors.joining(".")));
 513         pre.ifPresent(v -> sb.append("-").append(v));
 514 
 515         if (build.isPresent()) {
 516             sb.append("+").append(build.getWhenPresent());
 517             if (optional.isPresent())
 518                 sb.append("-").append(optional.getWhenPresent());
 519         } else {
 520             if (optional.isPresent()) {
 521                 sb.append(pre.isPresent() ? "-" : "+-");
 522                 sb.append(optional.getWhenPresent());
 523             }
 524         }
 525 
 526         return sb.toString();
 527     }
 528 
 529     /**
 530      * Determines whether this {@code Version} is equal to another object.
 531      *
 532      * <p> Two {@code Version}s are equal if and only if they represent the
 533      * same version string.
 534      *
 535      * <p> This method satisfies the general contract of the {@link
 536      * Object#equals(Object) Object.equals} method. </p>
 537      *
 538      * @param  ob
 539      *         The object to which this {@code Version} is to be compared
 540      *
 541      * @return  {@code true} if, and only if, the given object is a {@code
 542      *          Version} that is identical to this {@code Version}


< prev index next >