Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0ab331da4270061331c4a93aae4b9e3904bafc66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.prov.core.helpers;

import java.io.PrintStream;
import java.io.Serializable;
import java.util.*;

/**
 * The purpose of this class is to provide a two-tier map.
 * MapOf(key1 => MapOf(key2 => value)).
 * Even though this class implements Map the behavior of
 * the methods aren't exactly the same as that of a real
 * Map - especially, entrySet(), keySet() etc. works off
 * the outer map while values() returns all the values of
 * all the inner maps.
 */
public class TwoTierMap implements Map, Serializable {

	private static final long serialVersionUID = 362497720873186265L;

	private Map outerMap;
	private int policy;

	public static final int POLICY_NONE = 0, POLICY_BOTH_MAPS_PRESERVE_ORDERING = 1 << 0, POLICY_INNER_MAP_PRESERVE_EXISTING = 1 << 1, POLICY_INNER_MAP_SORTED_ASCENDING = 1 << 2, POLICY_INNER_MAP_SORTED_DESCENDING = 1 << 3, POLICY_INNER_MAP_ENSURE_SINGLETON = 1 << 4;

	private static final int POLICY_INNER_MAP_SORTED_MASK = POLICY_INNER_MAP_SORTED_ASCENDING | POLICY_INNER_MAP_SORTED_DESCENDING;

	public TwoTierMap() {
		this(8, POLICY_NONE);
	}

	public TwoTierMap(int initialCapacity) {
		this(initialCapacity, POLICY_NONE);
	}

	/**
	 * Creates a two-tier map with the specified
	 * initialCapacity and policy. The policy determines
	 * whether the outer map is ordered, inner map is
	 * sorted, clobber values of inner map etc.
	 */
	public TwoTierMap(int initialCapacity, int policy) {
		this.policy = policy;
		this.outerMap = shouldUseOrderedMap() ? new LinkedHashMap(initialCapacity) : new HashMap(initialCapacity);
	}

	/**
	 * Insert the value with key key1 into the inner map 
	 * that is obtained from the outer map with key key2.
	 * If you have set POLICY_INNER_MAP_PRESERVE_EXISTING
	 * at the time of creating this, it will not overwrite
	 * if there is already a non-null value at key2. 
	 * The other POLICY_INNER_MAP_* policies determine 
	 * what kind of inner map is created.
	 * @param key1 The key for outer map.
	 * @param key2 The key for inner map.
	 * @param value The value.
	 * @return Existing value if any, otherwise null.
	 */
	public Object put(Object key1, Object key2, Object value) {
		Map innerMap = (Map) this.outerMap.get(key1);
		if (innerMap == null) {
			if (shouldUseSingletonInnerMap()) {
				this.outerMap.put(key1, Collections.singletonMap(key2, value));
				return null;
			}
			innerMap = createInnerMap();
			this.outerMap.put(key1, innerMap);
		}
		// It is faster to check for already existing entry 
		// this way instead of containsKey() check. Of course,
		// this will prevent us from recognizing a null entry, 
		// which I think shouldn't be a problem.
		Object existing = innerMap.put(key2, value);
		if (existing != null && shouldPreserveExisting()) {
			innerMap.put(key2, existing);
		}
		return existing;
	}

	/**
	 * Get the object stored in the inner map using key2
	 * as key where the inner map is obtained from the 
	 * outer map using key1.
	 * @param key1 The key for outer map.
	 * @param key2 The key for inner map.
	 * @return The object for key2 in inner map for key1 
	 * in the outer map. 
	 */
	public Object get(Object key1, Object key2) {
		if (key1 == null || key2 == null)
			return getAll(key1);
		Map innerMap = (Map) this.outerMap.get(key1);
		Object value = innerMap == null ? null : innerMap.get(key2);
		return value;
	}

	/**
	 * Get all the values in the inner map for key1 in
	 * the outer map.
	 * @param key1 The key for outer map.
	 * @return Collection of values in the inner map.
	 */
	public Collection getAll(Object key1) {
		if (key1 == null)
			// return all
			return values();
		Map innerMap = (Map) this.outerMap.get(key1);
		return innerMap == null ? Collections.EMPTY_LIST : Collections.unmodifiableCollection(innerMap.values());

	}

	public Object remove(Object key1, Object key2) {
		if (key1 == null || key2 == null)
			return removeAll(key1);
		Map innerMap = (Map) this.outerMap.get(key1);
		if (innerMap == null)
			return null;
		if (shouldUseSingletonInnerMap()) {
			Object result = innerMap.get(key2);
			if (result != null) {
				this.outerMap.remove(key1);
			}
			return result;
		}
		Object result = (innerMap == null) ? null : innerMap.remove(key2);
		if (result != null && innerMap.isEmpty()) {
			this.outerMap.remove(key1);
		}
		return result;
	}

	public Collection removeAll(Object key1) {
		if (key1 == null)
			return Collections.EMPTY_LIST;
		Map innerMap = (Map) this.outerMap.remove(key1);
		return innerMap == null ? Collections.EMPTY_LIST : innerMap.values();
	}

	/**
	 * Determine whether there exists a valid object for
	 * key2 in the inner map for key1 in the outer map.
	 * @param key1 The key for outer map.
	 * @param key2 The key for inner map.
	 * @return true if a non-null object exists; otherwise
	 * false.
	 */
	public boolean containsKey(Object key1, Object key2) {
		if (key1 == null)
			return false;
		if (key2 == null)
			return containsKey(key1);
		return get(key1, key2) != null;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#clear()
	 */
	public void clear() {
		this.outerMap.clear();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#containsKey(java.lang.Object)
	 */
	public boolean containsKey(Object key) {
		return this.outerMap.containsKey(key);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#containsValue(java.lang.Object)
	 */
	public boolean containsValue(Object value) {
		for (Iterator it = entrySet().iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			Map innerMap = (Map) entry.getValue();
			if (innerMap.containsValue(value))
				return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#size()
	 */
	public int size() {
		return this.outerMap.size();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#isEmpty()
	 */
	public boolean isEmpty() {
		return this.size() == 0;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#entrySet()
	 */
	public Set entrySet() {
		return Collections.unmodifiableSet(this.outerMap.entrySet());
	}

	/* (non-Javadoc)
	 * @see java.util.Map#values()
	 */
	public Collection values() {
		ArrayList result = new ArrayList(size());
		for (Iterator it = this.outerMap.keySet().iterator(); it.hasNext();) {
			Object key = it.next();
			// A null key will cause infinite recursion!
			if (key != null) {
				result.addAll(getAll(key));
			}
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see java.util.Map#get(java.lang.Object)
	 */
	public Object get(Object key) {
		if (key instanceof Object[]) {
			Object[] keys = (Object[]) key;
			return get(keys[0], keys[1]);
		} else
			return getAll(key);
	}

	/* (non-Javadoc)
	 * @see java.util.Map#keySet()
	 */
	public Set keySet() {
		return Collections.unmodifiableSet(this.outerMap.keySet());
	}

	/* (non-Javadoc)
	 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
	 */
	public Object put(Object key, Object value) {
		if (key instanceof Object[]) {
			Object[] keys = (Object[]) key;
			return put(keys[0], keys[1], value);
		}
		throw new IllegalArgumentException("First arg should be an array!"); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see java.util.Map#putAll(java.util.Map)
	 */
	public void putAll(Map t) {
		throw new UnsupportedOperationException();
	}

	/* (non-Javadoc)
	 * @see java.util.Map#remove(java.lang.Object)
	 */
	public Object remove(Object key) {
		if (key instanceof Object[]) {
			Object[] keys = (Object[]) key;
			return remove(keys[0], keys[1]);
		} else
			return removeAll(key);
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		if (this.outerMap.isEmpty()) {
			sb.append("  (Empty)"); //$NON-NLS-1$
		} else {
			for (Iterator it = this.outerMap.entrySet().iterator(); it.hasNext();) {
				Map.Entry entry = (Map.Entry) it.next();
				sb.append("  ").append(entry.getKey()) //$NON-NLS-1$
						.append(" = ") //$NON-NLS-1$
						.append(entry.getValue()).append('\n');
			}
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}

	public void dump(PrintStream ps) {
		if (ps == null) {
			ps = System.out;
		}
		ps.println(this.toString());
	}

	private Map createInnerMap() {
		Map innerMap;
		if (shouldUseSortedInnerMap()) {
			innerMap = new TreeMap(new ValueComparator(shouldSortInAscendingOrder()));
		} else if (shouldUseOrderedMap()) {
			innerMap = new LinkedHashMap(2);
		} else {
			innerMap = new HashMap(2);
		}
		return innerMap;
	}

	private boolean shouldPreserveExisting() {
		return (this.policy & POLICY_INNER_MAP_PRESERVE_EXISTING) == POLICY_INNER_MAP_PRESERVE_EXISTING;
	}

	private boolean shouldUseOrderedMap() {
		return (this.policy & POLICY_BOTH_MAPS_PRESERVE_ORDERING) == POLICY_BOTH_MAPS_PRESERVE_ORDERING;
	}

	private boolean shouldUseSortedInnerMap() {
		return (this.policy & POLICY_INNER_MAP_SORTED_MASK) != 0;
	}

	private boolean shouldSortInAscendingOrder() {
		return (this.policy & POLICY_INNER_MAP_SORTED_MASK) == POLICY_INNER_MAP_SORTED_ASCENDING;
	}

	private boolean shouldUseSingletonInnerMap() {
		return (this.policy & POLICY_INNER_MAP_ENSURE_SINGLETON) == POLICY_INNER_MAP_ENSURE_SINGLETON;
	}

	private static class ValueComparator implements Comparator, Serializable {
		private static final long serialVersionUID = 362497720873186266L;
		private boolean ascending;

		public ValueComparator(boolean ascending) {
			this.ascending = ascending;
		}

		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(Object o1, Object o2) {
			try {
				if (o1 instanceof Comparable) {
					int cmp = ((Comparable) o1).compareTo(o2);
					return this.ascending ? cmp : (0 - cmp);
				}
			} catch (Exception e) {
				// Ignore
			}
			return 1;
		}
	}

}

Back to the top