| ConcurrentHashSet.java |
1 /**
2 * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 2.1 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
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 }