一、前言
关于apache ant的ant-tools源码包org.apache.tools.ant.util.CollectionUtils集合工具类,对集合是否相等equals、格式化为字符串flattenToString及合并putAll等操作。
二、源码说明
package org.apache.tools.ant.util;@b@@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.Collections;@b@import java.util.Dictionary;@b@import java.util.Enumeration;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.NoSuchElementException;@b@import java.util.Objects;@b@import java.util.Vector;@b@import java.util.stream.Collectors;@b@@b@@b@public class CollectionUtils {@b@@b@ @SuppressWarnings("rawtypes")@b@ @Deprecated@b@ public static final List EMPTY_LIST = Collections.EMPTY_LIST; //NOSONAR@b@@b@ @b@ @Deprecated@b@ public static boolean equals(Vector<?> v1, Vector<?> v2) {@b@ return Objects.equals(v1, v2);@b@ }@b@@b@ @b@ @Deprecated@b@ public static boolean equals(Dictionary<?, ?> d1, Dictionary<?, ?> d2) {@b@ if (d1 == d2) {@b@ return true;@b@ }@b@@b@ if (d1 == null || d2 == null) {@b@ return false;@b@ }@b@@b@ if (d1.size() != d2.size()) {@b@ return false;@b@ }@b@@b@ Enumeration<?> e1 = d1.keys();@b@ while (e1.hasMoreElements()) {@b@ Object key = e1.nextElement();@b@ Object value1 = d1.get(key);@b@ Object value2 = d2.get(key);@b@ if (value2 == null || !value1.equals(value2)) {@b@ return false;@b@ }@b@ }@b@@b@ return true;@b@ }@b@@b@ @b@ public static String flattenToString(Collection<?> c) {@b@ return c.stream().map(String::valueOf).collect(Collectors.joining(","));@b@ }@b@@b@ @b@ @Deprecated@b@ public static <K, V> void putAll(Dictionary<? super K, ? super V> m1,@b@ Dictionary<? extends K, ? extends V> m2) {@b@ for (Enumeration<? extends K> it = m2.keys(); it.hasMoreElements();) {@b@ K key = it.nextElement();@b@ m1.put(key, m2.get(key));@b@ }@b@ }@b@@b@ @b@ @Deprecated@b@ public static final class EmptyEnumeration<E> implements Enumeration<E> {@b@@b@ @b@ @Override@b@ public boolean hasMoreElements() {@b@ return false;@b@ }@b@@b@ @b@ @Override@b@ public E nextElement() throws NoSuchElementException {@b@ throw new NoSuchElementException();@b@ }@b@ }@b@@b@ @b@ public static <E> Enumeration<E> append(Enumeration<E> e1, Enumeration<E> e2) {@b@ return new CompoundEnumeration<>(e1, e2);@b@ }@b@@b@ @b@ public static <E> Enumeration<E> asEnumeration(final Iterator<E> iter) {@b@ return new Enumeration<E>() {@b@ @Override@b@ public boolean hasMoreElements() {@b@ return iter.hasNext();@b@ }@b@ @Override@b@ public E nextElement() {@b@ return iter.next();@b@ }@b@ };@b@ }@b@@b@ @b@ public static <E> Iterator<E> asIterator(final Enumeration<E> e) {@b@ return new Iterator<E>() {@b@ @Override@b@ public boolean hasNext() {@b@ return e.hasMoreElements();@b@ }@b@ @Override@b@ public E next() {@b@ return e.nextElement();@b@ }@b@ @Override@b@ public void remove() {@b@ throw new UnsupportedOperationException();@b@ }@b@ };@b@ }@b@@b@ @b@ public static <T> Collection<T> asCollection(final Iterator<? extends T> iter) {@b@ List<T> l = new ArrayList<>();@b@ iter.forEachRemaining(l::add);@b@ return l;@b@ }@b@@b@ private static final class CompoundEnumeration<E> implements Enumeration<E> {@b@@b@ private final Enumeration<E> e1, e2;@b@@b@ public CompoundEnumeration(Enumeration<E> e1, Enumeration<E> e2) {@b@ this.e1 = e1;@b@ this.e2 = e2;@b@ }@b@@b@ @Override@b@ public boolean hasMoreElements() {@b@ return e1.hasMoreElements() || e2.hasMoreElements();@b@ }@b@@b@ @Override@b@ public E nextElement() throws NoSuchElementException {@b@ if (e1.hasMoreElements()) {@b@ return e1.nextElement();@b@ }@b@ return e2.nextElement();@b@ }@b@@b@ }@b@@b@ @b@ @Deprecated@b@ public static int frequency(Collection<?> c, Object o) {@b@ return c == null ? 0 : Collections.frequency(c, o);@b@ }@b@@b@ private CollectionUtils() {@b@ }@b@}