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.element;
018
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.Iterator;
022import java.util.Set;
023
024import org.jivesoftware.smack.packet.ExtensionElement;
025import org.jivesoftware.smack.util.Objects;
026import org.jivesoftware.smack.util.XmlStringBuilder;
027
028import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
029
030/**
031 * A OMEMO device list update containing the IDs of all active devices of a contact.
032 *
033 * @author Paul Schaub
034 */
035public abstract class OmemoDeviceListElement implements ExtensionElement {
036
037    public static final String DEVICE = "device";
038    public static final String ID = "id";
039    public static final String LIST = "list";
040
041    /**
042     * Unmodifiable set of device IDs.
043     */
044    private final Set<Integer> deviceIds;
045
046    public OmemoDeviceListElement(Set<Integer> deviceIds) {
047        deviceIds = Objects.requireNonNull(deviceIds);
048        this.deviceIds = Collections.unmodifiableSet(deviceIds);
049    }
050
051    public OmemoDeviceListElement(OmemoCachedDeviceList cachedList) {
052        this.deviceIds = Collections.unmodifiableSet(cachedList.getActiveDevices());
053    }
054
055    public Set<Integer> getDeviceIds() {
056        return deviceIds;
057    }
058
059    public Set<Integer> copyDeviceIds() {
060        return new HashSet<>(deviceIds);
061    }
062
063    @Override
064    public String getElementName() {
065        return LIST;
066    }
067
068    @Override
069    public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
070        XmlStringBuilder sb = new XmlStringBuilder(this).rightAngleBracket();
071
072        for (Integer id : deviceIds) {
073            sb.halfOpenElement(DEVICE).attribute(ID, id).closeEmptyElement();
074        }
075
076        sb.closeElement(this);
077        return sb;
078    }
079
080    @Override
081    public final String toString() {
082        StringBuilder sb = new StringBuilder("OmemoDeviceListElement[");
083        Iterator<Integer> iterator = deviceIds.iterator();
084        for (int i : deviceIds) {
085            sb.append(i);
086            if (iterator.hasNext()) {
087                sb.append(',');
088            }
089        }
090        return sb.append(']').toString();
091    }
092}