View Javadoc

1   /*
2    * Copyright 2006-2008 the original author or authors.
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.springframework.osgi.util.internal;
17  
18  import java.util.Collection;
19  import java.util.Dictionary;
20  import java.util.Enumeration;
21  import java.util.Iterator;
22  import java.util.LinkedHashMap;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.springframework.util.Assert;
27  
28  /**
29   * Dictionary implementation backed by a map instance. While the JDK provides a
30   * Dictionary implementation through Hashtable, the class itself is always
31   * synchronized and does not maintain the internal order.
32   * 
33   * <p/> This simple wrapper, accepts any type of Map as backing storage allowing
34   * more options in choosing the appropriate implementation. By default, a
35   * {@link java.util.LinkedHashMap} is used, if no Map is specified.
36   * 
37   * <p/> This implementation will enforce the Dictionary behaviour over the map
38   * when it comes to handling null values. As opposed to a Map, the Dictionary
39   * always throws {@link NullPointerException} if a given argument is null.
40   * 
41   * @see java.util.Map
42   * @see java.util.Dictionary
43   * @author Costin Leau
44   */
45  public class MapBasedDictionary extends Dictionary implements Map {
46  
47  	private Map map;
48  
49  	/**
50  	 * Enumeration wrapper around an Iterator.
51  	 * 
52  	 * @author Costin Leau
53  	 * 
54  	 */
55  	private static class IteratorBasedEnumeration implements Enumeration {
56  
57  		private Iterator it;
58  
59  		public IteratorBasedEnumeration(Iterator it) {
60  			Assert.notNull(it);
61  			this.it = it;
62  		}
63  
64  		public IteratorBasedEnumeration(Collection col) {
65  			this(col.iterator());
66  		}
67  
68  		public boolean hasMoreElements() {
69  			return it.hasNext();
70  		}
71  
72  		public Object nextElement() {
73  			return it.next();
74  		}
75  
76  	}
77  
78  	public MapBasedDictionary(Map map) {
79  		this.map = (map == null ? new LinkedHashMap() : map);
80  	}
81  
82  	/**
83  	 * Default constructor.
84  	 * 
85  	 */
86  	public MapBasedDictionary() {
87  		this.map = new LinkedHashMap();
88  	}
89  
90  	public MapBasedDictionary(int initialCapacity) {
91  		this.map = new LinkedHashMap(initialCapacity);
92  	}
93  
94  	/**
95  	 * Constructor for dealing with existing Dictionary. Will copy the content
96  	 * into the inner Map.
97  	 * 
98  	 * @param dictionary
99  	 */
100 	public MapBasedDictionary(Dictionary dictionary) {
101 		this(new LinkedHashMap(), dictionary);
102 	}
103 
104 	public MapBasedDictionary(Map map, Dictionary dictionary) {
105 		this(map);
106 		if (dictionary != null)
107 			putAll(dictionary);
108 	}
109 
110 	public void clear() {
111 		map.clear();
112 	}
113 
114 	public boolean containsKey(Object key) {
115 		return map.containsKey(key);
116 	}
117 
118 	public boolean containsValue(Object value) {
119 		return map.containsValue(value);
120 	}
121 
122 	public Set entrySet() {
123 		return map.entrySet();
124 	}
125 
126 	public Object get(Object key) {
127 		if (key == null)
128 			throw new NullPointerException();
129 		return map.get(key);
130 	}
131 
132 	public boolean isEmpty() {
133 		return map.isEmpty();
134 	}
135 
136 	public Set keySet() {
137 		return map.keySet();
138 	}
139 	
140 	public Object put(Object key, Object value) {
141 		if (key == null || value == null)
142 			throw new NullPointerException();
143 
144 		return map.put(key, value);
145 	}
146 
147 	public void putAll(Map t) {
148 		map.putAll(t);
149 	}
150 
151 	public void putAll(Dictionary dictionary) {
152 		if (dictionary != null)
153 			// copy the dictionary
154 			for (Enumeration enm = dictionary.keys(); enm.hasMoreElements();) {
155 				Object key = enm.nextElement();
156 				map.put(key, dictionary.get(key));
157 			}
158 	}
159 
160 	public Object remove(Object key) {
161 		if (key == null)
162 			throw new NullPointerException();
163 
164 		return map.remove(key);
165 	}
166 
167 	public int size() {
168 		return map.size();
169 	}
170 
171 	public Collection values() {
172 		return map.values();
173 	}
174 
175 	public Enumeration elements() {
176 		return new IteratorBasedEnumeration(map.values());
177 	}
178 
179 	public Enumeration keys() {
180 		return new IteratorBasedEnumeration(map.keySet());
181 	}
182 
183 	public String toString() {
184 		return map.toString();
185 	}
186 
187 	public boolean equals(Object obj) {
188 		// this should work nicely since the Dictionary implementations inside
189 		// the JDK are Maps also
190 		return map.equals(obj);
191 	}
192 
193 	public int hashCode() {
194 		return map.hashCode();
195 	}
196 
197 }