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