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.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;

import java.util.*;

public class TrackHashDAOInMemoryImpl implements TrackHashDAO {

    private final long mask;
    private final Long2ObjectMap> index;

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

    public TrackHashDAOInMemoryImpl(long mask){
        this.index = new Long2ObjectOpenHashMap<>();
        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());
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy