-
Notifications
You must be signed in to change notification settings - Fork 587
/
Bag.java
313 lines (278 loc) · 9.88 KB
/
Bag.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (c) 2018 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.api.bag;
import java.util.Collection;
import java.util.DoubleSummaryStatistics;
import java.util.IntSummaryStatistics;
import java.util.LongSummaryStatistics;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Stream;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.primitive.DoubleFunction;
import org.eclipse.collections.api.block.function.primitive.FloatFunction;
import org.eclipse.collections.api.block.function.primitive.IntFunction;
import org.eclipse.collections.api.block.function.primitive.LongFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.list.ListIterable;
import org.eclipse.collections.api.map.MapIterable;
import org.eclipse.collections.api.multimap.bag.BagMultimap;
import org.eclipse.collections.api.partition.bag.PartitionBag;
import org.eclipse.collections.api.set.SetIterable;
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
/**
* A Bag is a Collection whose elements are unordered and may contain duplicate entries. It varies from
* MutableCollection in that it adds a protocol for determining, adding, and removing the number of occurrences for an
* item.
*
* @since 1.0
*/
public interface Bag<T>
extends RichIterable<T>
{
/**
* Two bags {@code b1} and {@code b2} are equal if {@code m1.toMapOfItemToCount().equals(m2.toMapOfItemToCount())}.
*
* @see Map#equals(Object)
*/
@Override
boolean equals(Object object);
/**
* Returns the hash code for this Bag, defined as <em>this.{@link #toMapOfItemToCount()}.hashCode()</em>.
*
* @see Map#hashCode()
*/
@Override
int hashCode();
@Override
Bag<T> tap(Procedure<? super T> procedure);
@Override
Bag<T> select(Predicate<? super T> predicate);
@Override
<P> Bag<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter);
@Override
Bag<T> reject(Predicate<? super T> predicate);
@Override
<P> Bag<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter);
@Override
PartitionBag<T> partition(Predicate<? super T> predicate);
@Override
<P> PartitionBag<T> partitionWith(Predicate2<? super T, ? super P> predicate, P parameter);
@Override
<S> Bag<S> selectInstancesOf(Class<S> clazz);
@Override
<V> BagMultimap<V, T> groupBy(Function<? super T, ? extends V> function);
@Override
<V> BagMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function);
@Override
SetIterable<Pair<T, Integer>> zipWithIndex();
/**
* For each distinct item, with the number of occurrences, execute the specified procedure.
*/
void forEachWithOccurrences(ObjectIntProcedure<? super T> procedure);
/**
* The occurrences of a distinct item in the bag.
*/
int occurrencesOf(Object item);
/**
* Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
*
* @since 3.0
*/
Bag<T> selectByOccurrences(IntPredicate predicate);
/**
* Returns all elements of the bag that have more than one occurrence.
*
* @since 9.2
*/
default Bag<T> selectDuplicates()
{
return this.selectByOccurrences(occurrences -> occurrences > 1);
}
/**
* Returns a set containing all elements of the bag that have exactly one occurrence.
*
* @since 9.2
*/
SetIterable<T> selectUnique();
/**
* Returns the {@code count} most frequently occurring items.
*
* In the event of a tie, all of the items with the number of occurrences that match the occurrences of the last
* item will be returned.
*
* @since 6.0
*/
ListIterable<ObjectIntPair<T>> topOccurrences(int count);
/**
* Returns the {@code count} least frequently occurring items.
*
* In the event of a tie, all of the items with the number of occurrences that match the occurrences of the last
* item will be returned.
*
* @since 6.0
*/
ListIterable<ObjectIntPair<T>> bottomOccurrences(int count);
/**
* The size of the Bag when counting only distinct elements.
*/
int sizeDistinct();
/**
* Converts the Bag to a Map of the Item type to its count as an Integer.
*/
MapIterable<T, Integer> toMapOfItemToCount();
/**
* Returns a string representation of this bag. The string representation consists of a list of element-count mappings.
* <p>
* <pre>
* Assert.assertEquals("{1=1, 2=2, 3=3}", Bags.mutable.with(1, 2, 2, 3, 3, 3).toStringOfItemToCount());
* </pre>
* This string representation is similar to {@link java.util.AbstractMap#toString()}, not {@link RichIterable#toString()},
* whereas the {@code toString()} implementation for a Bag is consistent with {@link RichIterable#toString()}.
*
* @return a string representation of this bag
* @since 3.0
*/
String toStringOfItemToCount();
ImmutableBagIterable<T> toImmutable();
/**
* @since 8.0
*/
@Override
default IntSummaryStatistics summarizeInt(IntFunction<? super T> function)
{
IntSummaryStatistics stats = new IntSummaryStatistics();
this.forEachWithOccurrences((each, occurrences) ->
{
int result = function.intValueOf(each);
for (int i = 0; i < occurrences; i++)
{
stats.accept(result);
}
});
return stats;
}
/**
* @since 8.0
*/
@Override
default DoubleSummaryStatistics summarizeFloat(FloatFunction<? super T> function)
{
DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
this.forEachWithOccurrences((each, occurrences) ->
{
float result = function.floatValueOf(each);
for (int i = 0; i < occurrences; i++)
{
stats.accept(result);
}
});
return stats;
}
/**
* @since 8.0
*/
@Override
default LongSummaryStatistics summarizeLong(LongFunction<? super T> function)
{
LongSummaryStatistics stats = new LongSummaryStatistics();
this.forEachWithOccurrences((each, occurrences) ->
{
long result = function.longValueOf(each);
for (int i = 0; i < occurrences; i++)
{
stats.accept(result);
}
});
return stats;
}
/**
* @since 8.0
*/
@Override
default DoubleSummaryStatistics summarizeDouble(DoubleFunction<? super T> function)
{
DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
this.forEachWithOccurrences((each, occurrences) ->
{
double result = function.doubleValueOf(each);
for (int i = 0; i < occurrences; i++)
{
stats.accept(result);
}
});
return stats;
}
/**
* This method produces the equivalent result as {@link Stream#collect(Collector)}.
*
* @since 8.0
*/
@Override
default <R, A> R reduceInPlace(Collector<? super T, A, R> collector)
{
A mutableResult = collector.supplier().get();
BiConsumer<A, ? super T> accumulator = collector.accumulator();
this.forEachWithOccurrences((each, occurrences) ->
{
for (int i = 0; i < occurrences; i++)
{
accumulator.accept(mutableResult, each);
}
});
return collector.finisher().apply(mutableResult);
}
/**
* This method produces the equivalent result as {@link Stream#collect(Supplier, BiConsumer, BiConsumer)}.
*
* @since 8.0
*/
@Override
default <R> R reduceInPlace(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator)
{
R mutableResult = supplier.get();
this.forEachWithOccurrences((each, occurrences) ->
{
for (int i = 0; i < occurrences; i++)
{
accumulator.accept(mutableResult, each);
}
});
return mutableResult;
}
/**
* Iterates over the unique elements and their occurrences and collects the results of applying the specified function.
*
* @since 10.0
*/
<V> RichIterable<V> collectWithOccurrences(ObjectIntToObjectFunction<? super T, ? extends V> function);
/**
* Iterates over the unique elements and their occurrences and collects the results of applying the
* specified function into the target collection.
*
* @since 9.1.
*/
default <V, R extends Collection<V>> R collectWithOccurrences(
ObjectIntToObjectFunction<? super T, ? extends V> function,
R target)
{
this.forEachWithOccurrences((each, occurrences) -> target.add(function.valueOf(each, occurrences)));
return target;
}
}