package com.uas.search.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

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

	/**
	 * 判断是否为null、空数组、空串或者空集合
	 * 
	 * @param obj
	 * @return
	 */
	public static boolean isEmpty(Object obj) {
		if (obj == null) {
			return true;
		}
		if (obj.getClass().isArray()) {
			if (obj instanceof long[]) {
				return ArrayUtils.isEmpty((long[]) obj);
			} else if (obj instanceof int[]) {
				return ArrayUtils.isEmpty((int[]) obj);
			} else if (obj instanceof short[]) {
				return ArrayUtils.isEmpty((short[]) obj);
			} else if (obj instanceof byte[]) {
				return ArrayUtils.isEmpty((byte[]) obj);
			} else if (obj instanceof char[]) {
				return ArrayUtils.isEmpty((char[]) obj);
			} else if (obj instanceof boolean[]) {
				return ArrayUtils.isEmpty((boolean[]) obj);
			} else if (obj instanceof float[]) {
				return ArrayUtils.isEmpty((float[]) obj);
			} else if (obj instanceof double[]) {
				return ArrayUtils.isEmpty((double[]) obj);
			}
			return ArrayUtils.isEmpty((Object[]) obj);
		}
		return (obj instanceof String && StringUtils.isEmpty((String) obj))
				|| (obj instanceof Collection && CollectionUtils.isEmpty((Collection<?>) obj))
				|| (obj instanceof Map && CollectionUtils.isEmpty((Map<?, ?>) obj));
	}

	/**
	 * 将对象转为 String
	 * 
	 * @param obj
	 * @return
	 */
	public static String toString(Object obj) {
		if (obj == null) {
			return "";
		}
		if (obj instanceof String) {
			return (String) obj;
		}
		if (obj.getClass().isArray()) {
			if (obj instanceof long[]) {
				return Arrays.toString((long[]) obj);
			} else if (obj instanceof int[]) {
				return Arrays.toString((int[]) obj);
			} else if (obj instanceof short[]) {
				return Arrays.toString((short[]) obj);
			} else if (obj instanceof byte[]) {
				return Arrays.toString((byte[]) obj);
			} else if (obj instanceof char[]) {
				return Arrays.toString((char[]) obj);
			} else if (obj instanceof boolean[]) {
				return Arrays.toString((boolean[]) obj);
			} else if (obj instanceof float[]) {
				return Arrays.toString((float[]) obj);
			} else if (obj instanceof double[]) {
				return Arrays.toString((double[]) obj);
			}
			return Arrays.toString((Object[]) obj);
		}
		return obj.toString();
	}

	/**
	 * 深克隆对象
	 * 
	 * @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();
	}

	/**
	 * 深克隆集合对象
	 * 
	 * @param collection
	 *            要克隆的对象
	 * @return 克隆得到的对象
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws NotSerializableException
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static <T> Collection<T> clone(Collection<T> collection) throws InstantiationException,
			IllegalAccessException, NotSerializableException, ClassNotFoundException, IOException {
		if (collection == null) {
			return null;
		}
		Collection<T> result = collection.getClass().newInstance();
		for (T t : collection) {
			if (t instanceof Serializable) {
				result.add((T) clone((Serializable) t));
			} else if (t instanceof Collection) {
				result.add((T) clone((Collection<?>) t));
			} else if (t instanceof Map) {
				result.add((T) clone((Map<String, ?>) t));
			} else {
				throw new IllegalArgumentException("未实现 Serializable 接口");
			}
		}
		return result;
	}

	/**
	 * 深克隆 Map 对象
	 * 
	 * @param map
	 *            要克隆的对象
	 * @return 克隆得到的对象
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws NotSerializableException
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static <T> Map<String, T> clone(Map<String, T> map) throws InstantiationException, IllegalAccessException,
			NotSerializableException, ClassNotFoundException, IOException {
		if (map == null) {
			return null;
		}
		Map<String, T> result = map.getClass().newInstance();
		Set<Entry<String, T>> entrySet = map.entrySet();
		for (Entry<String, T> entry : entrySet) {
			T value = entry.getValue();
			if (value instanceof Serializable) {
				result.put(entry.getKey(), (T) clone((Serializable) value));
			} else if (value instanceof Collection) {
				result.put(entry.getKey(), (T) clone((Collection<?>) value));
			} else if (value instanceof Map) {
				result.put(entry.getKey(), (T) clone((Map<String, ?>) value));
			} else {
				throw new IllegalArgumentException("未实现 Serializable 接口");
			}
		}
		return result;
	}
}
