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.Playlist; 006import org.intellimate.izou.sdk.resource.Resource; 007 008import java.util.HashMap; 009import java.util.Optional; 010 011/** 012 * returns the current played playlist if asked via the resourceManager 013 * @author LeanderK 014 * @version 1.0 015 */ 016@SuppressWarnings("unused") 017public class NowPlayingResource extends Resource<HashMap<String, Object>> { 018 @SuppressWarnings("SpellCheckingInspection") 019 public static final String ID = "izou.music.resource.nowplaying"; 020 021 /** 022 * creates a new Resource. 023 * This method is thread-safe. 024 * 025 * @param provider the Provider of the Resource 026 */ 027 public NowPlayingResource(Identification provider) { 028 super(ID, provider); 029 } 030 031 /** 032 * creates a new Resource. 033 * This method is thread-safe. 034 * 035 * @param provider the Provider of the Resource 036 * @param playlist the resource 037 */ 038 public NowPlayingResource(Identification provider, Playlist playlist) { 039 super(ID, provider, playlist.export()); 040 } 041 042 /** 043 * creates a new Resource. 044 * This method is thread-safe. 045 * 046 * @param provider the Provider of the Resource 047 * @param consumer the ID of the Consumer 048 */ 049 public NowPlayingResource(Identification provider, Identification consumer) { 050 super(ID, provider, consumer); 051 } 052 053 /** 054 * creates a new Resource. 055 * This method is thread-safe. 056 * 057 * @param provider the Provider of the Resource 058 * @param playlist the resource 059 * @param consumer the ID of the Consumer 060 */ 061 public NowPlayingResource(Identification provider, Playlist playlist, Identification consumer) { 062 super(ID, provider, playlist.export(), consumer); 063 } 064 065 /** 066 * gets the first playlist if found in the EventModel 067 * @param eventModel the EventModel 068 * @return return the optional Playlist 069 */ 070 public static Optional<Playlist> getPlaylist(EventModel eventModel) { 071 if (eventModel.getListResourceContainer().containsResourcesFromSource(ID)) { 072 return eventModel 073 .getListResourceContainer() 074 .provideResource(ID) 075 .stream() 076 .findAny() 077 .flatMap(Playlist::importResource); 078 } else { 079 return Optional.empty(); 080 } 081 } 082}