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

angry1980.audio.fingerprint.GetOrCreateFingerprint Maven / Gradle / Ivy

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

import angry1980.audio.model.Fingerprint;
import angry1980.audio.dao.FingerprintDAO;
import angry1980.audio.dao.TrackDAO;

import java.util.Objects;
import java.util.function.LongFunction;

public class GetOrCreateFingerprint implements LongFunction{

    private FingerprintDAO fingerprintDAO;
    private TrackDAO trackDAO;
    private Calculator fingerprintCalculator;
    private InvertedIndex invertedIndex;

    public GetOrCreateFingerprint(FingerprintDAO fingerprintDAO,
                                  TrackDAO trackDAO,
                                  Calculator fingerprintCalculator,
                                  InvertedIndex invertedIndex) {
        this.fingerprintDAO = Objects.requireNonNull(fingerprintDAO);
        this.trackDAO = Objects.requireNonNull(trackDAO);
        this.fingerprintCalculator = Objects.requireNonNull(fingerprintCalculator);
        this.invertedIndex = Objects.requireNonNull(invertedIndex);
    }

    @Override
    public F apply(long trackId) {
        return fingerprintDAO.findByTrackId(trackId)
                .orElseGet(() ->
                        trackDAO.get(trackId)
                                .flatMap(fingerprintCalculator::calculate)
                                .flatMap(fingerprintDAO::create)
                                .map(invertedIndex::save)
                                .orElse(null)
                );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy