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 agents.org.apache.commons.lang.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 | * @author Apache Software Foundation
|
---|
31 | */
|
---|
32 | final class IDKey {
|
---|
33 | private final Object value;
|
---|
34 | private final int id;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Constructor for IDKey
|
---|
38 | * @param _value The value
|
---|
39 | */
|
---|
40 | public IDKey(Object _value) {
|
---|
41 | // This is the Object hashcode
|
---|
42 | id = System.identityHashCode(_value);
|
---|
43 | // There have been some cases (LANG-459) that return the
|
---|
44 | // same identity hash code for different objects. So
|
---|
45 | // the value is also added to disambiguate these cases.
|
---|
46 | value = _value;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * returns hashcode - i.e. the system identity hashcode.
|
---|
51 | * @return the hashcode
|
---|
52 | */
|
---|
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 | public boolean equals(Object other) {
|
---|
63 | if (!(other instanceof IDKey)) {
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 | IDKey idKey = (IDKey) other;
|
---|
67 | if (id != idKey.id) {
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | // Note that identity equals is used.
|
---|
71 | return value == idKey.value;
|
---|
72 | }
|
---|
73 | }
|
---|