View Javadoc

1   /*
2    * $Id: ConcurrentHashSet.java 21939 2011-05-18 13:32:09Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  import java.util.concurrent.ConcurrentHashMap;
26  
27  public class ConcurrentHashSet/* <E> */extends AbstractSet/* <E> */implements Set/* <E> */, Serializable
28  {
29      private static final long serialVersionUID = 2454657854757543876L;
30  
31      private final ConcurrentHashMap/* <E, Boolean> */map;
32      private transient Set/* <E> */keySet;
33  
34      public ConcurrentHashSet()
35      {
36          map = new ConcurrentHashMap/* <E, Boolean> */();
37          keySet = map.keySet();
38      }
39  
40      public ConcurrentHashSet(int initialCapacity)
41      {
42          map = new ConcurrentHashMap/* <E, Boolean> */(initialCapacity);
43          keySet = map.keySet();
44      }
45  
46      public ConcurrentHashSet(int initialCapacity, float loadFactor, int concurrencyLevel)
47      {
48          map = new ConcurrentHashMap/* <E, Boolean> */(initialCapacity, loadFactor, concurrencyLevel);
49          keySet = map.keySet();
50      }
51  
52      public int size()
53      {
54          return map.size();
55      }
56  
57      public boolean isEmpty()
58      {
59          return map.isEmpty();
60      }
61  
62      public boolean contains(Object o)
63      {
64          return map.containsKey(o);
65      }
66  
67      public Iterator/* <E> */iterator()
68      {
69          return keySet.iterator();
70      }
71  
72      public Object[] toArray()
73      {
74          return keySet.toArray();
75      }
76  
77      public/* <T> T[] */Object[] toArray(Object[]/* T[] */a)
78      {
79          return keySet.toArray(a);
80      }
81  
82      public boolean add(Object/* E */e)
83      {
84          return map.put(e, Boolean.TRUE) == null;
85      }
86  
87      public boolean remove(Object o)
88      {
89          return map.remove(o) != null;
90      }
91  
92      public boolean removeAll(Collection/* <?> */c)
93      {
94          return keySet.removeAll(c);
95      }
96  
97      public boolean retainAll(Collection/* <?> */c)
98      {
99          return keySet.retainAll(c);
100     }
101 
102     public void clear()
103     {
104         map.clear();
105     }
106 
107     public boolean equals(Object o)
108     {
109         return keySet.equals(o);
110     }
111 
112     public int hashCode()
113     {
114         return keySet.hashCode();
115     }
116 
117     private void readObject(java.io.ObjectInputStream s) throws IOException, ClassNotFoundException
118     {
119         s.defaultReadObject();
120         keySet = map.keySet();
121     }
122 
123 }