Displaying and editing the complex value types for attributes.
[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.HashSet;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Set;
24
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;
29
30 /**
31  * This class has common utility methods.
32  */
33 public class Utility {
34     public static List<String> convertSetToList(Set<String> typeSet) {
35         if (null == typeSet) {
36             return null;
37         }
38         List<String> list = new ArrayList<String>();
39         Iterator<String> typeItr = typeSet.iterator();
40         while (typeItr.hasNext()) {
41             list.add(typeItr.next());
42         }
43         return list;
44     }
45
46     public static String getObservableInString(boolean observable) {
47         if (observable) {
48             return Constants.YES;
49         } else {
50             return Constants.NO;
51         }
52     }
53
54     public static String[] convertListToString(List<String> valueList) {
55         String[] strArr;
56         if (null != valueList && valueList.size() > 0) {
57             strArr = valueList.toArray(new String[1]);
58         } else {
59             strArr = new String[1];
60         }
61         return strArr;
62     }
63
64     public static Set<String> splitStringByComma(String text) {
65         Set<String> tokenSet = null;
66         if (null != text) {
67             String[] token = text.split(",");
68             if (null != token) {
69                 tokenSet = new HashSet<String>();
70                 for (String tok : token) {
71                     tok = tok.trim();
72                     if (tok.length() > 0) {
73                         tokenSet.add(tok);
74                     }
75                 }
76             }
77         }
78         return tokenSet;
79     }
80
81     public static String getSimulatorErrorString(Exception e, String info) {
82         if (null == e) {
83             return null;
84         }
85         String detail;
86         if (e instanceof SimulatorException) {
87             SimulatorException simEx = (SimulatorException) e;
88             detail = simEx.message() + "\n";
89             detail += "Exception Type: " + simEx.getClass().getSimpleName()
90                     + "\n";
91             detail += "Error code: " + simEx.code().toString();
92         } else {
93             detail = info + "\n";
94             detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
95             detail += "Message: " + e.getMessage();
96         }
97         return detail;
98     }
99
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) {
103         if (null == val) {
104             return null;
105         }
106         Object value = val.get();
107         if (null == value) {
108             return null;
109         }
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)) {
114             return null;
115         }
116         if (type.mType == ValueType.ARRAY) {
117             if (type.mBaseType == ValueType.INTEGER) {
118                 Integer[] values = (Integer[]) value;
119                 if (null == values || values.length < 1) {
120                     return null;
121                 }
122                 List<Integer> list = new ArrayList<Integer>();
123                 for (Integer i : values) {
124                     list.add(i);
125                 }
126                 return list.toString();
127             } else if (type.mBaseType == ValueType.DOUBLE) {
128                 Double[] values = (Double[]) value;
129                 if (null == values || values.length < 1) {
130                     return null;
131                 }
132                 List<Double> list = new ArrayList<Double>();
133                 for (Double i : values) {
134                     list.add(i);
135                 }
136                 return list.toString();
137             } else if (type.mBaseType == ValueType.BOOLEAN) {
138                 Boolean[] values = (Boolean[]) value;
139                 if (null == values || values.length < 1) {
140                     return null;
141                 }
142                 List<Boolean> list = new ArrayList<Boolean>();
143                 for (Boolean i : values) {
144                     list.add(i);
145                 }
146                 return list.toString();
147             } else if (type.mBaseType == ValueType.STRING) {
148                 String[] values = (String[]) value;
149                 if (null == values || values.length < 1) {
150                     return null;
151                 }
152                 List<String> list = new ArrayList<String>();
153                 for (String i : values) {
154                     list.add(i);
155                 }
156                 return list.toString();
157             } else {
158                 return null;
159             }
160         } else {
161             return String.valueOf(value);
162         }
163     }
164 }