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

com.github.datalking.util.CollectionUtils Maven / Gradle / Ivy

The newest version!
package com.github.datalking.util;

import com.github.datalking.common.MultiValueMap;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

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

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

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

    public static List arrayToList(Object source) {
        return Arrays.asList(ObjectUtils.toObjectArray(source));
    }


    public static void mergeArrayIntoCollection(Object array, Collection collection) {
        if (collection == null) {
            throw new IllegalArgumentException("Collection must not be null");
        }
        Object[] arr = ObjectUtils.toObjectArray(array);
        for (Object elem : arr) {
            collection.add(elem);
        }
    }

    public static void mergePropertiesIntoMap(Properties props, Map map) {
        if (map == null) {
            throw new IllegalArgumentException("Map must not be null");
        }
        if (props != null) {
            for (Enumeration en = props.propertyNames(); en.hasMoreElements(); ) {
                String key = (String) en.nextElement();
                Object value = props.getProperty(key);
                if (value == null) {
                    // Potentially a non-String value...
                    value = props.get(key);
                }
                map.put(key, value);
            }
        }
    }


    public static boolean contains(Iterator iterator, Object element) {
        if (iterator != null) {
            while (iterator.hasNext()) {
                Object candidate = iterator.next();
                if (ObjectUtils.nullSafeEquals(candidate, element)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean contains(Enumeration enumeration, Object element) {
        if (enumeration != null) {
            while (enumeration.hasMoreElements()) {
                Object candidate = enumeration.nextElement();
                if (ObjectUtils.nullSafeEquals(candidate, element)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean containsInstance(Collection collection, Object element) {
        if (collection != null) {
            for (Object candidate : collection) {
                if (candidate == element) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean containsAny(Collection source, Collection candidates) {
        if (isEmpty(source) || isEmpty(candidates)) {
            return false;
        }
        for (Object candidate : candidates) {
            if (source.contains(candidate)) {
                return true;
            }
        }
        return false;
    }

    public static Object findFirstMatch(Collection source, Collection candidates) {
        if (isEmpty(source) || isEmpty(candidates)) {
            return null;
        }
        for (Object candidate : candidates) {
            if (source.contains(candidate)) {
                return candidate;
            }
        }
        return null;
    }

    public static  T findValueOfType(Collection collection, Class type) {
        if (isEmpty(collection)) {
            return null;
        }
        T value = null;
        for (Object element : collection) {
            if (type == null || type.isInstance(element)) {
                if (value != null) {
                    // More than one value found... no clear single value.
                    return null;
                }
                value = (T) element;
            }
        }
        return value;
    }

    public static Object findValueOfType(Collection collection, Class[] types) {
        if (isEmpty(collection) || ObjectUtils.isEmpty(types)) {
            return null;
        }
        for (Class type : types) {
            Object value = findValueOfType(collection, type);
            if (value != null) {
                return value;
            }
        }
        return null;
    }


    public static boolean hasUniqueObject(Collection collection) {
        if (isEmpty(collection)) {
            return false;
        }
        boolean hasCandidate = false;
        Object candidate = null;
        for (Object elem : collection) {
            if (!hasCandidate) {
                hasCandidate = true;
                candidate = elem;
            } else if (candidate != elem) {
                return false;
            }
        }
        return true;
    }

    public static Class findCommonElementType(Collection collection) {
        if (isEmpty(collection)) {
            return null;
        }
        Class candidate = null;
        for (Object val : collection) {
            if (val != null) {
                if (candidate == null) {
                    candidate = val.getClass();
                } else if (candidate != val.getClass()) {
                    return null;
                }
            }
        }
        return candidate;
    }

    public static  A[] toArray(Enumeration enumeration, A[] array) {
        ArrayList elements = new ArrayList();
        while (enumeration.hasMoreElements()) {
            elements.add(enumeration.nextElement());
        }
        return elements.toArray(array);
    }


    public static  Iterator toIterator(Enumeration enumeration) {
        return new EnumerationIterator<>(enumeration);
    }

    public static  MultiValueMap unmodifiableMultiValueMap(MultiValueMap map) {
        Assert.notNull(map, "'map' must not be null");
        Map> result = new LinkedHashMap>(map.size());
        for (Map.Entry> entry : map.entrySet()) {
            List values = Collections.unmodifiableList(entry.getValue());
            result.put(entry.getKey(), values);
        }
        Map> unmodifiableMap = Collections.unmodifiableMap(result);
        return toMultiValueMap(unmodifiableMap);
    }

    public static  MultiValueMap toMultiValueMap(Map> map) {
        return new MultiValueMapAdapter(map);

    }



    private static class EnumerationIterator implements Iterator {

        private final Enumeration enumeration;

        public EnumerationIterator(Enumeration enumeration) {
            this.enumeration = enumeration;
        }

        public boolean hasNext() {
            return this.enumeration.hasMoreElements();
        }

        public E next() {
            return this.enumeration.nextElement();
        }

        public void remove() throws UnsupportedOperationException {
            throw new UnsupportedOperationException("Not supported");
        }
    }

    private static class MultiValueMapAdapter implements MultiValueMap, Serializable {

        private final Map> map;

        public MultiValueMapAdapter(Map> map) {
            Assert.notNull(map, "'map' must not be null");
            this.map = map;
        }

        public void add(K key, V value) {
            List values = this.map.get(key);
            if (values == null) {
                values = new LinkedList();
                this.map.put(key, values);
            }
            values.add(value);
        }

        public V getFirst(K key) {
            List values = this.map.get(key);
            return (values != null ? values.get(0) : null);
        }

        public void set(K key, V value) {
            List values = new LinkedList();
            values.add(value);
            this.map.put(key, values);
        }

        public void setAll(Map values) {
            for (Entry entry : values.entrySet()) {
                set(entry.getKey(), entry.getValue());
            }
        }

        public Map toSingleValueMap() {
            LinkedHashMap singleValueMap = new LinkedHashMap(this.map.size());
            for (Entry> entry : map.entrySet()) {
                singleValueMap.put(entry.getKey(), entry.getValue().get(0));
            }
            return singleValueMap;
        }

        public int size() {
            return this.map.size();
        }

        public boolean isEmpty() {
            return this.map.isEmpty();
        }

        public boolean containsKey(Object key) {
            return this.map.containsKey(key);
        }

        public boolean containsValue(Object value) {
            return this.map.containsValue(value);
        }

        public List get(Object key) {
            return this.map.get(key);
        }

        public List put(K key, List value) {
            return this.map.put(key, value);
        }

        public List remove(Object key) {
            return this.map.remove(key);
        }

        public void putAll(Map> map) {
            this.map.putAll(map);
        }

        public void clear() {
            this.map.clear();
        }

        public Set keySet() {
            return this.map.keySet();
        }

        public Collection> values() {
            return this.map.values();
        }

        public Set>> entrySet() {
            return this.map.entrySet();
        }

        @Override
        public boolean equals(Object other) {
            if (this == other) {
                return true;
            }
            return map.equals(other);
        }

        @Override
        public int hashCode() {
            return this.map.hashCode();
        }

        @Override
        public String toString() {
            return this.map.toString();
        }
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy