Skip to content

Commit c6c2680

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Expose more Java 8 APIs to Android users.
RELNOTES=Exposed some additional Java 8 APIs to Android users. PiperOrigin-RevId: 689846217
1 parent 984f713 commit c6c2680

File tree

17 files changed

+548
-22
lines changed

17 files changed

+548
-22
lines changed

android/guava-tests/test/com/google/common/primitives/ImmutableDoubleArrayTest.java

+37-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.common.primitives.TestPlatform.reduceIterationsIfGwt;
1919
import static com.google.common.testing.SerializableTester.reserialize;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static java.util.Arrays.stream;
2122

2223
import com.google.common.annotations.GwtCompatible;
2324
import com.google.common.annotations.GwtIncompatible;
@@ -38,6 +39,7 @@
3839
import java.util.List;
3940
import java.util.Random;
4041
import java.util.concurrent.atomic.AtomicInteger;
42+
import java.util.stream.DoubleStream;
4143
import junit.framework.Test;
4244
import junit.framework.TestCase;
4345
import junit.framework.TestSuite;
@@ -46,7 +48,6 @@
4648
* @author Kevin Bourrillion
4749
*/
4850
@GwtCompatible(emulated = true)
49-
@ElementTypesAreNonnullByDefault
5051
public class ImmutableDoubleArrayTest extends TestCase {
5152
// Test all creation paths very lazily: by assuming asList() works
5253

@@ -142,6 +143,14 @@ public void testCopyOf_collection_nonempty() {
142143
assertThat(iia.asList()).containsExactly(0.0, 1.0, 3.0).inOrder();
143144
}
144145

146+
public void testCopyOf_stream() {
147+
assertThat(ImmutableDoubleArray.copyOf(DoubleStream.empty()))
148+
.isSameInstanceAs(ImmutableDoubleArray.of());
149+
assertThat(ImmutableDoubleArray.copyOf(DoubleStream.of(0, 1, 3)).asList())
150+
.containsExactly(0.0, 1.0, 3.0)
151+
.inOrder();
152+
}
153+
145154
public void testBuilder_presize_zero() {
146155
ImmutableDoubleArray.Builder builder = ImmutableDoubleArray.builder(0);
147156
builder.add(5.0);
@@ -211,6 +220,16 @@ void doIt(ImmutableDoubleArray.Builder builder, AtomicInteger counter) {
211220
builder.addAll(iterable(list));
212221
}
213222
},
223+
ADD_STREAM {
224+
@Override
225+
void doIt(ImmutableDoubleArray.Builder builder, AtomicInteger counter) {
226+
double[] array = new double[RANDOM.nextInt(10)];
227+
for (int i = 0; i < array.length; i++) {
228+
array[i] = counter.getAndIncrement();
229+
}
230+
builder.addAll(stream(array));
231+
}
232+
},
214233
ADD_IIA {
215234
@Override
216235
void doIt(ImmutableDoubleArray.Builder builder, AtomicInteger counter) {
@@ -316,6 +335,23 @@ public void testContains() {
316335
assertThat(iia.subArray(1, 5).contains(1)).isTrue();
317336
}
318337

338+
public void testForEach() {
339+
ImmutableDoubleArray.of().forEach(i -> fail());
340+
ImmutableDoubleArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());
341+
342+
AtomicInteger count = new AtomicInteger(0);
343+
ImmutableDoubleArray.of(0, 1, 2, 3)
344+
.forEach(i -> assertThat(i).isEqualTo((double) count.getAndIncrement()));
345+
assertThat(count.get()).isEqualTo(4);
346+
}
347+
348+
public void testStream() {
349+
ImmutableDoubleArray.of().stream().forEach(i -> fail());
350+
ImmutableDoubleArray.of(0, 1, 3).subArray(1, 1).stream().forEach(i -> fail());
351+
assertThat(ImmutableDoubleArray.of(0, 1, 3).stream().toArray())
352+
.isEqualTo(new double[] {0, 1, 3});
353+
}
354+
319355
public void testSubArray() {
320356
ImmutableDoubleArray iia0 = ImmutableDoubleArray.of();
321357
ImmutableDoubleArray iia1 = ImmutableDoubleArray.of(5);

android/guava-tests/test/com/google/common/primitives/ImmutableIntArrayTest.java

+35
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.common.primitives.TestPlatform.reduceIterationsIfGwt;
1919
import static com.google.common.testing.SerializableTester.reserialize;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static java.util.Arrays.stream;
2122

2223
import com.google.common.annotations.GwtCompatible;
2324
import com.google.common.annotations.GwtIncompatible;
@@ -38,6 +39,7 @@
3839
import java.util.List;
3940
import java.util.Random;
4041
import java.util.concurrent.atomic.AtomicInteger;
42+
import java.util.stream.IntStream;
4143
import junit.framework.Test;
4244
import junit.framework.TestCase;
4345
import junit.framework.TestSuite;
@@ -136,6 +138,14 @@ public void testCopyOf_collection_nonempty() {
136138
assertThat(iia.asList()).containsExactly(0, 1, 3).inOrder();
137139
}
138140

141+
public void testCopyOf_stream() {
142+
assertThat(ImmutableIntArray.copyOf(IntStream.empty()))
143+
.isSameInstanceAs(ImmutableIntArray.of());
144+
assertThat(ImmutableIntArray.copyOf(IntStream.of(0, 1, 3)).asList())
145+
.containsExactly(0, 1, 3)
146+
.inOrder();
147+
}
148+
139149
public void testBuilder_presize_zero() {
140150
ImmutableIntArray.Builder builder = ImmutableIntArray.builder(0);
141151
builder.add(5);
@@ -205,6 +215,16 @@ void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
205215
builder.addAll(iterable(list));
206216
}
207217
},
218+
ADD_STREAM {
219+
@Override
220+
void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
221+
int[] array = new int[RANDOM.nextInt(10)];
222+
for (int i = 0; i < array.length; i++) {
223+
array[i] = counter.getAndIncrement();
224+
}
225+
builder.addAll(stream(array));
226+
}
227+
},
208228
ADD_IIA {
209229
@Override
210230
void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
@@ -300,6 +320,21 @@ public void testContains() {
300320
assertThat(iia.subArray(1, 5).contains(1)).isTrue();
301321
}
302322

323+
public void testForEach() {
324+
ImmutableIntArray.of().forEach(i -> fail());
325+
ImmutableIntArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());
326+
327+
AtomicInteger count = new AtomicInteger(0);
328+
ImmutableIntArray.of(0, 1, 2, 3).forEach(i -> assertThat(i).isEqualTo(count.getAndIncrement()));
329+
assertThat(count.get()).isEqualTo(4);
330+
}
331+
332+
public void testStream() {
333+
ImmutableIntArray.of().stream().forEach(i -> fail());
334+
ImmutableIntArray.of(0, 1, 3).subArray(1, 1).stream().forEach(i -> fail());
335+
assertThat(ImmutableIntArray.of(0, 1, 3).stream().toArray()).isEqualTo(new int[] {0, 1, 3});
336+
}
337+
303338
public void testSubArray() {
304339
ImmutableIntArray iia0 = ImmutableIntArray.of();
305340
ImmutableIntArray iia1 = ImmutableIntArray.of(5);

android/guava-tests/test/com/google/common/primitives/ImmutableLongArrayTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.common.primitives.TestPlatform.reduceIterationsIfGwt;
1919
import static com.google.common.testing.SerializableTester.reserialize;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static java.util.Arrays.stream;
2122

2223
import com.google.common.annotations.GwtCompatible;
2324
import com.google.common.annotations.GwtIncompatible;
@@ -38,6 +39,7 @@
3839
import java.util.List;
3940
import java.util.Random;
4041
import java.util.concurrent.atomic.AtomicLong;
42+
import java.util.stream.LongStream;
4143
import junit.framework.Test;
4244
import junit.framework.TestCase;
4345
import junit.framework.TestSuite;
@@ -138,6 +140,14 @@ public void testCopyOf_collection_nonempty() {
138140
assertThat(iia.asList()).containsExactly(0L, 1L, 3L).inOrder();
139141
}
140142

143+
public void testCopyOf_stream() {
144+
assertThat(ImmutableLongArray.copyOf(LongStream.empty()))
145+
.isSameInstanceAs(ImmutableLongArray.of());
146+
assertThat(ImmutableLongArray.copyOf(LongStream.of(0, 1, 3)).asList())
147+
.containsExactly(0L, 1L, 3L)
148+
.inOrder();
149+
}
150+
141151
public void testBuilder_presize_zero() {
142152
ImmutableLongArray.Builder builder = ImmutableLongArray.builder(0);
143153
builder.add(5L);
@@ -207,6 +217,16 @@ void doIt(ImmutableLongArray.Builder builder, AtomicLong counter) {
207217
builder.addAll(iterable(list));
208218
}
209219
},
220+
ADD_STREAM {
221+
@Override
222+
void doIt(ImmutableLongArray.Builder builder, AtomicLong counter) {
223+
long[] array = new long[RANDOM.nextInt(10)];
224+
for (int i = 0; i < array.length; i++) {
225+
array[i] = counter.getAndIncrement();
226+
}
227+
builder.addAll(stream(array));
228+
}
229+
},
210230
ADD_IIA {
211231
@Override
212232
void doIt(ImmutableLongArray.Builder builder, AtomicLong counter) {
@@ -302,6 +322,22 @@ public void testContains() {
302322
assertThat(iia.subArray(1, 5).contains(1)).isTrue();
303323
}
304324

325+
public void testForEach() {
326+
ImmutableLongArray.of().forEach(i -> fail());
327+
ImmutableLongArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());
328+
329+
AtomicLong count = new AtomicLong(0);
330+
ImmutableLongArray.of(0, 1, 2, 3)
331+
.forEach(i -> assertThat(i).isEqualTo(count.getAndIncrement()));
332+
assertThat(count.get()).isEqualTo(4);
333+
}
334+
335+
public void testStream() {
336+
ImmutableLongArray.of().stream().forEach(i -> fail());
337+
ImmutableLongArray.of(0, 1, 3).subArray(1, 1).stream().forEach(i -> fail());
338+
assertThat(ImmutableLongArray.of(0, 1, 3).stream().toArray()).isEqualTo(new long[] {0, 1, 3});
339+
}
340+
305341
public void testSubArray() {
306342
ImmutableLongArray iia0 = ImmutableLongArray.of();
307343
ImmutableLongArray iia1 = ImmutableLongArray.of(5);

android/guava/src/com/google/common/primitives/Doubles.java

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.Comparator;
3535
import java.util.List;
3636
import java.util.RandomAccess;
37+
import java.util.Spliterator;
38+
import java.util.Spliterators;
3739
import javax.annotation.CheckForNull;
3840

3941
/**
@@ -616,6 +618,17 @@ public Double get(int index) {
616618
return array[start + index];
617619
}
618620

621+
@Override
622+
@SuppressWarnings("Java7ApiChecker")
623+
/*
624+
* This is an override that is not directly visible to callers, so NewApi will catch calls to
625+
* Collection.spliterator() where necessary.
626+
*/
627+
@IgnoreJRERequirement
628+
public Spliterator.OfDouble spliterator() {
629+
return Spliterators.spliterator(array, start, end, 0);
630+
}
631+
619632
@Override
620633
public boolean contains(@CheckForNull Object target) {
621634
// Overridden to prevent a ton of boxing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2019 The Guava Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
15+
package com.google.common.primitives;
16+
17+
import static java.lang.annotation.ElementType.CONSTRUCTOR;
18+
import static java.lang.annotation.ElementType.METHOD;
19+
import static java.lang.annotation.ElementType.TYPE;
20+
21+
import java.lang.annotation.Target;
22+
23+
/**
24+
* Disables Animal Sniffer's checking of compatibility with older versions of Java/Android.
25+
*
26+
* <p>Each package's copy of this annotation needs to be listed in our {@code pom.xml}.
27+
*/
28+
@Target({METHOD, CONSTRUCTOR, TYPE})
29+
@ElementTypesAreNonnullByDefault
30+
@interface IgnoreJRERequirement {}

0 commit comments

Comments
 (0)