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

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

package angry1980.audio.dao;

import angry1980.audio.model.ImmutableTrack;
import angry1980.audio.model.NetflixNodeType;
import angry1980.audio.model.NetflixRelationType;
import angry1980.audio.model.Track;
import com.netflix.nfgraph.util.OrdinalMap;

import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

public class TrackDAONetflixImpl extends Netflix implements TrackDAO{

    public TrackDAONetflixImpl(NetflixData data) {
        super(data);
    }

    @Override
    public Collection findByCluster(long cluster) {
        int ordinal = data.getClusters().get(cluster);
        return getAll().map(tracks -> tracks.stream()
                                .filter(track -> ordinal == getClusterNode(track.getId()))
                                .collect(Collectors.toList())
        ).orElseGet(() -> Collections.emptyList());
    }

    @Override
    public Track tryToGet(long id) {
        int ordinal = data.getTracks().get(id);
        if(ordinal < 0){
            return null;
        }
        return track(id);
    }

    @Override
    public Collection tryToGetAll() {
        return getAllNodes()
                .map(id -> this.track(id))
                .collect(Collectors.toList());
    }

    @Override
    public Track tryToCreate(Track entity) {
        addConnection(entity.getId(), NetflixRelationType.IS, data.getClusters().add(entity.getCluster()));
        addConnection(entity.getId(), NetflixRelationType.SITUATED, data.getPaths().add(entity.getPath()));
        return entity;
    }

    private Track track(long id){
        return ImmutableTrack.builder().id(id).path(trackPath(id)).cluster(trackCluster(id)).build();
    }

    private String trackPath(long id){
        return connectionValue(data.getPaths(), this::getPathNode, id);
    }

    private long trackCluster(long id){
        return connectionValue(data.getClusters(), this::getClusterNode, id);
    }

    private int getClusterNode(long trackId){
        return getConnectionNode(trackId, NetflixRelationType.IS);
    }

    private int getPathNode(long trackId){
        return getConnectionNode(trackId, NetflixRelationType.SITUATED);
    }

    @Override
    protected NetflixNodeType getNodeType() {
        return NetflixNodeType.TRACK;
    }

    @Override
    protected OrdinalMap getValues(NetflixData data) {
        return data.getTracks();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy