Skip to content

Commit

Permalink
Prepare to expose (all?) remaining collectors as part of `guava-andro…
Browse files Browse the repository at this point in the history
…id`.

This CL is further progress toward #6567

RELNOTES=n/a
PiperOrigin-RevId: 581031659
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Nov 9, 2023
1 parent 15fca29 commit ed21dbb
Show file tree
Hide file tree
Showing 19 changed files with 41 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ public void testToStringImplWithNullValues() throws Exception {
}

@GwtIncompatible // NullPointerTester
@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointerExceptions() {
new NullPointerTester().testAllPublicStaticMethods(Maps.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ public void testFilteredKeysListMultimapGetBadValue() {
}

@GwtIncompatible // NullPointerTester
@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointers() {
new NullPointerTester().testAllPublicStaticMethods(Multimaps.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ public void testHighestCountFirst() {
}

@GwtIncompatible // NullPointerTester
@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointers() {
new NullPointerTester().testAllPublicStaticMethods(Multisets.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ public void testComplementOfEmptySetWithoutTypeDoesntWork() {
}

@GwtIncompatible // NullPointerTester
@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointerExceptions() {
new NullPointerTester()
.setDefault(Enum.class, SomeEnum.A)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public void testFailureWhenMoreThan255HashFunctionsAreNeeded() {
});
}

@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointers() {
NullPointerTester tester = new NullPointerTester();
tester.testAllPublicInstanceMethods(BloomFilter.create(Funnels.unencodedCharsFunnel(), 100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.math;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.NaN;
Expand All @@ -39,8 +40,8 @@
* @author Pete Gillin
*/
class StatsTesting {

static final double ALLOWED_ERROR = 1e-10;
// TODO(cpovirk): Convince myself that this larger error makes sense.
static final double ALLOWED_ERROR = isAndroid() ? .25 : 1e-10;

// Inputs and their statistics:

Expand Down Expand Up @@ -501,5 +502,9 @@ static PairedStatsAccumulator createPartitionedFilledPairedStatsAccumulator(
return accumulator;
}

private static boolean isAndroid() {
return checkNotNull(System.getProperty("java.runtime.name", "")).contains("Android");
}

private StatsTesting() {}
}
1 change: 1 addition & 0 deletions android/guava/src/com/google/common/collect/Sets.java
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,7 @@ protected Set<E> computeNext() {
if (bitToFlip == index.size()) {
return endOfData();
}

/*
* The current set in sorted order looks like
* {firstSetBit, firstSetBit + 1, ..., bitToFlip - 1, ...}
Expand Down
7 changes: 7 additions & 0 deletions android/guava/src/com/google/common/collect/TopKSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ private void swap(int i, int j) {
buffer[j] = tmp;
}

TopKSelector<T> combine(TopKSelector<T> other) {
for (int i = 0; i < other.bufferSize; i++) {
this.offer(uncheckedCastNullableTToT(other.buffer[i]));
}
return this;
}

/**
* Adds each member of {@code elements} as a candidate for the top {@code k} elements. This
* operation takes amortized linear time in the length of {@code elements}.
Expand Down
2 changes: 2 additions & 0 deletions android/guava/src/com/google/common/hash/BloomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,6 @@ public void writeTo(OutputStream out) throws IOException {
throw new IOException(message, e);
}
}

private static final long serialVersionUID = 0xdecaf;
}
16 changes: 0 additions & 16 deletions guava-tests/test/com/google/common/collect/ComparatorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.testing.Helpers;
import com.google.common.testing.CollectorTester;
import com.google.common.testing.EqualsTester;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Optional;
Expand Down Expand Up @@ -79,20 +77,6 @@ public void testIsInStrictOrder() {
assertTrue(Comparators.isInStrictOrder(Collections.<Integer>emptyList(), Ordering.natural()));
}

public void testLeastCollector() {
CollectorTester.of(Comparators.<Integer>least(2, Comparator.naturalOrder()))
.expectCollects(Arrays.asList(1, 2), 1, 2, 3, 4, 5, 6)
.expectCollects(Arrays.asList(1), 1)
.expectCollects(Collections.emptyList());
}

public void testGreatestCollector() {
CollectorTester.of(Comparators.<Integer>greatest(2, Comparator.naturalOrder()))
.expectCollects(Arrays.asList(6, 5), 1, 2, 3, 4, 5, 6)
.expectCollects(Arrays.asList(1), 1)
.expectCollects(Collections.emptyList());
}

public void testEmptiesFirst() {
Optional<String> empty = Optional.empty();
Optional<String> abc = Optional.of("abc");
Expand Down
1 change: 1 addition & 0 deletions guava-tests/test/com/google/common/collect/MapsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ public void testToStringImplWithNullValues() throws Exception {
}

@GwtIncompatible // NullPointerTester
@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointerExceptions() {
new NullPointerTester().testAllPublicStaticMethods(Maps.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,7 @@ public void testFilteredKeysListMultimapGetBadValue() {
}

@GwtIncompatible // NullPointerTester
@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointers() {
new NullPointerTester().testAllPublicStaticMethods(Multimaps.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public void testToMultisetCountFunction() {
}

@GwtIncompatible // NullPointerTester
@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointers() {
new NullPointerTester().testAllPublicStaticMethods(Multisets.class);
}
Expand Down
1 change: 1 addition & 0 deletions guava-tests/test/com/google/common/collect/SetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ public void testComplementOfEmptySetWithoutTypeDoesntWork() {
}

@GwtIncompatible // NullPointerTester
@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointerExceptions() {
new NullPointerTester()
.setDefault(Enum.class, SomeEnum.A)
Expand Down
31 changes: 0 additions & 31 deletions guava-tests/test/com/google/common/collect/TablesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.Table.Cell;
import com.google.common.testing.CollectorTester;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.SerializableTester;
import java.util.stream.Collector;
import junit.framework.TestCase;

/**
Expand All @@ -32,35 +30,6 @@
*/
@GwtCompatible(emulated = true)
public class TablesTest extends TestCase {

// The bulk of the toTable tests can be found in TableCollectorsTest.
// This gives minimal coverage to the forwarding functions
public void testToTableSanityTest() {
Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
Tables.toTable(Cell::getRowKey, Cell::getColumnKey, Cell::getValue, HashBasedTable::create);
HashBasedTable<String, String, Integer> expected = HashBasedTable.create();
expected.put("one", "uno", 1);
CollectorTester.of(collector)
.expectCollects(HashBasedTable.create())
.expectCollects(expected, Tables.immutableCell("one", "uno", 1));
}

public void testToTableMergingSanityTest() {
Collector<Cell<String, String, Integer>, ?, Table<String, String, Integer>> collector =
Tables.toTable(
Cell::getRowKey,
Cell::getColumnKey,
Cell::getValue,
Integer::sum,
HashBasedTable::create);
HashBasedTable<String, String, Integer> expected = HashBasedTable.create();
expected.put("one", "uno", 3);
CollectorTester.of(collector)
.expectCollects(HashBasedTable.create())
.expectCollects(
expected, Tables.immutableCell("one", "uno", 1), Tables.immutableCell("one", "uno", 2));
}

@GwtIncompatible // SerializableTester
public void testImmutableEntrySerialization() {
Cell<String, Integer, Character> entry = Tables.immutableCell("foo", 1, 'a');
Expand Down
17 changes: 1 addition & 16 deletions guava-tests/test/com/google/common/hash/BloomFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import junit.framework.TestCase;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand Down Expand Up @@ -247,6 +246,7 @@ public void testFailureWhenMoreThan255HashFunctionsAreNeeded() {
});
}

@AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
public void testNullPointers() {
NullPointerTester tester = new NullPointerTester();
tester.testAllPublicInstanceMethods(BloomFilter.create(Funnels.unencodedCharsFunnel(), 100));
Expand Down Expand Up @@ -370,21 +370,6 @@ public void testEquals_empty() {
.testEquals();
}

public void testCollector() {
BloomFilter<String> bf1 = BloomFilter.create(Funnels.unencodedCharsFunnel(), 100);
bf1.put("1");
bf1.put("2");

assertEquals(
bf1,
Stream.of("1", "2")
.collect(BloomFilter.toBloomFilter(Funnels.unencodedCharsFunnel(), 100)));
assertEquals(
bf1,
Stream.of("2", "1")
.collect(BloomFilter.toBloomFilter(Funnels.unencodedCharsFunnel(), 100)));
}

public void testEquals() {
BloomFilter<String> bf1 = BloomFilter.create(Funnels.unencodedCharsFunnel(), 100);
bf1.put("1");
Expand Down
24 changes: 0 additions & 24 deletions guava-tests/test/com/google/common/math/StatsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.common.math;

import static com.google.common.math.Stats.toStats;
import static com.google.common.math.StatsTesting.ALLOWED_ERROR;
import static com.google.common.math.StatsTesting.ALL_MANY_VALUES;
import static com.google.common.math.StatsTesting.ALL_STATS;
Expand Down Expand Up @@ -85,7 +84,6 @@
import com.google.common.primitives.Longs;
import com.google.common.testing.EqualsTester;
import com.google.common.testing.SerializableTester;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.DoubleSummaryStatistics;
Expand Down Expand Up @@ -444,28 +442,6 @@ public void testOfPrimitiveLongStream() {
assertThat(stats.max()).isEqualTo(MEGA_STREAM_MAX);
}

public void testBoxedDoubleStreamToStats() {
Stats stats = megaPrimitiveDoubleStream().boxed().collect(toStats());
assertThat(stats.count()).isEqualTo(MEGA_STREAM_COUNT);
assertThat(stats.mean()).isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT).of(MEGA_STREAM_MEAN);
assertThat(stats.populationVariance())
.isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT)
.of(MEGA_STREAM_POPULATION_VARIANCE);
assertThat(stats.min()).isEqualTo(MEGA_STREAM_MIN);
assertThat(stats.max()).isEqualTo(MEGA_STREAM_MAX);
}

public void testBoxedBigDecimalStreamToStats() {
Stats stats = megaPrimitiveDoubleStream().mapToObj(BigDecimal::valueOf).collect(toStats());
assertThat(stats.count()).isEqualTo(MEGA_STREAM_COUNT);
assertThat(stats.mean()).isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT).of(MEGA_STREAM_MEAN);
assertThat(stats.populationVariance())
.isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT)
.of(MEGA_STREAM_POPULATION_VARIANCE);
assertThat(stats.min()).isEqualTo(MEGA_STREAM_MIN);
assertThat(stats.max()).isEqualTo(MEGA_STREAM_MAX);
}

public void testEqualsAndHashCode() {
new EqualsTester()
.addEqualityGroup(
Expand Down
18 changes: 12 additions & 6 deletions guava-tests/test/com/google/common/math/StatsTesting.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.common.math;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.truth.Truth.assertThat;
import static java.lang.Double.NEGATIVE_INFINITY;
import static java.lang.Double.NaN;
Expand All @@ -40,8 +41,8 @@
* @author Pete Gillin
*/
class StatsTesting {

static final double ALLOWED_ERROR = 1e-10;
// TODO(cpovirk): Convince myself that this larger error makes sense.
static final double ALLOWED_ERROR = isAndroid() ? .25 : 1e-10;

// Inputs and their statistics:

Expand Down Expand Up @@ -234,11 +235,12 @@ static DoubleStream megaPrimitiveDoubleStreamPart2() {
return DoubleStream.iterate(999_999.0, x -> x - 2.0).limit(MEGA_STREAM_COUNT / 2).parallel();
}

static final long MEGA_STREAM_COUNT = 1_000_000;
static final double MEGA_STREAM_MEAN = 999_999.0 / 2;
static final double MEGA_STREAM_POPULATION_VARIANCE = 999_999.0 * 1_000_001.0 / 12;
static final long MEGA_STREAM_COUNT = isAndroid() ? 100 : 1_000_000;
static final double MEGA_STREAM_MIN = 0.0;
static final double MEGA_STREAM_MAX = 999_999.0;
static final double MEGA_STREAM_MAX = MEGA_STREAM_COUNT - 1;
static final double MEGA_STREAM_MEAN = MEGA_STREAM_MAX / 2;
static final double MEGA_STREAM_POPULATION_VARIANCE =
(MEGA_STREAM_COUNT - 1) * (MEGA_STREAM_COUNT + 1) / 12.0;

// Stats instances:

Expand Down Expand Up @@ -530,5 +532,9 @@ static PairedStatsAccumulator createPartitionedFilledPairedStatsAccumulator(
return accumulator;
}

private static boolean isAndroid() {
return checkNotNull(System.getProperty("java.runtime.name", "")).contains("Android");
}

private StatsTesting() {}
}
2 changes: 2 additions & 0 deletions guava/src/com/google/common/hash/BloomFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -633,4 +633,6 @@ public void writeTo(OutputStream out) throws IOException {
throw new IOException(message, e);
}
}

private static final long serialVersionUID = 0xcafebabe;
}

0 comments on commit ed21dbb

Please sign in to comment.