001package org.intellimate.izou.sdk.frameworks.presence.provider;
002
003import org.intellimate.izou.resource.ResourceModel;
004
005import java.util.HashMap;
006import java.util.Optional;
007
008/**
009 * this class holds all important information about the Presence of the user
010 * @author LeanderK
011 * @version 1.0
012 */
013public class Presence {
014    public static final String LEVEL_DESCRIPTOR = "izou.presence.provider.presence.level";
015    private final PresenceIndicatorLevel level;
016    public static final String PRESENT_DESCRIPTOR = "izou.presence.provider.presence.present";
017    private final boolean present;
018    public static final String STRICT_DESCRIPTOR = "izou.presence.provider.presence.strict";
019    private final boolean strict;
020    public static final String KNOWN_DESCRIPTOR = "izou.presence.provider.presence.known";
021    private final boolean known;
022
023    /**
024     * returns a new Presence-Object
025     * @param level the level of the Presence (mostly used internally)
026     * @param present whether it is present
027     * @param strict whether it is strict
028     * @param known whether it is known that the user caused this
029     */
030    public Presence(PresenceIndicatorLevel level, boolean present, boolean strict, boolean known) {
031        this.level = level;
032        this.present = present;
033        this.strict = strict;
034        this.known = known;
035    }
036
037    /**
038     * returns the (vague) level of the reliability of the data. Mostly used internally)
039     * @return the Level
040     */
041    public PresenceIndicatorLevel getLevel() {
042        return level;
043    }
044
045    /**
046     * whether it is present AND known
047     * @return true if present
048     */
049    public boolean isPresent() {
050        if (known) return present;
051        else return false;
052    }
053
054    /**
055     * whether it is strict (very high probability that the user is around)
056     * @return true if strict
057     */
058    public boolean isStrict() {
059        return strict;
060    }
061
062    /**
063     * whether it is known that the user cause the Event
064     * @return true if known
065     */
066    public boolean isKnown() {
067        return known;
068    }
069
070    /**
071     * exports the Presence to a HashMap
072     * @return the resulting HashMap
073     */
074    public HashMap<String, Object> export() {
075        HashMap<String, Object> data = new HashMap<>();
076        data.put(LEVEL_DESCRIPTOR, level.name());
077        data.put(PRESENT_DESCRIPTOR, present);
078        data.put(STRICT_DESCRIPTOR, strict);
079        data.put(KNOWN_DESCRIPTOR, known);
080        return data;
081    }
082
083    /**
084     * imports (if no errors occurred) the Presence from the ResourceModel
085     * @param resourceModel the resourcemodel to import from
086     * @return the (optional) presence
087     */
088    public static Optional<Presence> importPresence(ResourceModel resourceModel) {
089        Object resource = resourceModel.getResource();
090        try {
091            //noinspection unchecked
092            HashMap<String, Object> data = (HashMap<String, Object>) resource;
093            PresenceIndicatorLevel level;
094            try {
095                level = PresenceIndicatorLevel.valueOf((String) data.get(LEVEL_DESCRIPTOR));
096            } catch (IllegalArgumentException e) {
097                level = PresenceIndicatorLevel.VERY_WEAK;
098            }
099            boolean present = (boolean) data.get(PRESENT_DESCRIPTOR);
100            boolean strict = (boolean) data.get(STRICT_DESCRIPTOR);
101            boolean known = (boolean) data.get(KNOWN_DESCRIPTOR);
102            return Optional.of(new Presence(level, present, strict, known));
103        } catch (Exception e) {
104            return Optional.empty();
105        }
106    }
107}