-
Notifications
You must be signed in to change notification settings - Fork 587
/
HashBag.java
170 lines (146 loc) · 4.39 KB
/
HashBag.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
/*
* Copyright (c) 2022 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.impl.bag.mutable;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Arrays;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.factory.primitive.ObjectIntMaps;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;
/**
* A HashBag is a MutableBag which uses a Map as its underlying data store. Each key in the Map represents some item,
* and the value in the map represents the current number of occurrences of that item.
*
* @since 1.0
*/
public class HashBag<T>
extends AbstractHashBag<T>
implements Externalizable
{
private static final long serialVersionUID = 1L;
public HashBag()
{
this.items = ObjectIntMaps.mutable.empty();
}
public HashBag(int size)
{
this.items = ObjectIntMaps.mutable.withInitialCapacity(size);
}
private HashBag(MutableObjectIntMap<T> map)
{
this.items = map;
this.size = (int) map.sum();
}
public static <E> HashBag<E> newBag()
{
return new HashBag<>();
}
public static <E> HashBag<E> newBag(int size)
{
return new HashBag<>(size);
}
public static <E> HashBag<E> newBag(Bag<? extends E> source)
{
//noinspection SSBasedInspection
HashBag<E> result = HashBag.newBag(source.sizeDistinct());
result.addAllBag(source);
return result;
}
public static <E> HashBag<E> newBag(Iterable<? extends E> source)
{
if (source instanceof Bag)
{
return HashBag.newBag((Bag<E>) source);
}
return HashBag.newBagWith((E[]) Iterate.toArray(source));
}
public static <E> HashBag<E> newBagWith(E... elements)
{
HashBag<E> result = HashBag.newBag();
ArrayIterate.addAllTo(elements, result);
return result;
}
@Override
protected int computeHashCode(T item)
{
return item.hashCode();
}
@Override
public MutableBag<T> selectByOccurrences(IntPredicate predicate)
{
MutableObjectIntMap<T> map = this.items.select((each, occurrences) -> predicate.accept(occurrences));
return new HashBag<>(map);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
((Externalizable) this.items).writeExternal(out);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
this.items = ObjectIntMaps.mutable.empty();
((Externalizable) this.items).readExternal(in);
this.size = (int) this.items.sum();
}
@Override
public HashBag<T> without(T element)
{
this.remove(element);
return this;
}
@Override
public MutableBag<T> newEmpty()
{
return HashBag.newBag();
}
@Override
public HashBag<T> with(T element)
{
this.add(element);
return this;
}
@Override
public HashBag<T> withAll(Iterable<? extends T> iterable)
{
this.addAllIterable(iterable);
return this;
}
@Override
public HashBag<T> withoutAll(Iterable<? extends T> iterable)
{
this.removeAllIterable(iterable);
return this;
}
public HashBag<T> with(T... elements)
{
this.addAll(Arrays.asList(elements));
return this;
}
public HashBag<T> with(T element1, T element2)
{
this.add(element1);
this.add(element2);
return this;
}
public HashBag<T> with(T element1, T element2, T element3)
{
this.add(element1);
this.add(element2);
this.add(element3);
return this;
}
}