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.io.IOException;
018    import java.io.ObjectInputStream;
019    import java.io.Serializable;
020    
021    import java.util.AbstractSet;
022    import java.util.Collection;
023    import java.util.Iterator;
024    import java.util.Map;
025    import java.util.Set;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class MapBackedSet<E> extends AbstractSet<E> implements Serializable {
031    
032            public MapBackedSet(Map<E, Boolean> backedMap) {
033                    if (!backedMap.isEmpty()) {
034                            throw new IllegalArgumentException("Map is not empty");
035                    }
036    
037                    _backedMap = backedMap;
038                    _backedMapKeySet = backedMap.keySet();
039            }
040    
041            public boolean add(E element) {
042                    if (_backedMap.put(element, Boolean.TRUE) == null) {
043                            return true;
044                    }
045                    else {
046                            return false;
047                    }
048            }
049    
050            public void clear() {
051                    _backedMap.clear();
052            }
053    
054            public boolean contains(Object obj) {
055                    return _backedMap.containsKey(obj);
056            }
057    
058            public boolean containsAll(Collection<?> collection) {
059                    return _backedMapKeySet.containsAll(collection);
060            }
061    
062            public boolean equals(Object obj) {
063                    if ((obj == this) || _backedMapKeySet.equals(obj)) {
064                            return true;
065                    }
066                    else {
067                            return false;
068                    }
069            }
070    
071            public int hashCode() {
072                    return _backedMapKeySet.hashCode();
073            }
074    
075            public boolean isEmpty() {
076                    return _backedMap.isEmpty();
077            }
078    
079            public Iterator<E> iterator() {
080                    return _backedMapKeySet.iterator();
081            }
082    
083            public boolean remove(Object obj) {
084                    if (_backedMap.remove(obj) != null) {
085                            return true;
086                    }
087                    else {
088                            return false;
089                    }
090            }
091    
092            public boolean removeAll(Collection<?> collection) {
093                    return _backedMapKeySet.removeAll(collection);
094            }
095    
096            public boolean retainAll(Collection<?> collection) {
097                    return _backedMapKeySet.retainAll(collection);
098            }
099    
100            public int size() {
101                    return _backedMap.size();
102            }
103    
104            public Object[] toArray() {
105                    return _backedMapKeySet.toArray();
106            }
107    
108            public <T> T[] toArray(T[] array) {
109                    return _backedMapKeySet.toArray(array);
110            }
111    
112            public String toString() {
113                    return _backedMapKeySet.toString();
114            }
115    
116            private void readObject(ObjectInputStream objectInputStream)
117                    throws ClassNotFoundException, IOException {
118    
119                    objectInputStream.defaultReadObject();
120    
121                    _backedMapKeySet = _backedMap.keySet();
122            }
123    
124            private final Map<E, Boolean> _backedMap;
125            private transient Set<E> _backedMapKeySet;
126    
127    }