All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.voovan.tools.TObject Maven / Gradle / Ivy

package org.voovan.tools;

import org.voovan.tools.reflect.TReflect;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.*;

/**
 * 对象工具类
 *
 * @author helyho
 *
 * Voovan Framework.
 * WebSite: https://github.com/helyho/Voovan
 * Licence: Apache v2 License
 */
public class TObject {
	/**
	 * 类型转换
	 * 	JDK 1.8 使用效果最好,否则有可能会转换失败
	 *
	 * @param  范型
	 * @param obj 被转换对象
	 * @return	转换后的对象
	 */
	@SuppressWarnings("unchecked")
	public static  T cast(Object obj){
		return TObject.cast(obj);
	}

	/**
	 * 空值默认值
	 * @param  范型
	 * @param source	检测对象
	 * @param defValue		null 值替换值
	 * @return	如果非 null 则返回 source,如果为 null 则返回 defValue。
	 */
	public static T nullDefault(T source,T defValue){
		return source!=null?source:defValue;
	}

	/**
	 * 初始化一个 List
	 * @param objs List 列表的每一个元素
	 * @return	初始化完成的List对象
	 */
	@SuppressWarnings("rawtypes")
	public static List asList(Object ...objs){
		List result = new ArrayList();
		for(Object o : objs) {
			result.add(o);
		}
		return result;
	}

	/**
	 * 初始化一个 Map
	 * @param objs		每两个参数组成一个键值对,来初始化一个 Map. 如:key1,value1,key2,value2.....
	 * @return	初始化完成的Map对象
	 */
	@SuppressWarnings("rawtypes")
	public static Map asMap(Object ...objs){
		Map map = new LinkedHashMap();
		for(int i=1;i it = source.values().iterator(); it.hasNext(); ) {
			Object obj = it.next();
			if (obj == null) {
				it.remove();
			}
			if (obj instanceof String && withEmptyString) {
				if (TString.isNullOrEmpty((String) obj)) {
					it.remove();
				}
			}
		}
		return source;
	}

	/**
	 * 移除 map 中的 null 和 空字符串
	 * @param source 被处理的 map
	 * @return 移除 map 中的 null 和 空字符串后的 map
	 */
	public static Map removeMapNullValue(Map source) {
		return removeMapNullValue(source, true);
	}

	/**
	 * 将 Map 的值转换成 List
	 * @param map 需转换的 Map 对象
	 * @return 转后的 Value 的 list
	 */
	public static List mapValueToList(Map map){
		ArrayList result = new ArrayList();
		for(Map.Entry entry : map.entrySet()){
			result.add(entry.getValue());
		}
		return result;
	}

	/**
	 * 将 Map 的值转换成 List
	 * @param map 需转换的 Map 对象
	 * @return 转后的 key 的 list
	 */
	public static List mapKeyToList(Map map){
		ArrayList result = new ArrayList();
		for(Map.Entry entry : map.entrySet()){
			result.add(entry.getKey());
		}
		return result;
	}

	/**
	 * 将数组转换成 Map
	 * 			key 位置坐标, 从 1 开始计数
	 *          value 数组值
	 * @param objs    	待转换的数组
	 * @param  范型
	 * @return 转换后的 Map  [序号, 值]
	 */
	public static  Map arrayToMap(T[] objs){
		Map arrayMap = new LinkedHashMap();
		for(int i=0;i 范型
	 * @return 转换后的 Map  [序号, 值]
	 */
	public static  Map collectionToMap(Collection objs){
		Map arrayMap = new LinkedHashMap();
		int i = 0;
		for(T t : objs){
			arrayMap.put(Integer.toString(++i), t);
		}
		return arrayMap;
	}

	/**
	 * 数组拼接
	 * @param firstArray		   首个数组
	 * @param firstArrayLength     首个数组长度
	 * @param lastArray			   拼接在后的数组
	 * @param lastArrayLength      拼接在后的数组长度
	 * @return 拼接后的数组
	 */
	public static Object[] arrayConcat(Object[] firstArray,int firstArrayLength, Object[] lastArray,int lastArrayLength) {
		if (lastArray.length == 0)
			return firstArray;
		Object[] target = new Object[firstArrayLength + lastArrayLength];
		System.arraycopy(firstArray, 0, target, 0, firstArrayLength);
		System.arraycopy(lastArrayLength, 0, target, firstArrayLength, lastArrayLength);
		return target;
	}


	/**
	 * 在数组中查找元素
	 * @param source 数组
	 * @param mark 被查找的元素
	 * @return 索引位置
	 */
	public static int indexOfArray(Object[] source, Object mark){
		for(int i=0;i= 0; p--){
						if(mark[p] == source[i+mark.length-j]){
							pos = p ;
						}
					}

					if( pos== -1){
						i = i + mark.length + 1 - j;
						j = 0 ;
					}else{
						i = i + mark.length - pos - j;
						j = 0;
					}
				}else{
					if(j == (mark.length - 1)){
						i = i - j + 1 ;
						j = 0;
						index  = i-j - 1;
						break;
					}else{
						i++;
						j++;
					}
				}
			}

			return index;
	}


	/**
	 * 比较两个对象的差异, 并返回变更的数据
	 *
	 * @param origin 原对象
	 * @param current 当前对象
	 * @param compareSuperField 是否比较父级属性
	 * @return 返回变更的数据 Map{变更的Field, [原数据, 当前数据]}
	 * @throws IllegalAccessException 反射非法访问异常
	 */
	public static  Map compare(Object origin, Object current, boolean compareSuperField) throws IllegalAccessException {
			Class clazz = origin.getClass();

			if (!origin.getClass().equals(current.getClass())) {
				return null;
			}

			Field[] fields = TReflect.getFields(clazz);

			HashMap ret = new HashMap();

			List modifyField = new ArrayList();
			for (Field field : fields) {
				if(!compareSuperField || field.getDeclaringClass().equals(clazz) ) {

					Object originFieldValue = field.get(origin);
					Object currentFieldValue = field.get(current);

					if (originFieldValue == null && currentFieldValue == null) {
						continue;
					} else if (originFieldValue != null && !originFieldValue.equals(currentFieldValue)) {
						ret.put(field, new Object[]{originFieldValue, currentFieldValue});
					} else if (currentFieldValue != null && !currentFieldValue.equals(originFieldValue)) {
						ret.put(field, new Object[]{originFieldValue, currentFieldValue});
					}
				}
			}

			return ret;
	}

	/**
	 * 深克隆
	 * @param obj 被克隆的对象
	 * @param  范型
	 * @return 克隆后的新对象
	 * @throws ReflectiveOperationException 反射异常
	 * @throws ParseException 解析异常
	 */
	public static  T clone(T obj) throws ReflectiveOperationException, ParseException {
		Map dataMap = TReflect.getMapfromObject(obj);
		return (T)TReflect.getObjectFromMap(obj.getClass(),dataMap, false);
	}

}