/** * Arrays.asList()的加强版, 返回一个底层为原始类型double的List * * 与保存Double相比节约空间,同时也避免了AutoBoxing. * * @see java.util.Arrays#asList(Object...) * @see com.google.common.primitives.Doubles#asList(double...) */ public static List<Double> doubleAsList(double... backingArray) { return Doubles.asList(backingArray); }
private static List<Double> asList(Double[] values) { double[] temp = new double[values.length]; for (int i = 0; i < values.length; i++) { temp[i] = checkNotNull(values[i]); // checkNotNull for GWT (do not optimize). } return Doubles.asList(temp); }
ManyValues(double[] values) { this.values = ImmutableList.copyOf(Doubles.asList(values)); }
@Override public void addTArgument(double... arg) { if (arg != null) addTArgument(Doubles.asList(arg).toArray(new Double[arg.length])); }
/** * Fails if the values in the subject were expected to be within the tolerance of the given * values but were not <i>or</i> if they were expected <i>not</i> to be within the tolerance but * were. The subject and tolerance are specified earlier in the fluent call chain. */ public void of(double... expected) { ofElementsIn(Doubles.asList(expected)); }
/** * As {@link #containsNoneOf(Object, Object, Object...)} but taking a primitive double array. */ public void containsNoneOf(double[] excluded) { containsNoneIn(Doubles.asList(excluded)); } }
public static IterableChecker<List<Double>, Double> check(double[] actualDoubleArray) { return check(actualDoubleArray == null ? null : Doubles.asList(actualDoubleArray)); }
/** As {@link #containsAnyOf(Object, Object, Object...)} but taking a primitive double array. */ public void containsAnyOf(double[] expected) { containsAnyIn(Doubles.asList(expected)); }
/** * Sets a new positional binding. * * @param value a double value or a list of double values to be set as a new positional binding */ public Builder<V> addBinding(double... value) { positionalBindings.add(toBinding(DoubleValue.MARSHALLER, Doubles.asList(value))); return this; }
private static IterableSubject assertThat(DoubleStream stream) { return Truth.assertThat(Doubles.asList(stream.toArray())); } }
public void testAsListEmpty() { assertSame(Collections.emptyList(), Doubles.asList(EMPTY)); }
public void testFuzzyEqualsZeroTolerance() { // make sure we test -0 tolerance for (double zero : Doubles.asList(0.0, -0.0)) { for (double a : ALL_DOUBLE_CANDIDATES) { for (double b : ALL_DOUBLE_CANDIDATES) { assertEquals( a == b || (Double.isNaN(a) && Double.isNaN(b)), DoubleMath.fuzzyEquals(a, b, zero)); } } } }
/** As {@link #containsExactly(Object...)} but taking a primitive double array. */ @CanIgnoreReturnValue public Ordered containsExactly(double[] expected) { return containsExactlyElementsIn(Doubles.asList(expected)); }
/** As {@link #containsAllOf(Object, Object, Object...)} but taking a primitive double array. */ @CanIgnoreReturnValue public Ordered containsAllOf(double[] expected) { return containsAllIn(Doubles.asList(expected)); }
public void testAsList_isAView() { double[] array = {(double) 0, (double) 1}; List<Double> list = Doubles.asList(array); list.set(0, (double) 2); assertTrue(Arrays.equals(new double[] {(double) 2, (double) 1}, array)); array[1] = (double) 3; assertThat(list).containsExactly((double) 2, (double) 3).inOrder(); }
public void testAsList_toArray_roundTrip() { double[] array = {(double) 0, (double) 1, (double) 2}; List<Double> list = Doubles.asList(array); double[] newArray = Doubles.toArray(list); // Make sure it returned a copy list.set(0, (double) 4); assertTrue(Arrays.equals(new double[] {(double) 0, (double) 1, (double) 2}, newArray)); newArray[1] = (double) 5; assertEquals((double) 1, (double) list.get(1)); }
public void testAsList_subList_toArray_roundTrip() { double[] array = {(double) 0, (double) 1, (double) 2, (double) 3}; List<Double> list = Doubles.asList(array); assertTrue( Arrays.equals(new double[] {(double) 1, (double) 2}, Doubles.toArray(list.subList(1, 3)))); assertTrue(Arrays.equals(new double[] {}, Doubles.toArray(list.subList(2, 2)))); }
public void testToArray() { // need explicit type parameter to avoid javac warning!? List<Double> none = Arrays.<Double>asList(); assertTrue(Arrays.equals(EMPTY, Doubles.toArray(none))); List<Double> one = Arrays.asList((double) 1); assertTrue(Arrays.equals(ARRAY1, Doubles.toArray(one))); double[] array = {(double) 0, (double) 1, Math.PI}; List<Double> three = Arrays.asList((double) 0, (double) 1, Math.PI); assertTrue(Arrays.equals(array, Doubles.toArray(three))); assertTrue(Arrays.equals(array, Doubles.toArray(Doubles.asList(array)))); }
public void testToArray_threadSafe() { for (int delta : new int[] {+1, 0, -1}) { for (int i = 0; i < VALUES.length; i++) { List<Double> list = Doubles.asList(VALUES).subList(0, i); Collection<Double> misleadingSize = Helpers.misleadingSizeCollection(delta); misleadingSize.addAll(list); double[] arr = Doubles.toArray(misleadingSize); assertEquals(i, arr.length); for (int j = 0; j < i; j++) { assertEquals(VALUES[j], arr[j]); } } } }
private IterableSubject iterableSubject() { return checkNoNeedToDisplayBothValues("asList()") .about(iterablesWithCustomDoubleToString()) .that(Doubles.asList(actual())); }