1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.IOException;
18  import java.io.ObjectInputStream;
19  import java.io.Serializable;
20  
21  import java.util.AbstractSet;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.Map;
25  import java.util.Set;
26  
27  /**
28   * <a href="MapBackedSet.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Shuyang Zhou
31   */
32  public class MapBackedSet<E> extends AbstractSet<E> implements Serializable {
33  
34      public MapBackedSet(Map<E, Boolean> backedMap) {
35          if (!backedMap.isEmpty()) {
36              throw new IllegalArgumentException("Map is not empty");
37          }
38  
39          _backedMap = backedMap;
40          _backedMapKeySet = backedMap.keySet();
41      }
42  
43      public boolean add(E element) {
44          if (_backedMap.put(element, Boolean.TRUE) == null) {
45              return true;
46          }
47          else {
48              return false;
49          }
50      }
51  
52      public void clear() {
53          _backedMap.clear();
54      }
55  
56      public boolean contains(Object obj) {
57          return _backedMap.containsKey(obj);
58      }
59  
60      public boolean containsAll(Collection<?> collection) {
61          return _backedMapKeySet.containsAll(collection);
62      }
63  
64      public boolean equals(Object obj) {
65          if ((obj == this) || _backedMapKeySet.equals(obj)) {
66              return true;
67          }
68          else {
69              return false;
70          }
71      }
72  
73      public int hashCode() {
74          return _backedMapKeySet.hashCode();
75      }
76  
77      public boolean isEmpty() {
78          return _backedMap.isEmpty();
79      }
80  
81      public Iterator<E> iterator() {
82          return _backedMapKeySet.iterator();
83      }
84  
85      public boolean remove(Object obj) {
86          if (_backedMap.remove(obj) != null) {
87              return true;
88          }
89          else {
90              return false;
91          }
92      }
93  
94      public boolean removeAll(Collection<?> collection) {
95          return _backedMapKeySet.removeAll(collection);
96      }
97  
98      public boolean retainAll(Collection<?> collection) {
99          return _backedMapKeySet.retainAll(collection);
100     }
101 
102     public int size() {
103         return _backedMap.size();
104     }
105 
106     public Object[] toArray() {
107         return _backedMapKeySet.toArray();
108     }
109 
110     public <T> T[] toArray(T[] array) {
111         return _backedMapKeySet.toArray(array);
112     }
113 
114     public String toString() {
115         return _backedMapKeySet.toString();
116     }
117 
118     private void readObject(ObjectInputStream objectInputStream)
119         throws ClassNotFoundException, IOException {
120 
121         objectInputStream.defaultReadObject();
122 
123         _backedMapKeySet = _backedMap.keySet();
124     }
125 
126     private final Map<E, Boolean> _backedMap;
127     private transient Set<E> _backedMapKeySet;
128 
129 }