package cn.com.countrygarden.bi.main.util.erp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import cn.com.countrygarden.bi.main.model.erp.UniqueBean;

/**
 * @Author Owen
 * @Description String工具类
 */
public class StringUtil {
	public static final String SPACE = " ";
	public static final String EMPTY = "";
	public static final String LF = "\n";
	public static final String CR = "\r";
	public static final int INDEX_NOT_FOUND = -1;
	private static final int PAD_LIMIT = 8192;


	public StringUtil() {
	}

	public static boolean isNotEmpty(String fileName) {
		return StringUtils.isNotEmpty(fileName);
	}
    
	//空值判断
	public static boolean isEmpty(CharSequence cs) {
		return cs == null || cs.length() == 0;
	}

	public static boolean isNotEmpty(CharSequence cs) {
		return !isEmpty(cs);
	}

	public static boolean isAnyEmpty(CharSequence... css) {
		if (ArrayUtils.isEmpty(css)) {
			return false;
		} else {
			CharSequence[] arr$ = css;
			int len$ = css.length;

			for (int i$ = 0; i$ < len$; ++i$) {
				CharSequence cs = arr$[i$];
				if (isEmpty(cs)) {
					return true;
				}
			}

			return false;
		}
	}

	public static boolean isNoneEmpty(CharSequence... css) {
		return !isAnyEmpty(css);
	}

	public static boolean isAllEmpty(CharSequence... css) {
		if (ArrayUtils.isEmpty(css)) {
			return true;
		} else {
			CharSequence[] arr$ = css;
			int len$ = css.length;

			for (int i$ = 0; i$ < len$; ++i$) {
				CharSequence cs = arr$[i$];
				if (isNotEmpty(cs)) {
					return false;
				}
			}

			return true;
		}
	}

	// 空值判断
	public static boolean isBlank(CharSequence cs) {
		int strLen;
		if (cs != null && (strLen = cs.length()) != 0) {
			for (int i = 0; i < strLen; ++i) {
				if (!Character.isWhitespace(cs.charAt(i))) {
					return false;
				}
			}
			return true;
		} else {
			return true;
		}
	}

	public static boolean isNotBlank(CharSequence cs) {
		return !isBlank(cs);
	}

	public static boolean isAnyBlank(CharSequence... css) {
		if (ArrayUtils.isEmpty(css)) {
			return false;
		} else {
			CharSequence[] arr$ = css;
			int len$ = css.length;

			for (int i$ = 0; i$ < len$; ++i$) {
				CharSequence cs = arr$[i$];
				if (isBlank(cs)) {
					return true;
				}
			}

			return false;
		}
	}

	public static boolean isNoneBlank(CharSequence... css) {
		return !isAnyBlank(css);
	}

	public static boolean isAllBlank(CharSequence... css) {
		if (ArrayUtils.isEmpty(css)) {
			return true;
		} else {
			CharSequence[] arr$ = css;
			int len$ = css.length;

			for (int i$ = 0; i$ < len$; ++i$) {
				CharSequence cs = arr$[i$];
				if (isNotBlank(cs)) {
					return false;
				}
			}

			return true;
		}
	}

	public static boolean isNotBlankAndNotEmpty(CharSequence cs) {
		return StringUtil.isNotEmpty(cs) && StringUtil.isNotBlank(cs);
	}
    
	
	public static boolean isBlankOrEmpty(CharSequence cs) {
		return StringUtil.isEmpty(cs) || StringUtil.isBlank(cs);
	}
	
	/**
	 * @author Austin
	 * 验证电话号码格式
	 */
	public static boolean checkCellphone(String cellphone) {
        String regex = "^\\d{11}$"; //11位纯数字
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher=pattern.matcher(cellphone);
        return matcher.matches();
    }
	
	/**
	 * @author Austin
	 * 验证日期格式
	 */
	public static boolean checkDate(String date) {
//        String regex = "^(\\d{1,4})(\\/)(\\d{1,2})\\2(\\d{1,2})$"; //例 2018/09/08
        String regex = "^[1-2][0-9][0-9][0-9]/([1][0-2]|0?[1-9])/([12][0-9]|3[01]|0?[1-9])$";
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher=pattern.matcher(date);
        return matcher.matches();
    }
	
	/**
	 * @author Austin
	 * 验证身份证格式
	 */
	public static boolean checkPaperCode(String code) {
        if(code.trim().length() == 18) //去出空格之后，保证18位即可
        	return true;
        else
        	return false;
    }
	
	/**
	 * @author Austin
	 * 验证名称格式
	 */
	public static boolean checkName(String name) {
	    String regex = "^[\\u4e00-\\u9fa5_a-zA-Z0-9]+$"; //只允许汉字、英文字母和数字
	    Pattern pattern=Pattern.compile(regex);
	    Matcher matcher=pattern.matcher(name);
	    return matcher.matches();
	}
	
	/**
	 * @author Austin
	 * 验证名称格式
	 */
	public static boolean checkGender(String gender) {
	    //性别只能填 男 或者 女
		if(!"男".equals(gender) && !"女".equals(gender)){
	    	return false;
	    }
	    return true;
	}
	/**
	 * @author Austin
	 * 验证数字
	 */
	public static boolean checkNum(String num){
		String regex = "^[0-9]+$"; //纯数字
	    Pattern pattern=Pattern.compile(regex);
	    Matcher matcher=pattern.matcher(num);
	    return matcher.matches();
	}
	/**
	 * @author Austin
	 * 验证房屋编号
	 */
	public static boolean checkRoomSign(String roomSign){
		String regex = "^[0-9-]+$"; //数字和下划线
	    Pattern pattern=Pattern.compile(regex);
	    Matcher matcher=pattern.matcher(roomSign);
	    return matcher.matches();
	}
	
	/**
	 * @author Austin
	 * 验证房屋编号
	 */
	public static boolean checkMoney(String money){
		String regex = "-[0-9]+(.[0-9]+)?|[0-9]+(.[0-9]+)?";//正负小数
	    Pattern pattern=Pattern.compile(regex);
	    Matcher matcher=pattern.matcher(money);
	    return matcher.matches();
	}
	
	// 测试 正则表达式
	public static void main(String[] args) {
		String str = "01_002_9";
		String str1 = "艾伦abc123";
		String str2 = "1231243";
		String str3 = "2018/05/04";
		System.out.println(checkRoomSign(str));
		System.out.println(checkName(str1));
		System.out.println(checkNum(str2));
		System.out.println(checkDate(str3));
		
		//测试唯一bean
		UniqueBean ub = new UniqueBean();
		ub.setLymc("2幢1单元");
		ub.setDy("1");
		ub.setLc("1");
		
		UniqueBean ub2 = new UniqueBean();
		ub2.setLymc("2幢1单元");
		ub2.setDy("1");
		ub2.setLc("1");
		List<UniqueBean> ublist = new ArrayList<UniqueBean>();
		ublist.add(ub);
		System.out.println("##################"+ublist.contains(ub2)+"=================="+ub.equals(ub2));
		
		boolean flag = ub.equals(ub2);
		System.out.println(flag);
		List<String> list = new ArrayList<String>();
		list.add("123");
		for(String str22 : list){
			System.out.println("===== "+str22);
		}
		
		
		
		Map<String,String> testMap = new HashMap<String,String>();
		testMap.put("1", "ccc");
		testMap.put("1", "ccc");
		testMap.put("1", "ccc");
		testMap.put("1", "ccc");
		for(Entry<String,String> entry :testMap.entrySet()){
			System.out.println(entry.getKey()+" ==== "+entry.getValue());
		}
		
		String money = "-3433.23 ";
		System.out.println("~~~~~~~~~~~~~~~~"+checkMoney(money.trim()));
		
	}
}


