254f1f876c44070446e1f47fc032fb23a44a5b09
[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.SimulatorException;
26
27 /**
28  * This class has common utility methods.
29  */
30 public class Utility {
31     public static List<String> convertSetToList(Set<String> typeSet) {
32         if (null == typeSet) {
33             return null;
34         }
35         List<String> list = new ArrayList<String>();
36         Iterator<String> typeItr = typeSet.iterator();
37         while (typeItr.hasNext()) {
38             list.add(typeItr.next());
39         }
40         return list;
41     }
42
43     public static String getObservableInString(boolean observable) {
44         if (observable) {
45             return Constants.YES;
46         } else {
47             return Constants.NO;
48         }
49     }
50
51     public static String[] convertListToString(List<String> valueList) {
52         String[] strArr;
53         if (null != valueList && valueList.size() > 0) {
54             strArr = valueList.toArray(new String[1]);
55         } else {
56             strArr = new String[1];
57         }
58         return strArr;
59     }
60
61     public static Set<String> splitStringByComma(String text) {
62         Set<String> tokenSet = null;
63         if (null != text) {
64             String[] token = text.split(",");
65             if (null != token) {
66                 tokenSet = new HashSet<String>();
67                 for (String tok : token) {
68                     tok = tok.trim();
69                     if (tok.length() > 0) {
70                         tokenSet.add(tok);
71                     }
72                 }
73             }
74         }
75         return tokenSet;
76     }
77
78     public static String getSimulatorErrorString(Exception e, String info) {
79         if (null == e) {
80             return null;
81         }
82         String detail;
83         if (e instanceof SimulatorException) {
84             SimulatorException simEx = (SimulatorException) e;
85             detail = simEx.message() + "\n";
86             detail += "Exception Type: " + simEx.getClass().getSimpleName()
87                     + "\n";
88             detail += "Error code: " + simEx.code().toString();
89         } else {
90             detail = info + "\n";
91             detail += "Exception Type: " + e.getClass().getSimpleName() + "\n";
92             detail += "Message: " + e.getMessage();
93         }
94         return detail;
95     }
96 }