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

edi.rule.util.ZSVessel Maven / Gradle / Ivy

package edi.rule.util;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * @author 摩拉克斯
 * @date 2023年1月11日 下午3:32:52
 */
public class ZSVessel {

	public static boolean isEmpty(Collection cs){
		return cs == null || cs.isEmpty();
	}

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

	public static boolean isEmpty(Map map){
		return map == null || map.isEmpty();
	}

	public static boolean isNotEmpty(Map map){
		return !isEmpty(map);
	}

	public static boolean isEmpty(Object array) {
		try {
			return array == null || Array.getLength(array) == 0;
		} catch (final IllegalArgumentException e) {
			throw new IllegalArgumentException("Unsupported object type: " + array.getClass().getName());
		}
	}

	public static boolean isNotEmpty(Object array) {
		return !isEmpty(array);
	}
	/**
	 * @see ZSObject#copy(Class, Object)
	 * */
	@SuppressWarnings("unchecked")
	public static  Collection copyCollection(Collection source){
        try {
			Collection collection = source.getClass().getConstructor().newInstance();
			for (E e:(Collection)source) {
				collection.add((E) ZSObject.copy(e.getClass(),e));
			}
			return collection;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
	}
	/**
	 * @see ZSObject#copy(Class, Object)
	 * */
	@SuppressWarnings("unchecked")
	public static  Map copyMap(Map source){
        try {
			Map map = source.getClass().getConstructor().newInstance();
			source.forEach((k,v)->map.put((K) k, (V) ZSObject.copy(v.getClass(),v)));
			return map;
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
	}
	/**
	 * @see ZSObject#copy(Class, Object)
	 * */
	public static Object copyArray(Object source){
		int length = Array.getLength(source);
		Class type = source.getClass().getComponentType();
		Object array = Array.newInstance(type,length);
		for (int i=0;i T mapToObject(Map map,Class clazz) {
		return (T)ZSRule.om.convertValue(map, clazz);
	}
	/**
	 * 

根据list中的map中的某一个key值对list进行排序,10万条数据30毫秒 * @param key 为Map中对应的key,表示用于进行排序的字段,该key对应的value值必须为实现Comparable接口的类型,比如Long,Double,Integer等数字类型 *

注意:key对应的值必须都为相同的类型,即Map1.get(key)为Integer,Map2.get(key)就必须为Integer类型,否则报错 * */ @SuppressWarnings("rawtypes") public static void mapSort(List> list,String key,boolean isAsc) { list.sort(new Comparator>() { @SuppressWarnings("unchecked") @Override public int compare(Map sort1, Map sort2) { if (isAsc) { return ((T)sort1.get(key)).compareTo((T)sort2.get(key)); }else { return ((T)sort2.get(key)).compareTo((T)sort1.get(key)); } } }); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy