KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > IdentityMap


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.map;
17
18 import java.io.IOException JavaDoc;
19 import java.io.ObjectInputStream JavaDoc;
20 import java.io.ObjectOutputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * A <code>Map</code> implementation that matches keys and values based
26  * on <code>==</code> not <code>equals()</code>.
27  * <p>
28  * This map will violate the detail of various Map and map view contracts.
29  * As a general rule, don't compare this map to other maps.
30  *
31  * @since Commons Collections 3.0
32  * @version $Revision: 1.5 $ $Date: 2004/02/18 01:13:19 $
33  *
34  * @author java util HashMap
35  * @author Stephen Colebourne
36  */

37 public class IdentityMap
38         extends AbstractHashedMap implements Serializable JavaDoc, Cloneable JavaDoc {
39
40     /** Serialisation version */
41     private static final long serialVersionUID = 2028493495224302329L;
42
43     /**
44      * Constructs a new empty map with default size and load factor.
45      */

46     public IdentityMap() {
47         super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD);
48     }
49
50     /**
51      * Constructs a new, empty map with the specified initial capacity.
52      *
53      * @param initialCapacity the initial capacity
54      * @throws IllegalArgumentException if the initial capacity is less than one
55      */

56     public IdentityMap(int initialCapacity) {
57         super(initialCapacity);
58     }
59
60     /**
61      * Constructs a new, empty map with the specified initial capacity and
62      * load factor.
63      *
64      * @param initialCapacity the initial capacity
65      * @param loadFactor the load factor
66      * @throws IllegalArgumentException if the initial capacity is less than one
67      * @throws IllegalArgumentException if the load factor is less than zero
68      */

69     public IdentityMap(int initialCapacity, float loadFactor) {
70         super(initialCapacity, loadFactor);
71     }
72
73     /**
74      * Constructor copying elements from another map.
75      *
76      * @param map the map to copy
77      * @throws NullPointerException if the map is null
78      */

79     public IdentityMap(Map JavaDoc map) {
80         super(map);
81     }
82
83     //-----------------------------------------------------------------------
84
/**
85      * Gets the hash code for the key specified.
86      * This implementation uses the identity hash code.
87      *
88      * @param key the key to get a hash code for
89      * @return the hash code
90      */

91     protected int hash(Object JavaDoc key) {
92         return System.identityHashCode(key);
93     }
94     
95     /**
96      * Compares two keys for equals.
97      * This implementation uses <code>==</code>.
98      *
99      * @param key1 the first key to compare
100      * @param key2 the second key to compare
101      * @return true if equal by identity
102      */

103     protected boolean isEqualKey(Object JavaDoc key1, Object JavaDoc key2) {
104         return (key1 == key2);
105     }
106     
107     /**
108      * Compares two values for equals.
109      * This implementation uses <code>==</code>.
110      *
111      * @param value1 the first value to compare
112      * @param value2 the second value to compare
113      * @return true if equal by identity
114      */

115     protected boolean isEqualValue(Object JavaDoc value1, Object JavaDoc value2) {
116         return (value1 == value2);
117     }
118     
119     /**
120      * Creates an entry to store the data.
121      * This implementation creates an IdentityEntry instance.
122      *
123      * @param next the next entry in sequence
124      * @param hashCode the hash code to use
125      * @param key the key to store
126      * @param value the value to store
127      * @return the newly created entry
128      */

129     protected HashEntry createEntry(HashEntry next, int hashCode, Object JavaDoc key, Object JavaDoc value) {
130         return new IdentityEntry(next, hashCode, key, value);
131     }
132     
133     //-----------------------------------------------------------------------
134
/**
135      * HashEntry
136      */

137     protected static class IdentityEntry extends HashEntry {
138         
139         protected IdentityEntry(HashEntry next, int hashCode, Object JavaDoc key, Object JavaDoc value) {
140             super(next, hashCode, key, value);
141         }
142         
143         public boolean equals(Object JavaDoc obj) {
144             if (obj == this) {
145                 return true;
146             }
147             if (obj instanceof Map.Entry JavaDoc == false) {
148                 return false;
149             }
150             Map.Entry JavaDoc other = (Map.Entry JavaDoc) obj;
151             return
152                 (getKey() == other.getKey()) &&
153                 (getValue() == other.getValue());
154         }
155         
156         public int hashCode() {
157             return System.identityHashCode(getKey()) ^
158                    System.identityHashCode(getValue());
159         }
160     }
161     
162     //-----------------------------------------------------------------------
163
/**
164      * Clones the map without cloning the keys or values.
165      *
166      * @return a shallow clone
167      */

168     public Object JavaDoc clone() {
169         return super.clone();
170     }
171     
172     /**
173      * Write the map out using a custom routine.
174      */

175     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
176         out.defaultWriteObject();
177         doWriteObject(out);
178     }
179
180     /**
181      * Read the map in using a custom routine.
182      */

183     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
184         in.defaultReadObject();
185         doReadObject(in);
186     }
187     
188 }
189
Popular Tags