Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / java / eclipse-plugin / ServiceProviderPlugin / src / oic / simulator / serviceprovider / 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.serviceprovider.utils;
18
19 import java.util.ArrayList;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Set;
23
24 /**
25  * This class has common utility methods.
26  */
27 public class Utility {
28
29     public static String uriToDisplayName(String uri) {
30         String result = null;
31         if (null != uri) {
32             String tokens[] = uri.split(Constants.FORWARD_SLASH);
33             if (null != tokens && tokens.length > 2) {
34                 result = tokens[tokens.length - 3] + Constants.UNDERSCORE
35                         + tokens[tokens.length - 1];
36             }
37         }
38         return result;
39     }
40
41     public static String fileNameToDisplay(String fileName) {
42         if (null == fileName || fileName.length() < 1) {
43             return null;
44         }
45         // Remove the RAML file standard prefix
46         int len = Constants.RAML_FILE_PREFIX.length();
47         if (len > 0) {
48             if (fileName.startsWith(Constants.RAML_FILE_PREFIX)) {
49                 fileName = fileName.substring(len);
50             }
51         }
52
53         // Removing the file extension
54         int index = fileName.lastIndexOf('.');
55         fileName = fileName.substring(0, index);
56         return fileName;
57     }
58
59     public static String displayToFileName(String displayName) {
60         if (null == displayName || displayName.length() < 1) {
61             return null;
62         }
63         String fileName;
64         // Adding the prefix
65         fileName = Constants.RAML_FILE_PREFIX + displayName;
66
67         // Adding the file extension
68         fileName = fileName + Constants.RAML_FILE_EXTENSION;
69
70         return fileName;
71     }
72
73     public static String getAutomationStatus(boolean status) {
74         if (status) {
75             return Constants.ENABLED;
76         } else {
77             return Constants.DISABLED;
78         }
79     }
80
81     public static String getAutomationString(boolean status) {
82         if (status) {
83             return Constants.ENABLE;
84         } else {
85             return Constants.DISABLE;
86         }
87     }
88
89     public static boolean getAutomationBoolean(String status) {
90         if (null != status) {
91             if (status.equals(Constants.ENABLE)) {
92                 return true;
93             }
94         }
95         return false;
96     }
97
98     public static int getUpdateIntervalFromString(String value) {
99         int result = Constants.DEFAULT_AUTOMATION_INTERVAL;
100         if (null != value) {
101             try {
102                 result = Integer.parseInt(value);
103             } catch (NumberFormatException nfe) {
104                 System.out
105                         .println("Getting UpdateInterval from string failed!");
106             }
107         }
108         return result;
109     }
110
111     public static List<String> convertSetToList(Set<String> typeSet) {
112         if (null == typeSet) {
113             return null;
114         }
115         List<String> list = new ArrayList<String>();
116         Iterator<String> typeItr = typeSet.iterator();
117         while (typeItr.hasNext()) {
118             list.add(typeItr.next());
119         }
120         return list;
121     }
122 }