001    /**
002     * Copyright (c) 2000-2013 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            @Override
042            public boolean add(E element) {
043                    if (_backedMap.put(element, Boolean.TRUE) == null) {
044                            return true;
045                    }
046                    else {
047                            return false;
048                    }
049            }
050    
051            @Override
052            public void clear() {
053                    _backedMap.clear();
054            }
055    
056            @Override
057            public boolean contains(Object obj) {
058                    return _backedMap.containsKey(obj);
059            }
060    
061            @Override
062            public boolean containsAll(Collection<?> collection) {
063                    return _backedMapKeySet.containsAll(collection);
064            }
065    
066            @Override
067            public boolean equals(Object obj) {
068                    if ((obj == this) || _backedMapKeySet.equals(obj)) {
069                            return true;
070                    }
071                    else {
072                            return false;
073                    }
074            }
075    
076            @Override
077            public int hashCode() {
078                    return _backedMapKeySet.hashCode();
079            }
080    
081            @Override
082            public boolean isEmpty() {
083                    return _backedMap.isEmpty();
084            }
085    
086            @Override
087            public Iterator<E> iterator() {
088                    return _backedMapKeySet.iterator();
089            }
090    
091            @Override
092            public boolean remove(Object obj) {
093                    if (_backedMap.remove(obj) != null) {
094                            return true;
095                    }
096                    else {
097                            return false;
098                    }
099            }
100    
101            @Override
102            public boolean removeAll(Collection<?> collection) {
103                    return _backedMapKeySet.removeAll(collection);
104            }
105    
106            @Override
107            public boolean retainAll(Collection<?> collection) {
108                    return _backedMapKeySet.retainAll(collection);
109            }
110    
111            @Override
112            public int size() {
113                    return _backedMap.size();
114            }
115    
116            @Override
117            public Object[] toArray() {
118                    return _backedMapKeySet.toArray();
119            }
120    
121            @Override
122            public <T> T[] toArray(T[] array) {
123                    return _backedMapKeySet.toArray(array);
124            }
125    
126            @Override
127            public String toString() {
128                    return _backedMapKeySet.toString();
129            }
130    
131            private void readObject(ObjectInputStream objectInputStream)
132                    throws ClassNotFoundException, IOException {
133    
134                    objectInputStream.defaultReadObject();
135    
136                    _backedMapKeySet = _backedMap.keySet();
137            }
138    
139            private final Map<E, Boolean> _backedMap;
140            private transient Set<E> _backedMapKeySet;
141    
142    }