OmemoDeviceListElement.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo.element;

  18. import java.util.Collections;
  19. import java.util.HashSet;
  20. import java.util.Iterator;
  21. import java.util.Set;

  22. import org.jivesoftware.smack.packet.ExtensionElement;
  23. import org.jivesoftware.smack.util.Objects;
  24. import org.jivesoftware.smack.util.XmlStringBuilder;

  25. import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;

  26. /**
  27.  * A OMEMO device list update containing the IDs of all active devices of a contact.
  28.  *
  29.  * @author Paul Schaub
  30.  */
  31. public abstract class OmemoDeviceListElement implements ExtensionElement {

  32.     public static final String DEVICE = "device";
  33.     public static final String ID = "id";
  34.     public static final String LIST = "list";

  35.     /**
  36.      * Unmodifiable set of device IDs.
  37.      */
  38.     private final Set<Integer> deviceIds;

  39.     public OmemoDeviceListElement(Set<Integer> deviceIds) {
  40.         deviceIds = Objects.requireNonNull(deviceIds);
  41.         this.deviceIds = Collections.unmodifiableSet(deviceIds);
  42.     }

  43.     public OmemoDeviceListElement(OmemoCachedDeviceList cachedList) {
  44.         this.deviceIds = Collections.unmodifiableSet(cachedList.getActiveDevices());
  45.     }

  46.     public Set<Integer> getDeviceIds() {
  47.         return deviceIds;
  48.     }

  49.     public Set<Integer> copyDeviceIds() {
  50.         return new HashSet<>(deviceIds);
  51.     }

  52.     @Override
  53.     public String getElementName() {
  54.         return LIST;
  55.     }

  56.     @Override
  57.     public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  58.         XmlStringBuilder sb = new XmlStringBuilder(this).rightAngleBracket();

  59.         for (Integer id : deviceIds) {
  60.             sb.halfOpenElement(DEVICE).attribute(ID, id).closeEmptyElement();
  61.         }

  62.         sb.closeElement(this);
  63.         return sb;
  64.     }

  65.     @Override
  66.     public final String toString() {
  67.         StringBuilder sb = new StringBuilder("OmemoDeviceListElement[");
  68.         Iterator<Integer> iterator = deviceIds.iterator();
  69.         for (int i : deviceIds) {
  70.             sb.append(i);
  71.             if (iterator.hasNext()) {
  72.                 sb.append(',');
  73.             }
  74.         }
  75.         return sb.append(']').toString();
  76.     }
  77. }