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 org.jivesoftware.smackx.omemo.util.OmemoConstants;
020
021import org.jxmpp.jid.BareJid;
022
023/**
024 * Class that combines a BareJid and a deviceId.
025 *
026 * @author Paul Schaub
027 */
028public class OmemoDevice {
029    private final BareJid jid;
030    private final int deviceId;
031
032    /**
033     * Create a new OmemoDevice.
034     *
035     * @param jid jid of the contact
036     * @param deviceId deviceId if the device.
037     */
038    public OmemoDevice(BareJid jid, int deviceId) {
039        this.jid = jid;
040        this.deviceId = deviceId;
041    }
042
043    /**
044     * Return the BareJid of the device owner.
045     *
046     * @return bare JID of the device owner.
047     */
048    public BareJid getJid() {
049        return this.jid;
050    }
051
052    /**
053     * Return the OMEMO device Id of the device.
054     *
055     * @return OMEMO device ID.
056     */
057    public int getDeviceId() {
058        return this.deviceId;
059    }
060
061    @Override
062    public String toString() {
063        return jid.toString() + ":" + deviceId;
064    }
065
066    @Override
067    public boolean equals(Object other) {
068        return other instanceof OmemoDevice &&
069                ((OmemoDevice) other).getJid().equals(this.getJid()) &&
070                ((OmemoDevice) other).getDeviceId() == this.getDeviceId();
071    }
072
073    @Override
074    public int hashCode() {
075        Integer i;
076        i = jid.hashCode() + deviceId;
077        return i.hashCode();
078    }
079
080    /**
081     * Return the name of the PubSub {@link org.jivesoftware.smackx.pubsub.LeafNode} of this device.
082     * @return node name.
083     */
084    public String getBundleNodeName() {
085        return OmemoConstants.PEP_NODE_BUNDLE_FROM_DEVICE_ID(getDeviceId());
086    }
087}