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.resource.ResourceModel;
006import org.intellimate.izou.sdk.resource.Resource;
007
008/**
009 * this resource signals that an addon requests playing.
010 * <p>
011 * It is mostly used for non-permanent Music-Usage which blocks the Event in the Output lifecycle.<br>
012 * For example if you are an alarm you can add this resource to an Event and it will play the alarm sound, blocking
013 * the execution for other outputPlugins.
014 * </p>
015 * @author LeanderK
016 * @version 1.0
017 */
018public class MusicUsageResource extends Resource<Boolean> {
019    @SuppressWarnings("SpellCheckingInspection")
020    public static final String ID = "izou.music.resource.permanent";
021
022    /**
023     * creates a new Resource.
024     *
025     * @param provider   the Provider of the Resource
026     * @param permanent   true if permanent, false if not
027     */
028    public MusicUsageResource(Identification provider, Boolean permanent) {
029        super(ID, provider, permanent);
030    }
031
032    /**
033     * gets whether the request is permanent (=permanent resource is available and true)
034     * @param eventModel the EventModel
035     * @return true if permanent
036     */
037    public static boolean isPermanent(EventModel eventModel) {
038        if (eventModel.getListResourceContainer().containsResourcesFromSource(ID)) {
039            return eventModel
040                    .getListResourceContainer()
041                    .provideResource(ID)
042                    .stream()
043                    .map(ResourceModel::getResource)
044                    .filter(ob -> ob instanceof Boolean)
045                    .map(ob -> (Boolean) ob)
046                    .findAny()
047                    .orElse(false);
048
049        } else {
050            return false;
051        }
052    }
053
054    /**
055     * returns true if the resource is true, otherwise returns false
056     * @param resourceModel the resourceModel
057     * @return true if permanent
058     */
059    public static boolean isPermanent(ResourceModel resourceModel) {
060        Object resource = resourceModel.getResource();
061        try {
062            return (Boolean) resource;
063        } catch (ClassCastException e) {
064            return false;
065        }
066    }
067}