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.element.NodeInfo; 027 028public final class Thing { 029 030 private final HashMap<String, Tag> metaTags; 031 private final boolean selfOwned; 032 private final NodeInfo nodeInfo; 033 034 private final ThingMomentaryReadOutRequest momentaryReadOutRequestHandler; 035 private final ThingControlRequest controlRequestHandler; 036 037 private Thing(Builder builder) { 038 this.metaTags = builder.metaTags; 039 this.selfOwned = builder.selfOwned; 040 041 this.nodeInfo = builder.nodeInfo; 042 043 this.momentaryReadOutRequestHandler = builder.momentaryReadOutRequest; 044 this.controlRequestHandler = builder.controlRequest; 045 } 046 047 public Collection<Tag> getMetaTags() { 048 return metaTags.values(); 049 } 050 051 public boolean isSelfOwened() { 052 return selfOwned; 053 } 054 055 public NodeInfo getNodeInfo() { 056 return nodeInfo; 057 } 058 059 public String getNodeId() { 060 return nodeInfo.getNodeId(); 061 } 062 063 public String getSourceId() { 064 return nodeInfo.getSourceId(); 065 } 066 067 public String getCacheType() { 068 return nodeInfo.getCacheType(); 069 } 070 071 public ThingMomentaryReadOutRequest getMomentaryReadOutRequestHandler() { 072 return momentaryReadOutRequestHandler; 073 } 074 075 public ThingControlRequest getControlRequestHandler() { 076 return controlRequestHandler; 077 } 078 079 private String toStringCache; 080 081 @SuppressWarnings("ObjectToString") 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, Tag.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, Tag.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, Tag.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, Tag.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, Tag.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}