001package org.intellimate.izou.sdk.frameworks.presence.consumer;
002
003import org.intellimate.izou.sdk.frameworks.presence.resources.PresenceResourceHelper;
004import org.intellimate.izou.sdk.util.ThreadPoolUser;
005
006import java.util.concurrent.ExecutionException;
007import java.util.concurrent.TimeUnit;
008import java.util.concurrent.TimeoutException;
009
010/**
011 * utility class used interacting with information about presence obtained through resource.
012 * For events, please use the normal Listeners.
013 * @author LeanderK
014 * @version 1.0
015 */
016public interface PresenceResourceUser extends PresenceResourceHelper, ThreadPoolUser {
017    /**
018     * returns a CompletableFuture containing true if present, else false.
019     * if not presence-providers were found, it returns false.
020     * Time-Outs every Provider after 500 milliseconds.
021     * @return a future true if present, false if not
022     */
023    default boolean isPresent() {
024        return isPresent(false, false, 500);
025    }
026
027    /**
028     * returns a CompletableFuture containing true if present, else false
029     * if not presence-providers were found, it returns false.
030     * Time-Outs every Provider after 500 milliseconds.
031     * @param strict true if only addons where it is highly likely that the user is around should be creating the result
032     * @return a future true if present, false if not
033     */
034    default boolean isPresent(boolean strict) {
035        return isPresent(strict, false, 500);
036    }
037
038    /**
039     * returns a CompletableFuture containing true if present, else false.
040     * if not presence-providers were found, it returns false.
041     * Time-Outs every Provider after 500 milliseconds.
042     * @param strict true if only addons where it is highly likely that the user is around should be creating the result
043     * @param ifNotPresent the default value
044     * @return a future true if present, false if not
045     */
046    default boolean isPresent(boolean strict, boolean ifNotPresent) {
047        return isPresent(strict, ifNotPresent, 500);
048    }
049
050    /**
051     * returns a CompletableFuture containing true if present, else false.
052     * if not presence-providers were found, it returns false.
053     * Time-Outs every Provider after 500 milliseconds.
054     * @param strict true if only addons where it is highly likely that the user is around should be creating the result
055     * @param ifNotPresent the default value
056     * @param timeout the timeout in milliseconds
057     * @return a future true if present, false if not
058     */
059    default boolean isPresent(boolean strict, boolean ifNotPresent, int timeout) {
060        try {
061            return getIsPresent(strict, ifNotPresent)
062                    .get(timeout, TimeUnit.MILLISECONDS);
063        } catch (InterruptedException | ExecutionException | TimeoutException e) {
064            error("unable to get Value", e);
065            return false;
066        }
067    }
068}