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.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
25 import org.oic.simulator.AttributeValue;
26 import org.oic.simulator.SimulatorException;
27 import org.oic.simulator.AttributeValue.TypeInfo;
28 import org.oic.simulator.AttributeValue.ValueType;
31 * This class has common utility methods.
33 public class Utility {
34 public static List<String> convertSetToList(Set<String> typeSet) {
35 if (null == typeSet) {
38 List<String> list = new ArrayList<String>();
39 Iterator<String> typeItr = typeSet.iterator();
40 while (typeItr.hasNext()) {
41 list.add(typeItr.next());
46 public static String getObservableInString(boolean observable) {
54 public static String[] convertListToString(List<String> valueList) {
56 if (null != valueList && valueList.size() > 0) {
57 strArr = valueList.toArray(new String[1]);
59 strArr = new String[1];
64 public static Set<String> splitStringByComma(String text) {
65 Set<String> tokenSet = null;
67 String[] token = text.split(",");
69 tokenSet = new HashSet<String>();
70 for (String tok : token) {
72 if (tok.length() > 0) {
81 public static String getSimulatorErrorString(Exception e, String info) {
86 if (e instanceof SimulatorException) {
87 SimulatorException simEx = (SimulatorException) e;
88 detail = simEx.message() + "\n";
89 detail += "Exception Type: " + simEx.getClass().getSimpleName()
91 detail += "Error code: " + simEx.code().toString();
94 detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
95 detail += "Message: " + e.getMessage();
100 // This method only works for attributes whose values are of type int,
101 // double, bool, string and 1-D array of primitive types
102 public static String getAttributeValueAsString(AttributeValue val) {
106 Object value = val.get();
110 TypeInfo type = val.typeInfo();
111 if (type.mType == ValueType.RESOURCEMODEL
112 || (type.mType == ValueType.ARRAY && type.mBaseType == ValueType.RESOURCEMODEL)
113 || (type.mType == ValueType.ARRAY && type.mDepth > 1)) {
116 if (type.mType == ValueType.ARRAY) {
117 if (type.mBaseType == ValueType.INTEGER) {
118 Integer[] values = (Integer[]) value;
119 if (null == values || values.length < 1) {
122 List<Integer> list = new ArrayList<Integer>();
123 for (Integer i : values) {
126 return list.toString();
127 } else if (type.mBaseType == ValueType.DOUBLE) {
128 Double[] values = (Double[]) value;
129 if (null == values || values.length < 1) {
132 List<Double> list = new ArrayList<Double>();
133 for (Double i : values) {
136 return list.toString();
137 } else if (type.mBaseType == ValueType.BOOLEAN) {
138 Boolean[] values = (Boolean[]) value;
139 if (null == values || values.length < 1) {
142 List<Boolean> list = new ArrayList<Boolean>();
143 for (Boolean i : values) {
146 return list.toString();
147 } else if (type.mBaseType == ValueType.STRING) {
148 String[] values = (String[]) value;
149 if (null == values || values.length < 1) {
152 List<String> list = new ArrayList<String>();
153 for (String i : values) {
156 return list.toString();
161 return String.valueOf(value);