package com.leanwo.activiti.util;

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

public class StringUtil {
	/**
	 * 判断字符串是否为空
	 * @param content
	 * @return
	 */
	public static boolean isEmpty(String content){
		return (content == null) || (content.length() == 0);
	}
	
	/**
	 * 判断字符串是否不为空
	 * @param content
	 * @return
	 */
	public static boolean isNotEmpty(String content){
		return !isEmpty(content);
	}
	
	/**
	 * 获取不为空的字符串，如果content为空，则返回""，否则返回content
	 * @param content
	 * @return
	 */
	public static String getNotNullString(String content){
		if(content == null)
			return "";
		return content;
	}
	
	/**
	 * 返回拼接字符串
	 * @param items
	 * @return
	 */
	public static String join(String... items){
		StringBuilder stringBuilder = new StringBuilder();
		for(String item : items){
			if(item != null){
				stringBuilder.append(item);
			}
		}
		return stringBuilder.toString();
	}
	

	/**
	 * 返回拼接字符串
	 * @param items
	 * @return
	 */
	public static String join(Object... items){
		StringBuilder stringBuilder = new StringBuilder();
		for(Object item : items){
			if(item != null){
				stringBuilder.append(item);
			}
		}
		return stringBuilder.toString();
	}
	

	/**
	 * 字符串数组里面是否包含待比较的字符串
	 * @param items 字符串数组
	 * @param targetString 待比较的字符串
	 * @return true：包含, false：不包含
	 */
	public static boolean arrayEquals(String[] items, String targetString){
		if(items == null || items.length == 0) {
			return false;
		}
		for(String item : items){
			if(item != null && item.equals(targetString)){
				return true;
			}
		}
		return false;
	}
	
	
	/** 
     * byte数组转换成16进制字符串 
     * @param src 
     * @return 
     */  
	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder();
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}
	
	/**
	 * 判断字符串是否是数字
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
}
