001package org.intellimate.izou.sdk.frameworks.presence.resources;
002
003import org.intellimate.izou.sdk.frameworks.presence.provider.Presence;
004import org.intellimate.izou.sdk.util.ResourceUser;
005
006import java.util.ArrayList;
007import java.util.Optional;
008import java.util.concurrent.CompletableFuture;
009import java.util.stream.Collectors;
010
011/**
012 * provides basic methods to interact with the Presence Resource
013 * @author LeanderK
014 * @version 1.0
015 */
016public interface PresenceResourceHelper extends ResourceUser {
017    /**
018     * updates the boolean a non Strict provider is available
019     */
020    default CompletableFuture<Boolean> nonStrictAvailable() {
021        return generateResource(PresenceResource.ID)
022                .orElse(CompletableFuture.completedFuture(new ArrayList<>()))
023                .thenApply(list -> list.stream()
024                        .map(Presence::importPresence)
025                        .filter(Optional::isPresent)
026                        .map(Optional::get)
027                        .noneMatch(Presence::isStrict));
028    }
029
030    /**
031     * returns a CompletableFuture containing true if present, else false.
032     * if not presence-providers were found, it returns false.
033     * @param strict true if only addons where it is highly likely that the user is around should be creating the result
034     * @param ifNotPresent the default value
035     * @return a future true if present, false if not
036     */
037    default CompletableFuture<Boolean> getIsPresent(boolean strict, boolean ifNotPresent) {
038        return generateResource(PresenceResource.ID)
039                .map(future -> future.thenApply(
040                        list -> list.stream()
041                                .map(Presence::importPresence)
042                                .filter(Optional::isPresent)
043                                .map(Optional::get)
044                                .filter(Presence::isKnown)
045                                .filter(presence -> !strict || presence.isStrict())
046                                .collect(Collectors.toList()))
047                        .thenApply(list -> {
048                            if (list.isEmpty()) {
049                                return ifNotPresent;
050                            } else {
051                                return list.stream()
052                                        .anyMatch(Presence::isPresent);
053                            }
054                        })
055                )
056                .orElse(CompletableFuture.completedFuture(ifNotPresent));
057    }
058}