1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. 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
19 package org.apache.commons.lang3.builder;
20
21 // adapted from org.apache.axis.utils.IDKey
22
23 /**
24 * Wrap an identity key (System.identityHashCode())
25 * so that an object can only be equal() to itself.
26 *
27 * This is necessary to disambiguate the occasional duplicate
28 * identityHashCodes that can occur.
29 *
30 */
31 final class IDKey {
32 private final Object value;
33 private final int id;
34
35 /**
36 * Constructor for IDKey
37 * @param _value The value
38 */
39 public IDKey(final Object _value) {
40 // This is the Object hashcode
41 id = System.identityHashCode(_value);
42 // There have been some cases (LANG-459) that return the
43 // same identity hash code for different objects. So
44 // the value is also added to disambiguate these cases.
45 value = _value;
46 }
47
48 /**
49 * returns hashcode - i.e. the system identity hashcode.
50 * @return the hashcode
51 */
52 @Override
53 public int hashCode() {
54 return id;
55 }
56
57 /**
58 * checks if instances are equal
59 * @param other The other object to compare to
60 * @return if the instances are for the same object
61 */
62 @Override
63 public boolean equals(final Object other) {
64 if (!(other instanceof IDKey)) {
65 return false;
66 }
67 final IDKey idKey = (IDKey) other;
68 if (id != idKey.id) {
69 return false;
70 }
71 // Note that identity equals is used.
72 return value == idKey.value;
73 }
74 }