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

io.atleon.core.UuidHashGroupExtractor 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.nio.ByteBuffer;
import java.util.UUID;
import java.util.function.Function;

public abstract class UuidHashGroupExtractor implements Function {

    private final HashFunction hashFunction = Hashing.murmur3_32();

    private final int modulus;

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

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

    @Override
    public Integer apply(T t) {
        byte[] uuidBytes = extractUuidBytes(t);
        HashCode hash = hashFunction.hashBytes(uuidBytes);
        return Math.abs(hash.asInt() % modulus);
    }

    protected final byte[] extractUuidBytes(T t) {
        UUID uuid = extractUuid(t);
        ByteBuffer uuidBytes = ByteBuffer.allocate(Long.BYTES * 2);
        uuidBytes.putLong(uuid == null ? 0L : uuid.getMostSignificantBits());  // Defend against null UUIDs
        uuidBytes.putLong(uuid == null ? 0L : uuid.getLeastSignificantBits());
        return uuidBytes.array();
    }

    protected abstract UUID extractUuid(T t);

    private static final class Composed extends UuidHashGroupExtractor {

        private final Function extractor;

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

        @Override
        protected UUID extractUuid(T t) {
            return extractor.apply(t);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy