Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ObjectIdentityMap |
|
| 3.6666666666666665;3.667 |
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 | 142 | public class ObjectIdentityMap |
26 | { |
|
27 | ||
28 | 142 | 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 | 1436 | for(int i = 0; i < _pairCount; i++) |
46 | { |
|
47 | 1248 | int index = 2 * i; |
48 | ||
49 | 1248 | if (_pairs[index] == key) |
50 | { |
|
51 | 0 | _pairs[index + 1] = value; |
52 | 0 | return; |
53 | } |
|
54 | } |
|
55 | ||
56 | 188 | expandPairsIfNeeded(); |
57 | ||
58 | 188 | int index = 2 * _pairCount; |
59 | ||
60 | 188 | _pairs[index] = key; |
61 | 188 | _pairs[index + 1] = value; |
62 | ||
63 | 188 | _pairCount++; |
64 | 188 | } |
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 | 1441 | for(int i = 0; i < _pairCount; i++) |
75 | { |
|
76 | 1302 | int index = 2 * i; |
77 | ||
78 | 1302 | if (_pairs[index] == key) { return _pairs[index + 1]; } |
79 | } |
|
80 | ||
81 | 139 | return null; |
82 | } |
|
83 | ||
84 | private void expandPairsIfNeeded() |
|
85 | { |
|
86 | 188 | int currentSize = _pairs == null ? 0 : _pairs.length; |
87 | ||
88 | 188 | int newLength = 2 * (_pairCount + 1); |
89 | ||
90 | 188 | if (newLength >= currentSize) |
91 | { |
|
92 | // Expand to dobule current size. Allocate room for 5 keys and 5 |
|
93 | // values |
|
94 | // initially. |
|
95 | ||
96 | 120 | int newSize = Math.max(10, 2 * currentSize); |
97 | ||
98 | 120 | Object[] newPairsArray = new Object[newSize]; |
99 | ||
100 | 120 | if (currentSize > 0) |
101 | 4 | System.arraycopy(_pairs, 0, newPairsArray, 0, currentSize); |
102 | ||
103 | 120 | _pairs = newPairsArray; |
104 | } |
|
105 | 188 | } |
106 | } |