Skip to content

Commit 96fca0b

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Make our Collector APIs available in guava-android.
(for example, `ImmutableList.toImmutableList()`) For now, we're making them `@Beta` just in case users encounter enough problems that we find it less disruptive to revert this change than to keep it. However, we plan to remove `@Beta` soon, at which point we'll be committed to this APIs. If you use Guava under Android, please [test with Guava 33.0.0 or higher](https://groups.google.com/g/guava-announce/c/9-dw_C6G_NM), ideally with 34.0.0 or higher (which will contain this commit), and [report any problems](https://github.com/google/guava/issues/new?assignees=&labels=type%3Ddefect&projects=&template=bug_report.yaml). Our expectation is that this commit should not cause problems, even for users who don't use enable [library desugaring](https://developer.android.com/studio/write/java11-default-support-table). But we will see what happens in wild. Of course, if you want to actually _use_ these APIs, then you'll need to [enable library desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring) or target a [new enough version of Android](https://developer.android.com/reference/java/util/stream/Stream), just as with any other `Stream`-based APIs. (progress toward #6567) RELNOTES=`collect`: Made our `Collector` APIs (e.g., `ImmutableList.toImmutableList()`) available in `guava-android`. PiperOrigin-RevId: 629491350
1 parent fddc95d commit 96fca0b

20 files changed

+192
-57
lines changed

android/guava/src/com/google/common/collect/Comparators.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
2121

22+
import com.google.common.annotations.Beta;
2223
import com.google.common.annotations.GwtCompatible;
2324
import java.util.Comparator;
2425
import java.util.Iterator;
@@ -127,10 +128,12 @@ private Comparators() {}
127128
* log n) time and O(n) space.
128129
*
129130
* @throws IllegalArgumentException if {@code k < 0}
131+
* @since NEXT (available since 22.0 in guava-jre)
130132
*/
131133
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
132134
@IgnoreJRERequirement // Users will use this only if they're already using streams.
133-
static <T extends @Nullable Object> Collector<T, ?, List<T>> least(
135+
@Beta // TODO: b/288085449 - Remove.
136+
public static <T extends @Nullable Object> Collector<T, ?, List<T>> least(
134137
int k, Comparator<? super T> comparator) {
135138
checkNonnegative(k, "k");
136139
checkNotNull(comparator);
@@ -160,10 +163,12 @@ private Comparators() {}
160163
* takes O(n log n) time and O(n) space.
161164
*
162165
* @throws IllegalArgumentException if {@code k < 0}
166+
* @since NEXT (available since 22.0 in guava-jre)
163167
*/
164168
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
165169
@IgnoreJRERequirement // Users will use this only if they're already using streams.
166-
static <T extends @Nullable Object> Collector<T, ?, List<T>> greatest(
170+
@Beta // TODO: b/288085449 - Remove.
171+
public static <T extends @Nullable Object> Collector<T, ?, List<T>> greatest(
167172
int k, Comparator<? super T> comparator) {
168173
return least(k, comparator.reversed());
169174
}

android/guava/src/com/google/common/collect/ImmutableBiMap.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.collect.CollectPreconditions.checkEntryNotNull;
2020
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
2121

22+
import com.google.common.annotations.Beta;
2223
import com.google.common.annotations.GwtCompatible;
2324
import com.google.common.annotations.J2ktIncompatible;
2425
import com.google.errorprone.annotations.CanIgnoreReturnValue;
@@ -56,12 +57,16 @@ public abstract class ImmutableBiMap<K, V> extends ImmutableMap<K, V> implements
5657
* Object#equals(Object)}), an {@code IllegalArgumentException} is thrown when the collection
5758
* operation is performed. (This differs from the {@code Collector} returned by {@link
5859
* Collectors#toMap(Function, Function)}, which throws an {@code IllegalStateException}.)
60+
*
61+
* @since NEXT (available since 21.0 in guava-jre)
5962
*/
6063
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
6164
@IgnoreJRERequirement // Users will use this only if they're already using streams.
62-
static <T extends @Nullable Object, K, V> Collector<T, ?, ImmutableBiMap<K, V>> toImmutableBiMap(
63-
Function<? super T, ? extends K> keyFunction,
64-
Function<? super T, ? extends V> valueFunction) {
65+
@Beta // TODO: b/288085449 - Remove.
66+
public static <T extends @Nullable Object, K, V>
67+
Collector<T, ?, ImmutableBiMap<K, V>> toImmutableBiMap(
68+
Function<? super T, ? extends K> keyFunction,
69+
Function<? super T, ? extends V> valueFunction) {
6570
return CollectCollectors.toImmutableBiMap(keyFunction, valueFunction);
6671
}
6772

@@ -621,14 +626,17 @@ private void readObject(ObjectInputStream stream) throws InvalidObjectException
621626
*
622627
* @throws UnsupportedOperationException always
623628
* @deprecated Use {@link ImmutableBiMap#toImmutableBiMap}.
629+
* @since NEXT (available since 21.0 in guava-jre)
624630
*/
625631
@Deprecated
626632
@DoNotCall("Use toImmutableBiMap")
627633
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
628634
@IgnoreJRERequirement // Users will use this only if they're already using streams.
629-
static <T extends @Nullable Object, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
630-
Function<? super T, ? extends K> keyFunction,
631-
Function<? super T, ? extends V> valueFunction) {
635+
@Beta // TODO: b/288085449 - Remove.
636+
public static <T extends @Nullable Object, K, V>
637+
Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
638+
Function<? super T, ? extends K> keyFunction,
639+
Function<? super T, ? extends V> valueFunction) {
632640
throw new UnsupportedOperationException();
633641
}
634642

@@ -639,15 +647,18 @@ private void readObject(ObjectInputStream stream) throws InvalidObjectException
639647
*
640648
* @throws UnsupportedOperationException always
641649
* @deprecated
650+
* @since NEXT (available since 21.0 in guava-jre)
642651
*/
643652
@Deprecated
644653
@DoNotCall("Use toImmutableBiMap")
645654
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
646655
@IgnoreJRERequirement // Users will use this only if they're already using streams.
647-
static <T extends @Nullable Object, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
648-
Function<? super T, ? extends K> keyFunction,
649-
Function<? super T, ? extends V> valueFunction,
650-
BinaryOperator<V> mergeFunction) {
656+
@Beta // TODO: b/288085449 - Remove.
657+
public static <T extends @Nullable Object, K, V>
658+
Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
659+
Function<? super T, ? extends K> keyFunction,
660+
Function<? super T, ? extends V> valueFunction,
661+
BinaryOperator<V> mergeFunction) {
651662
throw new UnsupportedOperationException();
652663
}
653664

android/guava/src/com/google/common/collect/ImmutableList.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static com.google.common.collect.ObjectArrays.checkElementsNotNull;
2626
import static com.google.common.collect.RegularImmutableList.EMPTY;
2727

28+
import com.google.common.annotations.Beta;
2829
import com.google.common.annotations.GwtCompatible;
2930
import com.google.common.annotations.GwtIncompatible;
3031
import com.google.common.annotations.J2ktIncompatible;
@@ -66,10 +67,13 @@ public abstract class ImmutableList<E> extends ImmutableCollection<E>
6667
/**
6768
* Returns a {@code Collector} that accumulates the input elements into a new {@code
6869
* ImmutableList}, in encounter order.
70+
*
71+
* @since NEXT (available since 21.0 in guava-jre)
6972
*/
7073
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
7174
@IgnoreJRERequirement // Users will use this only if they're already using streams.
72-
static <E> Collector<E, ?, ImmutableList<E>> toImmutableList() {
75+
@Beta // TODO: b/288085449 - Remove.
76+
public static <E> Collector<E, ?, ImmutableList<E>> toImmutableList() {
7377
return CollectCollectors.toImmutableList();
7478
}
7579

android/guava/src/com/google/common/collect/ImmutableListMultimap.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static java.util.Objects.requireNonNull;
2020

21+
import com.google.common.annotations.Beta;
2122
import com.google.common.annotations.GwtCompatible;
2223
import com.google.common.annotations.GwtIncompatible;
2324
import com.google.common.annotations.J2ktIncompatible;
@@ -78,10 +79,13 @@ public class ImmutableListMultimap<K, V> extends ImmutableMultimap<K, V>
7879
* .putAll('c', "arrot", "herry")
7980
* .build();
8081
* }</pre>
82+
*
83+
* @since NEXT (available since 21.0 in guava-jre)
8184
*/
8285
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
8386
@IgnoreJRERequirement // Users will use this only if they're already using streams.
84-
static <T extends @Nullable Object, K, V>
87+
@Beta // TODO: b/288085449 - Remove.
88+
public static <T extends @Nullable Object, K, V>
8589
Collector<T, ?, ImmutableListMultimap<K, V>> toImmutableListMultimap(
8690
Function<? super T, ? extends K> keyFunction,
8791
Function<? super T, ? extends V> valueFunction) {
@@ -116,10 +120,13 @@ public class ImmutableListMultimap<K, V> extends ImmutableMultimap<K, V>
116120
* .build();
117121
* }
118122
* }</pre>
123+
*
124+
* @since NEXT (available since 21.0 in guava-jre)
119125
*/
120126
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
121127
@IgnoreJRERequirement // Users will use this only if they're already using streams.
122-
static <T extends @Nullable Object, K, V>
128+
@Beta // TODO: b/288085449 - Remove.
129+
public static <T extends @Nullable Object, K, V>
123130
Collector<T, ?, ImmutableListMultimap<K, V>> flatteningToImmutableListMultimap(
124131
Function<? super T, ? extends K> keyFunction,
125132
Function<? super T, ? extends Stream<? extends V>> valuesFunction) {

android/guava/src/com/google/common/collect/ImmutableMap.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.common.collect.CollectPreconditions.checkNonnegative;
2323
import static java.util.Objects.requireNonNull;
2424

25+
import com.google.common.annotations.Beta;
2526
import com.google.common.annotations.GwtCompatible;
2627
import com.google.common.annotations.GwtIncompatible;
2728
import com.google.common.annotations.J2ktIncompatible;
@@ -79,12 +80,16 @@ public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
7980
* IllegalArgumentException} is thrown when the collection operation is performed. (This differs
8081
* from the {@code Collector} returned by {@link Collectors#toMap(Function, Function)}, which
8182
* throws an {@code IllegalStateException}.)
83+
*
84+
* @since NEXT (available since 21.0 in guava-jre)
8285
*/
8386
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
8487
@IgnoreJRERequirement // Users will use this only if they're already using streams.
85-
static <T extends @Nullable Object, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
86-
Function<? super T, ? extends K> keyFunction,
87-
Function<? super T, ? extends V> valueFunction) {
88+
@Beta // TODO: b/288085449 - Remove.
89+
public static <T extends @Nullable Object, K, V>
90+
Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
91+
Function<? super T, ? extends K> keyFunction,
92+
Function<? super T, ? extends V> valueFunction) {
8893
return CollectCollectors.toImmutableMap(keyFunction, valueFunction);
8994
}
9095

@@ -98,13 +103,17 @@ public abstract class ImmutableMap<K, V> implements Map<K, V>, Serializable {
98103
* future occurrences of the key would reinsert it).
99104
*
100105
* <p>Entries will appear in the encounter order of the first occurrence of the key.
106+
*
107+
* @since NEXT (available since 21.0 in guava-jre)
101108
*/
102109
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
103110
@IgnoreJRERequirement // Users will use this only if they're already using streams.
104-
static <T extends @Nullable Object, K, V> Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
105-
Function<? super T, ? extends K> keyFunction,
106-
Function<? super T, ? extends V> valueFunction,
107-
BinaryOperator<V> mergeFunction) {
111+
@Beta // TODO: b/288085449 - Remove.
112+
public static <T extends @Nullable Object, K, V>
113+
Collector<T, ?, ImmutableMap<K, V>> toImmutableMap(
114+
Function<? super T, ? extends K> keyFunction,
115+
Function<? super T, ? extends V> valueFunction,
116+
BinaryOperator<V> mergeFunction) {
108117
return CollectCollectors.toImmutableMap(keyFunction, valueFunction, mergeFunction);
109118
}
110119

android/guava/src/com/google/common/collect/ImmutableMultiset.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static java.util.Objects.requireNonNull;
2121

22+
import com.google.common.annotations.Beta;
2223
import com.google.common.annotations.GwtCompatible;
2324
import com.google.common.annotations.GwtIncompatible;
2425
import com.google.common.annotations.J2ktIncompatible;
@@ -64,10 +65,13 @@ public abstract class ImmutableMultiset<E> extends ImmutableMultisetGwtSerializa
6465
* Returns a {@code Collector} that accumulates the input elements into a new {@code
6566
* ImmutableMultiset}. Elements iterate in order by the <i>first</i> appearance of that element in
6667
* encounter order.
68+
*
69+
* @since NEXT (available since 21.0 in guava-jre)
6770
*/
6871
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
6972
@IgnoreJRERequirement // Users will use this only if they're already using streams.
70-
static <E> Collector<E, ?, ImmutableMultiset<E>> toImmutableMultiset() {
73+
@Beta // TODO: b/288085449 - Remove.
74+
public static <E> Collector<E, ?, ImmutableMultiset<E>> toImmutableMultiset() {
7175
return CollectCollectors.toImmutableMultiset(Function.identity(), e -> 1);
7276
}
7377

@@ -79,11 +83,16 @@ public abstract class ImmutableMultiset<E> extends ImmutableMultisetGwtSerializa
7983
* <p>If the mapped elements contain duplicates (according to {@link Object#equals}), the first
8084
* occurrence in encounter order appears in the resulting multiset, with count equal to the sum of
8185
* the outputs of {@code countFunction.applyAsInt(t)} for each {@code t} mapped to that element.
86+
*
87+
* @since NEXT (available since 22.0 in guava-jre)
8288
*/
8389
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
8490
@IgnoreJRERequirement // Users will use this only if they're already using streams.
85-
static <T extends @Nullable Object, E> Collector<T, ?, ImmutableMultiset<E>> toImmutableMultiset(
86-
Function<? super T, ? extends E> elementFunction, ToIntFunction<? super T> countFunction) {
91+
@Beta // TODO: b/288085449 - Remove.
92+
public static <T extends @Nullable Object, E>
93+
Collector<T, ?, ImmutableMultiset<E>> toImmutableMultiset(
94+
Function<? super T, ? extends E> elementFunction,
95+
ToIntFunction<? super T> countFunction) {
8796
return CollectCollectors.toImmutableMultiset(elementFunction, countFunction);
8897
}
8998

android/guava/src/com/google/common/collect/ImmutableRangeMap.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static com.google.common.base.Preconditions.checkElementIndex;
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21+
import com.google.common.annotations.Beta;
2122
import com.google.common.annotations.GwtIncompatible;
2223
import com.google.common.annotations.J2ktIncompatible;
2324
import com.google.common.collect.SortedLists.KeyAbsentBehavior;
@@ -55,10 +56,13 @@ public class ImmutableRangeMap<K extends Comparable<?>, V> implements RangeMap<K
5556
/**
5657
* Returns a {@code Collector} that accumulates the input elements into a new {@code
5758
* ImmutableRangeMap}. As in {@link Builder}, overlapping ranges are not permitted.
59+
*
60+
* @since NEXT (available since 23.1 in guava-jre)
5861
*/
5962
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
6063
@IgnoreJRERequirement // Users will use this only if they're already using streams.
61-
static <T extends @Nullable Object, K extends Comparable<? super K>, V>
64+
@Beta // TODO: b/288085449 - Remove.
65+
public static <T extends @Nullable Object, K extends Comparable<? super K>, V>
6266
Collector<T, ?, ImmutableRangeMap<K, V>> toImmutableRangeMap(
6367
Function<? super T, Range<K>> keyFunction,
6468
Function<? super T, ? extends V> valueFunction) {

android/guava/src/com/google/common/collect/ImmutableRangeSet.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.common.collect.SortedLists.KeyPresentBehavior.ANY_PRESENT;
2323
import static java.util.Objects.requireNonNull;
2424

25+
import com.google.common.annotations.Beta;
2526
import com.google.common.annotations.GwtIncompatible;
2627
import com.google.common.annotations.J2ktIncompatible;
2728
import com.google.common.collect.SortedLists.KeyAbsentBehavior;
@@ -64,10 +65,13 @@ public final class ImmutableRangeSet<C extends Comparable> extends AbstractRange
6465
* Returns a {@code Collector} that accumulates the input elements into a new {@code
6566
* ImmutableRangeSet}. As in {@link Builder}, overlapping ranges are not permitted and adjacent
6667
* ranges will be merged.
68+
*
69+
* @since NEXT (available since 23.1 in guava-jre)
6770
*/
6871
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
6972
@IgnoreJRERequirement // Users will use this only if they're already using streams.
70-
static <E extends Comparable<? super E>>
73+
@Beta // TODO: b/288085449 - Remove.
74+
public static <E extends Comparable<? super E>>
7175
Collector<Range<E>, ?, ImmutableRangeSet<E>> toImmutableRangeSet() {
7276
return CollectCollectors.toImmutableRangeSet();
7377
}

android/guava/src/com/google/common/collect/ImmutableSet.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.common.collect.ObjectArrays.checkElementNotNull;
2323
import static java.util.Objects.requireNonNull;
2424

25+
import com.google.common.annotations.Beta;
2526
import com.google.common.annotations.GwtCompatible;
2627
import com.google.common.annotations.J2ktIncompatible;
2728
import com.google.common.annotations.VisibleForTesting;
@@ -58,10 +59,13 @@ public abstract class ImmutableSet<E> extends ImmutableCollection<E> implements
5859
* ImmutableSet}. Elements appear in the resulting set in the encounter order of the stream; if
5960
* the stream contains duplicates (according to {@link Object#equals(Object)}), only the first
6061
* duplicate in encounter order will appear in the result.
62+
*
63+
* @since NEXT (available since 21.0 in guava-jre)
6164
*/
6265
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
6366
@IgnoreJRERequirement // Users will use this only if they're already using streams.
64-
static <E> Collector<E, ?, ImmutableSet<E>> toImmutableSet() {
67+
@Beta // TODO: b/288085449 - Remove.
68+
public static <E> Collector<E, ?, ImmutableSet<E>> toImmutableSet() {
6569
return CollectCollectors.toImmutableSet();
6670
}
6771

android/guava/src/com/google/common/collect/ImmutableSetMultimap.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkNotNull;
2020
import static java.util.Objects.requireNonNull;
2121

22+
import com.google.common.annotations.Beta;
2223
import com.google.common.annotations.GwtCompatible;
2324
import com.google.common.annotations.GwtIncompatible;
2425
import com.google.common.annotations.J2ktIncompatible;
@@ -86,10 +87,13 @@ public class ImmutableSetMultimap<K, V> extends ImmutableMultimap<K, V>
8687
* .putAll('c', "arrot", "herry")
8788
* .build();
8889
* }</pre>
90+
*
91+
* @since NEXT (available since 21.0 in guava-jre)
8992
*/
9093
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
9194
@IgnoreJRERequirement // Users will use this only if they're already using streams.
92-
static <T extends @Nullable Object, K, V>
95+
@Beta // TODO: b/288085449 - Remove.
96+
public static <T extends @Nullable Object, K, V>
9397
Collector<T, ?, ImmutableSetMultimap<K, V>> toImmutableSetMultimap(
9498
Function<? super T, ? extends K> keyFunction,
9599
Function<? super T, ? extends V> valueFunction) {
@@ -133,10 +137,13 @@ public class ImmutableSetMultimap<K, V> extends ImmutableMultimap<K, V>
133137
* .build();
134138
* }
135139
* }</pre>
140+
*
141+
* @since NEXT (available since 21.0 in guava-jre)
136142
*/
137143
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
138144
@IgnoreJRERequirement // Users will use this only if they're already using streams.
139-
static <T extends @Nullable Object, K, V>
145+
@Beta // TODO: b/288085449 - Remove.
146+
public static <T extends @Nullable Object, K, V>
140147
Collector<T, ?, ImmutableSetMultimap<K, V>> flatteningToImmutableSetMultimap(
141148
Function<? super T, ? extends K> keyFunction,
142149
Function<? super T, ? extends Stream<? extends V>> valuesFunction) {

0 commit comments

Comments
 (0)