T
- type of the reference that is wrapped.public final class IdentityWrapper<T> extends Object
Identity semantics for any reference. This wrapper can be used when identity
semantics are needed for objects which might override
equals(...)
and hashCode()
.
This wrapper implements equals(Object)
in a way that an identity
wrapper is only equal to an identity wrapper that refers to exactly the same
object or if both identity wrappers refer to null
. The
hashCode()
method is implemented so that it returns the value of
System.identityHashCode(Object)
calculated for the reference.
Constructor and Description |
---|
IdentityWrapper(T reference)
Creates an identity wrapper that wraps the specified reference.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object obj)
Tells whether the specified object represents an identity wrapper that
refers to the same reference object.
|
T |
get()
Returns the reference that is wrapped by this object.
|
int |
hashCode()
Returns a hash code value for this object.
|
String |
toString()
Returns a string representation of this object.
|
static <T> IdentityWrapper<T> |
wrap(T reference)
Creates an identity wrapper that wraps the specified reference.
|
public IdentityWrapper(T reference)
reference
- reference that shall be wrapped. May be null
.public T get()
null
if this object was initialized with a reference to
null
.null
.public int hashCode()
Returns a hash code value for this object. This method is supported for
the benefit of hash tables. The hash code is calculated by calling
System.identityHashCode(Object)
, passing the reference hold by
this identity wrapper.
If two identity wrappers are equal (as indicated by the return value of
equals(Object)
), they also have the same hash code. However, the
fact that two identity wrappers have the same hash code does not imply
that they are equal.
If the object reference wrapped by this identity wrapper does not
override the hashCode
method but uses the default
implementation (Object.hashCode()
), this method returns the same
value as this.get().hashCode()
.
public boolean equals(Object obj)
Tells whether the specified object represents an identity wrapper that refers to the same reference object.
Two identity wrappers are considered equal if and only if their
references are identical. If obj
is an instance of
IdentityWrapper
, calling this method has the same effect as
using this.get() == obj.get()
. If both identity wrappers
refer to null
, they are also considered equal.
public String toString()
toString()
method or the string "null" if this identity
wrapper's reference is null
.public static <T> IdentityWrapper<T> wrap(T reference)
new IdentityWrapper<T>(reference)
.T
- type of the reference that is wrapped by the identity wrapper.reference
- reference that shall be wrapped. May be null
.reference
.Copyright © 2014–2017 aquenos GmbH. All rights reserved.