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.HashMap;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
27 import org.oic.simulator.AttributeValue;
28 import org.oic.simulator.SimulatorException;
29 import org.oic.simulator.AttributeValue.TypeInfo;
30 import org.oic.simulator.AttributeValue.ValueType;
33 * This class has common utility methods.
35 public class Utility {
36 public static List<String> convertSetToList(Set<String> typeSet) {
37 if (null == typeSet) {
40 List<String> list = new ArrayList<String>();
41 Iterator<String> typeItr = typeSet.iterator();
42 while (typeItr.hasNext()) {
43 list.add(typeItr.next());
48 public static String getObservableInString(boolean observable) {
56 public static String[] convertListToString(List<String> valueList) {
58 if (null != valueList && valueList.size() > 0) {
59 strArr = valueList.toArray(new String[1]);
61 strArr = new String[1];
66 public static Set<String> splitStringByComma(String text) {
67 Set<String> tokenSet = null;
69 String[] token = text.split(",");
71 tokenSet = new HashSet<String>();
72 for (String tok : token) {
74 if (tok.length() > 0) {
83 public static String getSimulatorErrorString(Exception e, String info) {
88 if (e instanceof SimulatorException) {
89 SimulatorException simEx = (SimulatorException) e;
90 detail = simEx.message() + "\n";
91 detail += "Exception Type: " + simEx.getClass().getSimpleName()
93 detail += "Error code: " + simEx.code().toString();
96 detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
97 detail += "Message: " + e.getMessage();
102 // This method only works for attributes whose values are of type int,
103 // double, bool, string and 1-D array of primitive types
104 public static String getAttributeValueAsString(AttributeValue val) {
108 Object value = val.get();
112 TypeInfo type = val.typeInfo();
113 if (type.mType == ValueType.RESOURCEMODEL
114 || (type.mType == ValueType.ARRAY && type.mBaseType == ValueType.RESOURCEMODEL)
115 || (type.mType == ValueType.ARRAY && type.mDepth > 1)) {
118 if (type.mType == ValueType.ARRAY) {
119 if (type.mBaseType == ValueType.INTEGER) {
120 Integer[] values = (Integer[]) value;
121 if (null == values || values.length < 1) {
124 List<Integer> list = new ArrayList<Integer>();
125 for (Integer i : values) {
128 return list.toString();
129 } else if (type.mBaseType == ValueType.DOUBLE) {
130 Double[] values = (Double[]) value;
131 if (null == values || values.length < 1) {
134 List<Double> list = new ArrayList<Double>();
135 for (Double i : values) {
138 return list.toString();
139 } else if (type.mBaseType == ValueType.BOOLEAN) {
140 Boolean[] values = (Boolean[]) value;
141 if (null == values || values.length < 1) {
144 List<Boolean> list = new ArrayList<Boolean>();
145 for (Boolean i : values) {
148 return list.toString();
149 } else if (type.mBaseType == ValueType.STRING) {
150 String[] values = (String[]) value;
151 if (null == values || values.length < 1) {
154 List<String> list = new ArrayList<String>();
155 for (String i : values) {
158 return list.toString();
163 return String.valueOf(value);
167 public static Map<String, String> getResourceInterfaces() {
168 Map<String, String> ifTypes = null;
169 ifTypes = new HashMap<String, String>();
170 ifTypes.put(Constants.BASELINE_INTERFACE, "Baseline");
171 ifTypes.put(Constants.LINKS_LIST_INTERFACE, "Links List");
172 ifTypes.put(Constants.BATCH_INTERFACE, "Batch");
173 ifTypes.put(Constants.LINK_BATCH_INTERFACE, "Link Batch");
174 ifTypes.put(Constants.READ_ONLY_INTERFACE, "Read-Only");
175 ifTypes.put(Constants.READ_WRITE_INTERFACE, "Read-Write");
176 ifTypes.put(Constants.ACTUATOR_INTERFACE, "Actuator");
177 ifTypes.put(Constants.SENSOR_INTERFACE, "Sensor");