001package org.intellimate.izou.sdk.frameworks.music.resources;
002
003import org.intellimate.izou.identification.Identification;
004import org.intellimate.izou.resource.ResourceModel;
005import org.intellimate.izou.sdk.frameworks.music.player.PlaybackState;
006import org.intellimate.izou.sdk.resource.Resource;
007
008import java.util.Optional;
009
010/**
011 * this class simply holds the playback-state for events,can also be obtained via the resourceManager
012 * @author LeanderK
013 * @version 1.0
014 */
015public class PlaybackStateResource extends Resource<String> {
016    @SuppressWarnings("SpellCheckingInspection")
017    public static final String ID = "izou.music.resource.playbackstate";
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 PlaybackStateResource(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 state      the PlaybackState
035     */
036    public PlaybackStateResource(Identification provider, PlaybackState state) {
037        super(ID, provider, state.name());
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 PlaybackStateResource(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 state      the PlaybackState
057     * @param consumer   the ID of the Consumer
058     */
059    public PlaybackStateResource(Identification provider, PlaybackState state, Identification consumer) {
060        super(ID, provider, state.name(), consumer);
061    }
062
063    /**
064     * returns the PlaybackState from the resource
065     * @param x the resourceModel
066     * @return the optional Playbackstate (empty if illegal resource)
067     */
068    public static Optional<PlaybackState> getPlaybackStateFromResource(ResourceModel x) {
069        if (!x.getResourceID().equals(ID))
070            return Optional.empty();
071        Object resource = x.getResource();
072        if (resource instanceof String) {
073            String state = (String) resource;
074            try {
075                return Optional.of(PlaybackState.valueOf(state));
076            } catch (IllegalArgumentException e) {
077                return Optional.empty();
078            }
079        } else {
080            return Optional.empty();
081        }
082    }
083}