2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.resmanagement.common.util;
19 import java.math.BigDecimal;
20 import java.text.DecimalFormat;
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
28 * String Utility Class.<br>
33 * @version VFC 1.0 Sep 10, 2016
35 public final class StringUtil {
37 private static final Logger LOGGER = LogManager.getLogger(StringUtil.class);
39 private StringUtil() {
44 * Check whether thestring is valid.<br>
50 public static boolean isValidString(String str) {
51 if(null == str || str.isEmpty()) {
59 * Check whether the value is larger than zero.<br>
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);
77 * Check whether the value is Integer.<br>
83 public static boolean isInteger(String... strs) {
85 for(String str : strs) {
86 if(!StringUtils.isEmpty(str) && Integer.parseInt(str) < 0) {
90 } catch(NumberFormatException e) {
98 * Check whether the input is Numeric.<br>
104 public static boolean isNumeric(String... strs) {
106 for(String str : strs) {
107 if(!StringUtils.isEmpty(str) && Float.parseFloat(str) < 0) {
111 } catch(NumberFormatException e) {
119 * Compare zero by float.<br>
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);
134 if(!StringUtils.isEmpty(used)) {
135 fused = new Float(used);
137 if(!StringUtils.isEmpty(drTotal)) {
138 fdrTotal = new Float(drTotal);
140 if(ftotal < fused + fdrTotal) {
149 * Compare zero by integer.<br>
157 public static boolean compareZeroByInteger(String tatol, String used, String drTotal) {
160 Integer fdrTotal = 0;
161 if(!StringUtils.isEmpty(tatol)) {
162 ftotal = Integer.valueOf(tatol);
164 if(!StringUtils.isEmpty(used)) {
165 fused = Integer.valueOf(used);
167 if(!StringUtils.isEmpty(drTotal)) {
168 fdrTotal = Integer.valueOf(drTotal);
170 if(ftotal < fused + fdrTotal) {
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);
192 return formatWithFraction.format(var);
205 public static boolean checkXss(String inputStr) {
206 return inputStr.matches("[A-Za-z0-9_.']+");