001/**
002 *
003 * Copyright 2017 Paul Schaub
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.omemo.internal;
018
019import java.io.Serializable;
020import java.util.HashSet;
021import java.util.Set;
022
023/**
024 * This class is used to represent device lists of contacts.
025 * There are active devices (a set of device ids, which was published with the last device list update)
026 * and inactive devices (set of devices that once were active, but are not included in recent list updates).
027 * Both kinds are cached by the client. When a device that was active in the last update is not included in
028 * a new update, it becomes an inactive device. Vice versa, inactive devices can also become active again, by
029 * being included in the latest device list update.
030 * <p>
031 * The client ensures, that his own device id is on the list of active devices, as soon as he gets online.
032 *
033 * @author Paul Schaub
034 */
035public class CachedDeviceList implements Serializable {
036    private static final long serialVersionUID = 3153579238321261203L;
037
038    private final Set<Integer> activeDevices;
039    private final Set<Integer> inactiveDevices;
040
041    public CachedDeviceList() {
042        this.activeDevices = new HashSet<>();
043        this.inactiveDevices = new HashSet<>();
044    }
045
046    /**
047     * Returns all active devices.
048     * Active devices are all devices that were in the latest DeviceList update.
049     *
050     * @return active devices
051     */
052    public Set<Integer> getActiveDevices() {
053        return activeDevices;
054    }
055
056    /**
057     * Return all inactive devices.
058     * Inactive devices are devices which were in a past DeviceList update once, but were not included in
059     * the latest update.
060     *
061     * @return inactive devices
062     */
063    public Set<Integer> getInactiveDevices() {
064        return inactiveDevices;
065    }
066
067    /**
068     * Returns an OmemoDeviceListElement containing all devices (active and inactive).
069     *
070     * @return all devices
071     */
072    public Set<Integer> getAllDevices() {
073        Set<Integer> all = new HashSet<>();
074        all.addAll(activeDevices);
075        all.addAll(inactiveDevices);
076        return all;
077    }
078
079    /**
080     * Merge a device list update into the CachedDeviceList.
081     * The source code should be self explanatory.
082     *
083     * @param deviceListUpdate received device list update
084     */
085    public void merge(Set<Integer> deviceListUpdate) {
086        inactiveDevices.addAll(activeDevices);
087        activeDevices.clear();
088        activeDevices.addAll(deviceListUpdate);
089        inactiveDevices.removeAll(activeDevices);
090    }
091
092    /**
093     * Add a device to the list of active devices.
094     *
095     * @param deviceId deviceId that will be added
096     */
097    public void addDevice(int deviceId) {
098        activeDevices.add(deviceId);
099    }
100
101    /**
102     * Returns true if deviceId is either in the list of active or inactive devices.
103     *
104     * @param deviceId id
105     * @return true or false
106     */
107    public boolean contains(int deviceId) {
108        return activeDevices.contains(deviceId) || inactiveDevices.contains(deviceId);
109    }
110
111    @Override
112    public String toString() {
113        String out = "active: [";
114        for (int id : activeDevices) {
115            out += id + " ";
116        }
117        out += "] inacitve: [";
118        for (int id : inactiveDevices) {
119            out += id + " ";
120        }
121        out += "]";
122        return out;
123    }
124}