1 /*
2 * Copyright (c) 2003, Henri Yandell
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or
6 * without modification, are permitted provided that the
7 * following conditions are met:
8 *
9 * + Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * + Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * + Neither the name of Genjava-Core nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package com.generationjava.collections;
33
34 import java.util.Map;
35 import java.util.HashMap;
36 import java.util.Collection;
37 import java.util.Set;
38 import java.util.Iterator;
39 import java.util.LinkedList;
40
41 /***
42 * A Map in which some key's values may point to another key.
43 * A process coined as 'aliasing'.
44 *
45 * @date 2000-05-14
46 */
47 public class AliasedMap implements Map {
48
49 // this is not lazy instantiation. One MUST be
50 // constructed in a constructor.
51 private Map myMap = null;
52
53 public AliasedMap(Map m) {
54 myMap = m;
55 }
56
57 public AliasedMap() {
58 this(new HashMap());
59 }
60
61 /***
62 * Alias an existing key with an aliased key. The existing key may
63 * be an alias.
64 *
65 * @param aliaskey Object new alias
66 * @param existingkey Object existing key
67 */
68 public void alias(Object aliaskey,Object existingkey) {
69 // enforce existing key?
70 Alias alias = new SimpleAlias(existingkey);
71 myMap.put(aliaskey,alias);
72 }
73
74 /***
75 * Get all aliases.
76 */
77 public Collection aliases() {
78 Collection values = myMap.values();
79 if(values == null) {
80 return null;
81 }
82
83 LinkedList ret = new LinkedList();
84
85 Iterator it = values.iterator();
86 while(it.hasNext()) {
87 Object obj = it.next();
88 if(obj instanceof Alias) {
89 ret.add(obj);
90 }
91 }
92
93 return ret;
94 }
95
96 /*
97 /**
98 * Creates a new AliasedMap with the existing aliases linked to their
99 * true values.
100 * /
101 public AliasedMap newMapWithAliases() {
102 return (AliasedMap)fillMapWithAliases(new AliasedMap());
103 }
104 /**
105 * Fills a Map with the existing aliases linked to their
106 * true values.
107 * /
108 public Map fillMapWithAliases(Map ret) {
109 Collection aliases = aliases();
110 if(aliases != null) {
111 Iterator it = aliases.iterator();
112 while(it.hasNext()) {
113 Alias alias = (Alias)it.next();
114 ret.put(alias,alias.getAlias());
115 }
116 }
117 return ret;
118 }
119 */
120
121
122 /* map interface */
123
124 /***
125 * Removes all mappings from this map.
126 */
127 public void clear() {
128 myMap.clear();
129 }
130
131 /***
132 * Returns true if this map contains a mapping for the specified key.
133 */
134 public boolean containsKey(Object key) {
135 return myMap.containsKey(key);
136 }
137
138 /***
139 * Returns true if this map maps one or more keys to the specified value.
140 */
141 public boolean containsValue(Object value) {
142 // QUERY: What if the value is an alias? Do we deny aliasing?
143 return myMap.containsValue(value);
144 }
145
146 /***
147 * Returns a set view of the mappings contained in this map.
148 */
149 public Set entrySet() {
150 // FIX : Make this actually work.
151 return myMap.entrySet();
152 }
153
154 /***
155 * Compares the specified object with this map for equality.
156 */
157 public boolean equals(Object o) {
158 // QUERY: Is this right?
159 return myMap.equals(o);
160 }
161
162 /***
163 * Returns the value to which this map maps the specified key.
164 */
165 public Object get(Object key) {
166 Object ob = myMap.get(key);
167 if(ob instanceof Alias) {
168 return get( ((Alias)ob).getAlias() );
169 } else {
170 return ob;
171 }
172 }
173
174 /***
175 * Returns the hash code value for this map.
176 */
177 public int hashCode() {
178 // QUERY: Is this right?
179 return myMap.hashCode();
180 }
181
182 /***
183 * Returns true if this map contains no key-value mappings.
184 */
185 public boolean isEmpty() {
186 // TODO : Make it ignore aliases.
187 return myMap.isEmpty();
188 }
189
190 /***
191 * Returns a set view of the keys contained in this map.
192 */
193 public Set keySet() {
194 return myMap.keySet();
195 }
196
197 /***
198 * Associates the specified value with the specified key in this map .
199 */
200 public Object put(Object key, Object value) {
201 // TODO : Make it return the value aliased, rather than the alias.
202 return myMap.put(key,value);
203 }
204
205 /***
206 * Copies all of the mappings from the specified map to this map.
207 */
208 public void putAll(Map t) {
209 myMap.putAll(t);
210 }
211
212 /***
213 * Removes the mapping for this key from this map if present.
214 */
215 public Object remove(Object key) {
216 // QUERY: Should it return value aliased, as it is doing now?
217 Object ob = get(key);
218 myMap.remove(key);
219 return ob;
220 }
221
222 /***
223 * Returns the number of key-value mappings in this map.
224 */
225 public int size() {
226 // QUERY: This is okay I think.
227 return myMap.size();
228 }
229
230 /***
231 * Returns a collection view of the values contained in this map.
232 */
233 public Collection values() {
234 // TODO : Remove aliases.
235 return myMap.values();
236 }
237
238 }
This page was automatically generated by Maven