@GwtCompatible(emulated=true) public final class Doubles extends Object
double
primitives, that are not already found in
either Double
or Arrays
.
See the Guava User Guide article on primitive utilities.
Modifier and Type | Field and Description |
---|---|
static int |
BYTES
The number of bytes required to represent a primitive
double value. |
Modifier and Type | Method and Description |
---|---|
static List<Double> |
asList(double... backingArray)
Returns a fixed-size list backed by the specified array, similar to
Arrays.asList(Object[]) . |
static int |
compare(double a,
double b)
Compares the two specified
double values. |
static double[] |
concat(double[]... arrays)
Returns the values from each provided array combined into a single array.
|
static double |
constrainToRange(double value,
double min,
double max)
Returns the value nearest to
value which is within the closed range [min..max] . |
static boolean |
contains(double[] array,
double target)
Returns
true if target is present as an element anywhere in array . |
static double[] |
ensureCapacity(double[] array,
int minLength,
int padding)
Returns an array containing the same values as
array , but guaranteed to be of a
specified minimum length. |
static int |
hashCode(double value)
Returns a hash code for
value ; equal to the result of invoking ((Double)
value).hashCode() . |
static int |
indexOf(double[] array,
double target)
Returns the index of the first appearance of the value
target in array . |
static int |
indexOf(double[] array,
double[] target)
Returns the start position of the first occurrence of the specified
target within
array , or -1 if there is no such occurrence. |
static boolean |
isFinite(double value)
Returns
true if value represents a real number. |
static String |
join(String separator,
double... array)
Returns a string containing the supplied
double values, converted to strings as
specified by Double.toString(double) , and separated by separator . |
static int |
lastIndexOf(double[] array,
double target)
Returns the index of the last appearance of the value
target in array . |
static Comparator<double[]> |
lexicographicalComparator()
Returns a comparator that compares two
double arrays lexicographically. |
static double |
max(double... array)
Returns the greatest value present in
array , using the same rules of comparison as
Math.max(double, double) . |
static double |
min(double... array)
Returns the least value present in
array , using the same rules of comparison as Math.min(double, double) . |
static void |
reverse(double[] array)
Reverses the elements of
array . |
static void |
reverse(double[] array,
int fromIndex,
int toIndex)
Reverses the elements of
array between fromIndex inclusive and toIndex
exclusive. |
static void |
sortDescending(double[] array)
Sorts the elements of
array in descending order. |
static void |
sortDescending(double[] array,
int fromIndex,
int toIndex)
Sorts the elements of
array between fromIndex inclusive and toIndex
exclusive in descending order. |
static Converter<String,Double> |
stringConverter()
Returns a serializable converter object that converts between strings and doubles using
Double.valueOf(java.lang.String) and Double.toString() . |
static double[] |
toArray(Collection<? extends Number> collection)
Returns an array containing each value of
collection , converted to a double
value in the manner of Number.doubleValue() . |
static @Nullable Double |
tryParse(String string)
Parses the specified string as a double-precision floating point value.
|
public static final int BYTES
double
value.
Java 8 users: use Double.BYTES
instead.
public static int hashCode(double value)
value
; equal to the result of invoking ((Double)
value).hashCode()
.
Java 8 users: use Double.hashCode(double)
instead.
value
- a primitive double
valuepublic static int compare(double a, double b)
double
values. The sign of the value returned is the same as
that of ((Double) a).compareTo(b)
. As with that
method, NaN
is treated as greater than all other values, and 0.0 > -0.0
.
Note: this method simply delegates to the JDK method Double.compare(double, double)
. It is
provided for consistency with the other primitive types, whose compare methods were not added
to the JDK until JDK 7.
a
- the first double
to compareb
- the second double
to comparea
is less than b
; a positive value if a
is
greater than b
; or zero if they are equalpublic static boolean isFinite(double value)
true
if value
represents a real number. This is equivalent to, but not
necessarily implemented as, !(Double.isInfinite(value) || Double.isNaN(value))
.
Java 8 users: use Double.isFinite(double)
instead.
public static boolean contains(double[] array, double target)
true
if target
is present as an element anywhere in array
. Note
that this always returns false
when target
is NaN
.array
- an array of double
values, possibly emptytarget
- a primitive double
valuetrue
if array[i] == target
for some value of i
public static int indexOf(double[] array, double target)
target
in array
. Note
that this always returns -1
when target
is NaN
.array
- an array of double
values, possibly emptytarget
- a primitive double
valuei
for which array[i] == target
, or -1
if no
such index exists.public static int indexOf(double[] array, double[] target)
target
within
array
, or -1
if there is no such occurrence.
More formally, returns the lowest index i
such that Arrays.copyOfRange(array,
i, i + target.length)
contains exactly the same elements as target
.
Note that this always returns -1
when target
contains NaN
.
array
- the array to search for the sequence target
target
- the array to search for as a sub-sequence of array
public static int lastIndexOf(double[] array, double target)
target
in array
. Note
that this always returns -1
when target
is NaN
.array
- an array of double
values, possibly emptytarget
- a primitive double
valuei
for which array[i] == target
, or -1
if no
such index exists.@GwtIncompatible(value="Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.") public static double min(double... array)
array
, using the same rules of comparison as Math.min(double, double)
.array
- a nonempty array of double
valuesarray
that is less than or equal to every other value in
the arrayIllegalArgumentException
- if array
is empty@GwtIncompatible(value="Available in GWT! Annotation is to avoid conflict with GWT specialization of base class.") public static double max(double... array)
array
, using the same rules of comparison as
Math.max(double, double)
.array
- a nonempty array of double
valuesarray
that is greater than or equal to every other value
in the arrayIllegalArgumentException
- if array
is empty@Beta public static double constrainToRange(double value, double min, double max)
value
which is within the closed range [min..max]
.
If value
is within the range [min..max]
, value
is returned
unchanged. If value
is less than min
, min
is returned, and if value
is greater than max
, max
is returned.
value
- the double
value to constrainmin
- the lower bound (inclusive) of the range to constrain value
tomax
- the upper bound (inclusive) of the range to constrain value
toIllegalArgumentException
- if min > max
public static double[] concat(double[]... arrays)
concat(new double[] {a, b}, new double[] {}, new double[] {c}
returns the array {a, b,
c}
.arrays
- zero or more double
arrays@Beta public static Converter<String,Double> stringConverter()
Double.valueOf(java.lang.String)
and Double.toString()
.public static double[] ensureCapacity(double[] array, int minLength, int padding)
array
, but guaranteed to be of a
specified minimum length. If array
already has a length of at least minLength
,
it is returned directly. Otherwise, a new array of size minLength + padding
is
returned, containing the values of array
, and zeroes in the remaining places.array
- the source arrayminLength
- the minimum length the returned array must guaranteepadding
- an extra amount to "grow" the array by if growth is necessaryarray
, with guaranteed minimum length minLength
IllegalArgumentException
- if minLength
or padding
is negativepublic static String join(String separator, double... array)
double
values, converted to strings as
specified by Double.toString(double)
, and separated by separator
. For example,
join("-", 1.0, 2.0, 3.0)
returns the string "1.0-2.0-3.0"
.
Note that Double.toString(double)
formats double
differently in GWT
sometimes. In the previous example, it returns the string "1-2-3"
.
separator
- the text that should appear between consecutive values in the resulting string
(but not at the start or end)array
- an array of double
values, possibly emptypublic static Comparator<double[]> lexicographicalComparator()
double
arrays lexicographically. That is, it
compares, using compare(double, double)
), the first pair of values that follow any
common prefix, or when one array is a prefix of the other, treats the shorter array as the
lesser. For example, [] < [1.0] < [1.0, 2.0] < [2.0]
.
The returned comparator is inconsistent with Object.equals(Object)
(since arrays
support only identity equality), but it is consistent with Arrays.equals(double[],
double[])
.
public static void sortDescending(double[] array)
array
in descending order.
Note that this method uses the total order imposed by Double.compare(double, double)
, which treats
all NaN values as equal and 0.0 as greater than -0.0.
public static void sortDescending(double[] array, int fromIndex, int toIndex)
array
between fromIndex
inclusive and toIndex
exclusive in descending order.
Note that this method uses the total order imposed by Double.compare(double, double)
, which treats
all NaN values as equal and 0.0 as greater than -0.0.
public static void reverse(double[] array)
array
. This is equivalent to Collections.reverse(Doubles.asList(array))
, but is likely to be more efficient.public static void reverse(double[] array, int fromIndex, int toIndex)
array
between fromIndex
inclusive and toIndex
exclusive. This is equivalent to Collections.reverse(Doubles.asList(array).subList(fromIndex, toIndex))
, but is likely to be
more efficient.IndexOutOfBoundsException
- if fromIndex < 0
, toIndex > array.length
, or
toIndex > fromIndex
public static double[] toArray(Collection<? extends Number> collection)
collection
, converted to a double
value in the manner of Number.doubleValue()
.
Elements are copied from the argument collection as if by collection.toArray()
.
Calling this method is as thread-safe as calling that method.
collection
- a collection of Number
instancescollection
, in the same order, converted
to primitivesNullPointerException
- if collection
or any of its elements is nullCollection<Double>
before 12.0)public static List<Double> asList(double... backingArray)
Arrays.asList(Object[])
. The list supports List.set(int, Object)
, but any attempt to
set a value to null
will result in a NullPointerException
.
The returned list maintains the values, but not the identities, of Double
objects
written to or read from it. For example, whether list.get(0) == list.get(0)
is true for
the returned list is unspecified.
The returned list may have unexpected behavior if it contains NaN
, or if NaN
is used as a parameter to any of its methods.
Note: when possible, you should represent your data as an ImmutableDoubleArray
instead, which has an asList
view.
backingArray
- the array to back the list@Beta @GwtIncompatible public static @Nullable Double tryParse(String string)
'-'
('\u002D'
) is recognized as the minus sign.
Unlike Double.parseDouble(String)
, this method returns null
instead of
throwing an exception if parsing fails. Valid inputs are exactly those accepted by Double.valueOf(String)
, except that leading and trailing whitespace is not permitted.
This implementation is likely to be faster than Double.parseDouble
if many failures
are expected.
string
- the string representation of a double
valuestring
, or null
if string
has a length of zero or cannot be parsed as a double
valueNullPointerException
- if string
is null
Copyright © 2010–2020. All rights reserved.