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          public IDKey(final Object _value) {
38              // This is the Object hashcode 
39              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              value = _value;
44          }
45  
46          /**
47           * returns hashcode - i.e. the system identity hashcode.
48           * @return the hashcode
49           */ 
50          @Override
51          public int hashCode() {
52             return id;
53          }
54  
55          /**
56           * checks if instances are equal
57           * @param other The other object to compare to
58           * @return if the instances are for the same object
59           */ 
60          @Override
61          public boolean equals(final Object other) {
62              if (!(other instanceof IDKey)) {
63                  return false;
64              }
65              final IDKey idKey = (IDKey) other;
66              if (id != idKey.id) {
67                  return false;
68              }
69              // Note that identity equals is used.
70              return value == idKey.value;
71           }
72  }