com.github.fashionbrot.validated.util.StringUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mars-validated Show documentation
Show all versions of mars-validated Show documentation
mars-validated 参数验证 https://github.com/fashionbrot/mars-validated
/*
* Copyright 1999-2018 Alibaba Group Holding 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 com.github.fashionbrot.validated.util;
import java.math.BigDecimal;
public final class StringUtil {
private static final String EMPTY = "";
public static boolean isBlank(String str) {
int strLen;
if (str == null || (strLen = str.length()) == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if ((!Character.isWhitespace(str.charAt(i)))) {
return false;
}
}
return true;
}
public static boolean isNotBlank(String str) {
return !isBlank(str);
}
public static boolean isEmpty(CharSequence str) {
return str == null || str.length() == 0;
}
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
public static String trimToEmpty(String str) {
return str == null ? EMPTY : str.trim();
}
public static String formatString(Object object){
if (object==null){
return EMPTY;
}else{
return object.toString();
}
}
public static boolean isDigits(String str) {
return isNumeric(str);
}
public static boolean isNumeric(CharSequence cs) {
if (isEmpty(cs)) {
return false;
} else {
int sz = cs.length();
for(int i = 0; i < sz; ++i) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}
}
public static String trim(String str) {
return str == null ? null : str.trim();
}
private static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
final CharSequence substring, final int start, final int length) {
if (cs instanceof String && substring instanceof String) {
return ((String)cs).regionMatches(ignoreCase, thisStart, (String)substring, start, length);
}
int index1 = thisStart;
int index2 = start;
int tmpLen = length;
// Extract these first so we detect NPEs the same as the java.lang.String version
final int srcLen = cs.length() - thisStart;
final int otherLen = substring.length() - start;
// Check for invalid parameters
if (thisStart < 0 || start < 0 || length < 0) {
return false;
}
// Check that the regions are long enough
if (srcLen < length || otherLen < length) {
return false;
}
while (tmpLen-- > 0) {
final char c1 = cs.charAt(index1++);
final char c2 = substring.charAt(index2++);
if (c1 == c2) {
continue;
}
if (!ignoreCase) {
return false;
}
// The same check as in String.regionMatches():
if (Character.toUpperCase(c1) != Character.toUpperCase(c2)
&& Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
return false;
}
}
return true;
}
public static String capitalize(String str) {
return changeFirstCharacterCase(str, true);
}
private static String changeFirstCharacterCase(String str, boolean capitalize) {
if (str == null || str.length() == 0) {
return str;
}
StringBuilder buf = new StringBuilder(str.length());
if (capitalize) {
buf.append(Character.toUpperCase(str.charAt(0)));
} else {
buf.append(Character.toLowerCase(str.charAt(0)));
}
buf.append(str.substring(1));
return buf.toString();
}
public static int getIntValue(Object object){
if (object==null){
return 0;
}else{
String str=object.toString();
if (!isNumeric(str)){
return 0;
}else{
try {
return Integer.valueOf(str);
}catch (Exception e){
return 0;
}
}
}
}
public static long getLongValue(Object object){
if (object==null){
return 0;
}else{
String str=object.toString();
if (!isNumeric(str)){
return 0;
}else{
try {
return Long.valueOf(str);
}catch (Exception e){
return 0;
}
}
}
}
public static double getDoubleValue(Object object){
if (object==null){
return 0;
}else{
try {
return Double.valueOf(object.toString());
}catch (Exception e){
return 0;
}
}
}
public static float getFloatValue(Object object){
if (object==null){
return 0;
}else{
try {
return Float.valueOf(object.toString());
}catch (Exception e){
return 0;
}
}
}
public static short getShortValue(Object object){
if (object==null){
return 0;
}else{
try {
return Short.valueOf(object.toString());
}catch (Exception e){
return 0;
}
}
}
public static BigDecimal getBigDecimalValue(Object object){
if (object==null){
return BigDecimal.ZERO;
}else{
try {
return new BigDecimal(object.toString());
}catch (Exception e){
return BigDecimal.ZERO;
}
}
}
public static boolean formatBoolean(Object object){
if (object==null){
return false;
}else{
String str=object.toString();
try {
return Boolean.valueOf(str);
}catch (Exception e){
return false;
}
}
}
public static void main(String[] args) {
System.out.println(getLongValue("10"));
System.out.println(getDoubleValue("0.11"));
System.out.println(getFloatValue("0.12"));
System.out.println(getShortValue("13"));
System.out.println(getBigDecimalValue("14.35"));
}
}