|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ObjectIdentityMap.java | 92.9% | 91.3% | 100% | 92.5% |
|
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 1.4's IdentityHashMap. | |
19 | * It is designed for <em>small</em> collections of objects. | |
20 | * | |
21 | * @author Howard Lewis Ship | |
22 | * @since 4.0 | |
23 | */ | |
24 | public class ObjectIdentityMap | |
25 | { | |
26 | private int _pairCount = 0; | |
27 | ||
28 | // Alternates between keys and values | |
29 | ||
30 | private Object[] _pairs; | |
31 | ||
32 | /** | |
33 | * Adds or updates a key in the bag. | |
34 | * | |
35 | * @param key | |
36 | * the key to store a value under; an existing value with the key is discarded | |
37 | * @param value | |
38 | * the value to store | |
39 | */ | |
40 | 1927 | public void put(Object key, Object value) |
41 | { | |
42 | 1927 | for (int i = 0; i < _pairCount; i++) |
43 | { | |
44 | 5489 | int index = 2 * i; |
45 | ||
46 | 5489 | if (_pairs[index] == key) |
47 | { | |
48 | 0 | _pairs[index + 1] = value; |
49 | 0 | return; |
50 | } | |
51 | } | |
52 | ||
53 | 1927 | expandPairsIfNeeded(); |
54 | ||
55 | 1927 | int index = 2 * _pairCount; |
56 | ||
57 | 1927 | _pairs[index] = key; |
58 | 1927 | _pairs[index + 1] = value; |
59 | ||
60 | 1927 | _pairCount++; |
61 | } | |
62 | ||
63 | /** | |
64 | * Returns the object stored for the given key. | |
65 | * | |
66 | * @return the value, or null if the key is not found | |
67 | */ | |
68 | ||
69 | 2742 | public Object get(Object key) |
70 | { | |
71 | 2742 | for (int i = 0; i < _pairCount; i++) |
72 | { | |
73 | 7978 | int index = 2 * i; |
74 | ||
75 | 7978 | if (_pairs[index] == key) |
76 | { | |
77 | 243 | return _pairs[index + 1]; |
78 | } | |
79 | } | |
80 | ||
81 | 2499 | return null; |
82 | } | |
83 | ||
84 | 1927 | private void expandPairsIfNeeded() |
85 | { | |
86 | 1927 | int currentSize = _pairs == null ? 0 : _pairs.length; |
87 | ||
88 | 1927 | int newLength = 2 * (_pairCount + 1); |
89 | ||
90 | 1927 | if (newLength >= currentSize) |
91 | { | |
92 | // Expand to dobule current size. Allocate room for 5 keys and 5 values | |
93 | // initially. | |
94 | ||
95 | 680 | int newSize = Math.max(10, 2 * currentSize); |
96 | ||
97 | 680 | Object[] newPairsArray = new Object[newSize]; |
98 | ||
99 | 680 | if (currentSize > 0) |
100 | 237 | System.arraycopy(_pairs, 0, newPairsArray, 0, currentSize); |
101 | ||
102 | 680 | _pairs = newPairsArray; |
103 | } | |
104 | } | |
105 | } |
|