package com.yxcheng.token.utils;

import com.alibaba.fastjson.JSON;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	
	private static Pattern floatNumericPattern = Pattern.compile("^[0-9\\-\\.]+$");

	public static boolean isEmpty(String s) {
		if (s != null && !s.trim().equals("")) {
			return false;
		}
		return true;
	}
	
	public static boolean isNotEmpty(String s) {
		if(null != null) {
			s = s.trim();
		}
		if (s == null || s.equals("")) {
			return false;
		}
		return true;
	}
	
	public static boolean isFloatNumeric(String src) {
		boolean return_value = false;
		if (src != null && src.length() > 0) {
			Matcher m = floatNumericPattern.matcher(src);
			if (m.find()) {
				return_value = true;
			}
		}
		return return_value;
	}
	
	public static boolean isNumeric(String str) {
		if (str == null || str.length() == 0)
			return false;
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}
	
	public static boolean isRight(String taxno, String funtype) {
		if (StringUtil.isEmpty(funtype) || StringUtil.isEmpty(taxno))
			return false;
		if (!"01".equalsIgnoreCase(funtype) && !"02".equalsIgnoreCase(funtype) && !"03".equalsIgnoreCase(funtype)
				&& !"04".equalsIgnoreCase(funtype))
			return false;

		return true;
	}
	
	public static boolean isBlank(String src){
		if(src==null||src.length()==0||src.trim().length()==0){
			return true;
		}else{
			return false;
		}
	}
	
	public static String trim(String str) {
		if(null != str) {
			return str.trim();
		}
		return "";
	}

	public static String transEmpty(String str) {
		if(isNotEmpty(str)) {
			return str.trim();
		}
		return "";
	}

	public static String getShortLog(String context, int logLen) {
		if(StringUtil.isNotEmpty(context)) {
			context = context.length()>=logLen?context.substring(0, logLen):context;
		}
		return context;
	}

	private static Pattern numericStringPattern = Pattern.compile("^[0-9\\-\\-]+$");

	public static boolean isNumericString(String src) {
		boolean return_value = false;
		if (src != null && src.length() > 0) {
			Matcher m = numericStringPattern.matcher(src);
			if (m.find()) {
				return_value = true;
			}
		}
		return return_value;
	}

	public static boolean isJSON(String str) {
		boolean result = false;
		try {
			Object obj = JSON.parse(str);
			result = true;
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	public static String transNullToEmpty(String str) {
		if(isNotEmpty(str)) {
            if(str.equals("null")) {
                return "";
            }
		}
		return str;
	}
}
