1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.util.AbstractSet;
18  import java.util.Iterator;
19  import java.util.Map;
20  import java.util.Set;
21  import java.util.concurrent.ConcurrentHashMap;
22  
23  /**
24   * <a href="ConcurrentHashSet.java.html"><b><i>View Source</i></b></a>
25   *
26   * @author Brian Wing Shun Chan
27   */
28  public class ConcurrentHashSet<E> extends AbstractSet<E> {
29  
30      public ConcurrentHashSet() {
31          _map = new ConcurrentHashMap<E, String>();
32      }
33  
34      public ConcurrentHashSet(int capacity) {
35          _map = new ConcurrentHashMap<E, String>(capacity);
36      }
37  
38      public ConcurrentHashSet(Set<E> set) {
39          Iterator<E> itr = set.iterator();
40  
41          while (itr.hasNext()) {
42              E e = itr.next();
43  
44              _map.put(e, StringPool.BLANK);
45          }
46      }
47  
48      public boolean add(E e) {
49          if (_map.put(e, StringPool.BLANK) == null) {
50              return true;
51          }
52          else {
53              return false;
54          }
55      }
56  
57      public void clear() {
58          _map.clear();
59      }
60  
61      public boolean contains(Object obj) {
62          if (_map.containsKey(obj)) {
63              return true;
64          }
65          else {
66              return false;
67          }
68      }
69  
70      public Iterator<E> iterator() {
71          return _map.keySet().iterator();
72      }
73  
74      public boolean remove(Object obj) {
75          if (_map.remove(obj) == null) {
76              return false;
77          }
78          else {
79              return true;
80          }
81      }
82  
83      public int size() {
84          return _map.size();
85      }
86  
87      private Map<E, String> _map;
88  
89  }