001/* 002 * Copyright (C) 2008 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.primitives; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkElementIndex; 019import static com.google.common.base.Preconditions.checkNotNull; 020import static com.google.common.base.Preconditions.checkPositionIndexes; 021import static java.lang.Double.NEGATIVE_INFINITY; 022import static java.lang.Double.POSITIVE_INFINITY; 023 024import com.google.common.annotations.Beta; 025import com.google.common.annotations.GwtCompatible; 026import com.google.common.annotations.GwtIncompatible; 027import com.google.common.base.Converter; 028import java.io.Serializable; 029import java.util.AbstractList; 030import java.util.Arrays; 031import java.util.Collection; 032import java.util.Collections; 033import java.util.Comparator; 034import java.util.List; 035import java.util.RandomAccess; 036import java.util.Spliterator; 037import java.util.Spliterators; 038import org.checkerframework.checker.nullness.qual.Nullable; 039 040/** 041 * Static utility methods pertaining to {@code double} primitives, that are not already found in 042 * either {@link Double} or {@link Arrays}. 043 * 044 * <p>See the Guava User Guide article on <a 045 * href="https://github.com/google/guava/wiki/PrimitivesExplained">primitive utilities</a>. 046 * 047 * @author Kevin Bourrillion 048 * @since 1.0 049 */ 050@GwtCompatible(emulated = true) 051public final class Doubles { 052 private Doubles() {} 053 054 /** 055 * The number of bytes required to represent a primitive {@code double} value. 056 * 057 * <p><b>Java 8 users:</b> use {@link Double#BYTES} instead. 058 * 059 * @since 10.0 060 */ 061 public static final int BYTES = Double.SIZE / Byte.SIZE; 062 063 /** 064 * Returns a hash code for {@code value}; equal to the result of invoking {@code ((Double) 065 * value).hashCode()}. 066 * 067 * <p><b>Java 8 users:</b> use {@link Double#hashCode(double)} instead. 068 * 069 * @param value a primitive {@code double} value 070 * @return a hash code for the value 071 */ 072 public static int hashCode(double value) { 073 return ((Double) value).hashCode(); 074 // TODO(kevinb): do it this way when we can (GWT problem): 075 // long bits = Double.doubleToLongBits(value); 076 // return (int) (bits ^ (bits >>> 32)); 077 } 078 079 /** 080 * Compares the two specified {@code double} values. The sign of the value returned is the same as 081 * that of <code>((Double) a).{@linkplain Double#compareTo compareTo}(b)</code>. As with that 082 * method, {@code NaN} is treated as greater than all other values, and {@code 0.0 > -0.0}. 083 * 084 * <p><b>Note:</b> this method simply delegates to the JDK method {@link Double#compare}. It is 085 * provided for consistency with the other primitive types, whose compare methods were not added 086 * to the JDK until JDK 7. 087 * 088 * @param a the first {@code double} to compare 089 * @param b the second {@code double} to compare 090 * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is 091 * greater than {@code b}; or zero if they are equal 092 */ 093 public static int compare(double a, double b) { 094 return Double.compare(a, b); 095 } 096 097 /** 098 * Returns {@code true} if {@code value} represents a real number. This is equivalent to, but not 099 * necessarily implemented as, {@code !(Double.isInfinite(value) || Double.isNaN(value))}. 100 * 101 * <p><b>Java 8 users:</b> use {@link Double#isFinite(double)} instead. 102 * 103 * @since 10.0 104 */ 105 public static boolean isFinite(double value) { 106 return NEGATIVE_INFINITY < value && value < POSITIVE_INFINITY; 107 } 108 109 /** 110 * Returns {@code true} if {@code target} is present as an element anywhere in {@code array}. Note 111 * that this always returns {@code false} when {@code target} is {@code NaN}. 112 * 113 * @param array an array of {@code double} values, possibly empty 114 * @param target a primitive {@code double} value 115 * @return {@code true} if {@code array[i] == target} for some value of {@code i} 116 */ 117 public static boolean contains(double[] array, double target) { 118 for (double value : array) { 119 if (value == target) { 120 return true; 121 } 122 } 123 return false; 124 } 125 126 /** 127 * Returns the index of the first appearance of the value {@code target} in {@code array}. Note 128 * that this always returns {@code -1} when {@code target} is {@code NaN}. 129 * 130 * @param array an array of {@code double} values, possibly empty 131 * @param target a primitive {@code double} value 132 * @return the least index {@code i} for which {@code array[i] == target}, or {@code -1} if no 133 * such index exists. 134 */ 135 public static int indexOf(double[] array, double target) { 136 return indexOf(array, target, 0, array.length); 137 } 138 139 // TODO(kevinb): consider making this public 140 private static int indexOf(double[] array, double target, int start, int end) { 141 for (int i = start; i < end; i++) { 142 if (array[i] == target) { 143 return i; 144 } 145 } 146 return -1; 147 } 148 149 /** 150 * Returns the start position of the first occurrence of the specified {@code target} within 151 * {@code array}, or {@code -1} if there is no such occurrence. 152 * 153 * <p>More formally, returns the lowest index {@code i} such that {@code Arrays.copyOfRange(array, 154 * i, i + target.length)} contains exactly the same elements as {@code target}. 155 * 156 * <p>Note that this always returns {@code -1} when {@code target} contains {@code NaN}. 157 * 158 * @param array the array to search for the sequence {@code target} 159 * @param target the array to search for as a sub-sequence of {@code array} 160 */ 161 public static int indexOf(double[] array, double[] target) { 162 checkNotNull(array, "array"); 163 checkNotNull(target, "target"); 164 if (target.length == 0) { 165 return 0; 166 } 167 168 outer: 169 for (int i = 0; i < array.length - target.length + 1; i++) { 170 for (int j = 0; j < target.length; j++) { 171 if (array[i + j] != target[j]) { 172 continue outer; 173 } 174 } 175 return i; 176 } 177 return -1; 178 } 179 180 /** 181 * Returns the index of the last appearance of the value {@code target} in {@code array}. Note 182 * that this always returns {@code -1} when {@code target} is {@code NaN}. 183 * 184 * @param array an array of {@code double} values, possibly empty 185 * @param target a primitive {@code double} value 186 * @return the greatest index {@code i} for which {@code array[i] == target}, or {@code -1} if no 187 * such index exists. 188 */ 189 public static int lastIndexOf(double[] array, double target) { 190 return lastIndexOf(array, target, 0, array.length); 191 } 192 193 // TODO(kevinb): consider making this public 194 private static int lastIndexOf(double[] array, double target, int start, int end) { 195 for (int i = end - 1; i >= start; i--) { 196 if (array[i] == target) { 197 return i; 198 } 199 } 200 return -1; 201 } 202 203 /** 204 * Returns the least value present in {@code array}, using the same rules of comparison as {@link 205 * Math#min(double, double)}. 206 * 207 * @param array a <i>nonempty</i> array of {@code double} values 208 * @return the value present in {@code array} that is less than or equal to every other value in 209 * the array 210 * @throws IllegalArgumentException if {@code array} is empty 211 */ 212 public static double min(double... array) { 213 checkArgument(array.length > 0); 214 double min = array[0]; 215 for (int i = 1; i < array.length; i++) { 216 min = Math.min(min, array[i]); 217 } 218 return min; 219 } 220 221 /** 222 * Returns the greatest value present in {@code array}, using the same rules of comparison as 223 * {@link Math#max(double, double)}. 224 * 225 * @param array a <i>nonempty</i> array of {@code double} values 226 * @return the value present in {@code array} that is greater than or equal to every other value 227 * in the array 228 * @throws IllegalArgumentException if {@code array} is empty 229 */ 230 public static double max(double... array) { 231 checkArgument(array.length > 0); 232 double max = array[0]; 233 for (int i = 1; i < array.length; i++) { 234 max = Math.max(max, array[i]); 235 } 236 return max; 237 } 238 239 /** 240 * Returns the value nearest to {@code value} which is within the closed range {@code [min..max]}. 241 * 242 * <p>If {@code value} is within the range {@code [min..max]}, {@code value} is returned 243 * unchanged. If {@code value} is less than {@code min}, {@code min} is returned, and if {@code 244 * value} is greater than {@code max}, {@code max} is returned. 245 * 246 * @param value the {@code double} value to constrain 247 * @param min the lower bound (inclusive) of the range to constrain {@code value} to 248 * @param max the upper bound (inclusive) of the range to constrain {@code value} to 249 * @throws IllegalArgumentException if {@code min > max} 250 * @since 21.0 251 */ 252 @Beta 253 public static double constrainToRange(double value, double min, double max) { 254 checkArgument(min <= max, "min (%s) must be less than or equal to max (%s)", min, max); 255 return Math.min(Math.max(value, min), max); 256 } 257 258 /** 259 * Returns the values from each provided array combined into a single array. For example, {@code 260 * concat(new double[] {a, b}, new double[] {}, new double[] {c}} returns the array {@code {a, b, 261 * c}}. 262 * 263 * @param arrays zero or more {@code double} arrays 264 * @return a single array containing all the values from the source arrays, in order 265 */ 266 public static double[] concat(double[]... arrays) { 267 int length = 0; 268 for (double[] array : arrays) { 269 length += array.length; 270 } 271 double[] result = new double[length]; 272 int pos = 0; 273 for (double[] array : arrays) { 274 System.arraycopy(array, 0, result, pos, array.length); 275 pos += array.length; 276 } 277 return result; 278 } 279 280 private static final class DoubleConverter extends Converter<String, Double> 281 implements Serializable { 282 static final DoubleConverter INSTANCE = new DoubleConverter(); 283 284 @Override 285 protected Double doForward(String value) { 286 return Double.valueOf(value); 287 } 288 289 @Override 290 protected String doBackward(Double value) { 291 return value.toString(); 292 } 293 294 @Override 295 public String toString() { 296 return "Doubles.stringConverter()"; 297 } 298 299 private Object readResolve() { 300 return INSTANCE; 301 } 302 303 private static final long serialVersionUID = 1; 304 } 305 306 /** 307 * Returns a serializable converter object that converts between strings and doubles using {@link 308 * Double#valueOf} and {@link Double#toString()}. 309 * 310 * @since 16.0 311 */ 312 @Beta 313 public static Converter<String, Double> stringConverter() { 314 return DoubleConverter.INSTANCE; 315 } 316 317 /** 318 * Returns an array containing the same values as {@code array}, but guaranteed to be of a 319 * specified minimum length. If {@code array} already has a length of at least {@code minLength}, 320 * it is returned directly. Otherwise, a new array of size {@code minLength + padding} is 321 * returned, containing the values of {@code array}, and zeroes in the remaining places. 322 * 323 * @param array the source array 324 * @param minLength the minimum length the returned array must guarantee 325 * @param padding an extra amount to "grow" the array by if growth is necessary 326 * @throws IllegalArgumentException if {@code minLength} or {@code padding} is negative 327 * @return an array containing the values of {@code array}, with guaranteed minimum length {@code 328 * minLength} 329 */ 330 public static double[] ensureCapacity(double[] array, int minLength, int padding) { 331 checkArgument(minLength >= 0, "Invalid minLength: %s", minLength); 332 checkArgument(padding >= 0, "Invalid padding: %s", padding); 333 return (array.length < minLength) ? Arrays.copyOf(array, minLength + padding) : array; 334 } 335 336 /** 337 * Returns a string containing the supplied {@code double} values, converted to strings as 338 * specified by {@link Double#toString(double)}, and separated by {@code separator}. For example, 339 * {@code join("-", 1.0, 2.0, 3.0)} returns the string {@code "1.0-2.0-3.0"}. 340 * 341 * <p>Note that {@link Double#toString(double)} formats {@code double} differently in GWT 342 * sometimes. In the previous example, it returns the string {@code "1-2-3"}. 343 * 344 * @param separator the text that should appear between consecutive values in the resulting string 345 * (but not at the start or end) 346 * @param array an array of {@code double} values, possibly empty 347 */ 348 public static String join(String separator, double... array) { 349 checkNotNull(separator); 350 if (array.length == 0) { 351 return ""; 352 } 353 354 // For pre-sizing a builder, just get the right order of magnitude 355 StringBuilder builder = new StringBuilder(array.length * 12); 356 builder.append(array[0]); 357 for (int i = 1; i < array.length; i++) { 358 builder.append(separator).append(array[i]); 359 } 360 return builder.toString(); 361 } 362 363 /** 364 * Returns a comparator that compares two {@code double} arrays <a 365 * href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographically</a>. That is, it 366 * compares, using {@link #compare(double, double)}), the first pair of values that follow any 367 * common prefix, or when one array is a prefix of the other, treats the shorter array as the 368 * lesser. For example, {@code [] < [1.0] < [1.0, 2.0] < [2.0]}. 369 * 370 * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays 371 * support only identity equality), but it is consistent with {@link Arrays#equals(double[], 372 * double[])}. 373 * 374 * @since 2.0 375 */ 376 public static Comparator<double[]> lexicographicalComparator() { 377 return LexicographicalComparator.INSTANCE; 378 } 379 380 private enum LexicographicalComparator implements Comparator<double[]> { 381 INSTANCE; 382 383 @Override 384 public int compare(double[] left, double[] right) { 385 int minLength = Math.min(left.length, right.length); 386 for (int i = 0; i < minLength; i++) { 387 int result = Double.compare(left[i], right[i]); 388 if (result != 0) { 389 return result; 390 } 391 } 392 return left.length - right.length; 393 } 394 395 @Override 396 public String toString() { 397 return "Doubles.lexicographicalComparator()"; 398 } 399 } 400 401 /** 402 * Sorts the elements of {@code array} in descending order. 403 * 404 * <p>Note that this method uses the total order imposed by {@link Double#compare}, which treats 405 * all NaN values as equal and 0.0 as greater than -0.0. 406 * 407 * @since 23.1 408 */ 409 public static void sortDescending(double[] array) { 410 checkNotNull(array); 411 sortDescending(array, 0, array.length); 412 } 413 414 /** 415 * Sorts the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} 416 * exclusive in descending order. 417 * 418 * <p>Note that this method uses the total order imposed by {@link Double#compare}, which treats 419 * all NaN values as equal and 0.0 as greater than -0.0. 420 * 421 * @since 23.1 422 */ 423 public static void sortDescending(double[] array, int fromIndex, int toIndex) { 424 checkNotNull(array); 425 checkPositionIndexes(fromIndex, toIndex, array.length); 426 Arrays.sort(array, fromIndex, toIndex); 427 reverse(array, fromIndex, toIndex); 428 } 429 430 /** 431 * Reverses the elements of {@code array}. This is equivalent to {@code 432 * Collections.reverse(Doubles.asList(array))}, but is likely to be more efficient. 433 * 434 * @since 23.1 435 */ 436 public static void reverse(double[] array) { 437 checkNotNull(array); 438 reverse(array, 0, array.length); 439 } 440 441 /** 442 * Reverses the elements of {@code array} between {@code fromIndex} inclusive and {@code toIndex} 443 * exclusive. This is equivalent to {@code 444 * Collections.reverse(Doubles.asList(array).subList(fromIndex, toIndex))}, but is likely to be 445 * more efficient. 446 * 447 * @throws IndexOutOfBoundsException if {@code fromIndex < 0}, {@code toIndex > array.length}, or 448 * {@code toIndex > fromIndex} 449 * @since 23.1 450 */ 451 public static void reverse(double[] array, int fromIndex, int toIndex) { 452 checkNotNull(array); 453 checkPositionIndexes(fromIndex, toIndex, array.length); 454 for (int i = fromIndex, j = toIndex - 1; i < j; i++, j--) { 455 double tmp = array[i]; 456 array[i] = array[j]; 457 array[j] = tmp; 458 } 459 } 460 461 /** 462 * Returns an array containing each value of {@code collection}, converted to a {@code double} 463 * value in the manner of {@link Number#doubleValue}. 464 * 465 * <p>Elements are copied from the argument collection as if by {@code collection.toArray()}. 466 * Calling this method is as thread-safe as calling that method. 467 * 468 * @param collection a collection of {@code Number} instances 469 * @return an array containing the same values as {@code collection}, in the same order, converted 470 * to primitives 471 * @throws NullPointerException if {@code collection} or any of its elements is null 472 * @since 1.0 (parameter was {@code Collection<Double>} before 12.0) 473 */ 474 public static double[] toArray(Collection<? extends Number> collection) { 475 if (collection instanceof DoubleArrayAsList) { 476 return ((DoubleArrayAsList) collection).toDoubleArray(); 477 } 478 479 Object[] boxedArray = collection.toArray(); 480 int len = boxedArray.length; 481 double[] array = new double[len]; 482 for (int i = 0; i < len; i++) { 483 // checkNotNull for GWT (do not optimize) 484 array[i] = ((Number) checkNotNull(boxedArray[i])).doubleValue(); 485 } 486 return array; 487 } 488 489 /** 490 * Returns a fixed-size list backed by the specified array, similar to {@link 491 * Arrays#asList(Object[])}. The list supports {@link List#set(int, Object)}, but any attempt to 492 * set a value to {@code null} will result in a {@link NullPointerException}. 493 * 494 * <p>The returned list maintains the values, but not the identities, of {@code Double} objects 495 * written to or read from it. For example, whether {@code list.get(0) == list.get(0)} is true for 496 * the returned list is unspecified. 497 * 498 * <p>The returned list may have unexpected behavior if it contains {@code NaN}, or if {@code NaN} 499 * is used as a parameter to any of its methods. 500 * 501 * <p><b>Note:</b> when possible, you should represent your data as an {@link 502 * ImmutableDoubleArray} instead, which has an {@link ImmutableDoubleArray#asList asList} view. 503 * 504 * @param backingArray the array to back the list 505 * @return a list view of the array 506 */ 507 public static List<Double> asList(double... backingArray) { 508 if (backingArray.length == 0) { 509 return Collections.emptyList(); 510 } 511 return new DoubleArrayAsList(backingArray); 512 } 513 514 @GwtCompatible 515 private static class DoubleArrayAsList extends AbstractList<Double> 516 implements RandomAccess, Serializable { 517 final double[] array; 518 final int start; 519 final int end; 520 521 DoubleArrayAsList(double[] array) { 522 this(array, 0, array.length); 523 } 524 525 DoubleArrayAsList(double[] array, int start, int end) { 526 this.array = array; 527 this.start = start; 528 this.end = end; 529 } 530 531 @Override 532 public int size() { 533 return end - start; 534 } 535 536 @Override 537 public boolean isEmpty() { 538 return false; 539 } 540 541 @Override 542 public Double get(int index) { 543 checkElementIndex(index, size()); 544 return array[start + index]; 545 } 546 547 @Override 548 public Spliterator.OfDouble spliterator() { 549 return Spliterators.spliterator(array, start, end, 0); 550 } 551 552 @Override 553 public boolean contains(Object target) { 554 // Overridden to prevent a ton of boxing 555 return (target instanceof Double) 556 && Doubles.indexOf(array, (Double) target, start, end) != -1; 557 } 558 559 @Override 560 public int indexOf(Object target) { 561 // Overridden to prevent a ton of boxing 562 if (target instanceof Double) { 563 int i = Doubles.indexOf(array, (Double) target, start, end); 564 if (i >= 0) { 565 return i - start; 566 } 567 } 568 return -1; 569 } 570 571 @Override 572 public int lastIndexOf(Object target) { 573 // Overridden to prevent a ton of boxing 574 if (target instanceof Double) { 575 int i = Doubles.lastIndexOf(array, (Double) target, start, end); 576 if (i >= 0) { 577 return i - start; 578 } 579 } 580 return -1; 581 } 582 583 @Override 584 public Double set(int index, Double element) { 585 checkElementIndex(index, size()); 586 double oldValue = array[start + index]; 587 // checkNotNull for GWT (do not optimize) 588 array[start + index] = checkNotNull(element); 589 return oldValue; 590 } 591 592 @Override 593 public List<Double> subList(int fromIndex, int toIndex) { 594 int size = size(); 595 checkPositionIndexes(fromIndex, toIndex, size); 596 if (fromIndex == toIndex) { 597 return Collections.emptyList(); 598 } 599 return new DoubleArrayAsList(array, start + fromIndex, start + toIndex); 600 } 601 602 @Override 603 public boolean equals(@Nullable Object object) { 604 if (object == this) { 605 return true; 606 } 607 if (object instanceof DoubleArrayAsList) { 608 DoubleArrayAsList that = (DoubleArrayAsList) object; 609 int size = size(); 610 if (that.size() != size) { 611 return false; 612 } 613 for (int i = 0; i < size; i++) { 614 if (array[start + i] != that.array[that.start + i]) { 615 return false; 616 } 617 } 618 return true; 619 } 620 return super.equals(object); 621 } 622 623 @Override 624 public int hashCode() { 625 int result = 1; 626 for (int i = start; i < end; i++) { 627 result = 31 * result + Doubles.hashCode(array[i]); 628 } 629 return result; 630 } 631 632 @Override 633 public String toString() { 634 StringBuilder builder = new StringBuilder(size() * 12); 635 builder.append('[').append(array[start]); 636 for (int i = start + 1; i < end; i++) { 637 builder.append(", ").append(array[i]); 638 } 639 return builder.append(']').toString(); 640 } 641 642 double[] toDoubleArray() { 643 return Arrays.copyOfRange(array, start, end); 644 } 645 646 private static final long serialVersionUID = 0; 647 } 648 649 /** 650 * This is adapted from the regex suggested by {@link Double#valueOf(String)} for prevalidating 651 * inputs. All valid inputs must pass this regex, but it's semantically fine if not all inputs 652 * that pass this regex are valid -- only a performance hit is incurred, not a semantics bug. 653 */ 654 @GwtIncompatible // regular expressions 655 static final 656 java.util.regex.Pattern 657 FLOATING_POINT_PATTERN = fpPattern(); 658 659 @GwtIncompatible // regular expressions 660 private static 661 java.util.regex.Pattern 662 fpPattern() { 663 /* 664 * We use # instead of * for possessive quantifiers. This lets us strip them out when building 665 * the regex for RE2 (which doesn't support them) but leave them in when building it for 666 * java.util.regex (where we want them in order to avoid catastrophic backtracking). 667 */ 668 String decimal = "(?:\\d+#(?:\\.\\d*#)?|\\.\\d+#)"; 669 String completeDec = decimal + "(?:[eE][+-]?\\d+#)?[fFdD]?"; 670 String hex = "(?:[0-9a-fA-F]+#(?:\\.[0-9a-fA-F]*#)?|\\.[0-9a-fA-F]+#)"; 671 String completeHex = "0[xX]" + hex + "[pP][+-]?\\d+#[fFdD]?"; 672 String fpPattern = "[+-]?(?:NaN|Infinity|" + completeDec + "|" + completeHex + ")"; 673 fpPattern = 674 fpPattern.replace( 675 "#", 676 "+" 677 ); 678 return 679 java.util.regex.Pattern 680 .compile(fpPattern); 681 } 682 683 /** 684 * Parses the specified string as a double-precision floating point value. The ASCII character 685 * {@code '-'} (<code>'\u002D'</code>) is recognized as the minus sign. 686 * 687 * <p>Unlike {@link Double#parseDouble(String)}, this method returns {@code null} instead of 688 * throwing an exception if parsing fails. Valid inputs are exactly those accepted by {@link 689 * Double#valueOf(String)}, except that leading and trailing whitespace is not permitted. 690 * 691 * <p>This implementation is likely to be faster than {@code Double.parseDouble} if many failures 692 * are expected. 693 * 694 * @param string the string representation of a {@code double} value 695 * @return the floating point value represented by {@code string}, or {@code null} if {@code 696 * string} has a length of zero or cannot be parsed as a {@code double} value 697 * @throws NullPointerException if {@code string} is {@code null} 698 * @since 14.0 699 */ 700 @Beta 701 @GwtIncompatible // regular expressions 702 public static @Nullable Double tryParse(String string) { 703 if (FLOATING_POINT_PATTERN.matcher(string).matches()) { 704 // TODO(lowasser): could be potentially optimized, but only with 705 // extensive testing 706 try { 707 return Double.parseDouble(string); 708 } catch (NumberFormatException e) { 709 // Double.parseDouble has changed specs several times, so fall through 710 // gracefully 711 } 712 } 713 return null; 714 } 715}