You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

CollectionUtils.java 7.7 kB

8 years ago
8 years ago
8 years ago
11 years ago
8 years ago
11 years ago
8 years ago
8 years ago
8 years ago
8 years ago
11 years ago
8 years ago
11 years ago
8 years ago
8 years ago
11 years ago
8 years ago
8 years ago
8 years ago
11 years ago
8 years ago
8 years ago
8 years ago
8 years ago
11 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant.util;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.Dictionary;
  23. import java.util.Enumeration;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import java.util.NoSuchElementException;
  27. import java.util.Objects;
  28. import java.util.Vector;
  29. import java.util.stream.Collectors;
  30. // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
  31. /**
  32. * A set of helper methods related to collection manipulation.
  33. *
  34. * @since Ant 1.5
  35. */
  36. public class CollectionUtils {
  37. @SuppressWarnings("rawtypes")
  38. @Deprecated
  39. public static final List EMPTY_LIST = Collections.EMPTY_LIST; //NOSONAR
  40. /**
  41. * Please use Vector.equals() or List.equals().
  42. * @param v1 the first vector.
  43. * @param v2 the second vector.
  44. * @return true if the vectors are equal.
  45. * @since Ant 1.5
  46. * @deprecated since 1.6.x.
  47. */
  48. @Deprecated
  49. public static boolean equals(Vector<?> v1, Vector<?> v2) {
  50. return Objects.equals(v1, v2);
  51. }
  52. /**
  53. * Dictionary does not have an equals.
  54. * Please use Map.equals().
  55. *
  56. * <p>Follows the equals contract of Java 2's Map.</p>
  57. * @param d1 the first directory.
  58. * @param d2 the second directory.
  59. * @return true if the directories are equal.
  60. * @since Ant 1.5
  61. * @deprecated since 1.6.x.
  62. */
  63. @Deprecated
  64. public static boolean equals(Dictionary<?, ?> d1, Dictionary<?, ?> d2) {
  65. if (d1 == d2) {
  66. return true;
  67. }
  68. if (d1 == null || d2 == null) {
  69. return false;
  70. }
  71. if (d1.size() != d2.size()) {
  72. return false;
  73. }
  74. Enumeration<?> e1 = d1.keys();
  75. while (e1.hasMoreElements()) {
  76. Object key = e1.nextElement();
  77. Object value1 = d1.get(key);
  78. Object value2 = d2.get(key);
  79. if (value2 == null || !value1.equals(value2)) {
  80. return false;
  81. }
  82. }
  83. // don't need the opposite check as the Dictionaries have the
  84. // same size, so we've also covered all keys of d2 already.
  85. return true;
  86. }
  87. /**
  88. * Creates a comma separated list of all values held in the given
  89. * collection.
  90. *
  91. * @param c collection to transform
  92. * @return string representation of the collection
  93. * @since Ant 1.8.0
  94. */
  95. public static String flattenToString(Collection<?> c) {
  96. return c.stream().map(String::valueOf).collect(Collectors.joining(","));
  97. }
  98. /**
  99. * Dictionary does not know the putAll method. Please use Map.putAll().
  100. * @param m1 the to directory.
  101. * @param m2 the from directory.
  102. * @param <K> type of the key
  103. * @param <V> type of the value
  104. * @since Ant 1.6
  105. * @deprecated since 1.6.x.
  106. */
  107. @Deprecated
  108. public static <K, V> void putAll(Dictionary<? super K, ? super V> m1,
  109. Dictionary<? extends K, ? extends V> m2) {
  110. for (Enumeration<? extends K> it = m2.keys(); it.hasMoreElements();) {
  111. K key = it.nextElement();
  112. m1.put(key, m2.get(key));
  113. }
  114. }
  115. /**
  116. * An empty enumeration.
  117. * @since Ant 1.6
  118. */
  119. @Deprecated
  120. public static final class EmptyEnumeration<E> implements Enumeration<E> {
  121. /**
  122. * @return false always.
  123. */
  124. @Override
  125. public boolean hasMoreElements() {
  126. return false;
  127. }
  128. /**
  129. * @return nothing.
  130. * @throws NoSuchElementException always.
  131. */
  132. @Override
  133. public E nextElement() throws NoSuchElementException {
  134. throw new NoSuchElementException();
  135. }
  136. }
  137. /**
  138. * Append one enumeration to another.
  139. * Elements are evaluated lazily.
  140. * @param e1 the first enumeration.
  141. * @param e2 the subsequent enumeration.
  142. * @param <E> element type
  143. * @return an enumeration representing e1 followed by e2.
  144. * @since Ant 1.6.3
  145. */
  146. public static <E> Enumeration<E> append(Enumeration<E> e1, Enumeration<E> e2) {
  147. return new CompoundEnumeration<>(e1, e2);
  148. }
  149. /**
  150. * Adapt the specified Iterator to the Enumeration interface.
  151. * @param iter the Iterator to adapt.
  152. * @param <E> element type
  153. * @return an Enumeration.
  154. */
  155. public static <E> Enumeration<E> asEnumeration(final Iterator<E> iter) {
  156. return new Enumeration<E>() {
  157. @Override
  158. public boolean hasMoreElements() {
  159. return iter.hasNext();
  160. }
  161. @Override
  162. public E nextElement() {
  163. return iter.next();
  164. }
  165. };
  166. }
  167. /**
  168. * Adapt the specified Enumeration to the Iterator interface.
  169. * @param e the Enumeration to adapt.
  170. * @param <E> element type
  171. * @return an Iterator.
  172. */
  173. public static <E> Iterator<E> asIterator(final Enumeration<E> e) {
  174. return new Iterator<E>() {
  175. @Override
  176. public boolean hasNext() {
  177. return e.hasMoreElements();
  178. }
  179. @Override
  180. public E next() {
  181. return e.nextElement();
  182. }
  183. @Override
  184. public void remove() {
  185. throw new UnsupportedOperationException();
  186. }
  187. };
  188. }
  189. /**
  190. * Returns a collection containing all elements of the iterator.
  191. *
  192. * @param iter the Iterator to convert
  193. * @param <T> element type
  194. * @return the collection
  195. * @since Ant 1.8.0
  196. */
  197. public static <T> Collection<T> asCollection(final Iterator<? extends T> iter) {
  198. List<T> l = new ArrayList<>();
  199. iter.forEachRemaining(l::add);
  200. return l;
  201. }
  202. private static final class CompoundEnumeration<E> implements Enumeration<E> {
  203. private final Enumeration<E> e1, e2;
  204. public CompoundEnumeration(Enumeration<E> e1, Enumeration<E> e2) {
  205. this.e1 = e1;
  206. this.e2 = e2;
  207. }
  208. @Override
  209. public boolean hasMoreElements() {
  210. return e1.hasMoreElements() || e2.hasMoreElements();
  211. }
  212. @Override
  213. public E nextElement() throws NoSuchElementException {
  214. if (e1.hasMoreElements()) {
  215. return e1.nextElement();
  216. }
  217. return e2.nextElement();
  218. }
  219. }
  220. /**
  221. * Counts how often the given Object occurs in the given
  222. * collection using equals() for comparison.
  223. *
  224. * @param c collection in which to search
  225. * @param o object to search
  226. * @return frequency
  227. * @since Ant 1.8.0
  228. */
  229. @Deprecated
  230. public static int frequency(Collection<?> c, Object o) {
  231. return c == null ? 0 : Collections.frequency(c, o);
  232. }
  233. private CollectionUtils() {
  234. }
  235. }