OmemoDevice.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.internal;

  18. import org.jivesoftware.smackx.omemo.util.OmemoConstants;

  19. import org.jxmpp.jid.BareJid;

  20. /**
  21.  * Class that combines a BareJid and a deviceId.
  22.  *
  23.  * @author Paul Schaub
  24.  */
  25. public class OmemoDevice {
  26.     private final BareJid jid;
  27.     private final int deviceId;

  28.     /**
  29.      * Create a new OmemoDevice.
  30.      *
  31.      * @param jid jid of the contact
  32.      * @param deviceId deviceId if the device.
  33.      */
  34.     public OmemoDevice(BareJid jid, int deviceId) {
  35.         this.jid = jid;
  36.         this.deviceId = deviceId;
  37.     }

  38.     /**
  39.      * Return the BareJid of the device owner.
  40.      *
  41.      * @return bare JID of the device owner.
  42.      */
  43.     public BareJid getJid() {
  44.         return this.jid;
  45.     }

  46.     /**
  47.      * Return the OMEMO device Id of the device.
  48.      *
  49.      * @return OMEMO device ID.
  50.      */
  51.     public int getDeviceId() {
  52.         return this.deviceId;
  53.     }

  54.     @Override
  55.     public String toString() {
  56.         return jid.toString() + ":" + deviceId;
  57.     }

  58.     @Override
  59.     public boolean equals(Object other) {
  60.         return other instanceof OmemoDevice &&
  61.                 ((OmemoDevice) other).getJid().equals(this.getJid()) &&
  62.                 ((OmemoDevice) other).getDeviceId() == this.getDeviceId();
  63.     }

  64.     @Override
  65.     public int hashCode() {
  66.         Integer i;
  67.         i = jid.hashCode() + deviceId;
  68.         return i.hashCode();
  69.     }

  70.     /**
  71.      * Return the name of the PubSub {@link org.jivesoftware.smackx.pubsub.LeafNode} of this device.
  72.      * @return node name.
  73.      */
  74.     public String getBundleNodeName() {
  75.         return OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(getDeviceId());
  76.     }
  77. }