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