Skip to content

Commit

Permalink
Expose more Java 8 APIs to Android users.
Browse files Browse the repository at this point in the history
RELNOTES=Exposed some additional Java 8 APIs to Android users.
PiperOrigin-RevId: 689846217
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 25, 2024
1 parent 984f713 commit c6c2680
Show file tree
Hide file tree
Showing 17 changed files with 548 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.primitives.TestPlatform.reduceIterationsIfGwt;
import static com.google.common.testing.SerializableTester.reserialize;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.stream;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand All @@ -38,6 +39,7 @@
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.DoubleStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
Expand All @@ -46,7 +48,6 @@
* @author Kevin Bourrillion
*/
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
public class ImmutableDoubleArrayTest extends TestCase {
// Test all creation paths very lazily: by assuming asList() works

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

public void testCopyOf_stream() {
assertThat(ImmutableDoubleArray.copyOf(DoubleStream.empty()))
.isSameInstanceAs(ImmutableDoubleArray.of());
assertThat(ImmutableDoubleArray.copyOf(DoubleStream.of(0, 1, 3)).asList())
.containsExactly(0.0, 1.0, 3.0)
.inOrder();
}

public void testBuilder_presize_zero() {
ImmutableDoubleArray.Builder builder = ImmutableDoubleArray.builder(0);
builder.add(5.0);
Expand Down Expand Up @@ -211,6 +220,16 @@ void doIt(ImmutableDoubleArray.Builder builder, AtomicInteger counter) {
builder.addAll(iterable(list));
}
},
ADD_STREAM {
@Override
void doIt(ImmutableDoubleArray.Builder builder, AtomicInteger counter) {
double[] array = new double[RANDOM.nextInt(10)];
for (int i = 0; i < array.length; i++) {
array[i] = counter.getAndIncrement();
}
builder.addAll(stream(array));
}
},
ADD_IIA {
@Override
void doIt(ImmutableDoubleArray.Builder builder, AtomicInteger counter) {
Expand Down Expand Up @@ -316,6 +335,23 @@ public void testContains() {
assertThat(iia.subArray(1, 5).contains(1)).isTrue();
}

public void testForEach() {
ImmutableDoubleArray.of().forEach(i -> fail());
ImmutableDoubleArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());

AtomicInteger count = new AtomicInteger(0);
ImmutableDoubleArray.of(0, 1, 2, 3)
.forEach(i -> assertThat(i).isEqualTo((double) count.getAndIncrement()));
assertThat(count.get()).isEqualTo(4);
}

public void testStream() {
ImmutableDoubleArray.of().stream().forEach(i -> fail());
ImmutableDoubleArray.of(0, 1, 3).subArray(1, 1).stream().forEach(i -> fail());
assertThat(ImmutableDoubleArray.of(0, 1, 3).stream().toArray())
.isEqualTo(new double[] {0, 1, 3});
}

public void testSubArray() {
ImmutableDoubleArray iia0 = ImmutableDoubleArray.of();
ImmutableDoubleArray iia1 = ImmutableDoubleArray.of(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.primitives.TestPlatform.reduceIterationsIfGwt;
import static com.google.common.testing.SerializableTester.reserialize;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.stream;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand All @@ -38,6 +39,7 @@
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
Expand Down Expand Up @@ -136,6 +138,14 @@ public void testCopyOf_collection_nonempty() {
assertThat(iia.asList()).containsExactly(0, 1, 3).inOrder();
}

public void testCopyOf_stream() {
assertThat(ImmutableIntArray.copyOf(IntStream.empty()))
.isSameInstanceAs(ImmutableIntArray.of());
assertThat(ImmutableIntArray.copyOf(IntStream.of(0, 1, 3)).asList())
.containsExactly(0, 1, 3)
.inOrder();
}

public void testBuilder_presize_zero() {
ImmutableIntArray.Builder builder = ImmutableIntArray.builder(0);
builder.add(5);
Expand Down Expand Up @@ -205,6 +215,16 @@ void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
builder.addAll(iterable(list));
}
},
ADD_STREAM {
@Override
void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
int[] array = new int[RANDOM.nextInt(10)];
for (int i = 0; i < array.length; i++) {
array[i] = counter.getAndIncrement();
}
builder.addAll(stream(array));
}
},
ADD_IIA {
@Override
void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
Expand Down Expand Up @@ -300,6 +320,21 @@ public void testContains() {
assertThat(iia.subArray(1, 5).contains(1)).isTrue();
}

public void testForEach() {
ImmutableIntArray.of().forEach(i -> fail());
ImmutableIntArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());

AtomicInteger count = new AtomicInteger(0);
ImmutableIntArray.of(0, 1, 2, 3).forEach(i -> assertThat(i).isEqualTo(count.getAndIncrement()));
assertThat(count.get()).isEqualTo(4);
}

public void testStream() {
ImmutableIntArray.of().stream().forEach(i -> fail());
ImmutableIntArray.of(0, 1, 3).subArray(1, 1).stream().forEach(i -> fail());
assertThat(ImmutableIntArray.of(0, 1, 3).stream().toArray()).isEqualTo(new int[] {0, 1, 3});
}

public void testSubArray() {
ImmutableIntArray iia0 = ImmutableIntArray.of();
ImmutableIntArray iia1 = ImmutableIntArray.of(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static com.google.common.primitives.TestPlatform.reduceIterationsIfGwt;
import static com.google.common.testing.SerializableTester.reserialize;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.stream;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand All @@ -38,6 +39,7 @@
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.LongStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
Expand Down Expand Up @@ -138,6 +140,14 @@ public void testCopyOf_collection_nonempty() {
assertThat(iia.asList()).containsExactly(0L, 1L, 3L).inOrder();
}

public void testCopyOf_stream() {
assertThat(ImmutableLongArray.copyOf(LongStream.empty()))
.isSameInstanceAs(ImmutableLongArray.of());
assertThat(ImmutableLongArray.copyOf(LongStream.of(0, 1, 3)).asList())
.containsExactly(0L, 1L, 3L)
.inOrder();
}

public void testBuilder_presize_zero() {
ImmutableLongArray.Builder builder = ImmutableLongArray.builder(0);
builder.add(5L);
Expand Down Expand Up @@ -207,6 +217,16 @@ void doIt(ImmutableLongArray.Builder builder, AtomicLong counter) {
builder.addAll(iterable(list));
}
},
ADD_STREAM {
@Override
void doIt(ImmutableLongArray.Builder builder, AtomicLong counter) {
long[] array = new long[RANDOM.nextInt(10)];
for (int i = 0; i < array.length; i++) {
array[i] = counter.getAndIncrement();
}
builder.addAll(stream(array));
}
},
ADD_IIA {
@Override
void doIt(ImmutableLongArray.Builder builder, AtomicLong counter) {
Expand Down Expand Up @@ -302,6 +322,22 @@ public void testContains() {
assertThat(iia.subArray(1, 5).contains(1)).isTrue();
}

public void testForEach() {
ImmutableLongArray.of().forEach(i -> fail());
ImmutableLongArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());

AtomicLong count = new AtomicLong(0);
ImmutableLongArray.of(0, 1, 2, 3)
.forEach(i -> assertThat(i).isEqualTo(count.getAndIncrement()));
assertThat(count.get()).isEqualTo(4);
}

public void testStream() {
ImmutableLongArray.of().stream().forEach(i -> fail());
ImmutableLongArray.of(0, 1, 3).subArray(1, 1).stream().forEach(i -> fail());
assertThat(ImmutableLongArray.of(0, 1, 3).stream().toArray()).isEqualTo(new long[] {0, 1, 3});
}

public void testSubArray() {
ImmutableLongArray iia0 = ImmutableLongArray.of();
ImmutableLongArray iia1 = ImmutableLongArray.of(5);
Expand Down
13 changes: 13 additions & 0 deletions android/guava/src/com/google/common/primitives/Doubles.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.Comparator;
import java.util.List;
import java.util.RandomAccess;
import java.util.Spliterator;
import java.util.Spliterators;
import javax.annotation.CheckForNull;

/**
Expand Down Expand Up @@ -616,6 +618,17 @@ public Double get(int index) {
return array[start + index];
}

@Override
@SuppressWarnings("Java7ApiChecker")
/*
* This is an override that is not directly visible to callers, so NewApi will catch calls to
* Collection.spliterator() where necessary.
*/
@IgnoreJRERequirement
public Spliterator.OfDouble spliterator() {
return Spliterators.spliterator(array, start, end, 0);
}

@Override
public boolean contains(@CheckForNull Object target) {
// Overridden to prevent a ton of boxing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2019 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.common.primitives;

import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;

import java.lang.annotation.Target;

/**
* Disables Animal Sniffer's checking of compatibility with older versions of Java/Android.
*
* <p>Each package's copy of this annotation needs to be listed in our {@code pom.xml}.
*/
@Target({METHOD, CONSTRUCTOR, TYPE})
@ElementTypesAreNonnullByDefault
@interface IgnoreJRERequirement {}
Loading

0 comments on commit c6c2680

Please sign in to comment.