summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-01-08 11:57:57 (EST)
committerEike Stepper2007-01-08 11:57:57 (EST)
commit2bd939fd8cad71a483fd600a7a00327c9dde8a1e (patch)
treeb5679fba2f7c67effa91cc831609226b36838952
parentd36d321be93df5030a4684753e2f145f058fe3d1 (diff)
downloadcdo-2bd939fd8cad71a483fd600a7a00327c9dde8a1e.zip
cdo-2bd939fd8cad71a483fd600a7a00327c9dde8a1e.tar.gz
cdo-2bd939fd8cad71a483fd600a7a00327c9dde8a1e.tar.bz2
Added HashBag
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/HashBag.java171
1 files changed, 171 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/HashBag.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/HashBag.java
new file mode 100644
index 0000000..29de7d4
--- /dev/null
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/HashBag.java
@@ -0,0 +1,171 @@
+/***************************************************************************
+ * Copyright (c) 2004, 2005, 2006 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.util;
+
+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();
+ }
+
+ public HashBag(int initialCapacity, float loadFactor)
+ {
+ map = new HashMap(initialCapacity, loadFactor);
+ }
+
+ public HashBag(int initialCapacity)
+ {
+ map = new HashMap(initialCapacity);
+ }
+
+ public HashBag(Map<? extends T, ? extends HashBag.Counter> m)
+ {
+ map = new HashMap(m);
+ }
+
+ public boolean add(T o)
+ {
+ HashBag.Counter counter = map.get(o);
+ if (counter == null)
+ {
+ counter = new Counter();
+ map.put(o, counter);
+ }
+
+ counter.incValue();
+ return true;
+ }
+
+ 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;
+
+ public Counter()
+ {
+ }
+
+ public int getValue()
+ {
+ return value;
+ }
+
+ public int incValue()
+ {
+ return ++value;
+ }
+
+ public int decValue()
+ {
+ return --value;
+ }
+ }
+} \ No newline at end of file