com.ajjpj.abase.collection.AEquality Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-base Show documentation
Show all versions of a-base Show documentation
a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations
package com.ajjpj.abase.collection;
/**
* This interface represents a strategy for handling equalityForEquals, typically between elements of a collection. It contains
* both equals()
and hashCode
methods because in general collections rely on both, and these
* two methods must be consistent with each other.
*
* There are two typical strategies that cover the vast majority of cases. They are readily available as constants in
* the interface:
*
* - EQUALS: Delegates to
Object.equals()
and Object.hashCode()
of the elements in
* question. This is the default behavior of standard library collections.
* - IDENTITY: Uses same-ness '==' to decide equalityForEquals, and
System.identityHashCode()
for hash code
* calculation. This is the behavior exhibited by IdentityHashMap
.
*
*
* While these two implementations are probably sufficient for most contexts, other strategies are possible (e.g. based
* on database primary keys).
*
* @author arno
*/
public interface AEquality {
boolean equals(Object o1, Object o2);
int hashCode(Object o);
AEquality EQUALS = new Equals();
AEquality IDENTITY = new Identity();
class Equals implements AEquality {
@Override
public boolean equals(Object o1, Object o2) {
if (o1 == null) {
return o2 == null;
}
else {
return o1.equals(o2);
}
}
@Override
public int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
}
class Identity implements AEquality {
@Override
public boolean equals(Object o1, Object o2) {
return o1 == o2;
}
@Override
public int hashCode(Object o) {
return System.identityHashCode(o);
}
}
}