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    @Override
083    public String toString() {
084        if (toStringCache == null) {
085            StringBuilder sb = new StringBuilder();
086            sb.append("Thing " + nodeInfo + " [");
087            Iterator<Tag> it = metaTags.values().iterator();
088            while (it.hasNext()) {
089                Tag tag = it.next();
090                sb.append(tag);
091                if (it.hasNext()) {
092                    sb.append(' ');
093                }
094            }
095            sb.append(']');
096            toStringCache = sb.toString();
097        }
098        return toStringCache;
099    }
100
101    public static Builder builder() {
102        return new Builder();
103    }
104
105    public static class Builder {
106        private HashMap<String, Tag> metaTags = new HashMap<>();
107        private boolean selfOwned;
108        private NodeInfo nodeInfo = NodeInfo.EMPTY;
109        private ThingMomentaryReadOutRequest momentaryReadOutRequest;
110        private ThingControlRequest controlRequest;
111
112        public Builder setSerialNumber(String sn) {
113            final String name = "SN";
114            Tag tag = new Tag(name, Type.str, sn);
115            metaTags.put(name, tag);
116            return this;
117        }
118
119        public Builder setKey(String key) {
120            final String name = "KEY";
121            Tag tag = new Tag(name, Type.str, key);
122            metaTags.put(name, tag);
123            return this;
124        }
125
126        public Builder setManufacturer(String manufacturer) {
127            final String name = "MAN";
128            Tag tag = new Tag(name, Type.str, manufacturer);
129            metaTags.put(name, tag);
130            return this;
131        }
132
133        public Builder setModel(String model) {
134            final String name = "MODEL";
135            Tag tag = new Tag(name, Type.str, model);
136            metaTags.put(name, tag);
137            return this;
138        }
139
140        public Builder setVersion(String version) {
141            final String name = "V";
142            Tag tag = new Tag(name, Type.num, version);
143            metaTags.put(name, tag);
144            return this;
145        }
146
147        public Builder setMomentaryReadOutRequestHandler(ThingMomentaryReadOutRequest momentaryReadOutRequestHandler) {
148            this.momentaryReadOutRequest = momentaryReadOutRequestHandler;
149            return this;
150        }
151
152        public Builder setControlRequestHandler(ThingControlRequest controlRequest) {
153            this.controlRequest = controlRequest;
154            return this;
155        }
156
157        public Thing build() {
158            return new Thing(this);
159        }
160    }
161}