001/**
002 *
003 * Copyright 2016-2017 Fernando Ramirez, Florian Schmaus
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.bob;
018
019import org.jivesoftware.smack.util.StringUtils;
020
021/**
022 * Bits of Binary hash class.
023 *
024 * @author Fernando Ramirez
025 * @author Florian Schmaus
026 * @see <a href="http://xmpp.org/extensions/xep-0231.html">XEP-0231: Bits of
027 *      Binary</a>
028 */
029public class BoBHash {
030
031    private final String hash;
032    private final String hashType;
033    private final String cid;
034
035    /**
036     * BoB hash constructor.
037     *
038     * @param hash
039     * @param hashType
040     */
041    public BoBHash(String hash, String hashType) {
042        this.hash = StringUtils.requireNotNullOrEmpty(hash, "hash must not be null or empty");
043        this.hashType = StringUtils.requireNotNullOrEmpty(hashType, "hashType must not be null or empty");
044        this.cid = this.hashType + '+' + this.hash + "@bob.xmpp.org";
045    }
046
047    /**
048     * Get the hash.
049     *
050     * @return the hash
051     */
052    public String getHash() {
053        return hash;
054    }
055
056    /**
057     * Get the hash type.
058     *
059     * @return the hash type
060     */
061    public String getHashType() {
062        return hashType;
063    }
064
065    /**
066     * BoB hash to src attribute string.
067     *
068     * @return src attribute string
069     */
070    public String toSrc() {
071        return "cid:" + getCid();
072    }
073
074    /**
075     * BoB hash to cid attribute string.
076     *
077     * @return cid attribute string
078     */
079    public String getCid() {
080        return cid;
081    }
082
083    @Override
084    public boolean equals(Object other) {
085        if (other instanceof BoBHash) {
086            BoBHash otherBob = (BoBHash) other;
087            return cid.equals(otherBob.cid);
088        }
089        return false;
090    }
091
092    @Override
093    public int hashCode() {
094        return cid.hashCode();
095    }
096
097    /**
098     * Get BoB hash from src attribute string.
099     *
100     * @param src
101     * @return the BoB hash
102     */
103    public static BoBHash fromSrc(String src) {
104        String hashType = src.substring(src.lastIndexOf("cid:") + 4, src.indexOf("+"));
105        String hash = src.substring(src.indexOf("+") + 1, src.indexOf("@bob.xmpp.org"));
106        return new BoBHash(hash, hashType);
107    }
108
109    /**
110     * Get BoB hash from cid attribute string.
111     *
112     * @param cid
113     * @return the BoB hash
114     */
115    public static BoBHash fromCid(String cid) {
116        String hashType = cid.substring(0, cid.indexOf("+"));
117        String hash = cid.substring(cid.indexOf("+") + 1, cid.indexOf("@bob.xmpp.org"));
118        return new BoBHash(hash, hashType);
119    }
120
121}