Interface CrossHash.IHasher

All Superinterfaces:
Serializable
All Known Implementing Classes:
HashCommon.EnumHasher, IColorCenter.Skeleton.GranularHasher
Enclosing class:
CrossHash

public static interface CrossHash.IHasher
extends Serializable
An interface that can be used to move the logic for the hashCode() and equals() methods from a class' methods to an implementation of IHasher that certain collections in SquidLib can use. Primarily useful when the key type is an array, which normally doesn't work as expected in Java hash-based collections, but can if the right collection and IHasher are used. See also Hashers for additional implementations, some of which need dependencies on things the rest of CrossHash doesn't, like a case-insensitive String hasher/equator that uses RegExodus to handle CharSequence comparison on GWT.
  • Method Summary

    Modifier and Type Method Description
    boolean areEqual​(Object left, Object right)
    Not all types you might want to use an IHasher on meaningfully implement .equals(), such as array types; in these situations the areEqual method helps quickly check for equality by potentially having special logic for the type this is meant to check.
    int hash​(Object data)
    If data is a type that this IHasher can specifically hash, this method should use that specific hash; in other situations, it should simply delegate to calling Object.hashCode() on data.
  • Method Details

    • hash

      int hash​(Object data)
      If data is a type that this IHasher can specifically hash, this method should use that specific hash; in other situations, it should simply delegate to calling Object.hashCode() on data. The body of an implementation of this method can be very small; for an IHasher that is meant for byte arrays, the body could be: return (data instanceof byte[]) ? CrossHash.Lightning.hash((byte[]) data) : data.hashCode();
      Parameters:
      data - the Object to hash; this method should take any type but often has special behavior for one type
      Returns:
      a 32-bit int hash code of data
    • areEqual

      boolean areEqual​(Object left, Object right)
      Not all types you might want to use an IHasher on meaningfully implement .equals(), such as array types; in these situations the areEqual method helps quickly check for equality by potentially having special logic for the type this is meant to check. The body of implementations for this method can be fairly small; for byte arrays, it looks like: return left == right || ((left instanceof byte[] && right instanceof byte[]) ? Arrays.equals((byte[]) left, (byte[]) right) : Objects.equals(left, right)); , but for multidimensional arrays you should use the CrossHash.equalityHelper(Object[], Object[], IHasher) method with an IHasher for the inner arrays that are 1D or otherwise already-hash-able, as can be seen in the body of the implementation for 2D char arrays, where charHasher is an existing IHasher that handles 1D arrays: return left == right || ((left instanceof char[][] && right instanceof char[][]) ? equalityHelper((char[][]) left, (char[][]) right, charHasher) : Objects.equals(left, right));
      Parameters:
      left - allowed to be null; most implementations will have special behavior for one type
      right - allowed to be null; most implementations will have special behavior for one type
      Returns:
      true if left is equal to right (preferably by value, but reference equality may sometimes be needed)