package com.uas.finance.util;

import java.io.*;
import java.util.Collection;
import java.util.Map;

/**
 * 对象工具类
 * 
 * @author sunyj
 * @since 2017年9月1日 下午3:54:26
 */
public class ObjectUtils {

	/**
	 * 判断是否为null、空串或者空集合
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj) {
		return obj == null || (obj instanceof String && StringUtils.isEmpty((String) obj))
				|| (obj instanceof Collection && CollectionUtils.isEmpty((Collection<?>) obj))
				|| (obj instanceof Map && CollectionUtils.isEmpty((Map<?, ?>) obj));
	}

	/**
	 * 深克隆对象
	 * 
	 * @param t
	 *            要克隆的对象
	 * @return 克隆得到的对象
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public static <T extends Serializable> T clone(T t)
			throws IOException, ClassNotFoundException, NotSerializableException {
		if (t == null) {
			return null;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		new ObjectOutputStream(out).writeObject(t);
		ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
		return (T) in.readObject();
	}
}
