1. /*
  2. * @(#)Collections.java 1.66 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util;
  8. import java.io.Serializable;
  9. /**
  10. * This class consists exclusively of static methods that operate on or return
  11. * collections. It contains polymorphic algorithms that operate on
  12. * collections, "wrappers", which return a new collection backed by a
  13. * specified collection, and a few other odds and ends.
  14. *
  15. * <p>The methods of this class all throw a <tt>NullPointerException</tt>
  16. * if the collections provided to them are null.
  17. *
  18. * <p>The documentation for the polymorphic algorithms contained in this class
  19. * generally includes a brief description of the <i>implementation</i>. Such
  20. * descriptions should be regarded as <i>implementation notes</i>, rather than
  21. * parts of the <i>specification</i>. Implementors should feel free to
  22. * substitute other algorithms, so long as the specification itself is adhered
  23. * to. (For example, the algorithm used by <tt>sort</tt> does not have to be
  24. * a mergesort, but it does have to be <i>stable</i>.)
  25. *
  26. * <p>The "destructive" algorithms contained in this class, that is, the
  27. * algorithms that modify the collection on which they operate, are specified
  28. * to throw <tt>UnsupportedOperationException</tt> if the collection does not
  29. * support the appropriate mutation primitive(s), such as the <tt>set</tt>
  30. * method. These algorithms may, but are not required to, throw this
  31. * exception if an invocation would have no effect on the collection. For
  32. * example, invoking the <tt>sort</tt> method on an unmodifiable list that is
  33. * already sorted may or may not throw <tt>UnsupportedOperationException</tt>.
  34. *
  35. * <p>This class is a member of the
  36. * <a href="{@docRoot}/../guide/collections/index.html">
  37. * Java Collections Framework</a>.
  38. *
  39. * @author Josh Bloch
  40. * @version 1.66, 01/23/03
  41. * @see Collection
  42. * @see Set
  43. * @see List
  44. * @see Map
  45. * @since 1.2
  46. */
  47. public class Collections {
  48. // Suppresses default constructor, ensuring non-instantiability.
  49. private Collections() {
  50. }
  51. // Algorithms
  52. /*
  53. * Tuning parameters for algorithms - Many of the List algorithms have
  54. * two implementations, one of which is appropriate for RandomAccess
  55. * lists, the other for "sequential." Often, the random access variant
  56. * yields better performance on small sequential access lists. The
  57. * tuning parameters below determine the cutoff point for what constitutes
  58. * a "small" sequential access list for each algorithm. The values below
  59. * were empirically determined to work well for LinkedList. Hopefully
  60. * they should be reasonable for other sequential access List
  61. * implementations. Those doing performance work on this code would
  62. * do well to validate the values of these parameters from time to time.
  63. * (The first word of each tuning parameter name is the algorithm to which
  64. * it applies.)
  65. */
  66. private static final int BINARYSEARCH_THRESHOLD = 5000;
  67. private static final int REVERSE_THRESHOLD = 18;
  68. private static final int SHUFFLE_THRESHOLD = 5;
  69. private static final int FILL_THRESHOLD = 25;
  70. private static final int ROTATE_THRESHOLD = 100;
  71. private static final int COPY_THRESHOLD = 10;
  72. private static final int REPLACEALL_THRESHOLD = 11;
  73. private static final int INDEXOFSUBLIST_THRESHOLD = 35;
  74. /**
  75. * Sorts the specified list into ascending order, according to the
  76. * <i>natural ordering</i> of its elements. All elements in the list must
  77. * implement the <tt>Comparable</tt> interface. Furthermore, all elements
  78. * in the list must be <i>mutually comparable</i> (that is,
  79. * <tt>e1.compareTo(e2)</tt> must not throw a <tt>ClassCastException</tt>
  80. * for any elements <tt>e1</tt> and <tt>e2</tt> in the list).<p>
  81. *
  82. * This sort is guaranteed to be <i>stable</i>: equal elements will
  83. * not be reordered as a result of the sort.<p>
  84. *
  85. * The specified list must be modifiable, but need not be resizable.<p>
  86. *
  87. * The sorting algorithm is a modified mergesort (in which the merge is
  88. * omitted if the highest element in the low sublist is less than the
  89. * lowest element in the high sublist). This algorithm offers guaranteed
  90. * n log(n) performance.
  91. *
  92. * This implementation dumps the specified list into an array, sorts
  93. * the array, and iterates over the list resetting each element
  94. * from the corresponding position in the array. This avoids the
  95. * n<sup>2</sup> log(n) performance that would result from attempting
  96. * to sort a linked list in place.
  97. *
  98. * @param list the list to be sorted.
  99. * @throws ClassCastException if the list contains elements that are not
  100. * <i>mutually comparable</i> (for example, strings and integers).
  101. * @throws UnsupportedOperationException if the specified list's
  102. * list-iterator does not support the <tt>set</tt> operation.
  103. * @see Comparable
  104. */
  105. public static void sort(List list) {
  106. Object a[] = list.toArray();
  107. Arrays.sort(a);
  108. ListIterator i = list.listIterator();
  109. for (int j=0; j<a.length; j++) {
  110. i.next();
  111. i.set(a[j]);
  112. }
  113. }
  114. /**
  115. * Sorts the specified list according to the order induced by the
  116. * specified comparator. All elements in the list must be <i>mutually
  117. * comparable</i> using the specified comparator (that is,
  118. * <tt>c.compare(e1, e2)</tt> must not throw a <tt>ClassCastException</tt>
  119. * for any elements <tt>e1</tt> and <tt>e2</tt> in the list).<p>
  120. *
  121. * This sort is guaranteed to be <i>stable</i>: equal elements will
  122. * not be reordered as a result of the sort.<p>
  123. *
  124. * The sorting algorithm is a modified mergesort (in which the merge is
  125. * omitted if the highest element in the low sublist is less than the
  126. * lowest element in the high sublist). This algorithm offers guaranteed
  127. * n log(n) performance.
  128. *
  129. * The specified list must be modifiable, but need not be resizable.
  130. * This implementation dumps the specified list into an array, sorts
  131. * the array, and iterates over the list resetting each element
  132. * from the corresponding position in the array. This avoids the
  133. * n<sup>2</sup> log(n) performance that would result from attempting
  134. * to sort a linked list in place.
  135. *
  136. * @param list the list to be sorted.
  137. * @param c the comparator to determine the order of the list. A
  138. * <tt>null</tt> value indicates that the elements' <i>natural
  139. * ordering</i> should be used.
  140. * @throws ClassCastException if the list contains elements that are not
  141. * <i>mutually comparable</i> using the specified comparator.
  142. * @throws UnsupportedOperationException if the specified list's
  143. * list-iterator does not support the <tt>set</tt> operation.
  144. * @see Comparator
  145. */
  146. public static void sort(List list, Comparator c) {
  147. Object a[] = list.toArray();
  148. Arrays.sort(a, c);
  149. ListIterator i = list.listIterator();
  150. for (int j=0; j<a.length; j++) {
  151. i.next();
  152. i.set(a[j]);
  153. }
  154. }
  155. /**
  156. * Searches the specified list for the specified object using the binary
  157. * search algorithm. The list must be sorted into ascending order
  158. * according to the <i>natural ordering</i> of its elements (as by the
  159. * <tt>sort(List)</tt> method, above) prior to making this call. If it is
  160. * not sorted, the results are undefined. If the list contains multiple
  161. * elements equal to the specified object, there is no guarantee which one
  162. * will be found.<p>
  163. *
  164. * This method runs in log(n) time for a "random access" list (which
  165. * provides near-constant-time positional access). If the specified list
  166. * does not implement the {@link RandomAccess} and is large, this method
  167. * will do an iterator-based binary search that performs O(n) link
  168. * traversals and O(log n) element comparisons.
  169. *
  170. * @param list the list to be searched.
  171. * @param key the key to be searched for.
  172. * @return index of the search key, if it is contained in the list;
  173. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  174. * <i>insertion point</i> is defined as the point at which the
  175. * key would be inserted into the list: the index of the first
  176. * element greater than the key, or <tt>list.size()</tt>, if all
  177. * elements in the list are less than the specified key. Note
  178. * that this guarantees that the return value will be >= 0 if
  179. * and only if the key is found.
  180. * @throws ClassCastException if the list contains elements that are not
  181. * <i>mutually comparable</i> (for example, strings and
  182. * integers), or the search key in not mutually comparable
  183. * with the elements of the list.
  184. * @see Comparable
  185. * @see #sort(List)
  186. */
  187. public static int binarySearch(List list, Object key) {
  188. if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
  189. return indexedBinarySearch(list, key);
  190. else
  191. return iteratorBinarySearch(list, key);
  192. }
  193. private static int indexedBinarySearch(List list, Object key) {
  194. int low = 0;
  195. int high = list.size()-1;
  196. while (low <= high) {
  197. int mid = (low + high) >> 1;
  198. Object midVal = list.get(mid);
  199. int cmp = ((Comparable)midVal).compareTo(key);
  200. if (cmp < 0)
  201. low = mid + 1;
  202. else if (cmp > 0)
  203. high = mid - 1;
  204. else
  205. return mid; // key found
  206. }
  207. return -(low + 1); // key not found
  208. }
  209. private static int iteratorBinarySearch(List list, Object key) {
  210. int low = 0;
  211. int high = list.size()-1;
  212. ListIterator i = list.listIterator();
  213. while (low <= high) {
  214. int mid = (low + high) >> 1;
  215. Object midVal = get(i, mid);
  216. int cmp = ((Comparable)midVal).compareTo(key);
  217. if (cmp < 0)
  218. low = mid + 1;
  219. else if (cmp > 0)
  220. high = mid - 1;
  221. else
  222. return mid; // key found
  223. }
  224. return -(low + 1); // key not found
  225. }
  226. /**
  227. * Gets the ith element from the given list by repositioning the specified
  228. * list listIterator.
  229. */
  230. private static Object get(ListIterator i, int index) {
  231. Object obj = null;
  232. int pos = i.nextIndex();
  233. if (pos <= index) {
  234. do {
  235. obj = i.next();
  236. } while (pos++ < index);
  237. } else {
  238. do {
  239. obj = i.previous();
  240. } while (--pos > index);
  241. }
  242. return obj;
  243. }
  244. /**
  245. * Searches the specified list for the specified object using the binary
  246. * search algorithm. The list must be sorted into ascending order
  247. * according to the specified comparator (as by the <tt>Sort(List,
  248. * Comparator)</tt> method, above), prior to making this call. If it is
  249. * not sorted, the results are undefined. If the list contains multiple
  250. * elements equal to the specified object, there is no guarantee which one
  251. * will be found.<p>
  252. *
  253. * This method runs in log(n) time for a "random access" list (which
  254. * provides near-constant-time positional access). If the specified list
  255. * does not implement the {@link RandomAccess} and is large, this
  256. * this method will do an iterator-based binary search that performs
  257. * O(n) link traversals and O(log n) element comparisons.
  258. *
  259. * @param list the list to be searched.
  260. * @param key the key to be searched for.
  261. * @param c the comparator by which the list is ordered. A
  262. * <tt>null</tt> value indicates that the elements' <i>natural
  263. * ordering</i> should be used.
  264. * @return index of the search key, if it is contained in the list;
  265. * otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
  266. * <i>insertion point</i> is defined as the point at which the
  267. * key would be inserted into the list: the index of the first
  268. * element greater than the key, or <tt>list.size()</tt>, if all
  269. * elements in the list are less than the specified key. Note
  270. * that this guarantees that the return value will be >= 0 if
  271. * and only if the key is found.
  272. * @throws ClassCastException if the list contains elements that are not
  273. * <i>mutually comparable</i> using the specified comparator,
  274. * or the search key in not mutually comparable with the
  275. * elements of the list using this comparator.
  276. * @see Comparable
  277. * @see #sort(List, Comparator)
  278. */
  279. public static int binarySearch(List list, Object key, Comparator c) {
  280. if (c==null)
  281. return binarySearch(list, key);
  282. if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
  283. return indexedBinarySearch(list, key, c);
  284. else
  285. return iteratorBinarySearch(list, key, c);
  286. }
  287. private static int indexedBinarySearch(List l, Object key, Comparator c) {
  288. int low = 0;
  289. int high = l.size()-1;
  290. while (low <= high) {
  291. int mid = (low + high) >> 1;
  292. Object midVal = l.get(mid);
  293. int cmp = c.compare(midVal, key);
  294. if (cmp < 0)
  295. low = mid + 1;
  296. else if (cmp > 0)
  297. high = mid - 1;
  298. else
  299. return mid; // key found
  300. }
  301. return -(low + 1); // key not found
  302. }
  303. private static int iteratorBinarySearch(List l, Object key, Comparator c) {
  304. int low = 0;
  305. int high = l.size()-1;
  306. ListIterator i = l.listIterator();
  307. while (low <= high) {
  308. int mid = (low + high) >> 1;
  309. Object midVal = get(i, mid);
  310. int cmp = c.compare(midVal, key);
  311. if (cmp < 0)
  312. low = mid + 1;
  313. else if (cmp > 0)
  314. high = mid - 1;
  315. else
  316. return mid; // key found
  317. }
  318. return -(low + 1); // key not found
  319. }
  320. /**
  321. * Reverses the order of the elements in the specified list.<p>
  322. *
  323. * This method runs in linear time.
  324. *
  325. * @param list the list whose elements are to be reversed.
  326. * @throws UnsupportedOperationException if the specified list or
  327. * its list-iterator does not support the <tt>set</tt> method.
  328. */
  329. public static void reverse(List list) {
  330. int size = list.size();
  331. if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) {
  332. for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--)
  333. swap(list, i, j);
  334. } else {
  335. ListIterator fwd = list.listIterator();
  336. ListIterator rev = list.listIterator(size);
  337. for (int i=0, mid=list.size()>>1; i<mid; i++) {
  338. Object tmp = fwd.next();
  339. fwd.set(rev.previous());
  340. rev.set(tmp);
  341. }
  342. }
  343. }
  344. /**
  345. * Randomly permutes the specified list using a default source of
  346. * randomness. All permutations occur with approximately equal
  347. * likelihood.<p>
  348. *
  349. * The hedge "approximately" is used in the foregoing description because
  350. * default source of randomenss is only approximately an unbiased source
  351. * of independently chosen bits. If it were a perfect source of randomly
  352. * chosen bits, then the algorithm would choose permutations with perfect
  353. * uniformity.<p>
  354. *
  355. * This implementation traverses the list backwards, from the last element
  356. * up to the second, repeatedly swapping a randomly selected element into
  357. * the "current position". Elements are randomly selected from the
  358. * portion of the list that runs from the first element to the current
  359. * position, inclusive.<p>
  360. *
  361. * This method runs in linear time. If the specified list does not
  362. * implement the {@link RandomAccess} interface and is large, this
  363. * implementation dumps the specified list into an array before shuffling
  364. * it, and dumps the shuffled array back into the list. This avoids the
  365. * quadratic behavior that would result from shuffling a "sequential
  366. * access" list in place.
  367. *
  368. * @param list the list to be shuffled.
  369. * @throws UnsupportedOperationException if the specified list or
  370. * its list-iterator does not support the <tt>set</tt> method.
  371. */
  372. public static void shuffle(List list) {
  373. shuffle(list, r);
  374. }
  375. private static Random r = new Random();
  376. /**
  377. * Randomly permute the specified list using the specified source of
  378. * randomness. All permutations occur with equal likelihood
  379. * assuming that the source of randomness is fair.<p>
  380. *
  381. * This implementation traverses the list backwards, from the last element
  382. * up to the second, repeatedly swapping a randomly selected element into
  383. * the "current position". Elements are randomly selected from the
  384. * portion of the list that runs from the first element to the current
  385. * position, inclusive.<p>
  386. *
  387. * This method runs in linear time. If the specified list does not
  388. * implement the {@link RandomAccess} interface and is large, this
  389. * implementation dumps the specified list into an array before shuffling
  390. * it, and dumps the shuffled array back into the list. This avoids the
  391. * quadratic behavior that would result from shuffling a "sequential
  392. * access" list in place.
  393. *
  394. * @param list the list to be shuffled.
  395. * @param rnd the source of randomness to use to shuffle the list.
  396. * @throws UnsupportedOperationException if the specified list or its
  397. * list-iterator does not support the <tt>set</tt> operation.
  398. */
  399. public static void shuffle(List list, Random rnd) {
  400. int size = list.size();
  401. if (size < SHUFFLE_THRESHOLD || list instanceof RandomAccess) {
  402. for (int i=size; i>1; i--)
  403. swap(list, i-1, rnd.nextInt(i));
  404. } else {
  405. Object arr[] = list.toArray();
  406. // Shuffle array
  407. for (int i=size; i>1; i--)
  408. swap(arr, i-1, rnd.nextInt(i));
  409. // Dump array back into list
  410. ListIterator it = list.listIterator();
  411. for (int i=0; i<arr.length; i++) {
  412. it.next();
  413. it.set(arr[i]);
  414. }
  415. }
  416. }
  417. /**
  418. * Swaps the elements at the specified positions in the specified list.
  419. * (If the specified positions are equal, invoking this method leaves
  420. * the list unchanged.)
  421. *
  422. * @param list The list in which to swap elements.
  423. * @param i the index of one element to be swapped.
  424. * @param j the index of the other element to be swapped.
  425. * @throws IndexOutOfBoundsException if either <tt>i</tt> or <tt>j</tt>
  426. * is out of range (i < 0 || i >= list.size()
  427. * || j < 0 || j >= list.size()).
  428. * @since 1.4
  429. */
  430. public static void swap(List list, int i, int j) {
  431. list.set(i, list.set(j, list.get(i)));
  432. }
  433. /**
  434. * Swaps the two specified elements in the specified array.
  435. */
  436. private static void swap(Object[] arr, int i, int j) {
  437. Object tmp = arr[i];
  438. arr[i] = arr[j];
  439. arr[j] = tmp;
  440. }
  441. /**
  442. * Replaces all of the elements of the specified list with the specified
  443. * element. <p>
  444. *
  445. * This method runs in linear time.
  446. *
  447. * @param list the list to be filled with the specified element.
  448. * @param obj The element with which to fill the specified list.
  449. * @throws UnsupportedOperationException if the specified list or its
  450. * list-iterator does not support the <tt>set</tt> operation.
  451. */
  452. public static void fill(List list, Object obj) {
  453. int size = list.size();
  454. if (size < FILL_THRESHOLD || list instanceof RandomAccess) {
  455. for (int i=0; i<size; i++)
  456. list.set(i, obj);
  457. } else {
  458. ListIterator itr = list.listIterator();
  459. for (int i=0; i<size; i++) {
  460. itr.next();
  461. itr.set(obj);
  462. }
  463. }
  464. }
  465. /**
  466. * Copies all of the elements from one list into another. After the
  467. * operation, the index of each copied element in the destination list
  468. * will be identical to its index in the source list. The destination
  469. * list must be at least as long as the source list. If it is longer, the
  470. * remaining elements in the destination list are unaffected. <p>
  471. *
  472. * This method runs in linear time.
  473. *
  474. * @param dest The destination list.
  475. * @param src The source list.
  476. * @throws IndexOutOfBoundsException if the destination list is too small
  477. * to contain the entire source List.
  478. * @throws UnsupportedOperationException if the destination list's
  479. * list-iterator does not support the <tt>set</tt> operation.
  480. */
  481. public static void copy(List dest, List src) {
  482. int srcSize = src.size();
  483. if (srcSize > dest.size())
  484. throw new IndexOutOfBoundsException("Source does not fit in dest");
  485. if (srcSize < COPY_THRESHOLD ||
  486. (src instanceof RandomAccess && dest instanceof RandomAccess)) {
  487. for (int i=0; i<srcSize; i++)
  488. dest.set(i, src.get(i));
  489. } else {
  490. ListIterator di=dest.listIterator(), si=src.listIterator();
  491. for (int i=0; i<srcSize; i++) {
  492. di.next();
  493. di.set(si.next());
  494. }
  495. }
  496. }
  497. /**
  498. * Returns the minimum element of the given collection, according to the
  499. * <i>natural ordering</i> of its elements. All elements in the
  500. * collection must implement the <tt>Comparable</tt> interface.
  501. * Furthermore, all elements in the collection must be <i>mutually
  502. * comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a
  503. * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
  504. * <tt>e2</tt> in the collection).<p>
  505. *
  506. * This method iterates over the entire collection, hence it requires
  507. * time proportional to the size of the collection.
  508. *
  509. * @param coll the collection whose minimum element is to be determined.
  510. * @return the minimum element of the given collection, according
  511. * to the <i>natural ordering</i> of its elements.
  512. * @throws ClassCastException if the collection contains elements that are
  513. * not <i>mutually comparable</i> (for example, strings and
  514. * integers).
  515. * @throws NoSuchElementException if the collection is empty.
  516. * @see Comparable
  517. */
  518. public static Object min(Collection coll) {
  519. Iterator i = coll.iterator();
  520. Comparable candidate = (Comparable)(i.next());
  521. while(i.hasNext()) {
  522. Comparable next = (Comparable)(i.next());
  523. if (next.compareTo(candidate) < 0)
  524. candidate = next;
  525. }
  526. return candidate;
  527. }
  528. /**
  529. * Returns the minimum element of the given collection, according to the
  530. * order induced by the specified comparator. All elements in the
  531. * collection must be <i>mutually comparable</i> by the specified
  532. * comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a
  533. * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
  534. * <tt>e2</tt> in the collection).<p>
  535. *
  536. * This method iterates over the entire collection, hence it requires
  537. * time proportional to the size of the collection.
  538. *
  539. * @param coll the collection whose minimum element is to be determined.
  540. * @param comp the comparator with which to determine the minimum element.
  541. * A <tt>null</tt> value indicates that the elements' <i>natural
  542. * ordering</i> should be used.
  543. * @return the minimum element of the given collection, according
  544. * to the specified comparator.
  545. * @throws ClassCastException if the collection contains elements that are
  546. * not <i>mutually comparable</i> using the specified comparator.
  547. * @throws NoSuchElementException if the collection is empty.
  548. * @see Comparable
  549. */
  550. public static Object min(Collection coll, Comparator comp) {
  551. if (comp==null)
  552. return min(coll);
  553. Iterator i = coll.iterator();
  554. Object candidate = i.next();
  555. while(i.hasNext()) {
  556. Object next = i.next();
  557. if (comp.compare(next, candidate) < 0)
  558. candidate = next;
  559. }
  560. return candidate;
  561. }
  562. /**
  563. * Returns the maximum element of the given collection, according to the
  564. * <i>natural ordering</i> of its elements. All elements in the
  565. * collection must implement the <tt>Comparable</tt> interface.
  566. * Furthermore, all elements in the collection must be <i>mutually
  567. * comparable</i> (that is, <tt>e1.compareTo(e2)</tt> must not throw a
  568. * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
  569. * <tt>e2</tt> in the collection).<p>
  570. *
  571. * This method iterates over the entire collection, hence it requires
  572. * time proportional to the size of the collection.
  573. *
  574. * @param coll the collection whose maximum element is to be determined.
  575. * @return the maximum element of the given collection, according
  576. * to the <i>natural ordering</i> of its elements.
  577. * @throws ClassCastException if the collection contains elements that are
  578. * not <i>mutually comparable</i> (for example, strings and
  579. * integers).
  580. * @throws NoSuchElementException if the collection is empty.
  581. * @see Comparable
  582. */
  583. public static Object max(Collection coll) {
  584. Iterator i = coll.iterator();
  585. Comparable candidate = (Comparable)(i.next());
  586. while(i.hasNext()) {
  587. Comparable next = (Comparable)(i.next());
  588. if (next.compareTo(candidate) > 0)
  589. candidate = next;
  590. }
  591. return candidate;
  592. }
  593. /**
  594. * Returns the maximum element of the given collection, according to the
  595. * order induced by the specified comparator. All elements in the
  596. * collection must be <i>mutually comparable</i> by the specified
  597. * comparator (that is, <tt>comp.compare(e1, e2)</tt> must not throw a
  598. * <tt>ClassCastException</tt> for any elements <tt>e1</tt> and
  599. * <tt>e2</tt> in the collection).<p>
  600. *
  601. * This method iterates over the entire collection, hence it requires
  602. * time proportional to the size of the collection.
  603. *
  604. * @param coll the collection whose maximum element is to be determined.
  605. * @param comp the comparator with which to determine the maximum element.
  606. * A <tt>null</tt> value indicates that the elements' <i>natural
  607. * ordering</i> should be used.
  608. * @return the maximum element of the given collection, according
  609. * to the specified comparator.
  610. * @throws ClassCastException if the collection contains elements that are
  611. * not <i>mutually comparable</i> using the specified comparator.
  612. * @throws NoSuchElementException if the collection is empty.
  613. * @see Comparable
  614. */
  615. public static Object max(Collection coll, Comparator comp) {
  616. if (comp==null)
  617. return max(coll);
  618. Iterator i = coll.iterator();
  619. Object candidate = i.next();
  620. while(i.hasNext()) {
  621. Object next = i.next();
  622. if (comp.compare(next, candidate) > 0)
  623. candidate = next;
  624. }
  625. return candidate;
  626. }
  627. /**
  628. * Rotates the elements in the specified list by the specified distance.
  629. * After calling this method, the element at index <tt>i</tt> will be
  630. * the element previously at index <tt>(i - distance)</tt> mod
  631. * <tt>list.size()</tt>, for all values of <tt>i</tt> between <tt>0</tt>
  632. * and <tt>list.size()-1</tt>, inclusive. (This method has no effect on
  633. * the size of the list.)
  634. *
  635. * <p>For example, suppose <tt>list</tt> comprises<tt> [t, a, n, k, s]</tt>.
  636. * After invoking <tt>Collections.rotate(list, 1)</tt> (or
  637. * <tt>Collections.rotate(list, -4)</tt>), <tt>list</tt> will comprise
  638. * <tt>[s, t, a, n, k]</tt>.
  639. *
  640. * <p>Note that this method can usefully be applied to sublists to
  641. * move one or more elements within a list while preserving the
  642. * order of the remaining elements. For example, the following idiom
  643. * moves the element at index <tt>j</tt> forward to position
  644. * <tt>k</tt> (which must be greater than or equal to <tt>j</tt>):
  645. * <pre>
  646. * Collections.rotate(list.subList(j, k+1), -1);
  647. * </pre>
  648. * To make this concrete, suppose <tt>list</tt> comprises
  649. * <tt>[a, b, c, d, e]</tt>. To move the element at index <tt>1</tt>
  650. * (<tt>b</tt>) forward two positions, perform the following invocation:
  651. * <pre>
  652. * Collections.rotate(l.subList(1, 4), -1);
  653. * </pre>
  654. * The resulting list is <tt>[a, c, d, b, e]</tt>.
  655. *
  656. * <p>To move more than one element forward, increase the absolute value
  657. * of the rotation distance. To move elements backward, use a positive
  658. * shift distance.
  659. *
  660. * <p>If the specified list is small or implements the {@link
  661. * RandomAccess} interface, this implementation exchanges the first
  662. * element into the location it should go, and then repeatedly exchanges
  663. * the displaced element into the location it should go until a displaced
  664. * element is swapped into the first element. If necessary, the process
  665. * is repeated on the second and successive elements, until the rotation
  666. * is complete. If the specified list is large and doesn't implement the
  667. * <tt>RandomAccess</tt> interface, this implementation breaks the
  668. * list into two sublist views around index <tt>-distance mod size</tt>.
  669. * Then the {@link #reverse(List)} method is invoked on each sublist view,
  670. * and finally it is invoked on the entire list. For a more complete
  671. * description of both algorithms, see Section 2.3 of Jon Bentley's
  672. * <i>Programming Pearls</i> (Addison-Wesley, 1986).
  673. *
  674. * @param list the list to be rotated.
  675. * @param distance the distance to rotate the list. There are no
  676. * constraints on this value; it may be zero, negative, or
  677. * greater than <tt>list.size()</tt>.
  678. * @throws UnsupportedOperationException if the specified list or
  679. * its list-iterator does not support the <tt>set</tt> method.
  680. * @since 1.4
  681. */
  682. public static void rotate(List list, int distance) {
  683. if (list instanceof RandomAccess || list.size() < ROTATE_THRESHOLD)
  684. rotate1(list, distance);
  685. else
  686. rotate2(list, distance);
  687. }
  688. private static void rotate1(List list, int distance) {
  689. int size = list.size();
  690. if (size == 0)
  691. return;
  692. distance = distance % size;
  693. if (distance < 0)
  694. distance += size;
  695. if (distance == 0)
  696. return;
  697. for (int cycleStart = 0, nMoved = 0; nMoved != size; cycleStart++) {
  698. Object displaced = list.get(cycleStart);
  699. int i = cycleStart;
  700. do {
  701. i += distance;
  702. if (i >= size)
  703. i -= size;
  704. displaced = list.set(i, displaced);
  705. nMoved ++;
  706. } while(i != cycleStart);
  707. }
  708. }
  709. private static void rotate2(List list, int distance) {
  710. int size = list.size();
  711. if (size == 0)
  712. return;
  713. int mid = -distance % size;
  714. if (mid < 0)
  715. mid += size;
  716. if (mid == 0)
  717. return;
  718. Collections.reverse(list.subList(0, mid));
  719. Collections.reverse(list.subList(mid, size));
  720. Collections.reverse(list);
  721. }
  722. /**
  723. * Replaces all occurrences of one specified value in a list with another.
  724. * More formally, replaces with <tt>newVal</tt> each element <tt>e</tt>
  725. * in <tt>list</tt> such that
  726. * <tt>(oldVal==null ? e==null : oldVal.equals(e))</tt>.
  727. * (This method has no effect on the size of the list.)
  728. *
  729. * @param list the list in which replacement is to occur.
  730. * @param oldVal the old value to be replaced.
  731. * @param newVal the new value with which <tt>oldVal</tt> is to be
  732. * replaced.
  733. * @return <tt>true</tt> if <tt>list</tt> contained one or more elements
  734. * <tt>e</tt> such that
  735. * <tt>(oldVal==null ? e==null : oldVal.equals(e))</tt>.
  736. * @throws UnsupportedOperationException if the specified list or
  737. * its list-iterator does not support the <tt>set</tt> method.
  738. * @since 1.4
  739. */
  740. public static boolean replaceAll(List list, Object oldVal, Object newVal) {
  741. boolean result = false;
  742. int size = list.size();
  743. if (size < REPLACEALL_THRESHOLD || list instanceof RandomAccess) {
  744. if (oldVal==null) {
  745. for (int i=0; i<size; i++) {
  746. if (list.get(i)==null) {
  747. list.set(i, newVal);
  748. result = true;
  749. }
  750. }
  751. } else {
  752. for (int i=0; i<size; i++) {
  753. if (oldVal.equals(list.get(i))) {
  754. list.set(i, newVal);
  755. result = true;
  756. }
  757. }
  758. }
  759. } else {
  760. ListIterator itr=list.listIterator();
  761. if (oldVal==null) {
  762. for (int i=0; i<size; i++) {
  763. if (itr.next()==null) {
  764. itr.set(newVal);
  765. result = true;
  766. }
  767. }
  768. } else {
  769. for (int i=0; i<size; i++) {
  770. if (oldVal.equals(itr.next())) {
  771. itr.set(newVal);
  772. result = true;
  773. }
  774. }
  775. }
  776. }
  777. return result;
  778. }
  779. /**
  780. * Returns the starting position of the first occurrence of the specified
  781. * target list within the specified source list, or -1 if there is no
  782. * such occurrence. More formally, returns the the lowest index <tt>i</tt>
  783. * such that <tt>source.subList(i, i+target.size()).equals(target)</tt>,
  784. * or -1 if there is no such index. (Returns -1 if
  785. * <tt>target.size() > source.size()</tt>.)
  786. *
  787. * <p>This implementation uses the "brute force" technique of scanning
  788. * over the source list, looking for a match with the target at each
  789. * location in turn.
  790. *
  791. * @param source the list in which to search for the first occurrence
  792. * of <tt>target</tt>.
  793. * @param target the list to search for as a subList of <tt>source</tt>.
  794. * @return the starting position of the first occurrence of the specified
  795. * target list within the specified source list, or -1 if there
  796. * is no such occurrence.
  797. * @since 1.4
  798. */
  799. public static int indexOfSubList(List source, List target) {
  800. int sourceSize = source.size();
  801. int targetSize = target.size();
  802. int maxCandidate = sourceSize - targetSize;
  803. if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
  804. (source instanceof RandomAccess&&target instanceof RandomAccess)) {
  805. nextCand:
  806. for (int candidate = 0; candidate <= maxCandidate; candidate++) {
  807. for (int i=0, j=candidate; i<targetSize; i++, j++)
  808. if (!eq(target.get(i), source.get(j)))
  809. continue nextCand; // Element mismatch, try next cand
  810. return candidate; // All elements of candidate matched target
  811. }
  812. } else { // Iterator version of above algorithm
  813. ListIterator si = source.listIterator();
  814. nextCand:
  815. for (int candidate = 0; candidate <= maxCandidate; candidate++) {
  816. ListIterator ti = target.listIterator();
  817. for (int i=0; i<targetSize; i++) {
  818. if (!eq(ti.next(), si.next())) {
  819. // Back up source iterator to next candidate
  820. for (int j=0; j<i; j++)
  821. si.previous();
  822. continue nextCand;
  823. }
  824. }
  825. return candidate;
  826. }
  827. }
  828. return -1; // No candidate matched the target
  829. }
  830. /**
  831. * Returns the starting position of the last occurrence of the specified
  832. * target list within the specified source list, or -1 if there is no such
  833. * occurrence. More formally, returns the the highest index <tt>i</tt>
  834. * such that <tt>source.subList(i, i+target.size()).equals(target)</tt>,
  835. * or -1 if there is no such index. (Returns -1 if
  836. * <tt>target.size() > source.size()</tt>.)
  837. *
  838. * <p>This implementation uses the "brute force" technique of iterating
  839. * over the source list, looking for a match with the target at each
  840. * location in turn.
  841. *
  842. * @param source the list in which to search for the last occurrence
  843. * of <tt>target</tt>.
  844. * @param target the list to search for as a subList of <tt>source</tt>.
  845. * @return the starting position of the last occurrence of the specified
  846. * target list within the specified source list, or -1 if there
  847. * is no such occurrence.
  848. * @since 1.4
  849. */
  850. public static int lastIndexOfSubList(List source, List target) {
  851. int sourceSize = source.size();
  852. int targetSize = target.size();
  853. int maxCandidate = sourceSize - targetSize;
  854. if (sourceSize < INDEXOFSUBLIST_THRESHOLD ||
  855. source instanceof RandomAccess) { // Index access version
  856. nextCand:
  857. for (int candidate = maxCandidate; candidate >= 0; candidate--) {
  858. for (int i=0, j=candidate; i<targetSize; i++, j++)
  859. if (!eq(target.get(i), source.get(j)))
  860. continue nextCand; // Element mismatch, try next cand
  861. return candidate; // All elements of candidate matched target
  862. }
  863. } else { // Iterator version of above algorithm
  864. if (maxCandidate < 0)
  865. return -1;
  866. ListIterator si = source.listIterator(maxCandidate);
  867. nextCand:
  868. for (int candidate = maxCandidate; candidate >= 0; candidate--) {
  869. ListIterator ti = target.listIterator();
  870. for (int i=0; i<targetSize; i++) {
  871. if (!eq(ti.next(), si.next())) {
  872. if (candidate != 0) {
  873. // Back up source iterator to next candidate
  874. for (int j=0; j<=i+1; j++)
  875. si.previous();
  876. }
  877. continue nextCand;
  878. }
  879. }
  880. return candidate;
  881. }
  882. }
  883. return -1; // No candidate matched the target
  884. }
  885. // Unmodifiable Wrappers
  886. /**
  887. * Returns an unmodifiable view of the specified collection. This method
  888. * allows modules to provide users with "read-only" access to internal
  889. * collections. Query operations on the returned collection "read through"
  890. * to the specified collection, and attempts to modify the returned
  891. * collection, whether direct or via its iterator, result in an
  892. * <tt>UnsupportedOperationException</tt>.<p>
  893. *
  894. * The returned collection does <i>not</i> pass the hashCode and equals
  895. * operations through to the backing collection, but relies on
  896. * <tt>Object</tt>'s <tt>equals</tt> and <tt>hashCode</tt> methods. This
  897. * is necessary to preserve the contracts of these operations in the case
  898. * that the backing collection is a set or a list.<p>
  899. *
  900. * The returned collection will be serializable if the specified collection
  901. * is serializable.
  902. *
  903. * @param c the collection for which an unmodifiable view is to be
  904. * returned.
  905. * @return an unmodifiable view of the specified collection.
  906. */
  907. public static Collection unmodifiableCollection(Collection c) {
  908. return new UnmodifiableCollection(c);
  909. }
  910. /**
  911. * @serial include
  912. */
  913. static class UnmodifiableCollection implements Collection, Serializable {
  914. // use serialVersionUID from JDK 1.2.2 for interoperability
  915. private static final long serialVersionUID = 1820017752578914078L;
  916. Collection c;
  917. UnmodifiableCollection(Collection c) {
  918. if (c==null)
  919. throw new NullPointerException();
  920. this.c = c;
  921. }
  922. public int size() {return c.size();}
  923. public boolean isEmpty() {return c.isEmpty();}
  924. public boolean contains(Object o) {return c.contains(o);}
  925. public Object[] toArray() {return c.toArray();}
  926. public Object[] toArray(Object[] a) {return c.toArray(a);}
  927. public String toString() {return c.toString();}
  928. public Iterator iterator() {
  929. return new Iterator() {
  930. Iterator i = c.iterator();
  931. public boolean hasNext() {return i.hasNext();}
  932. public Object next() {return i.next();}
  933. public void remove() {
  934. throw new UnsupportedOperationException();
  935. }
  936. };
  937. }
  938. public boolean add(Object o){
  939. throw new UnsupportedOperationException();
  940. }
  941. public boolean remove(Object o) {
  942. throw new UnsupportedOperationException();
  943. }
  944. public boolean containsAll(Collection coll) {
  945. return c.containsAll(coll);
  946. }
  947. public boolean addAll(Collection coll) {
  948. throw new UnsupportedOperationException();
  949. }
  950. public boolean removeAll(Collection coll) {
  951. throw new UnsupportedOperationException();
  952. }
  953. public boolean retainAll(Collection coll) {
  954. throw new UnsupportedOperationException();
  955. }
  956. public void clear() {
  957. throw new UnsupportedOperationException();
  958. }
  959. }
  960. /**
  961. * Returns an unmodifiable view of the specified set. This method allows
  962. * modules to provide users with "read-only" access to internal sets.
  963. * Query operations on the returned set "read through" to the specified
  964. * set, and attempts to modify the returned set, whether direct or via its
  965. * iterator, result in an <tt>UnsupportedOperationException</tt>.<p>
  966. *
  967. * The returned set will be serializable if the specified set
  968. * is serializable.
  969. *
  970. * @param s the set for which an unmodifiable view is to be returned.
  971. * @return an unmodifiable view of the specified set.
  972. */
  973. public static Set unmodifiableSet(Set s) {
  974. return new UnmodifiableSet(s);
  975. }
  976. /**
  977. * @serial include
  978. */
  979. static class UnmodifiableSet extends UnmodifiableCollection
  980. implements Set, Serializable {
  981. UnmodifiableSet(Set s) {super(s);}
  982. public boolean equals(Object o) {return c.equals(o);}
  983. public int hashCode() {return c.hashCode();}
  984. }
  985. /**
  986. * Returns an unmodifiable view of the specified sorted set. This method
  987. * allows modules to provide users with "read-only" access to internal
  988. * sorted sets. Query operations on the returned sorted set "read
  989. * through" to the specified sorted set. Attempts to modify the returned
  990. * sorted set, whether direct, via its iterator, or via its
  991. * <tt>subSet</tt>, <tt>headSet</tt>, or <tt>tailSet</tt> views, result in
  992. * an <tt>UnsupportedOperationException</tt>.<p>
  993. *
  994. * The returned sorted set will be serializable if the specified sorted set
  995. * is serializable.
  996. *
  997. * @param s the sorted set for which an unmodifiable view is to be
  998. * returned.
  999. * @return an unmodifiable view of the specified sorted set.
  1000. */
  1001. public static SortedSet unmodifiableSortedSet(SortedSet s) {
  1002. return new UnmodifiableSortedSet(s);
  1003. }
  1004. /**
  1005. * @serial include
  1006. */
  1007. static class UnmodifiableSortedSet extends UnmodifiableSet
  1008. implements SortedSet, Serializable {
  1009. private SortedSet ss;
  1010. UnmodifiableSortedSet(SortedSet s) {super(s); ss = s;}
  1011. public Comparator comparator() {return ss.comparator();}
  1012. public SortedSet subSet(Object fromElement, Object toElement) {
  1013. return new UnmodifiableSortedSet(ss.subSet(fromElement,toElement));
  1014. }
  1015. public SortedSet headSet(Object toElement) {
  1016. return new UnmodifiableSortedSet(ss.headSet(toElement));
  1017. }
  1018. public SortedSet tailSet(Object fromElement) {
  1019. return new UnmodifiableSortedSet(ss.tailSet(fromElement));
  1020. }
  1021. public Object first() {return ss.first();}
  1022. public Object last() {return ss.last();}
  1023. }
  1024. /**
  1025. * Returns an unmodifiable view of the specified list. This method allows
  1026. * modules to provide users with "read-only" access to internal
  1027. * lists. Query operations on the returned list "read through" to the
  1028. * specified list, and attempts to modify the returned list, whether
  1029. * direct or via its iterator, result in an
  1030. * <tt>UnsupportedOperationException</tt>.<p>
  1031. *
  1032. * The returned list will be serializable if the specified list
  1033. * is serializable. Similarly, the returned list will implement
  1034. * {@link RandomAccess} if the specified list does.
  1035. * the
  1036. *
  1037. * @param list the list for which an unmodifiable view is to be returned.
  1038. * @return an unmodifiable view of the specified list.
  1039. */
  1040. public static List unmodifiableList(List list) {
  1041. return (list instanceof RandomAccess ?
  1042. new UnmodifiableRandomAccessList(list) :
  1043. new UnmodifiableList(list));
  1044. }
  1045. /**
  1046. * @serial include
  1047. */
  1048. static class UnmodifiableList extends UnmodifiableCollection
  1049. implements List {
  1050. static final long serialVersionUID = -283967356065247728L;
  1051. List list;
  1052. UnmodifiableList(List list) {
  1053. super(list);
  1054. this.list = list;
  1055. }
  1056. public boolean equals(Object o) {return list.equals(o);}
  1057. public int hashCode() {return list.hashCode();}
  1058. public Object get(int index) {return list.get(index);}
  1059. public Object set(int index, Object element) {
  1060. throw new UnsupportedOperationException();
  1061. }
  1062. public void add(int index, Object element) {
  1063. throw new UnsupportedOperationException();
  1064. }
  1065. public Object remove(int index) {
  1066. throw new UnsupportedOperationException();
  1067. }
  1068. public int indexOf(Object o) {return list.indexOf(o);}
  1069. public int lastIndexOf(Object o) {return list.lastIndexOf(o);}
  1070. public boolean addAll(int index, Collection c) {
  1071. throw new UnsupportedOperationException();
  1072. }
  1073. public ListIterator listIterator() {return listIterator(0);}
  1074. public ListIterator listIterator(final int index) {
  1075. return new ListIterator() {
  1076. ListIterator i = list.listIterator(index);
  1077. public boolean hasNext() {return i.hasNext();}
  1078. public Object next() {return i.next();}
  1079. public boolean hasPrevious() {return i.hasPrevious();}
  1080. public Object previous() {return i.previous();}
  1081. public int nextIndex() {return i.nextIndex();}
  1082. public int previousIndex() {return i.previousIndex();}
  1083. public void remove() {
  1084. throw new UnsupportedOperationException();
  1085. }
  1086. public void set(Object o) {
  1087. throw new UnsupportedOperationException();
  1088. }
  1089. public void add(Object o) {
  1090. throw new UnsupportedOperationException();
  1091. }
  1092. };
  1093. }
  1094. public List subList(int fromIndex, int toIndex) {
  1095. return new UnmodifiableList(list.subList(fromIndex, toIndex));
  1096. }
  1097. /**
  1098. * UnmodifiableRandomAccessList instances are serialized as
  1099. * UnmodifiableList instances to allow them to be deserialized
  1100. * in pre-1.4 JREs (which do not have UnmodifiableRandomAccessList).
  1101. * This method inverts the transformation. As a beneficial
  1102. * side-effect, it also grafts the RandomAccess marker onto
  1103. * UnmodifiableList instances that were serialized in pre-1.4 JREs.
  1104. *
  1105. * Note: Unfortunately, UnmodifiableRandomAccessList instances
  1106. * serialized in 1.4.1 and deserialized in 1.4 will become
  1107. * UnmodifiableList instances, as this method was missing in 1.4.
  1108. */
  1109. private Object readResolve() {
  1110. return (list instanceof