|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
IdentityMap.java | 0% | 42.9% | 50% | 41% |
|
1 | package org.drools.util; | |
2 | /* | |
3 | * Copyright 2003-2004 The Apache Software Foundation | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | import java.io.IOException; | |
19 | import java.io.ObjectInputStream; | |
20 | import java.io.ObjectOutputStream; | |
21 | import java.io.Serializable; | |
22 | import java.util.Map; | |
23 | ||
24 | /** | |
25 | * A <code>Map</code> implementation that matches keys and values based on | |
26 | * <code>==</code> not <code>equals()</code>. | |
27 | * <p> | |
28 | * This map will violate the detail of various Map and map view contracts. As a | |
29 | * general rule, don't compare this map to other maps. | |
30 | * | |
31 | * @since Commons Collections 3.0 | |
32 | * @version $Revision: 1.3 $ $Date: 2004/12/06 01:30:38 $ | |
33 | * | |
34 | * @author java util HashMap | |
35 | * @author Stephen Colebourne | |
36 | */ | |
37 | public class IdentityMap extends AbstractHashedMap | |
38 | implements | |
39 | Serializable, | |
40 | Cloneable | |
41 | { | |
42 | ||
43 | /** Serialisation version */ | |
44 | private static final long serialVersionUID = 2028493495224302329L; | |
45 | ||
46 | /** | |
47 | * Constructs a new empty map with default size and load factor. | |
48 | */ | |
49 | 71 | public IdentityMap() |
50 | { | |
51 | 71 | super( DEFAULT_CAPACITY, |
52 | DEFAULT_LOAD_FACTOR, | |
53 | DEFAULT_THRESHOLD ); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Constructs a new, empty map with the specified initial capacity. | |
58 | * | |
59 | * @param initialCapacity | |
60 | * the initial capacity | |
61 | * @throws IllegalArgumentException | |
62 | * if the initial capacity is less than one | |
63 | */ | |
64 | 0 | public IdentityMap(int initialCapacity) |
65 | { | |
66 | 0 | super( initialCapacity ); |
67 | } | |
68 | ||
69 | /** | |
70 | * Constructs a new, empty map with the specified initial capacity and load | |
71 | * factor. | |
72 | * | |
73 | * @param initialCapacity | |
74 | * the initial capacity | |
75 | * @param loadFactor | |
76 | * the load factor | |
77 | * @throws IllegalArgumentException | |
78 | * if the initial capacity is less than one | |
79 | * @throws IllegalArgumentException | |
80 | * if the load factor is less than zero | |
81 | */ | |
82 | 0 | public IdentityMap(int initialCapacity, |
83 | float loadFactor) | |
84 | { | |
85 | 0 | super( initialCapacity, |
86 | loadFactor ); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Constructor copying elements from another map. | |
91 | * | |
92 | * @param map | |
93 | * the map to copy | |
94 | * @throws NullPointerException | |
95 | * if the map is null | |
96 | */ | |
97 | 0 | public IdentityMap(Map map) |
98 | { | |
99 | 0 | super( map ); |
100 | } | |
101 | ||
102 | // ----------------------------------------------------------------------- | |
103 | /** | |
104 | * Gets the hash code for the key specified. This implementation uses the | |
105 | * identity hash code. | |
106 | * | |
107 | * @param key | |
108 | * the key to get a hash code for | |
109 | * @return the hash code | |
110 | */ | |
111 | 2941 | protected int hash(Object key) |
112 | { | |
113 | 2941 | return System.identityHashCode( key ); |
114 | } | |
115 | ||
116 | /** | |
117 | * Compares two keys for equals. This implementation uses <code>==</code>. | |
118 | * | |
119 | * @param key1 | |
120 | * the first key to compare | |
121 | * @param key2 | |
122 | * the second key to compare | |
123 | * @return true if equal by identity | |
124 | */ | |
125 | 732 | protected boolean isEqualKey(Object key1, |
126 | Object key2) | |
127 | { | |
128 | 732 | return (key1 == key2); |
129 | } | |
130 | ||
131 | /** | |
132 | * Compares two values for equals. This implementation uses <code>==</code>. | |
133 | * | |
134 | * @param value1 | |
135 | * the first value to compare | |
136 | * @param value2 | |
137 | * the second value to compare | |
138 | * @return true if equal by identity | |
139 | */ | |
140 | 0 | protected boolean isEqualValue(Object value1, |
141 | Object value2) | |
142 | { | |
143 | 0 | return (value1 == value2); |
144 | } | |
145 | ||
146 | /** | |
147 | * Creates an entry to store the data. This implementation creates an | |
148 | * IdentityEntry instance. | |
149 | * | |
150 | * @param next | |
151 | * the next entry in sequence | |
152 | * @param hashCode | |
153 | * the hash code to use | |
154 | * @param key | |
155 | * the key to store | |
156 | * @param value | |
157 | * the value to store | |
158 | * @return the newly created entry | |
159 | */ | |
160 | 1337 | protected HashEntry createEntry(HashEntry next, |
161 | int hashCode, | |
162 | Object key, | |
163 | Object value) | |
164 | { | |
165 | 1337 | return new IdentityEntry( next, |
166 | hashCode, | |
167 | key, | |
168 | value ); | |
169 | } | |
170 | ||
171 | // ----------------------------------------------------------------------- | |
172 | /** | |
173 | * HashEntry | |
174 | */ | |
175 | protected static class IdentityEntry extends HashEntry | |
176 | { | |
177 | ||
178 | 1337 | protected IdentityEntry(HashEntry next, |
179 | int hashCode, | |
180 | Object key, | |
181 | Object value) | |
182 | { | |
183 | 1337 | super( next, |
184 | hashCode, | |
185 | key, | |
186 | value ); | |
187 | } | |
188 | ||
189 | 0 | public boolean equals(Object obj) |
190 | { | |
191 | 0 | if ( obj == this ) |
192 | { | |
193 | 0 | return true; |
194 | } | |
195 | 0 | if ( obj instanceof Map.Entry == false ) |
196 | { | |
197 | 0 | return false; |
198 | } | |
199 | 0 | Map.Entry other = (Map.Entry) obj; |
200 | 0 | return (getKey( ) == other.getKey( )) && (getValue( ) == other.getValue( )); |
201 | } | |
202 | ||
203 | 0 | public int hashCode() |
204 | { | |
205 | 0 | return System.identityHashCode( getKey( ) ) ^ System.identityHashCode( getValue( ) ); |
206 | } | |
207 | } | |
208 | ||
209 | // ----------------------------------------------------------------------- | |
210 | /** | |
211 | * Clones the map without cloning the keys or values. | |
212 | * | |
213 | * @return a shallow clone | |
214 | */ | |
215 | 0 | public Object clone() |
216 | { | |
217 | 0 | return super.clone( ); |
218 | } | |
219 | ||
220 | /** | |
221 | * Write the map out using a custom routine. | |
222 | */ | |
223 | 15 | private void writeObject(ObjectOutputStream out) throws IOException |
224 | { | |
225 | 15 | out.defaultWriteObject( ); |
226 | 15 | doWriteObject( out ); |
227 | } | |
228 | ||
229 | /** | |
230 | * Read the map in using a custom routine. | |
231 | */ | |
232 | 15 | private void readObject(ObjectInputStream in) throws IOException, |
233 | ClassNotFoundException | |
234 | { | |
235 | 15 | in.defaultReadObject( ); |
236 | 15 | doReadObject( in ); |
237 | } | |
238 | ||
239 | } |
|