package com.mybatis.api.mybatis.util;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.List;
import java.util.Map;
public class StringUtil {
public static Boolean isEmpty(Object type){
if(type != null && !"".equals(type)){
return false;
}
return true;
}
public static Boolean isListEmpty(List list){
if(list != null && list.size() > 0){
return false;
}
return true;
}
@SuppressWarnings("rawtypes")
public static boolean isNullOrEmpty(Object obj) {
boolean isEmpty = false;
if (obj == null) {
isEmpty = true;
} else if (obj instanceof String) {
isEmpty = ((String) obj).trim().isEmpty();
} else if (obj instanceof Collection) {
isEmpty = (((Collection) obj).size() == 0);
} else if (obj instanceof Map) {
isEmpty = ((Map) obj).size() == 0;
} else if (obj.getClass().isArray()) {
isEmpty = Array.getLength(obj) == 0;
}
return isEmpty;
}
public static boolean checkEmail(String text, int length) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*") && text.length() <= length;
}
public static boolean checkTelephone(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches(
"(0\\d{2,3}-\\d{7,8})|" +
"(0\\d{9,11})|" +
"(\\d{7})|" +
"(\\d{8})|" +
"(4\\d{2}\\d{7})|" +
"(4\\d{2}-\\d{7})|" +
"(4\\d{2}-\\d{3}-\\d{4})|" +
"(4\\d{2}-\\d{4}-\\d{3})");
}
public static boolean checkMobilephone(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("^1(3[0-9]|4[579]|5[0-35-9]|8[0-9]|7[015-8])\\d{8}$");
}
public static boolean checkChineseName(String text, int length) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("^[\u4e00-\u9fa5]+$") && text.length() <= length;
}
public static boolean checkBlank(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("^\\s*|\\s*$");
}
public static boolean checkHtmlTag(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("<(\\S*?)[^>]*>.*?<!--\\1-->|<.*? />");
}
public static boolean checkURL(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("[a-zA-z]+://[^\\s]*");
}
public static boolean checkIP(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}");
}
public static boolean checkQQ(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("[1-9][0-9]{4,13}");
}
public static boolean checkPostCode(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("[1-9]\\d{5}(?!\\d)");
}
public static boolean checkIDCard(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("\\d{15}|\\d{18}|(\\d{17}[x|X])");
}
public static boolean checkLength(String text, int length) {
return ((StringUtil.isNullOrEmpty(text)) ? 0 : text.length()) <= length;
}
public static boolean isNumber(String text) {
if(StringUtil.isNullOrEmpty(text)){
return false;
}
return text.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
}
}