Skip to main content
summaryrefslogtreecommitdiffstats
blob: f541ab56764e1768472a7f43ecf09e43135a962b (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * 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.internal.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, HashBag.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 HashBag.Counter> m)
  {
    map = new HashMap<T, Counter>(m);
  }

  public boolean add(T o)
  {
    HashBag.Counter counter = map.get(o);
    if (counter == null)
    {
      counter = new Counter();
      map.put(o, counter);
      return true;
    }

    counter.incValue();
    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)
  {
    HashBag.Counter counter = map.get(o);
    if (counter == null)
    {
      return false;
    }

    if (counter.decValue() == 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);
  }

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

    public Counter()
    {
    }

    public int getValue()
    {
      return value;
    }

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

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

Back to the top