001package org.intellimate.izou.sdk.frameworks.music.resources;
002
003import org.intellimate.izou.events.EventModel;
004import org.intellimate.izou.identification.Identification;
005import org.intellimate.izou.sdk.frameworks.music.player.Progress;
006import org.intellimate.izou.sdk.resource.Resource;
007
008import java.util.HashMap;
009import java.util.Optional;
010
011/**
012 * this resource is simply used hold the progress for events,can also be obtained via the resourceManager
013 * @author LeanderK
014 * @version 1.0
015 */
016public class ProgressResource extends Resource<HashMap<String, Long>> {
017    public static final String ID = "izou.music.resource.progress";
018
019    /**
020     * creates a new Resource.
021     * This method is thread-safe.
022     *
023     * @param provider   the Provider of the Resource
024     */
025    public ProgressResource(Identification provider) {
026        super(ID, provider);
027    }
028
029    /**
030     * creates a new Resource.
031     * This method is thread-safe.
032     *
033     * @param provider   the Provider of the Resource
034     * @param progress   the resource
035     */
036    public ProgressResource(Identification provider, Progress progress) {
037        super(ID, provider, progress.export());
038    }
039
040    /**
041     * creates a new Resource.
042     * This method is thread-safe.
043     *
044     * @param provider   the Provider of the Resource
045     * @param consumer   the ID of the Consumer
046     */
047    public ProgressResource(Identification provider, Identification consumer) {
048        super(ID, provider, consumer);
049    }
050
051    /**
052     * creates a new Resource.
053     * This method is thread-safe.
054     *
055     * @param provider   the Provider of the Resource
056     * @param progress   the resource
057     * @param consumer   the ID of the Consumer
058     */
059    public ProgressResource(Identification provider, Progress progress, Identification consumer) {
060        super(ID, provider, progress.export(), consumer);
061    }
062
063    /**
064     * gets the first Progress if found in the EventModel
065     * @param eventModel the EventModel
066     * @return return the optional Progress
067     */
068    public static Optional<Progress> getProgress(EventModel eventModel) {
069        if (eventModel.getListResourceContainer().containsResourcesFromSource(ID)) {
070            return eventModel
071                    .getListResourceContainer()
072                    .provideResource(ID)
073                    .stream()
074                    .findAny()
075                    .flatMap(Progress::importResource);
076
077        } else {
078            return Optional.empty();
079        }
080    }
081}