View Javadoc

1   /*
2    * $Id: ConcurrentHashSet.java 8077 2007-08-27 20:15:25Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util.concurrent;
12  
13  /*
14   * Written by Doug Lea with assistance from members of JCP JSR-166 Expert Group and
15   * released to the public domain, as explained at
16   * http://creativecommons.org/licenses/publicdomain
17   */
18  
19  import java.io.IOException;
20  import java.io.Serializable;
21  import java.util.AbstractSet;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.Set;
25  
26  import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
27  
28  public class ConcurrentHashSet/* <E> */extends AbstractSet/* <E> */implements Set/* <E> */, Serializable
29  {
30      private static final long serialVersionUID = 2454657854757543876L;
31  
32      private final ConcurrentHashMap/* <E, Boolean> */map;
33      private transient Set/* <E> */keySet;
34  
35      public ConcurrentHashSet()
36      {
37          map = new ConcurrentHashMap/* <E, Boolean> */();
38          keySet = map.keySet();
39      }
40  
41      public ConcurrentHashSet(int initialCapacity)
42      {
43          map = new ConcurrentHashMap/* <E, Boolean> */(initialCapacity);
44          keySet = map.keySet();
45      }
46  
47      public ConcurrentHashSet(int initialCapacity, float loadFactor, int concurrencyLevel)
48      {
49          map = new ConcurrentHashMap/* <E, Boolean> */(initialCapacity, loadFactor, concurrencyLevel);
50          keySet = map.keySet();
51      }
52  
53      public int size()
54      {
55          return map.size();
56      }
57  
58      public boolean isEmpty()
59      {
60          return map.isEmpty();
61      }
62  
63      public boolean contains(Object o)
64      {
65          return map.containsKey(o);
66      }
67  
68      public Iterator/* <E> */iterator()
69      {
70          return keySet.iterator();
71      }
72  
73      public Object[] toArray()
74      {
75          return keySet.toArray();
76      }
77  
78      public/* <T> T[] */Object[] toArray(Object[]/* T[] */a)
79      {
80          return keySet.toArray(a);
81      }
82  
83      public boolean add(Object/* E */e)
84      {
85          return map.put(e, Boolean.TRUE) == null;
86      }
87  
88      public boolean remove(Object o)
89      {
90          return map.remove(o) != null;
91      }
92  
93      public boolean removeAll(Collection/* <?> */c)
94      {
95          return keySet.removeAll(c);
96      }
97  
98      public boolean retainAll(Collection/* <?> */c)
99      {
100         return keySet.retainAll(c);
101     }
102 
103     public void clear()
104     {
105         map.clear();
106     }
107 
108     public boolean equals(Object o)
109     {
110         return keySet.equals(o);
111     }
112 
113     public int hashCode()
114     {
115         return keySet.hashCode();
116     }
117 
118     private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException
119     {
120         s.defaultReadObject();
121         keySet = map.keySet();
122     }
123 
124 }