1. /*
  2. * Copyright 2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.set;
  17. import java.io.Serializable;
  18. import java.util.Collection;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. import java.util.Set;
  22. /**
  23. * Decorates a <code>Map</code> to obtain <code>Set</code> behaviour.
  24. * <p>
  25. * This class is used to create a <code>Set</code> with the same properties as
  26. * the key set of any map. Thus, a ReferenceSet can be created by wrapping a
  27. * <code>ReferenceMap</code> in an instance of this class.
  28. * <p>
  29. * Most map implementation can be used to create a set by passing in dummy values.
  30. * Exceptions include <code>BidiMap</code> implementations, as they require unique values.
  31. *
  32. * @since Commons Collections 3.1
  33. * @version $Revision: 1.2 $ $Date: 2004/06/02 22:00:47 $
  34. *
  35. * @author Stephen Colebourne
  36. */
  37. public final class MapBackedSet implements Set, Serializable {
  38. /** Serialization version */
  39. private static final long serialVersionUID = 6723912213766056587L;
  40. /** The map being used as the backing store */
  41. protected final Map map;
  42. /** The dummyValue to use */
  43. protected final Object dummyValue;
  44. /**
  45. * Factory method to create a set from a map.
  46. *
  47. * @param map the map to decorate, must not be null
  48. * @throws IllegalArgumentException if set is null
  49. */
  50. public static Set decorate(Map map) {
  51. return decorate(map, null);
  52. }
  53. /**
  54. * Factory method to create a set from a map.
  55. *
  56. * @param map the map to decorate, must not be null
  57. * @param dummyValue the dummy value to use
  58. * @throws IllegalArgumentException if map is null
  59. */
  60. public static Set decorate(Map map, Object dummyValue) {
  61. if (map == null) {
  62. throw new IllegalArgumentException("The map must not be null");
  63. }
  64. return new MapBackedSet(map, dummyValue);
  65. }
  66. //-----------------------------------------------------------------------
  67. /**
  68. * Constructor that wraps (not copies).
  69. *
  70. * @param map the map to decorate, must not be null
  71. * @param dummyValue the dummy value to use
  72. * @throws IllegalArgumentException if map is null
  73. */
  74. private MapBackedSet(Map map, Object dummyValue) {
  75. super();
  76. this.map = map;
  77. this.dummyValue = dummyValue;
  78. }
  79. //-----------------------------------------------------------------------
  80. public int size() {
  81. return map.size();
  82. }
  83. public boolean isEmpty() {
  84. return map.isEmpty();
  85. }
  86. public Iterator iterator() {
  87. return map.keySet().iterator();
  88. }
  89. public boolean contains(Object obj) {
  90. return map.containsKey(obj);
  91. }
  92. public boolean containsAll(Collection coll) {
  93. return map.keySet().containsAll(coll);
  94. }
  95. public boolean add(Object obj) {
  96. int size = map.size();
  97. map.put(obj, dummyValue);
  98. return (map.size() != size);
  99. }
  100. public boolean addAll(Collection coll) {
  101. int size = map.size();
  102. for (Iterator it = coll.iterator(); it.hasNext();) {
  103. Object obj = (Object) it.next();
  104. map.put(obj, dummyValue);
  105. }
  106. return (map.size() != size);
  107. }
  108. public boolean remove(Object obj) {
  109. int size = map.size();
  110. map.remove(obj);
  111. return (map.size() != size);
  112. }
  113. public boolean removeAll(Collection coll) {
  114. return map.keySet().removeAll(coll);
  115. }
  116. public boolean retainAll(Collection coll) {
  117. return map.keySet().retainAll(coll);
  118. }
  119. public void clear() {
  120. map.clear();
  121. }
  122. public Object[] toArray() {
  123. return map.keySet().toArray();
  124. }
  125. public Object[] toArray(Object[] array) {
  126. return map.keySet().toArray(array);
  127. }
  128. public boolean equals(Object obj) {
  129. return map.keySet().equals(obj);
  130. }
  131. public int hashCode() {
  132. return map.keySet().hashCode();
  133. }
  134. }