/
MoreCollectors.java
178 lines (160 loc) · 5.74 KB
/
MoreCollectors.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.common.collect;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Collections.emptyList;
import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collector;
import javax.annotation.CheckForNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Collectors not present in {@code java.util.stream.Collectors} that are not otherwise associated
* with a {@code com.google.common} type.
*
* @author Louis Wasserman
* @since 33.2.0 (available since 21.0 in guava-jre)
*/
@GwtCompatible
@ElementTypesAreNonnullByDefault
@SuppressWarnings({"AndroidJdkLibsChecker", "Java7ApiChecker"})
@IgnoreJRERequirement // Users will use this only if they're already using streams.
@Beta // TODO: b/288085449 - Remove.
public final class MoreCollectors {
/*
* TODO(lowasser): figure out if we can convert this to a concurrent AtomicReference-based
* collector without breaking j2cl?
*/
private static final Collector<Object, ?, Optional<Object>> TO_OPTIONAL =
Collector.of(
ToOptionalState::new,
ToOptionalState::add,
ToOptionalState::combine,
ToOptionalState::getOptional,
Collector.Characteristics.UNORDERED);
/**
* A collector that converts a stream of zero or one elements to an {@code Optional}.
*
* @throws IllegalArgumentException if the stream consists of two or more elements.
* @throws NullPointerException if any element in the stream is {@code null}.
* @return {@code Optional.of(onlyElement)} if the stream has exactly one element (must not be
* {@code null}) and returns {@code Optional.empty()} if it has none.
*/
@SuppressWarnings("unchecked")
public static <T> Collector<T, ?, Optional<T>> toOptional() {
return (Collector) TO_OPTIONAL;
}
private static final Object NULL_PLACEHOLDER = new Object();
private static final Collector<@Nullable Object, ?, @Nullable Object> ONLY_ELEMENT =
Collector.<@Nullable Object, ToOptionalState, @Nullable Object>of(
ToOptionalState::new,
(state, o) -> state.add((o == null) ? NULL_PLACEHOLDER : o),
ToOptionalState::combine,
state -> {
Object result = state.getElement();
return (result == NULL_PLACEHOLDER) ? null : result;
},
Collector.Characteristics.UNORDERED);
/**
* A collector that takes a stream containing exactly one element and returns that element. The
* returned collector throws an {@code IllegalArgumentException} if the stream consists of two or
* more elements, and a {@code NoSuchElementException} if the stream is empty.
*/
@SuppressWarnings("unchecked")
public static <T extends @Nullable Object> Collector<T, ?, T> onlyElement() {
return (Collector) ONLY_ELEMENT;
}
/**
* This atrocity is here to let us report several of the elements in the stream if there were more
* than one, not just two.
*/
private static final class ToOptionalState {
static final int MAX_EXTRAS = 4;
@CheckForNull Object element;
List<Object> extras;
ToOptionalState() {
element = null;
extras = emptyList();
}
IllegalArgumentException multiples(boolean overflow) {
StringBuilder sb =
new StringBuilder().append("expected one element but was: <").append(element);
for (Object o : extras) {
sb.append(", ").append(o);
}
if (overflow) {
sb.append(", ...");
}
sb.append('>');
throw new IllegalArgumentException(sb.toString());
}
void add(Object o) {
checkNotNull(o);
if (element == null) {
this.element = o;
} else if (extras.isEmpty()) {
// Replace immutable empty list with mutable list.
extras = new ArrayList<>(MAX_EXTRAS);
extras.add(o);
} else if (extras.size() < MAX_EXTRAS) {
extras.add(o);
} else {
throw multiples(true);
}
}
ToOptionalState combine(ToOptionalState other) {
if (element == null) {
return other;
} else if (other.element == null) {
return this;
} else {
if (extras.isEmpty()) {
// Replace immutable empty list with mutable list.
extras = new ArrayList<>();
}
extras.add(other.element);
extras.addAll(other.extras);
if (extras.size() > MAX_EXTRAS) {
extras.subList(MAX_EXTRAS, extras.size()).clear();
throw multiples(true);
}
return this;
}
}
@IgnoreJRERequirement // see enclosing class (whose annotation Animal Sniffer ignores here...)
Optional<Object> getOptional() {
if (extras.isEmpty()) {
return Optional.ofNullable(element);
} else {
throw multiples(false);
}
}
Object getElement() {
if (element == null) {
throw new NoSuchElementException();
} else if (extras.isEmpty()) {
return element;
} else {
throw multiples(false);
}
}
}
private MoreCollectors() {}
}