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.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. @Deprecated
  37. public class CollectionUtils {
  38. @SuppressWarnings("rawtypes")
  39. @Deprecated
  40. public static final List EMPTY_LIST = Collections.EMPTY_LIST; //NOSONAR
  41. /**
  42. * Please use Vector.equals() or List.equals().
  43. * @param v1 the first vector.
  44. * @param v2 the second vector.
  45. * @return true if the vectors are equal.
  46. * @since Ant 1.5
  47. * @deprecated since 1.6.x.
  48. */
  49. @Deprecated
  50. public static boolean equals(Vector<?> v1, Vector<?> v2) {
  51. return Objects.equals(v1, v2);
  52. }
  53. /**
  54. * Dictionary does not have an equals.
  55. * Please use Map.equals().
  56. *
  57. * <p>Follows the equals contract of Java 2's Map.</p>
  58. * @param d1 the first directory.
  59. * @param d2 the second directory.
  60. * @return true if the directories are equal.
  61. * @since Ant 1.5
  62. * @deprecated since 1.6.x.
  63. */
  64. @Deprecated
  65. public static boolean equals(Dictionary<?, ?> d1, Dictionary<?, ?> d2) {
  66. if (d1 == d2) {
  67. return true;
  68. }
  69. if (d1 == null || d2 == null) {
  70. return false;
  71. }
  72. if (d1.size() != d2.size()) {
  73. return false;
  74. }
  75. // don't need the opposite check as the Dictionaries have the
  76. // same size, so we've also covered all keys of d2 already.
  77. return Collections.list(d1.keys()).stream()
  78. .allMatch(key -> d1.get(key).equals(d2.get(key)));
  79. }
  80. /**
  81. * Creates a comma separated list of all values held in the given
  82. * collection.
  83. *
  84. * @param c collection to transform
  85. * @return string representation of the collection
  86. * @since Ant 1.8.0
  87. * @deprecated use stream().collect(Collectors.joining(","))
  88. */
  89. @Deprecated
  90. public static String flattenToString(Collection<?> c) {
  91. return c.stream().map(String::valueOf).collect(Collectors.joining(","));
  92. }
  93. /**
  94. * Dictionary does not know the putAll method. Please use Map.putAll().
  95. * @param m1 the to directory.
  96. * @param m2 the from directory.
  97. * @param <K> type of the key
  98. * @param <V> type of the value
  99. * @since Ant 1.6
  100. * @deprecated since 1.6.x.
  101. */
  102. @Deprecated
  103. public static <K, V> void putAll(Dictionary<? super K, ? super V> m1,
  104. Dictionary<? extends K, ? extends V> m2) {
  105. Collections.list(m2.keys()).forEach(key -> m1.put(key, m2.get(key)));
  106. }
  107. /**
  108. * An empty enumeration.
  109. * @since Ant 1.6
  110. */
  111. @Deprecated
  112. public static final class EmptyEnumeration<E> implements Enumeration<E> {
  113. /**
  114. * @return false always.
  115. */
  116. @Override
  117. public boolean hasMoreElements() {
  118. return false;
  119. }
  120. /**
  121. * @return nothing.
  122. * @throws NoSuchElementException always.
  123. */
  124. @Override
  125. public E nextElement() throws NoSuchElementException {
  126. throw new NoSuchElementException();
  127. }
  128. }
  129. /**
  130. * Append one enumeration to another.
  131. * Elements are evaluated lazily.
  132. * @param e1 the first enumeration.
  133. * @param e2 the subsequent enumeration.
  134. * @param <E> element type
  135. * @return an enumeration representing e1 followed by e2.
  136. * @since Ant 1.6.3
  137. * @deprecated use Stream.concat(Collections.list(e1).stream(), Collections.list(e2).stream())
  138. * .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::enumeration))
  139. */
  140. @Deprecated
  141. public static <E> Enumeration<E> append(Enumeration<E> e1, Enumeration<E> e2) {
  142. return new CompoundEnumeration<>(e1, e2);
  143. }
  144. /**
  145. * Adapt the specified Iterator to the Enumeration interface.
  146. * @param iter the Iterator to adapt.
  147. * @param <E> element type
  148. * @return an Enumeration.
  149. * @deprecated use Collections.enumeration()
  150. */
  151. @Deprecated
  152. public static <E> Enumeration<E> asEnumeration(final Iterator<E> iter) {
  153. return new Enumeration<E>() {
  154. @Override
  155. public boolean hasMoreElements() {
  156. return iter.hasNext();
  157. }
  158. @Override
  159. public E nextElement() {
  160. return iter.next();
  161. }
  162. };
  163. }
  164. /**
  165. * Adapt the specified Enumeration to the Iterator interface.
  166. * @param e the Enumeration to adapt.
  167. * @param <E> element type
  168. * @return an Iterator.
  169. * @deprecated use Collections.list(e).iterator()
  170. */
  171. @Deprecated
  172. public static <E> Iterator<E> asIterator(final Enumeration<E> e) {
  173. return new Iterator<E>() {
  174. @Override
  175. public boolean hasNext() {
  176. return e.hasMoreElements();
  177. }
  178. @Override
  179. public E next() {
  180. return e.nextElement();
  181. }
  182. @Override
  183. public void remove() {
  184. throw new UnsupportedOperationException();
  185. }
  186. };
  187. }
  188. /**
  189. * Returns a collection containing all elements of the iterator.
  190. *
  191. * @param iter the Iterator to convert
  192. * @param <T> element type
  193. * @return the collection
  194. * @since Ant 1.8.0
  195. * @deprecated instantiate a list an use forEachRemaining(list::add)
  196. */
  197. @Deprecated
  198. public static <T> Collection<T> asCollection(final Iterator<? extends T> iter) {
  199. List<T> l = new ArrayList<>();
  200. iter.forEachRemaining(l::add);
  201. return l;
  202. }
  203. private static final class CompoundEnumeration<E> implements Enumeration<E> {
  204. private final Enumeration<E> e1, e2;
  205. public CompoundEnumeration(Enumeration<E> e1, Enumeration<E> e2) {
  206. this.e1 = e1;
  207. this.e2 = e2;
  208. }
  209. @Override
  210. public boolean hasMoreElements() {
  211. return e1.hasMoreElements() || e2.hasMoreElements();
  212. }
  213. @Override
  214. public E nextElement() throws NoSuchElementException {
  215. if (e1.hasMoreElements()) {
  216. return e1.nextElement();
  217. }
  218. return e2.nextElement();
  219. }
  220. }
  221. /**
  222. * Counts how often the given Object occurs in the given
  223. * collection using equals() for comparison.
  224. *
  225. * @param c collection in which to search
  226. * @param o object to search
  227. * @return frequency
  228. * @since Ant 1.8.0
  229. */
  230. @Deprecated
  231. public static int frequency(Collection<?> c, Object o) {
  232. return c == null ? 0 : Collections.frequency(c, o);
  233. }
  234. private CollectionUtils() {
  235. }
  236. }