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

com.jnape.palatable.shoki.api.HashingAlgorithm Maven / Gradle / Ivy

The newest version!
package com.jnape.palatable.shoki.api;

import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.functor.Applicative;

import java.util.Objects;

/**
 * A {@link HashingAlgorithm hashing algorithm} is an {@link Fn1 arrow} A -> Integer. Good
 * hashing algorithms are generally fast and uniformly distribute values of type A across values of type
 * {@link Integer} (modulo cardinality differences where A's cardinality exceeds {@link Integer}'s).
 *
 * @param  the type to hash
 */
public interface HashingAlgorithm extends Fn1 {

    /**
     * {@inheritDoc}
     */
    @Override
    default HashingAlgorithm local(Fn1 fn) {
        return Fn1.super.local(fn)::apply;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    default HashingAlgorithm censor(Fn1 fn) {
        return Fn1.super.censor(fn)::apply;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    default  HashingAlgorithm discardR(Applicative> appB) {
        return Fn1.super.discardR(appB)::apply;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    default  HashingAlgorithm diMapL(Fn1 fn) {
        return Fn1.super.diMapL(fn)::apply;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    default  HashingAlgorithm contraMap(Fn1 fn) {
        return Fn1.super.contraMap(fn)::apply;
    }

    /**
     * A {@link HashingAlgorithm} implemented in terms of {@link Objects#hashCode(Object)}.
     *
     * @param  the type to hash
     * @return a {@link HashingAlgorithm} implemented in terms of {@link Objects#hashCode(Object)}.
     */
    static  HashingAlgorithm objectHashCode() {
        return Objects::hashCode;
    }

    /**
     * A {@link HashingAlgorithm} implemented in terms of {@link System#identityHashCode(Object)}.
     *
     * @param  the type to hash
     * @return a {@link HashingAlgorithm} implemented in terms of {@link System#identityHashCode(Object)}.
     */
    static  HashingAlgorithm identityHashCode() {
        return System::identityHashCode;
    }


    /**
     * A {@link HashingAlgorithm} implemented in terms of {@link java.util.Arrays#hashCode(Object[])}.
     *
     * @param  the array component type
     * @return a {@link HashingAlgorithm} implemented in terms of {@link java.util.Arrays#hashCode(Object[])}.
     */
    static  HashingAlgorithm arraysHashCode() {
        return java.util.Arrays::hashCode;
    }

    /**
     * A {@link HashingAlgorithm} implemented in terms of {@link java.util.Arrays#deepHashCode(Object[])}.
     *
     * @return a {@link HashingAlgorithm} implemented in terms of {@link java.util.Arrays#deepHashCode(Object[])}.
     */
    static HashingAlgorithm arraysDeepHashCode() {
        return java.util.Arrays::deepHashCode;
    }

    /**
     * Compute the hash of a in terms of the given {@link HashingAlgorithm hashingAlgorithm}.
     *
     * @param               the value type
     * @param hashingAlgorithm the {@link HashingAlgorithm}
     * @param a                the value
     * @return the hash
     */
    static  int hash(HashingAlgorithm hashingAlgorithm, A a) {
        return hashingAlgorithm.apply(a);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy