001package org.intellimate.izou.sdk.frameworks.common.resources;
002
003import org.intellimate.izou.events.EventModel;
004import org.intellimate.izou.identification.Identifiable;
005import org.intellimate.izou.identification.Identification;
006import org.intellimate.izou.resource.ResourceModel;
007import org.intellimate.izou.sdk.resource.Resource;
008
009import java.util.Collections;
010import java.util.Optional;
011
012/**
013 * A resource which holds an Identification used for example to select the Player
014 * @author LeanderK
015 * @version 1.0
016 */
017public class SelectorResource extends Resource<Identification> {
018    //also used in izou!
019    public static final String RESOURCE_ID = "izou.common.resource.selector";
020
021    /**
022     * creates a new Resource.
023     * This method is thread-safe.
024     *
025     * @param consumer   the Provider of the Resource
026     */
027    public SelectorResource(Identification consumer) {
028        super(RESOURCE_ID, null, null, consumer);
029    }
030
031    /**
032     * creates a new Resource.
033     * This method is thread-safe.
034     *
035     * @param consumer       the consumer of the Resource
036     * @param identification the resource
037     */
038    public SelectorResource(Identification consumer, Identification identification) {
039        super(RESOURCE_ID, identification, identification, consumer);
040    }
041
042    /**
043     * returns true if the identifiable is the target of the EventModel
044     * @param eventModel the EventModel to check
045     * @param identifiable the identifiable to check against
046     * @return maybe true if found
047     */
048    public static Optional<Boolean> isTarget(EventModel eventModel, Identifiable identifiable) {
049        if (eventModel.getListResourceContainer()
050                .providesResource(Collections.singletonList(SelectorResource.RESOURCE_ID))) {
051               return Optional.of(eventModel.getListResourceContainer()
052                       .provideResource(SelectorResource.RESOURCE_ID)
053                       .stream()
054                       .map(ResourceModel::getResource)
055                       .filter(resource -> resource instanceof Identification)
056                       .map(object -> (Identification) object)
057                       .anyMatch(identifiable::isOwner));
058        } else {
059            return Optional.empty();
060        }
061    }
062
063    /**
064     * returns true if the identifiable is the target of the EventModel
065     * @param eventModel the EventModel to check
066     * @param identification the identification to check against
067     * @return maybe true if found
068     */
069    public static Optional<Boolean> isTarget(EventModel eventModel, Identification identification) {
070        if (eventModel.getListResourceContainer()
071                .providesResource(Collections.singletonList(SelectorResource.RESOURCE_ID))) {
072            return Optional.of(eventModel.getListResourceContainer()
073                    .provideResource(SelectorResource.RESOURCE_ID)
074                    .stream()
075                    .map(ResourceModel::getResource)
076                    .filter(resource -> resource instanceof Identification)
077                    .map(object -> (Identification) object)
078                    .anyMatch(identification::equals));
079        } else {
080            return Optional.empty();
081        }
082    }
083}