zoukankan      html  css  js  c++  java
  • 自定义列表过滤

    ParamUtil 过滤类

    /**
     * 对列表进行 param 过滤返回
     *
     * @param result
     * @param param
     * @param <E>
     * @param <Q>
     * @return
     */
    public static <E, Q extends Param> List<E> filter(List<E> result, Q param) {
        // 1. 过滤
        List<Term> terms = param.getTerms();
        List<E> collect = result.stream().filter(it -> match(it, terms)).collect(Collectors.toList());
        return collect;
    }
    
    
    /**
     * 判断对象是否满足所有 terms(暂时只支持 and 一层)
     *
     * @param result
     * @param terms
     * @param <E>
     * @return
     */
    public static <E> Boolean match(E result, List<Term> terms) {
        return terms.stream().allMatch(it -> match(result, it));
    }
    
    
    /**
     * 判断对象是否满足当前 term (暂时只支持 and 一层)
     *
     * @param result
     * @param term
     * @param <E>
     * @return
     */
    public static <E> Boolean match(E result, Term term) {
        String column = term.getColumn();
        TermEnum termType = term.getTermType();
        Object value = term.getValue();
        // 通过反射拿到 要对比对象column 上面的值
        Object targetValue = ReflectionUtil.getFieldValue(result, column);
        switch (termType) {
            case not:
                return !Objects.equals(value, targetValue);
            case eq:
                return Objects.equals(value, targetValue);
            case in:
                if (value instanceof Collection) {
                    return ((Collection) value).contains(targetValue);
                }
                return false;
            case nin:
                if (value instanceof Collection) {
                    return !((Collection) value).contains(targetValue);
                }
                return false;
            case like:
                return StringUtils.contains(String.valueOf(targetValue), String.valueOf(value));
            case nlike:
                return !StringUtils.contains(String.valueOf(targetValue), String.valueOf(value));
            case isnull:
                return Objects.isNull(targetValue);
            default:
                break;
        }
        // 不支持的 termType 直接抛异常,业务调用需要自己 append 进去
        throw new CommonException("un support termType:{0}", termType.name());
    }
    
    /**
     * 封装分页 data 数据
     *
     * @param result
     * @param param
     * @param <E>
     * @param <Q>
     * @return
     */
    public static <E, Q extends QueryParam> List<E> buildPagerResultData(List<E> result, Q param) {
        if (!param.isPaging()) {
            return result;
        }
        // 加上分页信息
        int pageNo = param.getPageNo();
        int pageSize = param.getPageSize();
        int from = pageNo * pageSize;
        int to = (pageNo + 1) * pageSize;
        int size = result.size();
        // check
        if (from > to) {
            throw new CommonException("fromIndex(" + from +
                    ") > toIndex(" + to + ")");
        }
        if (from < 0) {
            throw new CommonException("fromIndex = " + from);
        }
        if (from >= size) {
            // 空数组对象
            return Lists.newArrayList();
        }
        if (to > size) {
            to = size;
        }
        return result.subList(from, to);
    }
    
    
    

    ReflectionUtil 反射工具类

    /**
     * 反射静态方法 通过fieldName 获取到对象对于的值
     **/
    public static Object getFieldValue(Object obj, String fieldName) throws IllegalArgumentException {
        Field field = getAccessibleField(obj, fieldName);
        if (field == null) {
            throw new IllegalArgumentException("Could not find field [" + fieldName + "] on target [" + obj + "]");
        } else {
            Object result = null;
    
            try {
                result = field.get(obj);
            } catch (IllegalAccessException var5) {
                log.error("不可能抛出的异常{}", var5.getMessage());
            }
    
            return result;
        }
    }
    
    /**
     * 反射静态方法 通过fieldName 获取到字段对象
     **/
    public static Field getAccessibleField(Object obj, String fieldName) {
        Validate.notNull(obj, "object can't be null", new Object[0]);
        return getAccessibleField(obj.getClass(), fieldName);
    }
    
    private static Table<Class, String, Field> classStringFieldTable = HashBasedTable.create();
    
    /**
     * 反射静态方法 通过fieldName 获取到字段对象
     **/
    public static Field getAccessibleField(Class clazz, String fieldName) {
        Validate.notNull(clazz, "object can't be null", new Object[0]);
        Validate.notBlank(fieldName, "fieldName can't be blank", new Object[0]);
        Field field = (Field)classStringFieldTable.get(clazz, fieldName);
        if (field == null) {
            Class superClass = clazz;
    
            while(superClass != Object.class) {
                try {
                    field = superClass.getDeclaredField(fieldName);
                    field.setAccessible(true);
                    classStringFieldTable.put(clazz, fieldName, field);
                    break;
                } catch (NoSuchFieldException var5) {
                    superClass = superClass.getSuperclass();
                }
            }
        }
    
        return field;
    }
    
  • 相关阅读:
    算法训练 P1103
    算法训练 表达式计算
    算法训练 表达式计算
    基础练习 时间转换
    基础练习 字符串对比
    Codeforces 527D Clique Problem
    Codeforces 527C Glass Carving
    Codeforces 527B Error Correct System
    Codeforces 527A Glass Carving
    Topcoder SRM 655 DIV1 250 CountryGroupHard
  • 原文地址:https://www.cnblogs.com/liuyupen/p/13965667.html
Copyright © 2011-2022 走看看