Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ObjectIdentityMap |
|
| 4.0;4 |
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.util; | |
16 | ||
17 | /** | |
18 | * A simple map-like collection, similar to (but more more limited than) JDK | |
19 | * 1.4's IdentityHashMap. It is designed for <em>small</em> collections of | |
20 | * objects. | |
21 | * | |
22 | * @author Howard Lewis Ship | |
23 | * @since 4.0 | |
24 | */ | |
25 | 0 | public class ObjectIdentityMap |
26 | { | |
27 | ||
28 | 0 | private int _pairCount = 0; |
29 | ||
30 | // Alternates between keys and values | |
31 | ||
32 | private Object[] _pairs; | |
33 | ||
34 | /** | |
35 | * Adds or updates a key in the bag. | |
36 | * | |
37 | * @param key | |
38 | * the key to store a value under; an existing value with the key | |
39 | * is discarded | |
40 | * @param value | |
41 | * the value to store | |
42 | */ | |
43 | public void put(Object key, Object value) | |
44 | { | |
45 | 0 | for(int i = 0; i < _pairCount; i++) |
46 | { | |
47 | 0 | int index = 2 * i; |
48 | ||
49 | 0 | if (_pairs[index] == key) |
50 | { | |
51 | 0 | _pairs[index + 1] = value; |
52 | 0 | return; |
53 | } | |
54 | } | |
55 | ||
56 | 0 | expandPairsIfNeeded(); |
57 | ||
58 | 0 | int index = 2 * _pairCount; |
59 | ||
60 | 0 | _pairs[index] = key; |
61 | 0 | _pairs[index + 1] = value; |
62 | ||
63 | 0 | _pairCount++; |
64 | 0 | } |
65 | ||
66 | /** | |
67 | * Returns the object stored for the given key. | |
68 | * | |
69 | * @return the value, or null if the key is not found | |
70 | */ | |
71 | ||
72 | public Object get(Object key) | |
73 | { | |
74 | 0 | for(int i = 0; i < _pairCount; i++) |
75 | { | |
76 | 0 | int index = 2 * i; |
77 | ||
78 | 0 | if (_pairs[index] == key) { return _pairs[index + 1]; } |
79 | } | |
80 | ||
81 | 0 | return null; |
82 | } | |
83 | ||
84 | private void expandPairsIfNeeded() | |
85 | { | |
86 | 0 | int currentSize = _pairs == null ? 0 : _pairs.length; |
87 | ||
88 | 0 | int newLength = 2 * (_pairCount + 1); |
89 | ||
90 | 0 | if (newLength >= currentSize) |
91 | { | |
92 | // Expand to dobule current size. Allocate room for 5 keys and 5 | |
93 | // values | |
94 | // initially. | |
95 | ||
96 | 0 | int newSize = Math.max(10, 2 * currentSize); |
97 | ||
98 | 0 | Object[] newPairsArray = new Object[newSize]; |
99 | ||
100 | 0 | if (currentSize > 0) |
101 | 0 | System.arraycopy(_pairs, 0, newPairsArray, 0, currentSize); |
102 | ||
103 | 0 | _pairs = newPairsArray; |
104 | } | |
105 | 0 | } |
106 | } |