8d4b62ce97c458c11026b3e3314a515ec6c32f35
[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.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25
26 /**
27  *
28  * String Utility Class.<br>
29  * <p>
30  * </p>
31  *
32  * @author
33  * @version     VFC 1.0  Sep 10, 2016
34  */
35 public final class StringUtil {
36
37     private static final Logger LOGGER = LogManager.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  VFC 1.0
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  VFC 1.0
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  VFC 1.0
82      */
83     public static boolean isInteger(String... strs) {
84         try {
85             for(String str : strs) {
86                 if(!StringUtils.isEmpty(str) && Integer.parseInt(str) < 0) {
87                     return false;
88                 }
89             }
90         } catch(NumberFormatException e) {
91             return false;
92         }
93         return true;
94     }
95
96     /**
97      *
98      * Check whether the input is Numeric.<br>
99      *
100      * @param strs
101      * @return
102      * @since  VFC 1.0
103      */
104     public static boolean isNumeric(String... strs) {
105         try {
106             for(String str : strs) {
107                 if(!StringUtils.isEmpty(str) && Float.parseFloat(str) < 0) {
108                     return false;
109                 }
110             }
111         } catch(NumberFormatException e) {
112             return false;
113         }
114         return true;
115     }
116
117     /**
118      *
119      * Compare zero by float.<br>
120      *
121      * @param tatol
122      * @param used
123      * @param drTotal
124      * @return
125      * @since  VFC 1.0
126      */
127     public static boolean compareZeroByFloat(String tatol, String used, String drTotal) {
128         Float ftotal = (float)0;
129         Float fused = (float)0;
130         Float fdrTotal = (float)0;
131         if(!StringUtils.isEmpty(tatol)) {
132             ftotal = new Float(tatol);
133         }
134         if(!StringUtils.isEmpty(used)) {
135             fused = new Float(used);
136         }
137         if(!StringUtils.isEmpty(drTotal)) {
138             fdrTotal = new Float(drTotal);
139         }
140         if(ftotal < fused + fdrTotal) {
141             return false;
142         }
143
144         return true;
145     }
146
147     /**
148      *
149      * Compare zero by integer.<br>
150      *
151      * @param tatol
152      * @param used
153      * @param drTotal
154      * @return
155      * @since  VFC 1.0
156      */
157     public static boolean compareZeroByInteger(String tatol, String used, String drTotal) {
158         Integer ftotal = 0;
159         Integer fused = 0;
160         Integer fdrTotal = 0;
161         if(!StringUtils.isEmpty(tatol)) {
162             ftotal = Integer.valueOf(tatol);
163         }
164         if(!StringUtils.isEmpty(used)) {
165             fused = Integer.valueOf(used);
166         }
167         if(!StringUtils.isEmpty(drTotal)) {
168             fdrTotal = Integer.valueOf(drTotal);
169         }
170         if(ftotal < fused + fdrTotal) {
171             return false;
172         }
173         return true;
174     }
175
176     /**
177      *
178      * Number format.<br>
179      *
180      * @param data
181      * @return
182      * @since  VFC 1.0
183      */
184     public static String numFormat(String data) {
185         if(null != data && !("".equals(data))) {
186             BigDecimal var = new BigDecimal(data);
187             DecimalFormat formatWithoutFraction = new DecimalFormat("############");
188             DecimalFormat formatWithFraction = new DecimalFormat("############.############");
189             if(new BigDecimal(var.intValue()).compareTo(var) == 0) {
190                 return formatWithoutFraction.format(var);
191             }
192             return formatWithFraction.format(var);
193         }
194         return null;
195     }
196
197     /**
198      *
199      * <br>
200      *
201      * @param inputStr
202      * @return
203      * @since  VFC 1.0
204      */
205     public static boolean checkXss(String inputStr) {
206         return inputStr.matches("[A-Za-z0-9_.']+");
207     }
208
209 }