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

io.atleon.core.StringHashGroupExtractor Maven / Gradle / Ivy

package io.atleon.core;

import com.google.common.hash.HashCode;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;

import java.util.Objects;
import java.util.function.Function;

public abstract class StringHashGroupExtractor implements Function {

    private final HashFunction hashFunction = Hashing.murmur3_32();

    private final int modulus;

    public StringHashGroupExtractor(int modulus) {
        this.modulus = modulus;
    }

    public static  StringHashGroupExtractor composed(Function extractor, int modulus) {
        return new Composed<>(extractor, modulus);
    }

    @Override
    public Integer apply(T t) {
        String string = Objects.toString(extractString(t), "");
        HashCode hash = hashFunction.hashUnencodedChars(string);
        return Math.abs(hash.asInt() % modulus);
    }

    protected abstract String extractString(T t);

    private static final class Composed extends StringHashGroupExtractor {

        private final Function extractor;

        public Composed(Function extractor, int modulus) {
            super(modulus);
            this.extractor = extractor;
        }

        @Override
        protected String extractString(T t) {
            return extractor.apply(t);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy