Skip to content

Commit 491f4c4

Browse files
java-team-github-botGoogle Java Core Libraries
authored andcommitted
Internal change
PiperOrigin-RevId: 868918021
1 parent 47dbf24 commit 491f4c4

File tree

2 files changed

+32
-54
lines changed

2 files changed

+32
-54
lines changed

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

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
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;
2120
import static java.util.Collections.emptyList;
2221

2322
import com.google.common.annotations.GwtCompatible;
24-
import com.google.common.base.Supplier;
2523
import java.util.ArrayList;
2624
import java.util.List;
2725
import java.util.NoSuchElementException;
@@ -46,7 +44,7 @@ public final class MoreCollectors {
4644
*/
4745
private static final Collector<Object, ?, Optional<Object>> TO_OPTIONAL =
4846
Collector.of(
49-
() -> new ToOptionalState<>(null),
47+
ToOptionalState::new,
5048
ToOptionalState::add,
5149
ToOptionalState::combine,
5250
ToOptionalState::getOptional,
@@ -68,8 +66,8 @@ public final class MoreCollectors {
6866
private static final Object NULL_PLACEHOLDER = new Object();
6967

7068
private static final Collector<@Nullable Object, ?, @Nullable Object> ONLY_ELEMENT =
71-
Collector.<@Nullable Object, ToOptionalState<Object>, @Nullable Object>of(
72-
() -> new ToOptionalState<>(null),
69+
Collector.<@Nullable Object, ToOptionalState, @Nullable Object>of(
70+
ToOptionalState::new,
7371
(state, o) -> state.add((o == null) ? NULL_PLACEHOLDER : o),
7472
ToOptionalState::combine,
7573
state -> {
@@ -93,23 +91,18 @@ public final class MoreCollectors {
9391
* than one, not just two.
9492
*/
9593
@SuppressWarnings("EmptyList") // ImmutableList doesn't support nullable element types
96-
private static final class ToOptionalState<T> {
94+
private static final class ToOptionalState {
9795
static final int MAX_EXTRAS = 4;
9896

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

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

109-
RuntimeException multiples(boolean overflow) {
110-
if (exceptionSupplier != null) {
111-
throw exceptionSupplier.get();
112-
}
105+
IllegalArgumentException multiples(boolean overflow) {
113106
StringBuilder sb =
114107
new StringBuilder().append("expected one element but was: <").append(element);
115108
for (Object o : extras) {
@@ -122,7 +115,7 @@ RuntimeException multiples(boolean overflow) {
122115
throw new IllegalArgumentException(sb.toString());
123116
}
124117

125-
void add(T o) {
118+
void add(Object o) {
126119
checkNotNull(o);
127120
if (element == null) {
128121
this.element = o;
@@ -137,7 +130,7 @@ void add(T o) {
137130
}
138131
}
139132

140-
ToOptionalState<T> combine(ToOptionalState<T> other) {
133+
ToOptionalState combine(ToOptionalState other) {
141134
if (element == null) {
142135
return other;
143136
} else if (other.element == null) {
@@ -158,23 +151,19 @@ ToOptionalState<T> combine(ToOptionalState<T> other) {
158151
}
159152

160153
@IgnoreJRERequirement // see enclosing class (whose annotation Animal Sniffer ignores here...)
161-
Optional<T> getOptional() {
154+
Optional<Object> getOptional() {
162155
if (extras.isEmpty()) {
163156
return Optional.ofNullable(element);
164157
} else {
165158
throw multiples(false);
166159
}
167160
}
168161

169-
T getElement() {
162+
Object getElement() {
170163
if (element == null) {
171-
if (exceptionSupplier != null) {
172-
throw exceptionSupplier.get();
173-
} else {
174-
throw new NoSuchElementException();
175-
}
164+
throw new NoSuchElementException();
176165
} else if (extras.isEmpty()) {
177-
return uncheckedCastNullableTToT(element);
166+
return element;
178167
} else {
179168
throw multiples(false);
180169
}

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

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
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;
2120
import static java.util.Collections.emptyList;
2221

2322
import com.google.common.annotations.GwtCompatible;
24-
import com.google.common.base.Supplier;
2523
import java.util.ArrayList;
2624
import java.util.List;
2725
import java.util.NoSuchElementException;
@@ -45,7 +43,7 @@ public final class MoreCollectors {
4543
*/
4644
private static final Collector<Object, ?, Optional<Object>> TO_OPTIONAL =
4745
Collector.of(
48-
() -> new ToOptionalState<>(null),
46+
ToOptionalState::new,
4947
ToOptionalState::add,
5048
ToOptionalState::combine,
5149
ToOptionalState::getOptional,
@@ -67,8 +65,8 @@ public final class MoreCollectors {
6765
private static final Object NULL_PLACEHOLDER = new Object();
6866

6967
private static final Collector<@Nullable Object, ?, @Nullable Object> ONLY_ELEMENT =
70-
Collector.<@Nullable Object, ToOptionalState<Object>, @Nullable Object>of(
71-
() -> new ToOptionalState<>(null),
68+
Collector.<@Nullable Object, ToOptionalState, @Nullable Object>of(
69+
ToOptionalState::new,
7270
(state, o) -> state.add((o == null) ? NULL_PLACEHOLDER : o),
7371
ToOptionalState::combine,
7472
state -> {
@@ -92,23 +90,18 @@ public final class MoreCollectors {
9290
* than one, not just two.
9391
*/
9492
@SuppressWarnings("EmptyList") // ImmutableList doesn't support nullable element types
95-
private static final class ToOptionalState<T> {
93+
private static final class ToOptionalState {
9694
static final int MAX_EXTRAS = 4;
9795

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

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

108-
RuntimeException multiples(boolean overflow) {
109-
if (exceptionSupplier != null) {
110-
throw exceptionSupplier.get();
111-
}
104+
IllegalArgumentException multiples(boolean overflow) {
112105
StringBuilder sb =
113106
new StringBuilder().append("expected one element but was: <").append(element);
114107
for (Object o : extras) {
@@ -121,7 +114,7 @@ RuntimeException multiples(boolean overflow) {
121114
throw new IllegalArgumentException(sb.toString());
122115
}
123116

124-
void add(T o) {
117+
void add(Object o) {
125118
checkNotNull(o);
126119
if (element == null) {
127120
this.element = o;
@@ -136,7 +129,7 @@ void add(T o) {
136129
}
137130
}
138131

139-
ToOptionalState<T> combine(ToOptionalState<T> other) {
132+
ToOptionalState combine(ToOptionalState other) {
140133
if (element == null) {
141134
return other;
142135
} else if (other.element == null) {
@@ -156,23 +149,19 @@ ToOptionalState<T> combine(ToOptionalState<T> other) {
156149
}
157150
}
158151

159-
Optional<T> getOptional() {
152+
Optional<Object> getOptional() {
160153
if (extras.isEmpty()) {
161154
return Optional.ofNullable(element);
162155
} else {
163156
throw multiples(false);
164157
}
165158
}
166159

167-
T getElement() {
160+
Object getElement() {
168161
if (element == null) {
169-
if (exceptionSupplier != null) {
170-
throw exceptionSupplier.get();
171-
} else {
172-
throw new NoSuchElementException();
173-
}
162+
throw new NoSuchElementException();
174163
} else if (extras.isEmpty()) {
175-
return uncheckedCastNullableTToT(element);
164+
return element;
176165
} else {
177166
throw multiples(false);
178167
}

0 commit comments

Comments
 (0)