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

angry1980.audio.service.TrackSimilarityServiceImpl Maven / Gradle / Ivy

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

import angry1980.audio.dao.TrackDAO;
import angry1980.audio.dao.TrackSimilarityDAO;
import angry1980.audio.model.FingerprintType;
import angry1980.audio.model.Track;
import angry1980.audio.model.TrackSimilarity;
import angry1980.audio.similarity.FindSimilarTracks;
import angry1980.audio.similarity.TrackSimilarities;
import angry1980.audio.similarity.TracksToCalculate;
import angry1980.utils.ImmutableCollectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rx.Observable;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class TrackSimilarityServiceImpl implements TrackSimilarityService {

    private static Logger LOG = LoggerFactory.getLogger(TrackSimilarityServiceImpl.class);

    private TrackDAO trackDAO;
    private TrackSimilarityDAO trackSimilarityDAO;
    private List findSimilarTracks;
    private TracksToCalculate tracksToCalculate;

    public TrackSimilarityServiceImpl(TrackDAO trackDAO,
                                      TrackSimilarityDAO trackSimilarityDAO,
                                      List findSimilarTracks,
                                      TracksToCalculate tracksToCalculate) {
        this.trackDAO = Objects.requireNonNull(trackDAO);
        this.trackSimilarityDAO = Objects.requireNonNull(trackSimilarityDAO);
        this.findSimilarTracks = Objects.requireNonNull(findSimilarTracks);
        this.tracksToCalculate = Objects.requireNonNull(tracksToCalculate);
    }

    @Override
    public Observable getTracksToCalculateSimilarity() {
        return tracksToCalculate.get();
    }

    @Override
    public Observable findOrCalculateSimilarities(Track track) {
        return Observable.just(
                    findSimilarTracks.stream()
                        .peek(handler -> LOG.debug("{} is getting ready to handle by {}", track.getId(), handler))
                        .flatMap(handler -> {
                            try{
                                return handler.apply(track.getId()).stream();
                            } catch(Exception e){
                                LOG.error("Error while trying to handle {} by {}", track.getId(), handler);
                            }
                            return Collections.emptyList().stream();
                        }).collect(ImmutableCollectors.toSet())
                ).map(s -> {
                        LOG.debug("{} was handled. There are {} similarities. ", track.getId(), s.size());
                        return new TrackSimilarities(track, s);
                });
    }

    @Override
    public Observable findOrCalculateSimilarities(Track track, FingerprintType fingerprintType) {
        throw new UnsupportedOperationException();
    }

    @Override
    public Observable getReport() {
        return Observable.create(subscriber -> {
            trackDAO.getAllOrEmpty().stream()
                    .map(track -> new TrackSimilarities(track, trackSimilarityDAO.findByTrackIdOrEmpty(track.getId())))
                    .forEach(subscriber::onNext);
            subscriber.onCompleted();
        });
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy