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