01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.core;
16:
17: import java.util.AbstractSet;
18: import java.util.Collection;
19: import java.util.Iterator;
20: import java.util.Set;
21: import java.util.WeakHashMap;
22:
23: /**
24: * A set backed onto a WeakHashMap. Will not allow nulls
25: *
26: * @author Jesse
27: * @since 1.1.0
28: */
29: public class WeakHashSet<T> extends AbstractSet<T> implements Set<T> {
30: WeakHashMap<T, T> set = new WeakHashMap<T, T>();
31:
32: public int size() {
33: return set.keySet().size();
34: }
35:
36: public boolean isEmpty() {
37: return set.isEmpty();
38: }
39:
40: public boolean contains(Object o) {
41: return set.containsKey(o);
42: }
43:
44: public Iterator<T> iterator() {
45: return set.keySet().iterator();
46: }
47:
48: public Object[] toArray() {
49: return set.keySet().toArray();
50: }
51:
52: public <M> M[] toArray(M[] a) {
53: return set.keySet().toArray(a);
54: }
55:
56: public boolean add(T o) {
57: if (o == null)
58: throw new NullPointerException("Argument is null"); //$NON-NLS-1$
59: if (set.containsKey(o))
60: return false;
61: set.put(o, o);
62: return true;
63: }
64:
65: public boolean remove(Object o) {
66: return set.remove(o) != null;
67: }
68:
69: public boolean containsAll(Collection<?> c) {
70: return set.keySet().containsAll(c);
71: }
72:
73: public boolean addAll(Collection<? extends T> c) {
74: boolean added = false;
75: for (T t : c) {
76: if (add(t))
77: added = true;
78: }
79: return added;
80: }
81:
82: public boolean removeAll(Collection<?> c) {
83: boolean removed = false;
84: for (Object object : c) {
85: if (remove(object))
86: removed = true;
87: }
88: return removed;
89: }
90:
91: public void clear() {
92: set.clear();
93: }
94:
95: }
|