Skip to content

Commit c7593e0

Browse files
java-team-github-botGoogle Java Core Libraries
authored andcommitted
Internal change
RELNOTES=n/a PiperOrigin-RevId: 869448999
1 parent 491f4c4 commit c7593e0

File tree

2 files changed

+56
-32
lines changed

2 files changed

+56
-32
lines changed

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package com.google.common.collect;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
2021
import static java.util.Collections.emptyList;
2122

2223
import com.google.common.annotations.GwtCompatible;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526
import java.util.NoSuchElementException;
2627
import java.util.Optional;
28+
import java.util.function.Supplier;
2729
import java.util.stream.Collector;
2830
import org.jspecify.annotations.Nullable;
2931

@@ -44,7 +46,7 @@ public final class MoreCollectors {
4446
*/
4547
private static final Collector<Object, ?, Optional<Object>> TO_OPTIONAL =
4648
Collector.of(
47-
ToOptionalState::new,
49+
() -> new ToOptionalState<>(null),
4850
ToOptionalState::add,
4951
ToOptionalState::combine,
5052
ToOptionalState::getOptional,
@@ -66,8 +68,8 @@ public final class MoreCollectors {
6668
private static final Object NULL_PLACEHOLDER = new Object();
6769

6870
private static final Collector<@Nullable Object, ?, @Nullable Object> ONLY_ELEMENT =
69-
Collector.<@Nullable Object, ToOptionalState, @Nullable Object>of(
70-
ToOptionalState::new,
71+
Collector.<@Nullable Object, ToOptionalState<Object>, @Nullable Object>of(
72+
() -> new ToOptionalState<>(null),
7173
(state, o) -> state.add((o == null) ? NULL_PLACEHOLDER : o),
7274
ToOptionalState::combine,
7375
state -> {
@@ -91,18 +93,24 @@ public final class MoreCollectors {
9193
* than one, not just two.
9294
*/
9395
@SuppressWarnings("EmptyList") // ImmutableList doesn't support nullable element types
94-
private static final class ToOptionalState {
96+
@IgnoreJRERequirement // see enclosing class (whose annotation Animal Sniffer ignores here...)
97+
private static final class ToOptionalState<T> {
9598
static final int MAX_EXTRAS = 4;
9699

97-
@Nullable Object element;
98-
List<Object> extras;
100+
@Nullable T element;
101+
List<T> extras;
102+
final @Nullable Supplier<? extends RuntimeException> exceptionSupplier;
99103

100-
ToOptionalState() {
101-
element = null;
102-
extras = emptyList();
104+
ToOptionalState(@Nullable Supplier<? extends RuntimeException> exceptionSupplier) {
105+
this.element = null;
106+
this.extras = emptyList();
107+
this.exceptionSupplier = exceptionSupplier;
103108
}
104109

105-
IllegalArgumentException multiples(boolean overflow) {
110+
RuntimeException multiples(boolean overflow) {
111+
if (exceptionSupplier != null) {
112+
throw exceptionSupplier.get();
113+
}
106114
StringBuilder sb =
107115
new StringBuilder().append("expected one element but was: <").append(element);
108116
for (Object o : extras) {
@@ -115,7 +123,7 @@ IllegalArgumentException multiples(boolean overflow) {
115123
throw new IllegalArgumentException(sb.toString());
116124
}
117125

118-
void add(Object o) {
126+
void add(T o) {
119127
checkNotNull(o);
120128
if (element == null) {
121129
this.element = o;
@@ -130,7 +138,7 @@ void add(Object o) {
130138
}
131139
}
132140

133-
ToOptionalState combine(ToOptionalState other) {
141+
ToOptionalState<T> combine(ToOptionalState<T> other) {
134142
if (element == null) {
135143
return other;
136144
} else if (other.element == null) {
@@ -151,19 +159,23 @@ ToOptionalState combine(ToOptionalState other) {
151159
}
152160

153161
@IgnoreJRERequirement // see enclosing class (whose annotation Animal Sniffer ignores here...)
154-
Optional<Object> getOptional() {
162+
Optional<T> getOptional() {
155163
if (extras.isEmpty()) {
156164
return Optional.ofNullable(element);
157165
} else {
158166
throw multiples(false);
159167
}
160168
}
161169

162-
Object getElement() {
170+
T getElement() {
163171
if (element == null) {
164-
throw new NoSuchElementException();
172+
if (exceptionSupplier != null) {
173+
throw exceptionSupplier.get();
174+
} else {
175+
throw new NoSuchElementException();
176+
}
165177
} else if (extras.isEmpty()) {
166-
return element;
178+
return uncheckedCastNullableTToT(element);
167179
} else {
168180
throw multiples(false);
169181
}

guava/src/com/google/common/collect/MoreCollectors.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
package com.google.common.collect;
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.collect.NullnessCasts.uncheckedCastNullableTToT;
2021
import static java.util.Collections.emptyList;
2122

2223
import com.google.common.annotations.GwtCompatible;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526
import java.util.NoSuchElementException;
2627
import java.util.Optional;
28+
import java.util.function.Supplier;
2729
import java.util.stream.Collector;
2830
import org.jspecify.annotations.Nullable;
2931

@@ -43,7 +45,7 @@ public final class MoreCollectors {
4345
*/
4446
private static final Collector<Object, ?, Optional<Object>> TO_OPTIONAL =
4547
Collector.of(
46-
ToOptionalState::new,
48+
() -> new ToOptionalState<>(null),
4749
ToOptionalState::add,
4850
ToOptionalState::combine,
4951
ToOptionalState::getOptional,
@@ -65,8 +67,8 @@ public final class MoreCollectors {
6567
private static final Object NULL_PLACEHOLDER = new Object();
6668

6769
private static final Collector<@Nullable Object, ?, @Nullable Object> ONLY_ELEMENT =
68-
Collector.<@Nullable Object, ToOptionalState, @Nullable Object>of(
69-
ToOptionalState::new,
70+
Collector.<@Nullable Object, ToOptionalState<Object>, @Nullable Object>of(
71+
() -> new ToOptionalState<>(null),
7072
(state, o) -> state.add((o == null) ? NULL_PLACEHOLDER : o),
7173
ToOptionalState::combine,
7274
state -> {
@@ -90,18 +92,24 @@ public final class MoreCollectors {
9092
* than one, not just two.
9193
*/
9294
@SuppressWarnings("EmptyList") // ImmutableList doesn't support nullable element types
93-
private static final class ToOptionalState {
95+
@IgnoreJRERequirement // see enclosing class (whose annotation Animal Sniffer ignores here...)
96+
private static final class ToOptionalState<T> {
9497
static final int MAX_EXTRAS = 4;
9598

96-
@Nullable Object element;
97-
List<Object> extras;
99+
@Nullable T element;
100+
List<T> extras;
101+
final @Nullable Supplier<? extends RuntimeException> exceptionSupplier;
98102

99-
ToOptionalState() {
100-
element = null;
101-
extras = emptyList();
103+
ToOptionalState(@Nullable Supplier<? extends RuntimeException> exceptionSupplier) {
104+
this.element = null;
105+
this.extras = emptyList();
106+
this.exceptionSupplier = exceptionSupplier;
102107
}
103108

104-
IllegalArgumentException multiples(boolean overflow) {
109+
RuntimeException multiples(boolean overflow) {
110+
if (exceptionSupplier != null) {
111+
throw exceptionSupplier.get();
112+
}
105113
StringBuilder sb =
106114
new StringBuilder().append("expected one element but was: <").append(element);
107115
for (Object o : extras) {
@@ -114,7 +122,7 @@ IllegalArgumentException multiples(boolean overflow) {
114122
throw new IllegalArgumentException(sb.toString());
115123
}
116124

117-
void add(Object o) {
125+
void add(T o) {
118126
checkNotNull(o);
119127
if (element == null) {
120128
this.element = o;
@@ -129,7 +137,7 @@ void add(Object o) {
129137
}
130138
}
131139

132-
ToOptionalState combine(ToOptionalState other) {
140+
ToOptionalState<T> combine(ToOptionalState<T> other) {
133141
if (element == null) {
134142
return other;
135143
} else if (other.element == null) {
@@ -149,19 +157,23 @@ ToOptionalState combine(ToOptionalState other) {
149157
}
150158
}
151159

152-
Optional<Object> getOptional() {
160+
Optional<T> getOptional() {
153161
if (extras.isEmpty()) {
154162
return Optional.ofNullable(element);
155163
} else {
156164
throw multiples(false);
157165
}
158166
}
159167

160-
Object getElement() {
168+
T getElement() {
161169
if (element == null) {
162-
throw new NoSuchElementException();
170+
if (exceptionSupplier != null) {
171+
throw exceptionSupplier.get();
172+
} else {
173+
throw new NoSuchElementException();
174+
}
163175
} else if (extras.isEmpty()) {
164-
return element;
176+
return uncheckedCastNullableTToT(element);
165177
} else {
166178
throw multiples(false);
167179
}

0 commit comments

Comments
 (0)