Imported Upstream version 1.1.0
[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.Comparator;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
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;
34
35 import oic.simulator.clientcontroller.remoteresource.AttributeElement;
36
37 /**
38  * This class has common utility methods.
39  */
40 public class Utility {
41     public static List<String> convertSetToList(Set<String> typeSet) {
42         if (null == typeSet) {
43             return null;
44         }
45         List<String> list = new ArrayList<String>();
46         Iterator<String> typeItr = typeSet.iterator();
47         while (typeItr.hasNext()) {
48             list.add(typeItr.next());
49         }
50         return list;
51     }
52
53     public static String getObservableInString(boolean observable) {
54         if (observable) {
55             return Constants.YES;
56         } else {
57             return Constants.NO;
58         }
59     }
60
61     public static String[] convertListToString(List<String> valueList) {
62         String[] strArr;
63         if (null != valueList && valueList.size() > 0) {
64             strArr = valueList.toArray(new String[1]);
65         } else {
66             strArr = new String[1];
67         }
68         return strArr;
69     }
70
71     public static Set<String> splitStringByComma(String text) {
72         Set<String> tokenSet = null;
73         if (null != text) {
74             String[] token = text.split(",");
75             if (null != token) {
76                 tokenSet = new HashSet<String>();
77                 for (String tok : token) {
78                     tok = tok.trim();
79                     if (tok.length() > 0) {
80                         tokenSet.add(tok);
81                     }
82                 }
83             }
84         }
85         return tokenSet;
86     }
87
88     public static String getSimulatorErrorString(Exception e, String info) {
89         if (null == e) {
90             return null;
91         }
92         String detail;
93         if (e instanceof SimulatorException) {
94             SimulatorException simEx = (SimulatorException) e;
95             detail = simEx.message() + "\n";
96             detail += "Exception Type: " + simEx.getClass().getSimpleName()
97                     + "\n";
98             detail += "Error code: " + simEx.code().toString();
99         } else {
100             detail = info + "\n";
101             detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
102             detail += "Message: " + e.getMessage();
103         }
104         return detail;
105     }
106
107     public static Comparator<AttributeElement> attributeComparator = new Comparator<AttributeElement>() {
108                                                                        public int compare(
109                                                                                AttributeElement att1,
110                                                                                AttributeElement att2) {
111                                                                            String s1 = att1
112                                                                                    .getSimulatorResourceAttribute()
113                                                                                    .name();
114                                                                            String s2 = att2
115                                                                                    .getSimulatorResourceAttribute()
116                                                                                    .name();
117
118                                                                            String s1Part = s1
119                                                                                    .replaceAll(
120                                                                                            "\\d",
121                                                                                            "");
122                                                                            String s2Part = s2
123                                                                                    .replaceAll(
124                                                                                            "\\d",
125                                                                                            "");
126
127                                                                            if (s1Part
128                                                                                    .equalsIgnoreCase(s2Part)) {
129                                                                                return extractInt(s1)
130                                                                                        - extractInt(s2);
131                                                                            }
132                                                                            return s1
133                                                                                    .compareTo(s2);
134                                                                        }
135
136                                                                        int extractInt(
137                                                                                String s) {
138                                                                            String num = s
139                                                                                    .replaceAll(
140                                                                                            "\\D",
141                                                                                            "");
142
143                                                                            // Return
144                                                                            // 0
145                                                                            // if
146                                                                            // no
147                                                                            // digits
148                                                                            // found
149                                                                            return num
150                                                                                    .isEmpty() ? 0
151                                                                                    : Integer
152                                                                                            .parseInt(num);
153                                                                        }
154                                                                    };
155
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) {
159         if (null == val) {
160             return null;
161         }
162
163         Object value = val.get();
164         if (null == value) {
165             return null;
166         }
167
168         TypeInfo type = val.typeInfo();
169         if (type.mBaseType == ValueType.RESOURCEMODEL
170                 || (type.mType == ValueType.ARRAY && type.mDepth > 1)) {
171             return null;
172         }
173
174         AttributeValueStringConverter converter = new AttributeValueStringConverter(
175                 val);
176         return converter.toString();
177     }
178
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");
189         return ifTypes;
190     }
191
192     public static String removeWhiteSpacesInArrayValues(String value) {
193         if (null == value || value.isEmpty())
194             return null;
195
196         value = value.trim();
197
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) {
203                 result.append(",");
204             }
205         }
206
207         return result.toString();
208     }
209
210     public static AttributeValue cloneAttributeValue(AttributeValue value)
211             throws InvalidArgsException, NullPointerException {
212         AttributeValue clone = null;
213
214         AttributeValue.TypeInfo typeInfo = value.typeInfo();
215
216         if (typeInfo.mType == AttributeValue.ValueType.RESOURCEMODEL) {
217             SimulatorResourceModel resModel = (SimulatorResourceModel) value
218                     .get();
219             SimulatorResourceModel modelCopy = new SimulatorResourceModel();
220
221             for (Map.Entry<String, AttributeValue> entry : resModel.get()
222                     .entrySet()) {
223                 String attName = entry.getKey();
224                 AttributeValue attValue = entry.getValue();
225                 modelCopy.set(attName, cloneAttributeValue(attValue));
226             }
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
232                         .get();
233                 SimulatorResourceModel[] modelArrayCopy = new SimulatorResourceModel[resModelArray.length];
234                 for (int i = 0; i < resModelArray.length; i++) {
235                     AttributeValue attValue = cloneAttributeValue(new AttributeValue(
236                             resModelArray[i]));
237                     if (null != attValue) {
238                         modelArrayCopy[i] = (SimulatorResourceModel) attValue
239                                 .get();
240                     }
241                 }
242                 clone = new AttributeValue(modelArrayCopy);
243             } else if (typeInfo.mDepth == 2) {
244                 SimulatorResourceModel[][] resModelArray = (SimulatorResourceModel[][]) value
245                         .get();
246                 SimulatorResourceModel[][] modelArrayCopy = new SimulatorResourceModel[resModelArray.length][];
247                 for (int i = 0; i < resModelArray.length; i++) {
248                     AttributeValue attValue = cloneAttributeValue(new AttributeValue(
249                             resModelArray[i]));
250                     if (null != attValue) {
251                         modelArrayCopy[i] = (SimulatorResourceModel[]) attValue
252                                 .get();
253                     }
254                 }
255                 clone = new AttributeValue(modelArrayCopy);
256             } else if (typeInfo.mDepth == 3) {
257                 SimulatorResourceModel[][][] resModelArray = (SimulatorResourceModel[][][]) value
258                         .get();
259                 SimulatorResourceModel[][][] modelArrayCopy = new SimulatorResourceModel[resModelArray.length][][];
260                 for (int i = 0; i < resModelArray.length; i++) {
261                     AttributeValue attValue = cloneAttributeValue(new AttributeValue(
262                             resModelArray[i]));
263                     if (null != attValue) {
264                         modelArrayCopy[i] = (SimulatorResourceModel[][]) attValue
265                                 .get();
266                     }
267                 }
268                 clone = new AttributeValue(modelArrayCopy);
269             }
270         } else {
271             String attValueInString = new AttributeValueStringConverter(value)
272                     .toString();
273             clone = AttributeValueBuilder.build(attValueInString,
274                     typeInfo.mBaseType);
275         }
276
277         return clone;
278     }
279 }