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

angry1980.audio.dao.TrackHashDAOInMemoryImpl Maven / Gradle / Ivy

There is a newer version: 0.0.10
Show newest version
package angry1980.audio.dao;

import angry1980.audio.model.TrackHash;
import it.unimi.dsi.fastutil.longs.*;

import java.util.*;
import java.util.stream.Collectors;

public class TrackHashDAOInMemoryImpl implements TrackHashDAO {

    private static Comparator c = Comparator.comparingInt(TrackHash::getTime);

    private final long mask;
    private final Long2ObjectMap> index;

    public TrackHashDAOInMemoryImpl(){
        this(16, -1);
    }

    public TrackHashDAOInMemoryImpl(long mask){
        this(16, mask);
    }

    public TrackHashDAOInMemoryImpl(int expectedSize, long mask){
        this.index = new Long2ObjectOpenHashMap<>(expectedSize);
        this.mask = mask;
    }

    @Override
    public Optional create(TrackHash hash) {
        if(hash != null){
            index.computeIfAbsent(getKey(hash.getHash()), l -> new ArrayList<>()).add(hash);
        }
        return Optional.ofNullable(hash);
    }

    @Override
    public Collection findByHash(long hash) {
        return index.getOrDefault(getKey(hash), Collections.emptyList());
    }

    @Override
    public Collection findByHashes(long[] hashes) {
        return Arrays.stream(hashes)
                .mapToObj(this::findByHash)
                .flatMap(data -> data.stream())
                .collect(Collectors.toSet());
    }

    private long getKey(long hash){
        if(mask == -1){
            return hash;
        }
        return hash & mask;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy