1 /*
2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import java.lang.reflect.Array;
29 import java.util.function.BiConsumer;
30 import java.util.function.BiFunction;
31 import java.util.function.Consumer;
32
33 /**
34 * This class implements the <tt>Map</tt> interface with a hash table, using
35 * reference-equality in place of object-equality when comparing keys (and
36 * values). In other words, in an <tt>IdentityHashMap</tt>, two keys
37 * <tt>k1</tt> and <tt>k2</tt> are considered equal if and only if
38 * <tt>(k1==k2)</tt>. (In normal <tt>Map</tt> implementations (like
39 * <tt>HashMap</tt>) two keys <tt>k1</tt> and <tt>k2</tt> are considered equal
40 * if and only if <tt>(k1==null ? k2==null : k1.equals(k2))</tt>.)
41 *
42 * <p><b>This class is <i>not</i> a general-purpose <tt>Map</tt>
43 * implementation! While this class implements the <tt>Map</tt> interface, it
44 * intentionally violates <tt>Map's</tt> general contract, which mandates the
45 * use of the <tt>equals</tt> method when comparing objects. This class is
46 * designed for use only in the rare cases wherein reference-equality
47 * semantics are required.</b>
48 *
49 * <p>A typical use of this class is <i>topology-preserving object graph
50 * transformations</i>, such as serialization or deep-copying. To perform such
51 * a transformation, a program must maintain a "node table" that keeps track
1287 s.writeObject(unmaskNull(key));
1288 s.writeObject(tab[i + 1]);
1289 }
1290 }
1291 }
1292
1293 /**
1294 * Reconstitutes the <tt>IdentityHashMap</tt> instance from a stream (i.e.,
1295 * deserializes it).
1296 */
1297 private void readObject(java.io.ObjectInputStream s)
1298 throws java.io.IOException, ClassNotFoundException {
1299 // Read in any hidden stuff
1300 s.defaultReadObject();
1301
1302 // Read in size (number of Mappings)
1303 int size = s.readInt();
1304 if (size < 0)
1305 throw new java.io.StreamCorruptedException
1306 ("Illegal mappings count: " + size);
1307 init(capacity(size));
1308
1309 // Read the keys and values, and put the mappings in the table
1310 for (int i=0; i<size; i++) {
1311 @SuppressWarnings("unchecked")
1312 K key = (K) s.readObject();
1313 @SuppressWarnings("unchecked")
1314 V value = (V) s.readObject();
1315 putForCreate(key, value);
1316 }
1317 }
1318
1319 /**
1320 * The put method for readObject. It does not resize the table,
1321 * update modCount, etc.
1322 */
1323 private void putForCreate(K key, V value)
1324 throws java.io.StreamCorruptedException
1325 {
1326 Object k = maskNull(key);
1327 Object[] tab = table;
|
1 /*
2 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.util;
27
28 import java.lang.reflect.Array;
29 import java.util.function.BiConsumer;
30 import java.util.function.BiFunction;
31 import java.util.function.Consumer;
32 import sun.misc.SharedSecrets;
33
34 /**
35 * This class implements the <tt>Map</tt> interface with a hash table, using
36 * reference-equality in place of object-equality when comparing keys (and
37 * values). In other words, in an <tt>IdentityHashMap</tt>, two keys
38 * <tt>k1</tt> and <tt>k2</tt> are considered equal if and only if
39 * <tt>(k1==k2)</tt>. (In normal <tt>Map</tt> implementations (like
40 * <tt>HashMap</tt>) two keys <tt>k1</tt> and <tt>k2</tt> are considered equal
41 * if and only if <tt>(k1==null ? k2==null : k1.equals(k2))</tt>.)
42 *
43 * <p><b>This class is <i>not</i> a general-purpose <tt>Map</tt>
44 * implementation! While this class implements the <tt>Map</tt> interface, it
45 * intentionally violates <tt>Map's</tt> general contract, which mandates the
46 * use of the <tt>equals</tt> method when comparing objects. This class is
47 * designed for use only in the rare cases wherein reference-equality
48 * semantics are required.</b>
49 *
50 * <p>A typical use of this class is <i>topology-preserving object graph
51 * transformations</i>, such as serialization or deep-copying. To perform such
52 * a transformation, a program must maintain a "node table" that keeps track
1288 s.writeObject(unmaskNull(key));
1289 s.writeObject(tab[i + 1]);
1290 }
1291 }
1292 }
1293
1294 /**
1295 * Reconstitutes the <tt>IdentityHashMap</tt> instance from a stream (i.e.,
1296 * deserializes it).
1297 */
1298 private void readObject(java.io.ObjectInputStream s)
1299 throws java.io.IOException, ClassNotFoundException {
1300 // Read in any hidden stuff
1301 s.defaultReadObject();
1302
1303 // Read in size (number of Mappings)
1304 int size = s.readInt();
1305 if (size < 0)
1306 throw new java.io.StreamCorruptedException
1307 ("Illegal mappings count: " + size);
1308 int cap = capacity(size);
1309 SharedSecrets.getJavaOISAccess().checkArray(s, Object[].class, cap);
1310 init(cap);
1311
1312 // Read the keys and values, and put the mappings in the table
1313 for (int i=0; i<size; i++) {
1314 @SuppressWarnings("unchecked")
1315 K key = (K) s.readObject();
1316 @SuppressWarnings("unchecked")
1317 V value = (V) s.readObject();
1318 putForCreate(key, value);
1319 }
1320 }
1321
1322 /**
1323 * The put method for readObject. It does not resize the table,
1324 * update modCount, etc.
1325 */
1326 private void putForCreate(K key, V value)
1327 throws java.io.StreamCorruptedException
1328 {
1329 Object k = maskNull(key);
1330 Object[] tab = table;
|