net.openhft.collect
Class Equivalence<T>

java.lang.Object
  extended by net.openhft.collect.Equivalence<T>
Type Parameters:
T - type of objects compared by this equivalence
Direct Known Subclasses:
StatelessEquivalence

public abstract class Equivalence<T>
extends Object

A strategy for determining whether two instances are considered equivalent.

This class is inspired and very similar to Guava's Equivalence, with two notable differences.

First, this Equivalence is consistently nullable, i. e. when null is passed instead of Equivalence instance anywhere in the library, default Java equality is assumed (see Object.equals(Object) and Object.hashCode()). In Guava there is a special instance for such equivalence.

Second, this Equivalence forces the actual implementation to override equals(Object) and hashCode(). Notice these are Equivalence's own equals and hashCode, not the strategy equivalent(Object, Object) and hash(Object) methods. It is needed because, for example, ObjCollection's equality depends on Equivalence equality.

In most cases, when Equivalence is stateless, you can extend StatelessEquivalence not to bother with implementing these methods. See examples in the documentation to identity and case insensitive equivalences.


Constructor Summary
protected Equivalence()
          Constructor for use by subclasses.
 
Method Summary
static Equivalence<String> caseInsensitive()
          Returns the String equivalence that uses String.equalsIgnoreCase(java.lang.String) to compare strings.
static
<K,V> Equivalence<Map.Entry<K,V>>
entryEquivalence(Equivalence<K> keyEquivalence, Equivalence<V> valueEquivalence)
          Returns a Map.Entry equivalence for the given key and value equivalences.
abstract  boolean equals(Object o)
           This method is made abstract to force the final implementation to override it.
abstract  boolean equivalent(T a, T b)
          Returns true if a and b are considered equivalent, false otherwise.
abstract  int hash(T t)
          Returns a hash code for the given object.
abstract  int hashCode()
           This method is made abstract to force the final implementation to override it.
static Equivalence<Object> identity()
          Returns the equivalence that uses == to compare objects and System.identityHashCode(Object) to compute the hash code.
 boolean nullableEquivalent(T a, T b)
          Returns true if a and b are considered equivalent, false otherwise.
 int nullableHash(T t)
          Returns a hash code for the given object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Equivalence

protected Equivalence()
Constructor for use by subclasses.

Method Detail

identity

@Nonnull
public static Equivalence<Object> identity()
Returns the equivalence that uses == to compare objects and System.identityHashCode(Object) to compute the hash code. nullableEquivalent(T, T) returns true if a == b, including in the case when a and b are both null.

This equivalence could be implemented as follows:

final class Identity extends StatelessEquivalence&lt;Object&gt; {
     static final Identity INSTANCE = new Identity();
     private Identity() {}
     &#064;Override
     public boolean equivalent(@Nonnull Object a, @Nonnull Object b) {
         return a == b;
     }
     &#064;Override
     public int hash(@Nonnull Object t) {
         return System.identityHashCode(t);
     }
 }
 

Returns:
the identity equivalence

caseInsensitive

@Nonnull
public static Equivalence<String> caseInsensitive()
Returns the String equivalence that uses String.equalsIgnoreCase(java.lang.String) to compare strings.

This equivalence could be implemented as follows:

final class CaseInsensitive extends StatelessEquivalence&lt;String&gt; {
     static final CaseInsensitive INSTANCE = new CaseInsensitive();
     private CaseInsensitive() {}
     &#064;Override
     public boolean equivalent(@Nonnull String a, @Nonnull String b) {
         return a.equalsIgnoreCase(b);
     }
     &#064;Override
     public int hash(@Nonnull String s) {
         return s.toLowerCase().hashCode();
     }
 }
 

Returns:
the case-insensitive String equivalence

entryEquivalence

@Nullable
public static <K,V> Equivalence<Map.Entry<K,V>> entryEquivalence(@Nullable
                                                                          Equivalence<K> keyEquivalence,
                                                                          @Nullable
                                                                          Equivalence<V> valueEquivalence)
Returns a Map.Entry equivalence for the given key and value equivalences.

Type Parameters:
K - the entry key type
V - the entry value type
Parameters:
keyEquivalence - the entry key equivalence
valueEquivalence - the entry value equivalence
Returns:
a Map.Entry equivalence for the given key and value equivalences

nullableEquivalent

public boolean nullableEquivalent(@Nullable
                                  T a,
                                  @Nullable
                                  T b)
Returns true if a and b are considered equivalent, false otherwise. a and b both might be null.

If the implementation overrides this method, it must ensure that it returns true if both the given objects are nulls and false, if only one of them is null. If both a and b are non-null, this method should perform just the same as equivalent(Object, Object) method does.

Parameters:
a - the first object to compare
b - the second object to compare
Returns:
true if a and b are considered equivalent, false otherwise

equivalent

public abstract boolean equivalent(@Nonnull
                                   T a,
                                   @Nonnull
                                   T b)
Returns true if a and b are considered equivalent, false otherwise. a and b are assumed to be non-null.

This method implements an equivalence relation on object references:

This method is called by nullableEquivalent(Object, Object).

Parameters:
a - the first object to compare
b - the second object to compare
Returns:
true if a and b are considered equivalent, false otherwise

nullableHash

public int nullableHash(@Nullable
                        T t)
Returns a hash code for the given object. The t object might be null.

If the implementation overrides this method, it must ensure that it returns 0 if the given object is null. Otherwise this method should perform just the same as hash(Object) method does.

Parameters:
t - the object to compute hash code for
Returns:
a hash code for the given object

hash

public abstract int hash(@Nonnull
                         T t)
Returns a hash code for the given object. The t object is assumed to be non-null.

This method has the following properties:

This method is called by nullableHash(Object).

Parameters:
t - the object to compute hash code for
Returns:
a hash code for the given object

equals

public abstract boolean equals(Object o)
This method is made abstract to force the final implementation to override it. It is needed because, for example, ObjObjMap's equality depends on key and value Equivalence equality.

Overrides:
equals in class Object

hashCode

public abstract int hashCode()
This method is made abstract to force the final implementation to override it. It is needed because, equals(Object) is needed to be overridden, and in Java Object.equals(Object) and Object.hashCode() should always be overridden simultaneously.

Overrides:
hashCode in class Object