KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > MapBackedSet


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
18 import java.io.Serializable JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 /**
25  * Decorates a <code>Map</code> to obtain <code>Set</code> behaviour.
26  * <p>
27  * This class is used to create a <code>Set</code> with the same properties as
28  * the key set of any map. Thus, a ReferenceSet can be created by wrapping a
29  * <code>ReferenceMap</code> in an instance of this class.
30  * <p>
31  * Most map implementation can be used to create a set by passing in dummy values.
32  * Exceptions include <code>BidiMap</code> implementations, as they require unique values.
33  *
34  * @since Commons Collections 3.1
35  * @version $Revision: 1.2 $ $Date: 2004/06/02 22:00:47 $
36  *
37  * @author Stephen Colebourne
38  */

39 public final class MapBackedSet implements Set JavaDoc, Serializable JavaDoc {
40
41     /** Serialization version */
42     private static final long serialVersionUID = 6723912213766056587L;
43
44     /** The map being used as the backing store */
45     protected final Map JavaDoc map;
46     /** The dummyValue to use */
47     protected final Object JavaDoc dummyValue;
48
49     /**
50      * Factory method to create a set from a map.
51      *
52      * @param map the map to decorate, must not be null
53      * @throws IllegalArgumentException if set is null
54      */

55     public static Set JavaDoc decorate(Map JavaDoc map) {
56         return decorate(map, null);
57     }
58
59     /**
60      * Factory method to create a set from a map.
61      *
62      * @param map the map to decorate, must not be null
63      * @param dummyValue the dummy value to use
64      * @throws IllegalArgumentException if map is null
65      */

66     public static Set JavaDoc decorate(Map JavaDoc map, Object JavaDoc dummyValue) {
67         if (map == null) {
68             throw new IllegalArgumentException JavaDoc("The map must not be null");
69         }
70         return new MapBackedSet(map, dummyValue);
71     }
72
73     //-----------------------------------------------------------------------
74
/**
75      * Constructor that wraps (not copies).
76      *
77      * @param map the map to decorate, must not be null
78      * @param dummyValue the dummy value to use
79      * @throws IllegalArgumentException if map is null
80      */

81     private MapBackedSet(Map JavaDoc map, Object JavaDoc dummyValue) {
82         super();
83         this.map = map;
84         this.dummyValue = dummyValue;
85     }
86
87     //-----------------------------------------------------------------------
88
public int size() {
89         return map.size();
90     }
91
92     public boolean isEmpty() {
93         return map.isEmpty();
94     }
95
96     public Iterator JavaDoc iterator() {
97         return map.keySet().iterator();
98     }
99
100     public boolean contains(Object JavaDoc obj) {
101         return map.containsKey(obj);
102     }
103
104     public boolean containsAll(Collection JavaDoc coll) {
105         return map.keySet().containsAll(coll);
106     }
107
108     public boolean add(Object JavaDoc obj) {
109         int size = map.size();
110         map.put(obj, dummyValue);
111         return (map.size() != size);
112     }
113
114     public boolean addAll(Collection JavaDoc coll) {
115         int size = map.size();
116         for (Iterator JavaDoc it = coll.iterator(); it.hasNext();) {
117             Object JavaDoc obj = (Object JavaDoc) it.next();
118             map.put(obj, dummyValue);
119         }
120         return (map.size() != size);
121     }
122
123     public boolean remove(Object JavaDoc obj) {
124         int size = map.size();
125         map.remove(obj);
126         return (map.size() != size);
127     }
128
129     public boolean removeAll(Collection JavaDoc coll) {
130         return map.keySet().removeAll(coll);
131     }
132
133     public boolean retainAll(Collection JavaDoc coll) {
134         return map.keySet().retainAll(coll);
135     }
136
137     public void clear() {
138         map.clear();
139     }
140
141     public Object JavaDoc[] toArray() {
142         return map.keySet().toArray();
143     }
144
145     public Object JavaDoc[] toArray(Object JavaDoc[] array) {
146         return map.keySet().toArray(array);
147     }
148
149     public boolean equals(Object JavaDoc obj) {
150         return map.keySet().equals(obj);
151     }
152
153     public int hashCode() {
154         return map.keySet().hashCode();
155     }
156
157 }
158
Popular Tags