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

com.jl.JLSet Maven / Gradle / Ivy

The newest version!
package com.jl;

import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 集合操作工具类
 */
public class JLSet {

    /**
     * map操作
     */
    public static  MapOper map(Map map) {
        return new MapOper<>(map);
    }

    /**
     * list操作
     */
    public static  ListOper list(List list) {
        return new ListOper<>(list);
    }

    /**
     * 数组操作
     */
    public static  ArrayOper array(T[] array) {
        return new ArrayOper<>(array);
    }

    /**
     * map操作类
     */
    public static class MapOper {
        private Map map;

        public MapOper(Map map) {
            this.map = map;
        }

        /**
         * 转list
         */
        public ToList toList() {
            return new ToList<>(map);
        }

        /**
         * 转实体
         */
        @SneakyThrows
        public  T toBean(Class t) {
            return JSONUtil.parseObj(map).toBean(t);
        }

        /**
         * 转换list操作类
         */
        public static class ToList {
            private Map map;

            public ToList(Map map) {
                this.map = map;
            }

            /**
             * key转换list
             */
            public List key() {
                return new ArrayList(map.keySet());
            }

            /**
             * value转换list
             */
            public List value() {
                return new ArrayList(map.values());
            }
        }
    }

    /**
     * list操作类
     */
    public static class ListOper {
        private List list;

        public ListOper(List list) {
            this.list = list;
        }

        /**
         * 转map(对象)
         */
        public  ToMapOper toMap(JLambda.JFunction jlFunction) {
            return new ToMapOper<>(list, jlFunction);
        }

        /**
         * 转map(元素)
         */
        public ToMapOper toMap() {
            return new ToMapOper<>(list);
        }

        /**
         * 去重(对象)
         */
        public List comparing(JLambda.JFunction... jlFunction) {
            return list.stream().collect(
                    Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(
                                            Comparator.comparing(o -> {
                                                try {
                                                    StringJoiner stringJoiner = new StringJoiner(";");
                                                    for (JLambda.JFunction tjlFunction : jlFunction) {
                                                        String property = JLambda.getProperty(tjlFunction);
                                                        JTuple.Tuple3> tuple3 = JReflect.PropertyReflect.getProperty(o, property);
                                                        if (tuple3 == null) {
                                                            return null;
                                                        }
                                                        stringJoiner.add(tuple3.getV2().toString());
                                                    }
                                                    return stringJoiner.toString();
                                                } catch (Exception e) {
                                                }
                                                return null;
                                            })
                                    )
                            ), ArrayList::new)
            );
        }

        /**
         * 去重(元素)
         */
        public List comparing() {
            return list.stream().distinct().collect(Collectors.toList());
        }

        /**
         * 正序(对象)
         */
        public List asc(JLambda.JFunction jlFunction) {
            Collections.sort(list, Comparator.comparing((JLambda.JFunction) jlFunction));
            return new ArrayList(list);
        }

        /**
         * 倒序(对象)
         */
        public List desc(JLambda.JFunction jlFunction) {
            Collections.sort(list, Comparator.comparing((JLambda.JFunction) jlFunction).reversed());
            return new ArrayList(list);
        }

        /**
         * 正序(元素)
         */
        public List asc() {
            Collections.sort((List) list);
            return new ArrayList(list);
        }

        /**
         * 倒序(元素)
         */
        public List desc() {
            Collections.sort(list, Collections.reverseOrder());
            return new ArrayList(list);
        }

        /**
         * 获取某个属性集合
         */
        public  List getProperty(JLambda.JFunction jlFunction) {
            return list.stream().map(jlFunction).collect(Collectors.toList());
        }

        /**
         * 洗牌
         */
        public List shuffle() {
            Collections.shuffle(list);
            return new ArrayList(list);
        }

        /**
         * 根据值获取下标 -1=不存在
         */
        public int getIndex(T t) {
            List targetList = Arrays.asList(t);
            int index = Collections.indexOfSubList(list, targetList);
            return index;
        }

        /**
         * 差集 List中有的但是List2中没有
         */
        public List diff(List list2) {
            list.removeAll(list2);
            return new ArrayList(list);
        }

        /**
         * 交集 List和List2中都有
         *
         * @param list2
         */
        public List section(List list2) {
            list.retainAll(list2);
            return new ArrayList(list);
        }

        /**
         * 按指定长度分隔为N个集合
         *
         * @param size 长度
         * @return
         */
        public List> partition(int size) {
            return Lists.partition(list, size);
        }

        /**
         * 查询
         */
        public Filter filter() {
            return new Filter<>(list);
        }

        /**
         * 转换map操作类
         */
        public static class ToMapOper {
            private List list;
            private JLambda.JFunction jlFunction;

            public ToMapOper(List list) {
                this.list = list;
            }

            public ToMapOper(List list, JLambda.JFunction jlFunction) {
                this.list = list;
                this.jlFunction = jlFunction;
            }

            /**
             * 重复覆盖
             */
            public Map cover() {
                if (jlFunction == null) {
                    return (Map) list.stream().collect(Collectors.toMap(o -> o, Function.identity(), (key1, key2) -> key2));
                }
                String property = JLambda.getProperty(jlFunction);
                return list.stream().collect(Collectors.toMap(o -> {
                    try {
                        JTuple.Tuple3> tuple3 = JReflect.PropertyReflect.getProperty(o, property);
                        if (tuple3 == null) {
                            return null;
                        }
                        R value = (R) tuple3.getV2();
                        return value;
                    } catch (Exception e) {
                    }
                    return null;
                }, Function.identity(), (key1, key2) -> key2));
            }

            /**
             * 重复分组
             */
            public Map> group() {
                if (jlFunction == null) {
                    return (Map>) list.stream().collect(Collectors.groupingBy(o -> o));
                }
                String property = JLambda.getProperty(jlFunction);
                return list.stream().collect(Collectors.groupingBy(o -> {
                    try {
                        JTuple.Tuple3> tuple3 = JReflect.PropertyReflect.getProperty(o, property);
                        if (tuple3 == null) {
                            return null;
                        }
                        R value = (R) tuple3.getV2();
                        return value;
                    } catch (Exception e) {
                    }
                    return null;
                }));
            }
        }

        /**
         * list查询操作类
         *
         * @param 
         */
        public static class Filter {
            private List list;
            private final static String EQ = "=", LT = "<", GT = ">", LE = "<=", GE = ">=", NE = "!=", LIKE = "like", IN = "in", NOT_IN = "notIn", IS_NULL = "isNull", IS_NOT_NULL = "isNotNull";

            public Filter(List list) {
                this.list = list;
            }

            /**
             * 对象执行
             *
             * @param jlFunction
             * @param propertyValue
             * @param oper
             */
            private void exec(JLambda.JFunction jlFunction, Object propertyValue, String oper) {
                String property = JLambda.getProperty(jlFunction);
                list = list.stream().filter(ss -> {
                    try {
                        JTuple.Tuple3> tuple3 = JReflect.PropertyReflect.getProperty(ss, property);
                        if (tuple3 != null) {
                            return propertyValue instanceof List ? inAdnNotInComArithmetic(tuple3.getV2(), (List) propertyValue, oper) : comArithmetic(tuple3.getV2(), propertyValue, oper);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return false;
                }).collect(Collectors.toList());
            }

            /**
             * 元素执行
             *
             * @param value
             * @param oper
             */
            private void exec(Object value, String oper) {
                list = list.stream().filter(temp -> value instanceof List ? inAdnNotInComArithmetic(temp, (List) value, oper) : comArithmetic(temp, value, oper)).collect(Collectors.toList());
            }

            /**
             * =(对象)
             *
             * @return
             */
            public Filter eq(JLambda.JFunction jlFunction, Object value) {
                exec(jlFunction, value, EQ);
                return this;
            }

            /**
             * =(元素)
             *
             * @param value
             * @return
             */
            public Filter eq(T value) {
                exec(value, EQ);
                return this;
            }

            /**
             * <(对象)
             *
             * @return
             */
            public Filter lt(JLambda.JFunction jlFunction, Object value) {
                exec(jlFunction, value, LT);
                return this;
            }

            /**
             * <(元素)
             *
             * @param value
             * @return
             */
            public Filter lt(T value) {
                exec(value, LT);
                return this;
            }

            /**
             * >(对象)
             *
             * @return
             */
            public Filter gt(JLambda.JFunction jlFunction, Object value) {
                exec(jlFunction, value, GT);
                return this;
            }

            /**
             * >(元素)
             *
             * @param value
             * @return
             */
            public Filter gt(T value) {
                exec(value, GT);
                return this;
            }

            /**
             * <=(对象)
             *
             * @return
             */
            public Filter le(JLambda.JFunction jlFunction, Object value) {
                exec(jlFunction, value, LE);
                return this;
            }

            /**
             * <=(元素)
             *
             * @param value
             * @return
             */
            public Filter le(T value) {
                exec(value, LE);
                return this;
            }

            /**
             * >=(对象)
             *
             * @return
             */
            public Filter ge(JLambda.JFunction jlFunction, Object value) {
                exec(jlFunction, value, GE);
                return this;
            }

            /**
             * >=(元素)
             *
             * @param value
             * @return
             */
            public Filter ge(T value) {
                exec(value, GE);
                return this;
            }

            /**
             * !=(对象)
             *
             * @return
             */
            public Filter ne(JLambda.JFunction jlFunction, Object value) {
                exec(jlFunction, value, NE);
                return this;
            }

            /**
             * !=(元素)
             *
             * @param value
             * @return
             */
            public Filter ne(T value) {
                exec(value, NE);
                return this;
            }

            /**
             * like(对象)
             *
             * @return
             */
            public Filter like(JLambda.JFunction jlFunction, Object value) {
                exec(jlFunction, value, LIKE);
                return this;
            }

            /**
             * like(元素)
             *
             * @param value
             * @return
             */
            public Filter like(T value) {
                exec(value, NE);
                return this;
            }

            /**
             * in(对象)
             *
             * @return
             */
            public  Filter in(JLambda.JFunction jlFunction, List value) {
                exec(jlFunction, value, IN);
                return this;
            }

            /**
             * in(元素)
             *
             * @param value
             * @return
             */
            public Filter in(List value) {
                exec(value, IN);
                return this;
            }

            /**
             * notin(对象)
             *
             * @return
             */
            public  Filter notIn(JLambda.JFunction jlFunction, List value) {
                exec(jlFunction, value, NOT_IN);
                return this;
            }

            /**
             * notin(元素)
             *
             * @param value
             * @return
             */
            public Filter notIn(List value) {
                exec(value, NOT_IN);
                return this;
            }

            /**
             * isNull(对象)
             *
             * @return
             */
            public  Filter isNull(JLambda.JFunction jlFunction) {
                exec(jlFunction, null, IS_NULL);
                return this;
            }

            /**
             * isNotNull(对象)
             *
             * @return
             */
            public  Filter isNotNull(JLambda.JFunction jlFunction) {
                exec(jlFunction, null, IS_NOT_NULL);
                return this;
            }

            /**
             * 获取list
             *
             * @return
             */
            public List list() {
                return new ArrayList(list);
            }

            /**
             * 获取对象
             *
             * @return
             */
            public T object() {
                if (list.size() > 0) {
                    return new ArrayList(list).get(0);
                }
                return null;
            }

            /**
             * 获取map(对象)
             *
             * @return
             */
            public  ToMapOper map(JLambda.JFunction jlFunction) {
                return new ToMapOper<>(list, jlFunction);
            }

            /**
             * 转map(元素)
             */
            public ToMapOper map() {
                return new ToMapOper<>(list);
            }

            private boolean inAdnNotInComArithmetic(Object value, List list, String comArithmetic) {
                boolean contains = list.contains(value);
                if (contains) {
                    return comArithmetic.equals(IN) ? true : false;
                } else {
                    return comArithmetic.equals(IN) ? false : true;
                }
            }

            private boolean comArithmetic(Object value, Object propertyValue, String comArithmetic) {
                if (value != null) {
                    if (comArithmetic.equals(IS_NOT_NULL)) {
                        return true;
                    }
                    if (value instanceof String) {
                        boolean fal = comArithmetic.equals(EQ) ? value.toString().equals(propertyValue.toString())
                                : comArithmetic.equals(LIKE) ? value.toString().indexOf(propertyValue.toString()) != -1
                                : false;
                        if (!fal) {
                            try {
                                int values = Integer.parseInt(value.toString());
                                int propertyValues = Integer.parseInt(propertyValue.toString());
                                fal = comArithmetic.equals(LT) ? values < propertyValues
                                        : (comArithmetic.equals(GT)) ? values > propertyValues
                                        : (comArithmetic.equals(LE)) ? values <= propertyValues
                                        : (comArithmetic.equals(GE)) ? values >= propertyValues
                                        : (comArithmetic.equals(NE)) ? values != propertyValues
                                        : false;
                            } catch (Exception e) {
                            }
                        }
                        return fal;
                    } else if (value instanceof Integer) {
                        int values = Integer.parseInt(value.toString());
                        int propertyValues = Integer.parseInt(propertyValue.toString());
                        boolean fal = comArithmetic.equals(EQ) ? values == propertyValues
                                : (comArithmetic.equals(LT)) ? values < propertyValues
                                : (comArithmetic.equals(GT)) ? values > propertyValues
                                : (comArithmetic.equals(LE)) ? values <= propertyValues
                                : (comArithmetic.equals(GE)) ? values >= propertyValues
                                : (comArithmetic.equals(NE)) ? values != propertyValues
                                : false;
                        return fal;
                    } else if (value instanceof BigDecimal) {
                        BigDecimal values = new BigDecimal(value.toString());
                        BigDecimal propertyValues = new BigDecimal(propertyValue.toString());
                        boolean fal = comArithmetic.equals(EQ) ? values.compareTo(propertyValues) == 0
                                : (comArithmetic.equals(LT)) ? values.compareTo(propertyValues) == -1
                                : (comArithmetic.equals(GT)) ? values.compareTo(propertyValues) == 1
                                : (comArithmetic.equals(LE)) ? values.compareTo(propertyValues) < 1
                                : (comArithmetic.equals(GE)) ? values.compareTo(propertyValues) > -1
                                : (comArithmetic.equals(NE)) ? values.compareTo(propertyValues) != 0
                                : false;
                        return fal;
                    } else {
                        //其他统一当成对象处理
                        boolean fal = comArithmetic.equals(EQ) ? value.equals(propertyValue) ? true : false
                                : comArithmetic.equals(NE) ? !value.equals(propertyValue) ? true : false
                                : false;
                        return fal;
                    }
                } else {
                    return comArithmetic.equals(IS_NULL) ? true : false;
                }
            }
        }
    }

    /**
     * 数组操作类
     */
    public static class ArrayOper {
        private T[] array;

        public ArrayOper(T[] array) {
            this.array = array;
        }

        /**
         * 转list
         */
        public List toList() {
            List list = new ArrayList(array.length);
            Collections.addAll(list, array);
            return list;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy