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.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
28 * String Utility Class.<br>
33 * @version NFVO 0.5 Sep 10, 2016
35 public final class StringUtil {
37 private static final Logger LOGGER = LoggerFactory.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)) {
87 int value = Integer.parseInt(str);
93 } catch(NumberFormatException e) {
101 * Check whether the input is Numeric.<br>
107 public static boolean isNumeric(String... strs) {
109 for(String str : strs) {
110 if(!StringUtils.isEmpty(str)) {
111 float value = Float.parseFloat(str);
117 } catch(NumberFormatException e) {
125 * Compare zero by float.<br>
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);
140 if(!StringUtils.isEmpty(used)) {
141 fused = new Float(used);
143 if(!StringUtils.isEmpty(drTotal)) {
144 fdrTotal = new Float(drTotal);
146 if(ftotal < fused + fdrTotal) {
155 * Compare zero by integer.<br>
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);
170 if(!StringUtils.isEmpty(used)) {
171 fused = Integer.valueOf(used);
173 if(!StringUtils.isEmpty(drTotal)) {
174 fdrTotal = Integer.valueOf(drTotal);
176 if(ftotal < fused + fdrTotal) {
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);
198 return formatWithFraction.format(var);
211 public static boolean checkXss(String inputStr) {
212 return inputStr.matches("[A-Za-z0-9_.']+");