001/**
002 *
003 * Copyright 2016 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.iot.element;
018
019import org.jivesoftware.smack.util.StringUtils;
020import org.jivesoftware.smack.util.XmlStringBuilder;
021
022public final class NodeInfo {
023
024    public static final NodeInfo EMPTY = new NodeInfo();
025
026    private final String nodeId;
027    private final String sourceId;
028    private final String cacheType;
029
030    /**
031     * The internal constructor for the {@link EMPTY} node info marker class.
032     */
033    private NodeInfo() {
034        this.nodeId = null;
035        this.sourceId = null;
036        this.cacheType = null;
037    }
038
039    public NodeInfo(String nodeId, String sourceId, String cacheType) {
040        this.nodeId = StringUtils.requireNotNullNorEmpty(nodeId, "Node ID must not be null nor empty");
041        this.sourceId = sourceId;
042        this.cacheType = cacheType;
043    }
044
045    public String getNodeId() {
046        return nodeId;
047    }
048
049    public String getSourceId() {
050        return sourceId;
051    }
052
053    public String getCacheType() {
054        return cacheType;
055    }
056
057    public void appendTo(XmlStringBuilder xml) {
058        if (nodeId == null) {
059            return;
060        }
061        xml.attribute("nodeId", nodeId).optAttribute("sourceId", sourceId).optAttribute("cacheType", cacheType);
062    }
063
064    @Override
065    @SuppressWarnings("ReferenceEquality")
066    public int hashCode() {
067        if (this == EMPTY) {
068            return 0;
069        }
070        final int prime = 31;
071        int result = 1;
072        result = prime * result + nodeId.hashCode();
073        result = prime * result + ((sourceId == null) ? 0 : sourceId.hashCode());
074        result = prime * result + ((cacheType == null) ? 0 : cacheType.hashCode());
075        return result;
076    }
077
078    @Override
079    public boolean equals(Object other) {
080        if (this == other) {
081            return true;
082        }
083        if (other == null) {
084            return false;
085        }
086        if (!(other instanceof NodeInfo)) {
087            return false;
088        }
089        NodeInfo otherNodeInfo = (NodeInfo) other;
090        if (!nodeId.equals(otherNodeInfo.nodeId)) {
091            return false;
092        }
093        if (StringUtils.nullSafeCharSequenceEquals(sourceId, otherNodeInfo.sourceId)
094                        && StringUtils.nullSafeCharSequenceEquals(cacheType, otherNodeInfo.cacheType)) {
095            return true;
096        }
097        return false;
098    }
099
100//    public static void eventuallyAppend(NodeInfo nodeInfo, XmlStringBuilder xml) {
101//        if (nodeInfo == null)
102//            return;
103//
104//        nodeInfo.appendTo(xml);
105//    }
106}