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

com.github.xphsc.util.CollectionUtil Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
package com.github.xphsc.util;



import org.apache.commons.collections4.IterableUtils;

import java.lang.reflect.Array;
import java.util.*;

/**
 * Created by ${huipei.x} on 2017-5-25.
 */
public class CollectionUtil {
    private CollectionUtil() {
    }


    @SafeVarargs
    public static  Collection union(Collection coll1, Collection coll2, Collection... otherColls) {
        Collection union = union(coll1, coll2);
        Collection[] arr$ = otherColls;
        int len$ = otherColls.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            Collection coll = arr$[i$];
            union = union(union, coll);
        }

        return union;
    }



    @SafeVarargs
    public static  Collection intersection(Collection coll1, Collection coll2, Collection... otherColls) {
        Collection intersection = intersection(coll1, coll2);
        if(isEmpty(intersection)) {
            return intersection;
        } else {
            Collection[] arr$ = otherColls;
            int len$ = otherColls.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                Collection coll = arr$[i$];
                intersection = intersection(intersection, coll);
                if(isEmpty(intersection)) {
                    return intersection;
                }
            }

            return intersection;
        }
    }



    public static boolean containsAny(Collection coll1, Collection coll2) {
        if(!isEmpty(coll1) && !isEmpty(coll2)) {
            Iterator i$;
            Object object;
            if(coll1.size() < coll2.size()) {
                i$ = coll1.iterator();

                while(i$.hasNext()) {
                    object = i$.next();
                    if(coll2.contains(object)) {
                        return true;
                    }
                }
            } else {
                i$ = coll2.iterator();

                while(i$.hasNext()) {
                    object = i$.next();
                    if(coll1.contains(object)) {
                        return true;
                    }
                }
            }

            return false;
        } else {
            return false;
        }
    }

    public static  Map countMap(Collection collection) {
        HashMap countMap = new HashMap();
        Iterator i$ = collection.iterator();

        while(i$.hasNext()) {
            Object t = i$.next();
            Integer count = (Integer)countMap.get(t);
            if(null == count) {
                countMap.put(t, Integer.valueOf(1));
            } else {
                countMap.put(t, Integer.valueOf(count.intValue() + 1));
            }
        }

        return countMap;
    }

    public static  String join(Iterable iterable, String conjunction) {
        return null == iterable?null:join(iterable.iterator(), conjunction);
    }

    public static  String join(Iterator iterator, String conjunction) {
        if(null == iterator) {
            return null;
        } else {
            StringBuilder sb = new StringBuilder();
            boolean isFirst = true;

            while(iterator.hasNext()) {
                if(isFirst) {
                    isFirst = false;
                } else {
                    sb.append(conjunction);
                }

                Object item = iterator.next();
                if(ArrayUtil.isArray(item)) {
                    sb.append(ArrayUtil.join(ArrayUtil.wrap(item), conjunction));
                } else if(item instanceof Iterable) {
                    sb.append(join((Iterable)item, conjunction));
                } else if(item instanceof Iterator) {
                    sb.append(join((Iterator)item, conjunction));
                } else {
                    sb.append(item);
                }
            }

            return sb.toString();
        }
    }



    public static  List popPart(Stack surplusAlaDatas, int partSize) {
        if(surplusAlaDatas != null && surplusAlaDatas.size() > 0) {
            ArrayList currentAlaDatas = new ArrayList();
            int size = surplusAlaDatas.size();
            int i;
            if(size > partSize) {
                for(i = 0; i < partSize; ++i) {
                    currentAlaDatas.add(surplusAlaDatas.pop());
                }
            } else {
                for(i = 0; i < size; ++i) {
                    currentAlaDatas.add(surplusAlaDatas.pop());
                }
            }

            return currentAlaDatas;
        } else {
            return null;
        }
    }

    public static  List popPart(Deque surplusAlaDatas, int partSize) {
        if(surplusAlaDatas != null && surplusAlaDatas.size() > 0) {
            ArrayList currentAlaDatas = new ArrayList();
            int size = surplusAlaDatas.size();
            int i;
            if(size > partSize) {
                for(i = 0; i < partSize; ++i) {
                    currentAlaDatas.add(surplusAlaDatas.pop());
                }
            } else {
                for(i = 0; i < size; ++i) {
                    currentAlaDatas.add(surplusAlaDatas.pop());
                }
            }

            return currentAlaDatas;
        } else {
            return null;
        }
    }

    public static  HashMap newHashMap() {
        return new HashMap();
    }

    public static  HashMap newHashMap(boolean isSorted) {
        return (HashMap)(isSorted?new LinkedHashMap():new HashMap());
    }

    public static  HashMap newHashMap(int size) {
        return new HashMap((int)((double)size / 0.75D));
    }

    @SafeVarargs
    public static  HashSet newHashSet(T... ts) {
        HashSet set = new HashSet(Math.max((int)((float)ts.length / 0.75F) + 1, 16));
        Object[] arr$ = ts;
        int len$ = ts.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            Object t = arr$[i$];
            set.add(t);
        }

        return set;
    }

    @SafeVarargs
    public static  HashSet newHashSet(boolean isSorted, T... ts) {
        int initialCapacity = Math.max((int)((float)ts.length / 0.75F) + 1, 16);
        Object set = isSorted?new LinkedHashSet(initialCapacity):new HashSet(initialCapacity);
        Object[] arr$ = ts;
        int len$ = ts.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            Object t = arr$[i$];
            ((HashSet)set).add(t);
        }

        return (HashSet)set;
    }

    public static  HashSet newHashSet(Collection collection) {
        return new HashSet(collection);
    }

    public static  HashSet newHashSet(boolean isSorted, Collection collection) {
        return (HashSet)(isSorted?new LinkedHashSet():new HashSet(collection));
    }

    @SafeVarargs
    public static  ArrayList newArrayList(T... values) {
        ArrayList arrayList = new ArrayList(values.length);
        Object[] arr$ = values;
        int len$ = values.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            Object t = arr$[i$];
            arrayList.add(t);
        }

        return arrayList;
    }

    public static  ArrayList newArrayList(Collection collection) {
        return new ArrayList(collection);
    }

    public static  ArrayList distinct(Collection collection) {
        return isEmpty(collection)?new ArrayList():(collection instanceof Set?new ArrayList(collection):new ArrayList(new LinkedHashSet(collection)));
    }

    public static  List sub(List list, int start, int end) {
        if(list != null && !list.isEmpty()) {
            if(start < 0) {
                start = 0;
            }

            if(end < 0) {
                end = 0;
            }

            int size;
            if(start > end) {
                size = start;
                start = end;
                end = size;
            }

            size = list.size();
            if(end > size) {
                if(start >= size) {
                    return null;
                }

                end = size;
            }

            return list.subList(start, end);
        } else {
            return null;
        }
    }

    public static  List sub(Collection list, int start, int end) {
        return list != null && !list.isEmpty()?sub((List)(new ArrayList(list)), start, end):null;
    }

    public static  List> split(Collection collection, int size) {
        ArrayList result = new ArrayList();
        ArrayList subList = new ArrayList(size);

        Object t;
        for(Iterator i$ = collection.iterator(); i$.hasNext(); subList.add(t)) {
            t = i$.next();
            if(subList.size() > size) {
                result.add(subList);
                subList = new ArrayList(size);
            }
        }

        result.add(subList);
        return result;
    }




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

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

    public static boolean isEmpty(Iterable iterable) {
        return null == iterable || isEmpty(iterable.iterator());
    }

    public static boolean isEmpty(Iterator Iterator) {
        return null == Iterator || !Iterator.hasNext();
    }

    public static boolean isEmpty(Enumeration enumeration) {
        return null == enumeration || !enumeration.hasMoreElements();
    }

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

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

    public static boolean isNotEmpty(Iterable iterable) {
        return null != iterable && isNotEmpty(iterable.iterator());
    }

    public static boolean isNotEmpty(Iterator Iterator) {
        return null != Iterator && Iterator.hasNext();
    }

    public static boolean isNotEmpty(Enumeration enumeration) {
        return null != enumeration && enumeration.hasMoreElements();
    }

    public static boolean sizeIsEmpty(Object object) {
        if(object == null) {
            return true;
        } else if(object instanceof Collection) {
            return ((Collection)object).isEmpty();
        } else if(object instanceof Iterable) {
            return IterableUtils.isEmpty((Iterable) object);
        } else if(object instanceof Map) {
            return ((Map)object).isEmpty();
        } else if(object instanceof Object[]) {
            return ((Object[])((Object[])object)).length == 0;
        } else if(object instanceof Iterator) {
            return !((Iterator)object).hasNext();
        } else if(object instanceof Enumeration) {
            return !((Enumeration)object).hasMoreElements();
        } else {
            try {
                return Array.getLength(object) == 0;
            } catch (IllegalArgumentException var2) {
                throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
            }
        }
    }


    public static  Map zip(Collection keys, Collection values) {
        if(!isEmpty(keys) && !isEmpty(values)) {
            ArrayList keyList = new ArrayList(keys);
            ArrayList valueList = new ArrayList(values);
            int size = Math.min(keys.size(), values.size());
            HashMap map = new HashMap((int)((double)size / 0.75D));

            for(int i = 0; i < size; ++i) {
                map.put(keyList.get(i), valueList.get(i));
            }

            return map;
        } else {
            return null;
        }
    }

    public static  HashMap toMap(Collection> entryCollection) {
        HashMap map = new HashMap();
        Iterator i$ = entryCollection.iterator();

        while(i$.hasNext()) {
            Map.Entry entry = (Map.Entry)i$.next();
            map.put(entry.getKey(), entry.getValue());
        }

        return map;
    }

    public static  TreeSet toTreeSet(Collection collection, Comparator comparator) {
        TreeSet treeSet = new TreeSet(comparator);
        Iterator i$ = collection.iterator();

        while(i$.hasNext()) {
            Object t = i$.next();
            treeSet.add(t);
        }

        return treeSet;
    }

    public static  List sort(Collection collection, Comparator comparator) {
        ArrayList list = new ArrayList(collection);
        Collections.sort(list, comparator);
        return list;
    }

    public static  Enumeration asEnumeration(final Iterator iter) {
        return new Enumeration() {
            public boolean hasMoreElements() {
                return iter.hasNext();
            }

            public E nextElement() {
                return iter.next();
            }
        };
    }

    public static  Iterator asIterator(final Enumeration e) {
        return new Iterator() {
            public boolean hasNext() {
                return e.hasMoreElements();
            }

            public E next() {
                return e.nextElement();
            }

            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

    public static  Collection addAll(Collection collection, Iterator iterator) {
        if(null != collection && null != iterator) {
            while(iterator.hasNext()) {
                collection.add(iterator.next());
            }
        }

        return collection;
    }

    public static  Collection addAll(Collection collection, Iterable iterable) {
        return addAll(collection, iterable.iterator());
    }

    public static  Collection addAll(Collection collection, Enumeration enumeration) {
        if(null != collection && null != enumeration) {
            while(enumeration.hasMoreElements()) {
                collection.add(enumeration.nextElement());
            }
        }

        return collection;
    }



    public static  T getFirst(Iterable iterable) {
        return null != iterable?getFirst(iterable.iterator()):null;
    }

    public static  T getFirst(Iterator iterator) {
        return null != iterator && iterator.hasNext()?iterator.next():null;
    }







    public interface KVConsumer {
        void accept(K var1, V var2, int var3);
    }

    public interface Consumer {
        void accept(T var1, int var2);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy