/*
* Copyright 2016 Huawei Technologies Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onap.vfc.nfvo.multivimproxy.common.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* String Utility Class.
*
*
*
* @author
* @version VFC 1.0 Sep 10, 2016
*/
public final class StringUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(StringUtil.class);
private StringUtil() {
}
/**
*
* Check whether thestring is valid.
*
* @param str
* @return
* @since VFC 1.0
*/
public static boolean isValidString(String str) {
if(null == str || str.isEmpty()) {
return false;
}
return true;
}
/**
*
* Check whether the value is larger than zero.
*
* @param strs
* @return
* @since VFC 1.0
*/
public static boolean isAnyLargeThanZero(String... strs) {
for(String str : strs) {
if(!StringUtils.isEmpty(str) && Float.parseFloat(str) > 0) {
LOGGER.info("isAnyLargeThanZero : {} is > 0", str);
return true;
}
}
return false;
}
/**
*
* Check whether the value is Integer.
*
* @param strs
* @return
* @since VFC 1.0
*/
public static boolean isInteger(String... strs) {
try {
for(String str : strs) {
if(!StringUtils.isEmpty(str) && Integer.parseInt(str) < 0) {
return false;
}
}
} catch(NumberFormatException e) {
return false;
}
return true;
}
/**
*
* Check whether the input is Numeric.
*
* @param strs
* @return
* @since VFC 1.0
*/
public static boolean isNumeric(String... strs) {
try {
for(String str : strs) {
if(!StringUtils.isEmpty(str) && Float.parseFloat(str) < 0) {
return false;
}
}
} catch(NumberFormatException e) {
return false;
}
return true;
}
/**
*
* Compare zero by float.
*
* @param tatol
* @param used
* @param drTotal
* @return
* @since VFC 1.0
*/
public static boolean compareZeroByFloat(String tatol, String used, String drTotal) {
Float ftotal = (float)0;
Float fused = (float)0;
Float fdrTotal = (float)0;
if(!StringUtils.isEmpty(tatol)) {
ftotal = new Float(tatol);
}
if(!StringUtils.isEmpty(used)) {
fused = new Float(used);
}
if(!StringUtils.isEmpty(drTotal)) {
fdrTotal = new Float(drTotal);
}
if(ftotal < fused + fdrTotal) {
return false;
}
return true;
}
/**
*
* Compare zero by integer.
*
* @param tatol
* @param used
* @param drTotal
* @return
* @since VFC 1.0
*/
public static boolean compareZeroByInteger(String tatol, String used, String drTotal) {
Integer ftotal = 0;
Integer fused = 0;
Integer fdrTotal = 0;
if(!StringUtils.isEmpty(tatol)) {
ftotal = Integer.valueOf(tatol);
}
if(!StringUtils.isEmpty(used)) {
fused = Integer.valueOf(used);
}
if(!StringUtils.isEmpty(drTotal)) {
fdrTotal = Integer.valueOf(drTotal);
}
if(ftotal < fused + fdrTotal) {
return false;
}
return true;
}
/**
*
* Number format.
*
* @param data
* @return
* @since VFC 1.0
*/
public static String numFormat(String data) {
if(null != data && !("".equals(data))) {
BigDecimal var = new BigDecimal(data);
DecimalFormat formatWithoutFraction = new DecimalFormat("############");
DecimalFormat formatWithFraction = new DecimalFormat("############.############");
if(new BigDecimal(var.intValue()).compareTo(var) == 0) {
return formatWithoutFraction.format(var);
}
return formatWithFraction.format(var);
}
return null;
}
/**
*
*
*
* @param inputStr
* @return
* @since VFC 1.0
*/
public static boolean checkXss(String inputStr) {
return inputStr.matches("[A-Za-z0-9_.']+");
}
}