2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package oic.simulator.clientcontroller.utils;
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
28 import org.oic.simulator.AttributeValue;
29 import org.oic.simulator.AttributeValue.TypeInfo;
30 import org.oic.simulator.AttributeValue.ValueType;
31 import org.oic.simulator.InvalidArgsException;
32 import org.oic.simulator.SimulatorException;
33 import org.oic.simulator.SimulatorResourceModel;
35 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
38 * This class has common utility methods.
40 public class Utility {
41 public static List<String> convertSetToList(Set<String> typeSet) {
42 if (null == typeSet) {
45 List<String> list = new ArrayList<String>();
46 Iterator<String> typeItr = typeSet.iterator();
47 while (typeItr.hasNext()) {
48 list.add(typeItr.next());
53 public static String getObservableInString(boolean observable) {
61 public static String[] convertListToString(List<String> valueList) {
63 if (null != valueList && valueList.size() > 0) {
64 strArr = valueList.toArray(new String[1]);
66 strArr = new String[1];
71 public static Set<String> splitStringByComma(String text) {
72 Set<String> tokenSet = null;
74 String[] token = text.split(",");
76 tokenSet = new HashSet<String>();
77 for (String tok : token) {
79 if (tok.length() > 0) {
88 public static String getSimulatorErrorString(Exception e, String info) {
93 if (e instanceof SimulatorException) {
94 SimulatorException simEx = (SimulatorException) e;
95 detail = simEx.message() + "\n";
96 detail += "Exception Type: " + simEx.getClass().getSimpleName()
98 detail += "Error code: " + simEx.code().toString();
100 detail = info + "\n";
101 detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
102 detail += "Message: " + e.getMessage();
107 public static Comparator<AttributeElement> attributeComparator = new Comparator<AttributeElement>() {
109 AttributeElement att1,
110 AttributeElement att2) {
112 .getSimulatorResourceAttribute()
115 .getSimulatorResourceAttribute()
128 .equalsIgnoreCase(s2Part)) {
129 return extractInt(s1)
156 // This method only works for attributes whose values are of type int,
157 // double, bool, string and 1-D array of primitive types
158 public static String getAttributeValueAsString(AttributeValue val) {
163 Object value = val.get();
168 TypeInfo type = val.typeInfo();
169 if (type.mBaseType == ValueType.RESOURCEMODEL
170 || (type.mType == ValueType.ARRAY && type.mDepth > 1)) {
174 AttributeValueStringConverter converter = new AttributeValueStringConverter(
176 return converter.toString();
179 public static Map<String, String> getResourceInterfaces() {
180 Map<String, String> ifTypes = new HashMap<String, String>();
181 ifTypes.put(Constants.BASELINE_INTERFACE, "Baseline");
182 ifTypes.put(Constants.LINKS_LIST_INTERFACE, "Links List");
183 ifTypes.put(Constants.BATCH_INTERFACE, "Batch");
184 ifTypes.put(Constants.LINK_BATCH_INTERFACE, "Link Batch");
185 ifTypes.put(Constants.READ_ONLY_INTERFACE, "Read-Only");
186 ifTypes.put(Constants.READ_WRITE_INTERFACE, "Read-Write");
187 ifTypes.put(Constants.ACTUATOR_INTERFACE, "Actuator");
188 ifTypes.put(Constants.SENSOR_INTERFACE, "Sensor");
192 public static String removeWhiteSpacesInArrayValues(String value) {
193 if (null == value || value.isEmpty())
196 value = value.trim();
198 String token[] = value.split(",");
199 StringBuilder result = new StringBuilder();
200 for (int i = 0; i < token.length; i++) {
201 result.append(token[i].trim());
202 if (i + 1 < token.length) {
207 return result.toString();
210 public static AttributeValue cloneAttributeValue(AttributeValue value)
211 throws InvalidArgsException, NullPointerException {
212 AttributeValue clone = null;
214 AttributeValue.TypeInfo typeInfo = value.typeInfo();
216 if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
217 SimulatorResourceModel resModel = (SimulatorResourceModel) value
219 SimulatorResourceModel modelCopy = new SimulatorResourceModel();
221 for (Map.Entry<String, AttributeValue> entry : resModel.get()
223 String attName = entry.getKey();
224 AttributeValue attValue = entry.getValue();
225 modelCopy.set(attName, cloneAttributeValue(attValue));
227 clone = new AttributeValue(modelCopy);
228 } else if (typeInfo.mType == AttributeValue.ValueType.ARRAY
229 && typeInfo.mBaseType == AttributeValue.ValueType.RESOURCEMODEL) {
230 if (typeInfo.mDepth == 1) {
231 SimulatorResourceModel[] resModelArray = (SimulatorResourceModel[]) value
233 SimulatorResourceModel[] modelArrayCopy = new SimulatorResourceModel[resModelArray.length];
234 for (int i = 0; i < resModelArray.length; i++) {
235 AttributeValue attValue = cloneAttributeValue(new AttributeValue(
237 if (null != attValue) {
238 modelArrayCopy[i] = (SimulatorResourceModel) attValue
242 clone = new AttributeValue(modelArrayCopy);
243 } else if (typeInfo.mDepth == 2) {
244 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) value
246 SimulatorResourceModel[][] modelArrayCopy = new SimulatorResourceModel[resModelArray.length][];
247 for (int i = 0; i < resModelArray.length; i++) {
248 AttributeValue attValue = cloneAttributeValue(new AttributeValue(
250 if (null != attValue) {
251 modelArrayCopy[i] = (SimulatorResourceModel[]) attValue
255 clone = new AttributeValue(modelArrayCopy);
256 } else if (typeInfo.mDepth == 3) {
257 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) value
259 SimulatorResourceModel[][][] modelArrayCopy = new SimulatorResourceModel[resModelArray.length][][];
260 for (int i = 0; i < resModelArray.length; i++) {
261 AttributeValue attValue = cloneAttributeValue(new AttributeValue(
263 if (null != attValue) {
264 modelArrayCopy[i] = (SimulatorResourceModel[][]) attValue
268 clone = new AttributeValue(modelArrayCopy);
271 String attValueInString = new AttributeValueStringConverter(value)
273 clone = AttributeValueBuilder.build(attValueInString,