17
package com.google.common.testing;
19
import static com.google.common.base.Preconditions.checkNotNull;
20
import static junit.framework.Assert.assertTrue;
22
import com.google.common.annotations.GwtCompatible;
23
import com.google.errorprone.annotations.CanIgnoreReturnValue;
24
import java.util.ArrayList;
25
import java.util.Arrays;
26
import java.util.Collections;
28
import java.util.Objects;
29
import java.util.function.BiPredicate;
30
import java.util.stream.Collector;
31
import org.checkerframework.checker.nullness.qual.Nullable;
49
@ElementTypesAreNonnullByDefault
50
public final class CollectorTester<
51
T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object> {
56
public static <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
57
CollectorTester<T, A, R> of(Collector<T, A, R> collector) {
58
return of(collector, Objects::equals);
65
public static <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
66
CollectorTester<T, A, R> of(
67
Collector<T, A, R> collector, BiPredicate<? super R, ? super R> equivalence) {
68
return new CollectorTester<>(collector, equivalence);
71
private final Collector<T, A, R> collector;
72
private final BiPredicate<? super R, ? super R> equivalence;
74
private CollectorTester(
75
Collector<T, A, R> collector, BiPredicate<? super R, ? super R> equivalence) {
76
this.collector = checkNotNull(collector);
77
this.equivalence = checkNotNull(equivalence);
84
enum CollectStrategy {
88
final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
89
A result(Collector<T, A, R> collector, Iterable<T> inputs) {
90
A accum = collector.supplier().get();
91
for (T input : inputs) {
92
collector.accumulator().accept(accum, input);
98
MERGE_LEFT_ASSOCIATIVE {
100
final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
101
A result(Collector<T, A, R> collector, Iterable<T> inputs) {
102
A accum = collector.supplier().get();
103
for (T input : inputs) {
104
A newAccum = collector.supplier().get();
105
collector.accumulator().accept(newAccum, input);
106
accum = collector.combiner().apply(accum, newAccum);
112
MERGE_RIGHT_ASSOCIATIVE {
114
final <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
115
A result(Collector<T, A, R> collector, Iterable<T> inputs) {
116
List<A> stack = new ArrayList<>();
117
for (T input : inputs) {
118
A newAccum = collector.supplier().get();
119
collector.accumulator().accept(newAccum, input);
120
push(stack, newAccum);
122
push(stack, collector.supplier().get());
123
while (stack.size() > 1) {
124
A right = pop(stack);
126
push(stack, collector.combiner().apply(left, right));
131
<E extends @Nullable Object> void push(List<E> stack, E value) {
135
<E extends @Nullable Object> E pop(List<E> stack) {
136
return stack.remove(stack.size() - 1);
140
abstract <T extends @Nullable Object, A extends @Nullable Object, R extends @Nullable Object>
141
A result(Collector<T, A, R> collector, Iterable<T> inputs);
149
@CanIgnoreReturnValue
150
@SuppressWarnings("nullness")
151
public final CollectorTester<T, A, R> expectCollects(R expectedResult, T... inputs) {
152
List<T> list = Arrays.asList(inputs);
153
doExpectCollects(expectedResult, list);
154
if (collector.characteristics().contains(Collector.Characteristics.UNORDERED)) {
155
Collections.reverse(list);
156
doExpectCollects(expectedResult, list);
161
private void doExpectCollects(R expectedResult, List<T> inputs) {
162
for (CollectStrategy scheme : CollectStrategy.values()) {
163
A finalAccum = scheme.result(collector, inputs);
164
if (collector.characteristics().contains(Collector.Characteristics.IDENTITY_FINISH)) {
165
@SuppressWarnings("unchecked")
166
R result = (R) finalAccum;
167
assertEquivalent(expectedResult, result);
169
assertEquivalent(expectedResult, collector.finisher().apply(finalAccum));
173
private void assertEquivalent(R expected, R actual) {
175
"Expected " + expected + " got " + actual + " modulo equivalence " + equivalence,
176
equivalence.test(expected, actual));