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;
018
019import java.util.Collection;
020import java.util.HashMap;
021import java.util.Iterator;
022
023import org.jivesoftware.smackx.iot.control.ThingControlRequest;
024import org.jivesoftware.smackx.iot.data.ThingMomentaryReadOutRequest;
025import org.jivesoftware.smackx.iot.discovery.element.Tag;
026import org.jivesoftware.smackx.iot.discovery.element.Tag.Type;
027import org.jivesoftware.smackx.iot.element.NodeInfo;
028
029public final class Thing {
030
031    private final HashMap<String, Tag> metaTags;
032    private final boolean selfOwned;
033    private final NodeInfo nodeInfo;
034
035    private final ThingMomentaryReadOutRequest momentaryReadOutRequestHandler;
036    private final ThingControlRequest controlRequestHandler;
037
038    private Thing(Builder builder) {
039        this.metaTags = builder.metaTags;
040        this.selfOwned = builder.selfOwned;
041
042        this.nodeInfo = builder.nodeInfo;
043
044        this.momentaryReadOutRequestHandler = builder.momentaryReadOutRequest;
045        this.controlRequestHandler = builder.controlRequest;
046    }
047
048    public Collection<Tag> getMetaTags() {
049        return metaTags.values();
050    }
051
052    public boolean isSelfOwened() {
053        return selfOwned;
054    }
055
056    public NodeInfo getNodeInfo() {
057        return nodeInfo;
058    }
059
060    public String getNodeId() {
061        return nodeInfo.getNodeId();
062    }
063
064    public String getSourceId() {
065        return nodeInfo.getSourceId();
066    }
067
068    public String getCacheType() {
069        return nodeInfo.getCacheType();
070    }
071
072    public ThingMomentaryReadOutRequest getMomentaryReadOutRequestHandler() {
073        return momentaryReadOutRequestHandler;
074    }
075
076    public ThingControlRequest getControlRequestHandler() {
077        return controlRequestHandler;
078    }
079
080    private String toStringCache;
081
082    @SuppressWarnings("ObjectToString")
083    @Override
084    public String toString() {
085        if (toStringCache == null) {
086            StringBuilder sb = new StringBuilder();
087            sb.append("Thing " + nodeInfo + " [");
088            Iterator<Tag> it = metaTags.values().iterator();
089            while (it.hasNext()) {
090                Tag tag = it.next();
091                sb.append(tag);
092                if (it.hasNext()) {
093                    sb.append(' ');
094                }
095            }
096            sb.append(']');
097            toStringCache = sb.toString();
098        }
099        return toStringCache;
100    }
101
102    public static Builder builder() {
103        return new Builder();
104    }
105
106    public static class Builder {
107        private HashMap<String, Tag> metaTags = new HashMap<>();
108        private boolean selfOwned;
109        private NodeInfo nodeInfo = NodeInfo.EMPTY;
110        private ThingMomentaryReadOutRequest momentaryReadOutRequest;
111        private ThingControlRequest controlRequest;
112
113        public Builder setSerialNumber(String sn) {
114            final String name = "SN";
115            Tag tag = new Tag(name, Type.str, sn);
116            metaTags.put(name, tag);
117            return this;
118        }
119
120        public Builder setKey(String key) {
121            final String name = "KEY";
122            Tag tag = new Tag(name, Type.str, key);
123            metaTags.put(name, tag);
124            return this;
125        }
126
127        public Builder setManufacturer(String manufacturer) {
128            final String name = "MAN";
129            Tag tag = new Tag(name, Type.str, manufacturer);
130            metaTags.put(name, tag);
131            return this;
132        }
133
134        public Builder setModel(String model) {
135            final String name = "MODEL";
136            Tag tag = new Tag(name, Type.str, model);
137            metaTags.put(name, tag);
138            return this;
139        }
140
141        public Builder setVersion(String version) {
142            final String name = "V";
143            Tag tag = new Tag(name, Type.num, version);
144            metaTags.put(name, tag);
145            return this;
146        }
147
148        public Builder setMomentaryReadOutRequestHandler(ThingMomentaryReadOutRequest momentaryReadOutRequestHandler) {
149            this.momentaryReadOutRequest = momentaryReadOutRequestHandler;
150            return this;
151        }
152
153        public Builder setControlRequestHandler(ThingControlRequest controlRequest) {
154            this.controlRequest = controlRequest;
155            return this;
156        }
157
158        public Thing build() {
159            return new Thing(this);
160        }
161    }
162}