View Javadoc
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  package org.apache.commons.lang3.builder;
19  
20  // adapted from org.apache.axis.utils.IDKey
21  
22  /**
23   * Wrap an identity key (System.identityHashCode())
24   * so that an object can only be equal() to itself.
25   *
26   * This is necessary to disambiguate the occasional duplicate
27   * identityHashCodes that can occur.
28   */
29  final class IDKey {
30          private final Object value;
31          private final int id;
32  
33          /**
34           * Constructor for IDKey
35           * @param value The value
36           */
37          IDKey(final Object value) {
38              // This is the Object hash code
39              this.id = System.identityHashCode(value);
40              // There have been some cases (LANG-459) that return the
41              // same identity hash code for different objects.  So
42              // the value is also added to disambiguate these cases.
43              this.value = value;
44          }
45  
46          /**
47           * checks if instances are equal
48           * @param other The other object to compare to
49           * @return if the instances are for the same object
50           */
51          @Override
52          public boolean equals(final Object other) {
53              if (!(other instanceof IDKey)) {
54                  return false;
55              }
56              final IDKey idKey = (IDKey) other;
57              if (id != idKey.id) {
58                  return false;
59              }
60              // Note that identity equals is used.
61              return value == idKey.value;
62           }
63  
64          /**
65           * returns hash code - i.e. the system identity hash code.
66           * @return the hash code
67           */
68          @Override
69          public int hashCode() {
70             return id;
71          }
72  }