1 16 package org.apache.commons.collections.set; 17 18 import java.io.Serializable ; 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 39 public final class MapBackedSet implements Set , Serializable { 40 41 42 private static final long serialVersionUID = 6723912213766056587L; 43 44 45 protected final Map map; 46 47 protected final Object dummyValue; 48 49 55 public static Set decorate(Map map) { 56 return decorate(map, null); 57 } 58 59 66 public static Set decorate(Map map, Object dummyValue) { 67 if (map == null) { 68 throw new IllegalArgumentException ("The map must not be null"); 69 } 70 return new MapBackedSet(map, dummyValue); 71 } 72 73 81 private MapBackedSet(Map map, Object dummyValue) { 82 super(); 83 this.map = map; 84 this.dummyValue = dummyValue; 85 } 86 87 public int size() { 89 return map.size(); 90 } 91 92 public boolean isEmpty() { 93 return map.isEmpty(); 94 } 95 96 public Iterator iterator() { 97 return map.keySet().iterator(); 98 } 99 100 public boolean contains(Object obj) { 101 return map.containsKey(obj); 102 } 103 104 public boolean containsAll(Collection coll) { 105 return map.keySet().containsAll(coll); 106 } 107 108 public boolean add(Object obj) { 109 int size = map.size(); 110 map.put(obj, dummyValue); 111 return (map.size() != size); 112 } 113 114 public boolean addAll(Collection coll) { 115 int size = map.size(); 116 for (Iterator it = coll.iterator(); it.hasNext();) { 117 Object obj = (Object ) it.next(); 118 map.put(obj, dummyValue); 119 } 120 return (map.size() != size); 121 } 122 123 public boolean remove(Object obj) { 124 int size = map.size(); 125 map.remove(obj); 126 return (map.size() != size); 127 } 128 129 public boolean removeAll(Collection coll) { 130 return map.keySet().removeAll(coll); 131 } 132 133 public boolean retainAll(Collection coll) { 134 return map.keySet().retainAll(coll); 135 } 136 137 public void clear() { 138 map.clear(); 139 } 140 141 public Object [] toArray() { 142 return map.keySet().toArray(); 143 } 144 145 public Object [] toArray(Object [] array) { 146 return map.keySet().toArray(array); 147 } 148 149 public boolean equals(Object obj) { 150 return map.keySet().equals(obj); 151 } 152 153 public int hashCode() { 154 return map.keySet().hashCode(); 155 } 156 157 } 158 | Popular Tags |