001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.util.AbstractSet;
018    import java.util.Iterator;
019    import java.util.Map;
020    import java.util.Set;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class ConcurrentHashSet<E> extends AbstractSet<E> {
027    
028            public ConcurrentHashSet() {
029                    _map = new ConcurrentHashMap<E, String>();
030            }
031    
032            public ConcurrentHashSet(int capacity) {
033                    _map = new ConcurrentHashMap<E, String>(capacity);
034            }
035    
036            public ConcurrentHashSet(Set<E> set) {
037                    Iterator<E> itr = set.iterator();
038    
039                    while (itr.hasNext()) {
040                            E e = itr.next();
041    
042                            _map.put(e, StringPool.BLANK);
043                    }
044            }
045    
046            public boolean add(E e) {
047                    if (_map.put(e, StringPool.BLANK) == null) {
048                            return true;
049                    }
050                    else {
051                            return false;
052                    }
053            }
054    
055            public void clear() {
056                    _map.clear();
057            }
058    
059            public boolean contains(Object obj) {
060                    if (_map.containsKey(obj)) {
061                            return true;
062                    }
063                    else {
064                            return false;
065                    }
066            }
067    
068            public Iterator<E> iterator() {
069                    return _map.keySet().iterator();
070            }
071    
072            public boolean remove(Object obj) {
073                    if (_map.remove(obj) == null) {
074                            return false;
075                    }
076                    else {
077                            return true;
078                    }
079            }
080    
081            public int size() {
082                    return _map.size();
083            }
084    
085            private Map<E, String> _map;
086    
087    }