e7f4943b48cebb1873e25ec87017b86712efe178
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
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 org.onap.vfc.nfvo.resmanagement.common.util;
18
19 import java.math.BigDecimal;
20 import java.text.DecimalFormat;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  *
28  * String Utility Class.<br>
29  * <p>
30  * </p>
31  *
32  * @author
33  * @version     NFVO 0.5  Sep 10, 2016
34  */
35 public final class StringUtil {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(StringUtil.class);
38
39     private StringUtil() {
40     }
41
42     /**
43      *
44      * Check whether thestring is valid.<br>
45      *
46      * @param str
47      * @return
48      * @since  NFVO 0.5
49      */
50     public static boolean isValidString(String str) {
51         if(null == str || str.isEmpty()) {
52             return false;
53         }
54         return true;
55     }
56
57     /**
58      *
59      * Check whether the value is larger than zero.<br>
60      *
61      * @param strs
62      * @return
63      * @since  NFVO 0.5
64      */
65     public static boolean isAnyLargeThanZero(String... strs) {
66         for(String str : strs) {
67             if(!StringUtils.isEmpty(str) && Float.parseFloat(str) > 0) {
68                 LOGGER.info("isAnyLargeThanZero : {} is > 0", str);
69                 return true;
70             }
71         }
72         return false;
73     }
74
75     /**
76      *
77      * Check whether the value is Integer.<br>
78      *
79      * @param strs
80      * @return
81      * @since  NFVO 0.5
82      */
83     public static boolean isInteger(String... strs) {
84         try {
85             for(String str : strs) {
86                 if(!StringUtils.isEmpty(str)) {
87                     int value = Integer.parseInt(str);
88                     if(value < 0) {
89                         return false;
90                     }
91                 }
92             }
93         } catch(NumberFormatException e) {
94             return false;
95         }
96         return true;
97     }
98
99     /**
100      *
101      * Check whether the input is Numeric.<br>
102      *
103      * @param strs
104      * @return
105      * @since  NFVO 0.5
106      */
107     public static boolean isNumeric(String... strs) {
108         try {
109             for(String str : strs) {
110                 if(!StringUtils.isEmpty(str)) {
111                     float value = Float.parseFloat(str);
112                     if(value < 0) {
113                         return false;
114                     }
115                 }
116             }
117         } catch(NumberFormatException e) {
118             return false;
119         }
120         return true;
121     }
122
123     /**
124      *
125      * Compare zero by float.<br>
126      *
127      * @param tatol
128      * @param used
129      * @param drTotal
130      * @return
131      * @since  NFVO 0.5
132      */
133     public static boolean compareZeroByFloat(String tatol, String used, String drTotal) {
134         Float ftotal = (float)0;
135         Float fused = (float)0;
136         Float fdrTotal = (float)0;
137         if(!StringUtils.isEmpty(tatol)) {
138             ftotal = new Float(tatol);
139         }
140         if(!StringUtils.isEmpty(used)) {
141             fused = new Float(used);
142         }
143         if(!StringUtils.isEmpty(drTotal)) {
144             fdrTotal = new Float(drTotal);
145         }
146         if(ftotal < fused + fdrTotal) {
147             return false;
148         }
149
150         return true;
151     }
152
153     /**
154      *
155      * Compare zero by integer.<br>
156      *
157      * @param tatol
158      * @param used
159      * @param drTotal
160      * @return
161      * @since  NFVO 0.5
162      */
163     public static boolean compareZeroByInteger(String tatol, String used, String drTotal) {
164         Integer ftotal = (int)0;
165         Integer fused = (int)0;
166         Integer fdrTotal = (int)0;
167         if(!StringUtils.isEmpty(tatol)) {
168             ftotal = Integer.valueOf(tatol);
169         }
170         if(!StringUtils.isEmpty(used)) {
171             fused = Integer.valueOf(used);
172         }
173         if(!StringUtils.isEmpty(drTotal)) {
174             fdrTotal = Integer.valueOf(drTotal);
175         }
176         if(ftotal < fused + fdrTotal) {
177             return false;
178         }
179         return true;
180     }
181
182     /**
183      *
184      * Number format.<br>
185      *
186      * @param data
187      * @return
188      * @since  NFVO 0.5
189      */
190     public static String numFormat(String data) {
191         if(null != data && !("".equals(data))) {
192             BigDecimal var = new BigDecimal(data);
193             DecimalFormat formatWithoutFraction = new DecimalFormat("############");
194             DecimalFormat formatWithFraction = new DecimalFormat("############.############");
195             if(new BigDecimal(var.intValue()).compareTo(var) == 0) {
196                 return formatWithoutFraction.format(var);
197             }
198             return formatWithFraction.format(var);
199         }
200         return null;
201     }
202
203     /**
204      *
205      * <br>
206      *
207      * @param inputStr
208      * @return
209      * @since  NFVO 0.5
210      */
211     public static boolean checkXss(String inputStr) {
212         return inputStr.matches("[A-Za-z0-9_.']+");
213     }
214
215 }