| ConcurrentHashSet.java |
1 /**
2 * Copyright (c) 2000-2009 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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.portal.kernel.util;
21
22 import java.util.AbstractSet;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 /**
29 * <a href="ConcurrentHashSet.java.html"><b><i>View Source</i></b></a>
30 *
31 * @author Brian Wing Shun Chan
32 *
33 */
34 public class ConcurrentHashSet<E> extends AbstractSet<E> {
35
36 public ConcurrentHashSet() {
37 _map = new ConcurrentHashMap<E, String>();
38 }
39
40 public ConcurrentHashSet(int capacity) {
41 _map = new ConcurrentHashMap<E, String>(capacity);
42 }
43
44 public ConcurrentHashSet(Set<E> set) {
45 Iterator<E> itr = set.iterator();
46
47 while (itr.hasNext()) {
48 E e = itr.next();
49
50 _map.put(e, StringPool.BLANK);
51 }
52 }
53
54 public boolean add(E e) {
55 if (_map.put(e, StringPool.BLANK) == null) {
56 return true;
57 }
58 else {
59 return false;
60 }
61 }
62
63 public void clear() {
64 _map.clear();
65 }
66
67 public boolean contains(Object obj) {
68 if (_map.containsKey(obj)) {
69 return true;
70 }
71 else {
72 return false;
73 }
74 }
75
76 public Iterator<E> iterator() {
77 return _map.keySet().iterator();
78 }
79
80 public boolean remove(Object obj) {
81 if (_map.remove(obj) == null) {
82 return false;
83 }
84 else {
85 return true;
86 }
87 }
88
89 public int size() {
90 return _map.size();
91 }
92
93 private Map<E, String> _map;
94
95 }