wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.vci.common.util;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
 
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 void mergePropertiesIntoMap(Properties props, Map<String, Object> 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)
                    value = props.get(key);
                map.put(key, value);
            }
    }
 
    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> T findValueOfType(Collection<?> collection, Class<T> type) {
        if (isEmpty(collection))
            return null;
        T value = null;
        for (Object element : collection) {
            if (type == null || type.isInstance(element)) {
                if (value != null)
                    return null;
                value = (T) element;
            }
        }
        return value;
    }
 
    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;
                continue;
            }
            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();
                    continue;
                }
                if (candidate != val.getClass())
                    return null;
            }
        }
        return candidate;
    }
 
    public static <A, E extends A> A[] toArray(Enumeration<E> enumeration, A[] array) {
        ArrayList<A> elements = new ArrayList<A>();
        while (enumeration.hasMoreElements())
            elements.add((A) enumeration.nextElement());
        return elements.toArray(array);
    }
 
    public static <E> Iterator<E> toIterator(Enumeration<E> enumeration) {
        return new EnumerationIterator<E>(enumeration);
    }
 
    private static class EnumerationIterator<E> implements Iterator<E> {
        private Enumeration<E> enumeration;
 
        public EnumerationIterator(Enumeration<E> 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");
        }
    }
 
}