All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ajjpj.abase.collection.AEquality Maven / Gradle / Ivy

Go to download

a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations

The newest version!
package com.ajjpj.abase.collection;


import java.io.Serializable;


/**
 * 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(); /** * Comparison by object identity. This equality strategy is intentionally not serializable since identity * of elements in a collection is bound to be lost during serialization. */ AEquality IDENTITY = new Identity(); class Equals implements AEquality, Serializable { private Object readResolve() { return EQUALS; } @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); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy