cb731b9e6ec8b97efac1bebeb7231bdff63a2f55
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ClientControllerPlugin / src / oic / simulator / clientcontroller / utils / Utility.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package oic.simulator.clientcontroller.utils;
18
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;
24 import java.util.Map;
25 import java.util.Set;
26
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;
31
32 /**
33  * This class has common utility methods.
34  */
35 public class Utility {
36     public static List<String> convertSetToList(Set<String> typeSet) {
37         if (null == typeSet) {
38             return null;
39         }
40         List<String> list = new ArrayList<String>();
41         Iterator<String> typeItr = typeSet.iterator();
42         while (typeItr.hasNext()) {
43             list.add(typeItr.next());
44         }
45         return list;
46     }
47
48     public static String getObservableInString(boolean observable) {
49         if (observable) {
50             return Constants.YES;
51         } else {
52             return Constants.NO;
53         }
54     }
55
56     public static String[] convertListToString(List<String> valueList) {
57         String[] strArr;
58         if (null != valueList && valueList.size() > 0) {
59             strArr = valueList.toArray(new String[1]);
60         } else {
61             strArr = new String[1];
62         }
63         return strArr;
64     }
65
66     public static Set<String> splitStringByComma(String text) {
67         Set<String> tokenSet = null;
68         if (null != text) {
69             String[] token = text.split(",");
70             if (null != token) {
71                 tokenSet = new HashSet<String>();
72                 for (String tok : token) {
73                     tok = tok.trim();
74                     if (tok.length() > 0) {
75                         tokenSet.add(tok);
76                     }
77                 }
78             }
79         }
80         return tokenSet;
81     }
82
83     public static String getSimulatorErrorString(Exception e, String info) {
84         if (null == e) {
85             return null;
86         }
87         String detail;
88         if (e instanceof SimulatorException) {
89             SimulatorException simEx = (SimulatorException) e;
90             detail = simEx.message() + "\n";
91             detail += "Exception Type: " + simEx.getClass().getSimpleName()
92                     + "\n";
93             detail += "Error code: " + simEx.code().toString();
94         } else {
95             detail = info + "\n";
96             detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
97             detail += "Message: " + e.getMessage();
98         }
99         return detail;
100     }
101
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) {
105         if (null == val) {
106             return null;
107         }
108         Object value = val.get();
109         if (null == value) {
110             return null;
111         }
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)) {
116             return null;
117         }
118         if (type.mType == ValueType.ARRAY) {
119             if (type.mBaseType == ValueType.INTEGER) {
120                 Integer[] values = (Integer[]) value;
121                 if (null == values || values.length < 1) {
122                     return null;
123                 }
124                 List<Integer> list = new ArrayList<Integer>();
125                 for (Integer i : values) {
126                     list.add(i);
127                 }
128                 return list.toString();
129             } else if (type.mBaseType == ValueType.DOUBLE) {
130                 Double[] values = (Double[]) value;
131                 if (null == values || values.length < 1) {
132                     return null;
133                 }
134                 List<Double> list = new ArrayList<Double>();
135                 for (Double i : values) {
136                     list.add(i);
137                 }
138                 return list.toString();
139             } else if (type.mBaseType == ValueType.BOOLEAN) {
140                 Boolean[] values = (Boolean[]) value;
141                 if (null == values || values.length < 1) {
142                     return null;
143                 }
144                 List<Boolean> list = new ArrayList<Boolean>();
145                 for (Boolean i : values) {
146                     list.add(i);
147                 }
148                 return list.toString();
149             } else if (type.mBaseType == ValueType.STRING) {
150                 String[] values = (String[]) value;
151                 if (null == values || values.length < 1) {
152                     return null;
153                 }
154                 List<String> list = new ArrayList<String>();
155                 for (String i : values) {
156                     list.add(i);
157                 }
158                 return list.toString();
159             } else {
160                 return null;
161             }
162         } else {
163             return String.valueOf(value);
164         }
165     }
166
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");
178         return ifTypes;
179     }
180 }