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

x7.repository.util.ResultSortUtil Maven / Gradle / Ivy

package x7.repository.util;

import x7.core.bean.BeanElement;
import x7.core.bean.Criteria;
import x7.core.bean.KV;
import x7.core.bean.Parsed;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ResultSortUtil {

    /**
     * by orderIn0
     * @param list
     * @param criteria
     * @param parsed
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public  static  void sort(List list, Criteria criteria, Parsed parsed) throws InvocationTargetException, IllegalAccessException {

        if (list.isEmpty())
            return;

        List fixedSortList = criteria.getFixedSortList();

        if (fixedSortList.isEmpty())
            return;

        KV kv0 = fixedSortList.get(0);

        List tempList = new ArrayList<>();
        tempList.addAll(list);

        list.clear();

        Class clz = parsed.getClz();

        String property = kv0.k;

        for (Object para : (List)kv0.v){
            for (T result: tempList){
                BeanElement be = parsed.getElement(property);
                Object o = be.getMethod.invoke(result);
                if (String.valueOf(para).equals(String.valueOf(o))){
                    list.add(result);
                }
            }
        }

    }

    public static void sort(List> list, Criteria.ResultMappedCriteria criteria) {

        if (list.isEmpty())
            return;

        List fixedSortList = criteria.getFixedSortList();

        if (fixedSortList.isEmpty())
            return;

        KV kv0 = fixedSortList.get(0);
        String property = kv0.k;

        Map test = list.get(0);
        if (!test.containsKey(property))
            return;

        List> tempList = new ArrayList<>();
        tempList.addAll(list);

        list.clear();

        for (Object para : (List)kv0.v){
            for (Map mapResult: tempList){
                Object o = mapResult.get(property);
                if (String.valueOf(para).equals(String.valueOf(o))){
                    list.add(mapResult);
                }
            }
        }

    }
}