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

There is a newer version: 1.0-pre11
Show newest version
package com.ajjpj.abase.collection;


/**
 * This interface represents a strategy for handling equality, 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 equality, 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); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy