KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > ConcurrentHashSet


1 /**
2  * $RCSfile: ConcurrentHashSet.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2004/10/21 07:28:12 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.util;
13
14 import java.util.*;
15 import java.util.concurrent.ConcurrentHashMap JavaDoc;
16
17 /**
18  * This class implements the <tt>Set</tt> interface, backed by a ConcurrentHashMap instance.
19  *
20  * @author Matt Tucker
21  */

22 public class ConcurrentHashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable JavaDoc,
23         java.io.Serializable JavaDoc
24 {
25
26     private transient ConcurrentHashMap JavaDoc<E,Object JavaDoc> map;
27
28     // Dummy value to associate with an Object in the backing Map
29
private static final Object JavaDoc PRESENT = new Object JavaDoc();
30
31     /**
32      * Constructs a new, empty set; the backing <tt>ConcurrentHashMap</tt> instance has
33      * default initial capacity (16) and load factor (0.75).
34      */

35     public ConcurrentHashSet() {
36         map = new ConcurrentHashMap JavaDoc<E,Object JavaDoc>();
37     }
38
39     /**
40      * Constructs a new set containing the elements in the specified
41      * collection. The <tt>ConcurrentHashMap</tt> is created with default load factor
42      * (0.75) and an initial capacity sufficient to contain the elements in
43      * the specified collection.
44      *
45      * @param c the collection whose elements are to be placed into this set.
46      * @throws NullPointerException if the specified collection is null.
47      */

48     public ConcurrentHashSet(Collection<? extends E> c) {
49         map = new ConcurrentHashMap JavaDoc<E,Object JavaDoc>(Math.max((int) (c.size()/.75f) + 1, 16));
50         addAll(c);
51     }
52
53     /**
54      * Constructs a new, empty set; the backing <tt>ConcurrentHashMap</tt> instance has
55      * the specified initial capacity and the specified load factor.
56      *
57      * @param initialCapacity the initial capacity of the hash map.
58      * @param loadFactor the load factor of the hash map.
59      * @throws IllegalArgumentException if the initial capacity is less
60      * than zero, or if the load factor is nonpositive.
61      */

62     public ConcurrentHashSet(int initialCapacity, float loadFactor) {
63         map = new ConcurrentHashMap JavaDoc<E,Object JavaDoc>(initialCapacity, loadFactor, 16);
64     }
65
66     /**
67      * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
68      * the specified initial capacity and default load factor, which is
69      * <tt>0.75</tt>.
70      *
71      * @param initialCapacity the initial capacity of the hash table.
72      * @throws IllegalArgumentException if the initial capacity is less
73      * than zero.
74      */

75     public ConcurrentHashSet(int initialCapacity) {
76         map = new ConcurrentHashMap JavaDoc<E,Object JavaDoc>(initialCapacity);
77     }
78
79     public Iterator<E> iterator() {
80         return map.keySet().iterator();
81     }
82
83     public int size() {
84         return map.size();
85     }
86
87     public boolean isEmpty() {
88         return map.isEmpty();
89     }
90
91     public boolean contains(Object JavaDoc o) {
92         return map.containsKey(o);
93     }
94
95     public boolean add(E o) {
96         return map.put(o, PRESENT)==null;
97     }
98
99     public boolean remove(Object JavaDoc o) {
100         return map.remove(o)==PRESENT;
101     }
102
103     public void clear() {
104         map.clear();
105     }
106
107     public Object JavaDoc clone() {
108         try {
109             ConcurrentHashSet<E> newSet = (ConcurrentHashSet<E>)super.clone();
110             newSet.map.putAll(map);
111             return newSet;
112         }
113         catch (CloneNotSupportedException JavaDoc e) {
114             throw new InternalError JavaDoc();
115         }
116     }
117
118     private void writeObject(java.io.ObjectOutputStream JavaDoc s) throws java.io.IOException JavaDoc {
119         s.defaultWriteObject();
120
121         // Write out size
122
s.writeInt(map.size());
123
124         // Write out all elements in the proper order.
125
for (Iterator i=map.keySet().iterator(); i.hasNext(); )
126             s.writeObject(i.next());
127         }
128
129     /**
130      * Reconstitute the <tt>HashSet</tt> instance from a stream (that is,
131      * deserialize it).
132      */

133     private void readObject(java.io.ObjectInputStream JavaDoc s)
134             throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc
135     {
136         s.defaultReadObject();
137
138         map = new ConcurrentHashMap JavaDoc<E, Object JavaDoc>();
139
140         // Read in size
141
int size = s.readInt();
142
143         // Read in all elements in the proper order.
144
for (int i=0; i<size; i++) {
145             E e = (E) s.readObject();
146             map.put(e, PRESENT);
147         }
148     }
149 }
Popular Tags