Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7e9e087bb2860c038a054e629aa048a2e3782775 (plain) (blame)
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
/*
 * Copyright (c) 2008-2012, 2014, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.util.collection;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public final class HashBag<T> implements Set<T>
{
  private Map<T, Counter> map;

  public HashBag()
  {
    map = new HashMap<T, Counter>();
  }

  public HashBag(int initialCapacity, float loadFactor)
  {
    map = new HashMap<T, Counter>(initialCapacity, loadFactor);
  }

  public HashBag(int initialCapacity)
  {
    map = new HashMap<T, Counter>(initialCapacity);
  }

  public HashBag(Map<? extends T, ? extends Counter> m)
  {
    map = new HashMap<T, Counter>(m);
  }

  /**
   * @since 3.0
   */
  public int getCounterFor(T o)
  {
    Counter counter = map.get(o);
    if (counter == null)
    {
      return 0;
    }

    return counter.getValue();
  }

  /**
   * @since 3.7
   */
  public int removeCounterFor(T o)
  {
    Counter counter = map.remove(o);
    if (counter == null)
    {
      return 0;
    }

    return counter.getValue();
  }

  public boolean add(T o)
  {
    return add(o, 1);
  }

  /**
   * @since 3.4
   */
  public boolean add(T o, int count)
  {
    Counter counter = map.get(o);
    if (counter == null)
    {
      counter = new Counter(count);
      map.put(o, counter);
      return true;
    }

    counter.incValue(count);
    return false;
  }

  public boolean addAll(Collection<? extends T> c)
  {
    for (T t : c)
    {
      add(t);
    }

    return true;
  }

  public void clear()
  {
    map.clear();
  }

  public boolean contains(Object o)
  {
    return map.containsKey(o);
  }

  public boolean containsAll(Collection<?> c)
  {
    return map.keySet().containsAll(c);
  }

  public boolean isEmpty()
  {
    return map.isEmpty();
  }

  public Iterator<T> iterator()
  {
    return map.keySet().iterator();
  }

  public boolean remove(Object o)
  {
    return remove(o, 1);
  }

  /**
   * @since 3.4
   */
  public boolean remove(Object o, int count)
  {
    Counter counter = map.get(o);
    if (counter == null)
    {
      return false;
    }

    if (counter.decValue(count) <= 0)
    {
      map.remove(o);
    }

    return true;
  }

  public boolean removeAll(Collection<?> c)
  {
    boolean changed = false;
    for (Object object : c)
    {
      if (remove(object))
      {
        changed = true;
      }
    }

    return changed;
  }

  public boolean retainAll(Collection<?> c)
  {
    throw new UnsupportedOperationException();
  }

  public int size()
  {
    return map.size();
  }

  public Object[] toArray()
  {
    return map.keySet().toArray();
  }

  @SuppressWarnings("hiding")
  public <T> T[] toArray(T[] a)
  {
    return map.keySet().toArray(a);
  }

  @Override
  public String toString()
  {
    return map.toString();
  }

  /**
   * @author Eike Stepper
   */
  private static final class Counter
  {
    private int value;

    public Counter(int value)
    {
      this.value = value;
    }

    public int getValue()
    {
      return value;
    }

    public int incValue(int count)
    {
      return value += count;
    }

    public int decValue(int count)
    {
      return value -= count;
    }

    @Override
    public String toString()
    {
      return Integer.toString(value);
    }
  }
}

Back to the top