PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Java.NET/JavApi Commons collections (Apache Port)/org.apache.commons.collections.ListUtils.cs

https://github.com/gadfly/nofs
C# | 431 lines | 142 code | 32 blank | 257 comment | 26 complexity | 077f2369d9c1c4588a7e42fee3c886ba MD5 | raw file
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. */
  15. using System;
  16. using java = biz.ritter.javapi;
  17. using org.apache.commons.collections.list;
  18. namespace org.apache.commons.collections
  19. {
  20. /**
  21. * Provides utility methods and decorators for {@link List} instances.
  22. *
  23. * @since Commons Collections 1.0
  24. * @version $Revision$ $Date$
  25. *
  26. * @author Federico Barbieri
  27. * @author Peter Donald
  28. * @author Paul Jack
  29. * @author Stephen Colebourne
  30. * @author Neil O'Toole
  31. * @author Matthew Hawthorne
  32. */
  33. public class ListUtils
  34. {
  35. /**
  36. * An empty unmodifiable list.
  37. * This uses the {@link Collections Collections} implementation
  38. * and is provided for completeness.
  39. */
  40. public static readonly java.util.List<Object> EMPTY_LIST = java.util.Collections.EMPTY_LIST;
  41. /**
  42. * <code>ListUtils</code> should not normally be instantiated.
  43. */
  44. public ListUtils()
  45. {
  46. }
  47. //-----------------------------------------------------------------------
  48. /**
  49. * Returns a new list containing all elements that are contained in
  50. * both given lists.
  51. *
  52. * @param list1 the first list
  53. * @param list2 the second list
  54. * @return the intersection of those two lists
  55. * @throws NullPointerException if either list is null
  56. */
  57. public static java.util.List<Object> intersection(java.util.List<Object> list1, java.util.List<Object> list2)
  58. {
  59. java.util.ArrayList<Object> result = new java.util.ArrayList<Object>();
  60. java.util.Iterator<Object> iterator = list2.iterator();
  61. while (iterator.hasNext())
  62. {
  63. Object o = iterator.next();
  64. if (list1.contains(o))
  65. {
  66. result.add(o);
  67. }
  68. }
  69. return result;
  70. }
  71. /**
  72. * Subtracts all elements in the second list from the first list,
  73. * placing the results in a new list.
  74. * <p>
  75. * This differs from {@link List#removeAll(Collection)} in that
  76. * cardinality is respected; if <Code>list1</Code> contains two
  77. * occurrences of <Code>null</Code> and <Code>list2</Code> only
  78. * contains one occurrence, then the returned list will still contain
  79. * one occurrence.
  80. *
  81. * @param list1 the list to subtract from
  82. * @param list2 the list to subtract
  83. * @return a new list containing the results
  84. * @throws NullPointerException if either list is null
  85. */
  86. public static java.util.List<Object> subtract(java.util.List<Object> list1, java.util.List<Object> list2)
  87. {
  88. java.util.ArrayList<Object> result = new java.util.ArrayList<Object>(list1);
  89. java.util.Iterator<Object> iterator = list2.iterator();
  90. while (iterator.hasNext())
  91. {
  92. result.remove(iterator.next());
  93. }
  94. return result;
  95. }
  96. /**
  97. * Returns the sum of the given lists. This is their intersection
  98. * subtracted from their union.
  99. *
  100. * @param list1 the first list
  101. * @param list2 the second list
  102. * @return a new list containing the sum of those lists
  103. * @throws NullPointerException if either list is null
  104. */
  105. public static java.util.List<Object> sum(java.util.List<Object> list1, java.util.List<Object> list2)
  106. {
  107. return subtract(union(list1, list2), intersection(list1, list2));
  108. }
  109. /**
  110. * Returns a new list containing the second list appended to the
  111. * first list. The {@link List#addAll(Collection)} operation is
  112. * used to append the two given lists into a new list.
  113. *
  114. * @param list1 the first list
  115. * @param list2 the second list
  116. * @return a new list containing the union of those lists
  117. * @throws NullPointerException if either list is null
  118. */
  119. public static java.util.List<Object> union(java.util.List<Object> list1, java.util.List<Object> list2)
  120. {
  121. java.util.ArrayList<Object> result = new java.util.ArrayList<Object>(list1);
  122. result.addAll(list2);
  123. return result;
  124. }
  125. /**
  126. * Tests two lists for value-equality as per the equality contract in
  127. * {@link java.util.List#equals(java.lang.Object)}.
  128. * <p>
  129. * This method is useful for implementing <code>List</code> when you cannot
  130. * extend AbstractList. The method takes Collection instances to enable other
  131. * collection types to use the List implementation algorithm.
  132. * <p>
  133. * The relevant text (slightly paraphrased as this is a static method) is:
  134. * <blockquote>
  135. * Compares the two list objects for equality. Returns
  136. * <tt>true</tt> if and only if both
  137. * lists have the same size, and all corresponding pairs of elements in
  138. * the two lists are <i>equal</i>. (Two elements <tt>e1</tt> and
  139. * <tt>e2</tt> are <i>equal</i> if <tt>(e1==null ? e2==null :
  140. * e1.equals(e2))</tt>.) In other words, two lists are defined to be
  141. * equal if they contain the same elements in the same order. This
  142. * definition ensures that the equals method works properly across
  143. * different implementations of the <tt>List</tt> interface.
  144. * </blockquote>
  145. *
  146. * <b>Note:</b> The behaviour of this method is undefined if the lists are
  147. * modified during the equals comparison.
  148. *
  149. * @see java.util.List
  150. * @param list1 the first list, may be null
  151. * @param list2 the second list, may be null
  152. * @return whether the lists are equal by value comparison
  153. */
  154. public static bool isEqualList(java.util.Collection<Object> list1, java.util.Collection<Object> list2)
  155. {
  156. if (list1 == list2)
  157. {
  158. return true;
  159. }
  160. if (list1 == null || list2 == null || list1.size() != list2.size())
  161. {
  162. return false;
  163. }
  164. java.util.Iterator<Object> it1 = list1.iterator();
  165. java.util.Iterator<Object> it2 = list2.iterator();
  166. Object obj1 = null;
  167. Object obj2 = null;
  168. while (it1.hasNext() && it2.hasNext())
  169. {
  170. obj1 = it1.next();
  171. obj2 = it2.next();
  172. if (!(obj1 == null ? obj2 == null : obj1.equals(obj2)))
  173. {
  174. return false;
  175. }
  176. }
  177. return !(it1.hasNext() || it2.hasNext());
  178. }
  179. /**
  180. * Generates a hash code using the algorithm specified in
  181. * {@link java.util.List#hashCode()}.
  182. * <p>
  183. * This method is useful for implementing <code>List</code> when you cannot
  184. * extend AbstractList. The method takes Collection instances to enable other
  185. * collection types to use the List implementation algorithm.
  186. *
  187. * @see java.util.List#hashCode()
  188. * @param list the list to generate the hashCode for, may be null
  189. * @return the hash code
  190. */
  191. public static int hashCodeForList(java.util.Collection<Object> list)
  192. {
  193. if (list == null)
  194. {
  195. return 0;
  196. }
  197. int hashCode = 1;
  198. java.util.Iterator<Object> it = list.iterator();
  199. Object obj = null;
  200. while (it.hasNext())
  201. {
  202. obj = it.next();
  203. hashCode = 31 * hashCode + (obj == null ? 0 : obj.GetHashCode());
  204. }
  205. return hashCode;
  206. }
  207. //-----------------------------------------------------------------------
  208. /**
  209. * Returns a List containing all the elements in <code>collection</code>
  210. * that are also in <code>retain</code>. The cardinality of an element <code>e</code>
  211. * in the returned list is the same as the cardinality of <code>e</code>
  212. * in <code>collection</code> unless <code>retain</code> does not contain <code>e</code>, in which
  213. * case the cardinality is zero. This method is useful if you do not wish to modify
  214. * the collection <code>c</code> and thus cannot call <code>collection.retainAll(retain);</code>.
  215. *
  216. * @param collection the collection whose contents are the target of the #retailAll operation
  217. * @param retain the collection containing the elements to be retained in the returned collection
  218. * @return a <code>List</code> containing all the elements of <code>c</code>
  219. * that occur at least once in <code>retain</code>.
  220. * @throws NullPointerException if either parameter is null
  221. * @since Commons Collections 3.2
  222. */
  223. public static java.util.List<Object> retainAll(java.util.Collection<Object> collection, java.util.Collection<Object> retain)
  224. {
  225. java.util.List<Object> list = new java.util.ArrayList<Object>(java.lang.Math.min(collection.size(), retain.size()));
  226. for (java.util.Iterator<Object> iter = collection.iterator(); iter.hasNext(); )
  227. {
  228. Object obj = iter.next();
  229. if (retain.contains(obj))
  230. {
  231. list.add(obj);
  232. }
  233. }
  234. return list;
  235. }
  236. /**
  237. * Removes the elements in <code>remove</code> from <code>collection</code>. That is, this
  238. * method returns a list containing all the elements in <code>c</code>
  239. * that are not in <code>remove</code>. The cardinality of an element <code>e</code>
  240. * in the returned collection is the same as the cardinality of <code>e</code>
  241. * in <code>collection</code> unless <code>remove</code> contains <code>e</code>, in which
  242. * case the cardinality is zero. This method is useful if you do not wish to modify
  243. * <code>collection</code> and thus cannot call <code>collection.removeAll(remove);</code>.
  244. *
  245. * @param collection the collection from which items are removed (in the returned collection)
  246. * @param remove the items to be removed from the returned <code>collection</code>
  247. * @return a <code>List</code> containing all the elements of <code>c</code> except
  248. * any elements that also occur in <code>remove</code>.
  249. * @throws NullPointerException if either parameter is null
  250. * @since Commons Collections 3.2
  251. */
  252. public static java.util.List<Object> removeAll(java.util.Collection<Object> collection, java.util.Collection<Object> remove)
  253. {
  254. java.util.List<Object> list = new java.util.ArrayList<Object>();
  255. for (java.util.Iterator<Object> iter = collection.iterator(); iter.hasNext(); )
  256. {
  257. Object obj = iter.next();
  258. if (remove.contains(obj) == false)
  259. {
  260. list.add(obj);
  261. }
  262. }
  263. return list;
  264. }
  265. //-----------------------------------------------------------------------
  266. /**
  267. * Returns a synchronized list backed by the given list.
  268. * <p>
  269. * You must manually synchronize on the returned buffer's iterator to
  270. * avoid non-deterministic behavior:
  271. *
  272. * <pre>
  273. * List list = ListUtils.synchronizedList(myList);
  274. * synchronized (list) {
  275. * Iterator i = list.iterator();
  276. * while (i.hasNext()) {
  277. * process (i.next());
  278. * }
  279. * }
  280. * </pre>
  281. *
  282. * This method uses the implementation in the decorators subpackage.
  283. *
  284. * @param list the list to synchronize, must not be null
  285. * @return a synchronized list backed by the given list
  286. * @throws IllegalArgumentException if the list is null
  287. */
  288. public static java.util.List<Object> synchronizedList(java.util.List<Object> list)
  289. {
  290. return SynchronizedList.decorate(list);
  291. }
  292. /**
  293. * Returns an unmodifiable list backed by the given list.
  294. * <p>
  295. * This method uses the implementation in the decorators subpackage.
  296. *
  297. * @param list the list to make unmodifiable, must not be null
  298. * @return an unmodifiable list backed by the given list
  299. * @throws IllegalArgumentException if the list is null
  300. */
  301. public static java.util.List<Object> unmodifiableList(java.util.List<Object> list)
  302. {
  303. return UnmodifiableList.decorate(list);
  304. }
  305. /**
  306. * Returns a predicated (validating) list backed by the given list.
  307. * <p>
  308. * Only objects that pass the test in the given predicate can be added to the list.
  309. * Trying to add an invalid object results in an IllegalArgumentException.
  310. * It is important not to use the original list after invoking this method,
  311. * as it is a backdoor for adding invalid objects.
  312. *
  313. * @param list the list to predicate, must not be null
  314. * @param predicate the predicate for the list, must not be null
  315. * @return a predicated list backed by the given list
  316. * @throws IllegalArgumentException if the List or Predicate is null
  317. */
  318. public static java.util.List<Object> predicatedList(java.util.List<Object> list, Predicate predicate)
  319. {
  320. return PredicatedList.decorate(list, predicate);
  321. }
  322. /**
  323. * Returns a typed list backed by the given list.
  324. * <p>
  325. * Only objects of the specified type can be added to the list.
  326. *
  327. * @param list the list to limit to a specific type, must not be null
  328. * @param type the type of objects which may be added to the list
  329. * @return a typed list backed by the specified list
  330. */
  331. public static java.util.List<Object> typedList(java.util.List<Object> list, java.lang.Class type)
  332. {
  333. return TypedList.decorate(list, type);
  334. }
  335. /**
  336. * Returns a transformed list backed by the given list.
  337. * <p>
  338. * Each object is passed through the transformer as it is added to the
  339. * List. It is important not to use the original list after invoking this
  340. * method, as it is a backdoor for adding untransformed objects.
  341. *
  342. * @param list the list to predicate, must not be null
  343. * @param transformer the transformer for the list, must not be null
  344. * @return a transformed list backed by the given list
  345. * @throws IllegalArgumentException if the List or Transformer is null
  346. */
  347. public static java.util.List<Object> transformedList(java.util.List<Object> list, Transformer transformer)
  348. {
  349. return TransformedList.decorate(list, transformer);
  350. }
  351. /**
  352. * Returns a "lazy" list whose elements will be created on demand.
  353. * <p>
  354. * When the index passed to the returned list's {@link List#get(int) get}
  355. * method is greater than the list's size, then the factory will be used
  356. * to create a new object and that object will be inserted at that index.
  357. * <p>
  358. * For instance:
  359. *
  360. * <pre>
  361. * Factory factory = new Factory() {
  362. * public Object create() {
  363. * return new Date();
  364. * }
  365. * }
  366. * List lazy = ListUtils.lazyList(new ArrayList(), factory);
  367. * Object obj = lazy.get(3);
  368. * </pre>
  369. *
  370. * After the above code is executed, <code>obj</code> will contain
  371. * a new <code>Date</code> instance. Furthermore, that <code>Date</code>
  372. * instance is the fourth element in the list. The first, second,
  373. * and third element are all set to <code>null</code>.
  374. *
  375. * @param list the list to make lazy, must not be null
  376. * @param factory the factory for creating new objects, must not be null
  377. * @return a lazy list backed by the given list
  378. * @throws IllegalArgumentException if the List or Factory is null
  379. */
  380. public static java.util.List<Object> lazyList(java.util.List<Object> list, Factory factory)
  381. {
  382. return LazyList.decorate(list, factory);
  383. }
  384. /**
  385. * Returns a fixed-sized list backed by the given list.
  386. * Elements may not be added or removed from the returned list, but
  387. * existing elements can be changed (for instance, via the
  388. * {@link List#set(int,Object)} method).
  389. *
  390. * @param list the list whose size to fix, must not be null
  391. * @return a fixed-size list backed by that list
  392. * @throws IllegalArgumentException if the List is null
  393. */
  394. public static java.util.List<Object> fixedSizeList(java.util.List<Object> list)
  395. {
  396. return FixedSizeList.decorate(list);
  397. }
  398. }
  399. }