0392857d2a0c41ecdc66c820f24131b5cf1d3631.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. package com.sinosoft.cm.common;
  2. import java.util.Arrays;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. public abstract class AbstractCollection<E> implements Collection<E> {
  6. /**
  7. * Sole constructor. (For invocation by subclass constructors, typically
  8. * implicit.)
  9. */
  10. protected AbstractCollection() {
  11. }
  12. // Query Operations
  13. /**
  14. * Returns an iterator over the elements contained in this collection.
  15. *
  16. * @return an iterator over the elements contained in this collection
  17. */
  18. public abstract Iterator<E> iterator();
  19. public abstract int size();
  20. /**
  21. * {@inheritDoc}
  22. *
  23. * <p>This implementation returns <tt>size() == 0</tt>.
  24. */
  25. public boolean isEmpty() {
  26. return size() == 0;
  27. }
  28. /**
  29. * {@inheritDoc}
  30. *
  31. * <p>This implementation iterates over the elements in the collection,
  32. * checking each element in turn for equality with the specified element.
  33. *
  34. * @throws ClassCastException {@inheritDoc}
  35. * @throws NullPointerException {@inheritDoc}
  36. */
  37. public boolean contains(Object o) {
  38. Iterator<E> it = iterator();
  39. if (o==null) {
  40. while (it.hasNext())
  41. if (it.next()==null)
  42. return true;
  43. } else {
  44. while (it.hasNext())
  45. if (o.equals(it.next()))
  46. return true;
  47. }
  48. return false;
  49. }
  50. /**
  51. * {@inheritDoc}
  52. *
  53. * <p>This implementation returns an array containing all the elements
  54. * returned by this collection's iterator, in the same order, stored in
  55. * consecutive elements of the array, starting with index {@code 0}.
  56. * The length of the returned array is equal to the number of elements
  57. * returned by the iterator, even if the size of this collection changes
  58. * during iteration, as might happen if the collection permits
  59. * concurrent modification during iteration. The {@code size} method is
  60. * called only as an optimization hint; the correct result is returned
  61. * even if the iterator returns a different number of elements.
  62. *
  63. * <p>This method is equivalent to:
  64. *
  65. * <pre> {@code
  66. * List<E> list = new ArrayList<E>(size());
  67. * for (E e : this)
  68. * list.add(e);
  69. * return list.toArray();
  70. * }</pre>
  71. */
  72. public Object[] toArray() {
  73. // Estimate size of array; be prepared to see more or fewer elements
  74. Object[] r = new Object[size()];
  75. Iterator<E> it = iterator();
  76. for (int i = 0; i < r.length; i++) {
  77. if (! it.hasNext()) // fewer elements than expected
  78. return Arrays.copyOf(r, i);
  79. r[i] = it.next();
  80. }
  81. return it.hasNext() ? finishToArray(r, it) : r;
  82. }
  83. /**
  84. * {@inheritDoc}
  85. *
  86. * <p>This implementation returns an array containing all the elements
  87. * returned by this collection's iterator in the same order, stored in
  88. * consecutive elements of the array, starting with index {@code 0}.
  89. * If the number of elements returned by the iterator is too large to
  90. * fit into the specified array, then the elements are returned in a
  91. * newly allocated array with length equal to the number of elements
  92. * returned by the iterator, even if the size of this collection
  93. * changes during iteration, as might happen if the collection permits
  94. * concurrent modification during iteration. The {@code size} method is
  95. * called only as an optimization hint; the correct result is returned
  96. * even if the iterator returns a different number of elements.
  97. *
  98. * <p>This method is equivalent to:
  99. *
  100. * <pre> {@code
  101. * List<E> list = new ArrayList<E>(size());
  102. * for (E e : this)
  103. * list.add(e);
  104. * return list.toArray(a);
  105. * }</pre>
  106. *
  107. * @throws ArrayStoreException {@inheritDoc}
  108. * @throws NullPointerException {@inheritDoc}
  109. */
  110. public <T> T[] toArray(T[] a) {
  111. // Estimate size of array; be prepared to see more or fewer elements
  112. int size = size();
  113. T[] r = a.length >= size ? a :
  114. (T[])java.lang.reflect.Array
  115. .newInstance(a.getClass().getComponentType(), size);
  116. Iterator<E> it = iterator();
  117. for (int i = 0; i < r.length; i++) {
  118. if (! it.hasNext()) { // fewer elements than expected
  119. if (a == r) {
  120. r[i] = null; // null-terminate
  121. } else if (a.length < i) {
  122. return Arrays.copyOf(r, i);
  123. } else {
  124. System.arraycopy(r, 0, a, 0, i);
  125. if (a.length > i) {
  126. a[i] = null;
  127. }
  128. }
  129. return a;
  130. }
  131. r[i] = (T)it.next();
  132. }
  133. // more elements than expected
  134. return it.hasNext() ? finishToArray(r, it) : r;
  135. }
  136. /**
  137. * The maximum size of array to allocate.
  138. * Some VMs reserve some header words in an array.
  139. * Attempts to allocate larger arrays may result in
  140. * OutOfMemoryError: Requested array size exceeds VM limit
  141. */
  142. private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
  143. /**
  144. * Reallocates the array being used within toArray when the iterator
  145. * returned more elements than expected, and finishes filling it from
  146. * the iterator.
  147. *
  148. * @param r the array, replete with previously stored elements
  149. * @param it the in-progress iterator over this collection
  150. * @return array containing the elements in the given array, plus any
  151. * further elements returned by the iterator, trimmed to size
  152. */
  153. private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
  154. int i = r.length;
  155. while (it.hasNext()) {
  156. int cap = r.length;
  157. if (i == cap) {
  158. int newCap = cap + (cap >> 1) + 1;
  159. // overflow-conscious code
  160. if (newCap - MAX_ARRAY_SIZE > 0)
  161. newCap = hugeCapacity(cap + 1);
  162. r = Arrays.copyOf(r, newCap);
  163. }
  164. r[i++] = (T)it.next();
  165. }
  166. // trim if overallocated
  167. return (i == r.length) ? r : Arrays.copyOf(r, i);
  168. }
  169. private static int hugeCapacity(int minCapacity) {
  170. if (minCapacity < 0) // overflow
  171. throw new OutOfMemoryError
  172. ("Required array size too large");
  173. return (minCapacity > MAX_ARRAY_SIZE) ?
  174. Integer.MAX_VALUE :
  175. MAX_ARRAY_SIZE;
  176. }
  177. // Modification Operations
  178. /**
  179. * {@inheritDoc}
  180. *
  181. * <p>This implementation always throws an
  182. * <tt>UnsupportedOperationException</tt>.
  183. *
  184. * @throws UnsupportedOperationException {@inheritDoc}
  185. * @throws ClassCastException {@inheritDoc}
  186. * @throws NullPointerException {@inheritDoc}
  187. * @throws IllegalArgumentException {@inheritDoc}
  188. * @throws IllegalStateException {@inheritDoc}
  189. */
  190. public boolean add(E e) {
  191. throw new UnsupportedOperationException();
  192. }
  193. /**
  194. * {@inheritDoc}
  195. *
  196. * <p>This implementation iterates over the collection looking for the
  197. * specified element. If it finds the element, it removes the element
  198. * from the collection using the iterator's remove method.
  199. *
  200. * <p>Note that this implementation throws an
  201. * <tt>UnsupportedOperationException</tt> if the iterator returned by this
  202. * collection's iterator method does not implement the <tt>remove</tt>
  203. * method and this collection contains the specified object.
  204. *
  205. * @throws UnsupportedOperationException {@inheritDoc}
  206. * @throws ClassCastException {@inheritDoc}
  207. * @throws NullPointerException {@inheritDoc}
  208. */
  209. public boolean remove(Object o) {
  210. Iterator<E> it = iterator();
  211. if (o==null) {
  212. while (it.hasNext()) {
  213. if (it.next()==null) {
  214. it.remove();
  215. return true;
  216. }
  217. }
  218. } else {
  219. while (it.hasNext()) {
  220. if (o.equals(it.next())) {
  221. it.remove();
  222. return true;
  223. }
  224. }
  225. }
  226. return false;
  227. }
  228. // Bulk Operations
  229. /**
  230. * {@inheritDoc}
  231. *
  232. * <p>This implementation iterates over the specified collection,
  233. * checking each element returned by the iterator in turn to see
  234. * if it's contained in this collection. If all elements are so
  235. * contained <tt>true</tt> is returned, otherwise <tt>false</tt>.
  236. *
  237. * @throws ClassCastException {@inheritDoc}
  238. * @throws NullPointerException {@inheritDoc}
  239. * @see #contains(Object)
  240. */
  241. public boolean containsAll(Collection<?> c) {
  242. for (Object e : c)
  243. if (!contains(e))
  244. return false;
  245. return true;
  246. }
  247. /**
  248. * {@inheritDoc}
  249. *
  250. * <p>This implementation iterates over the specified collection, and adds
  251. * each object returned by the iterator to this collection, in turn.
  252. *
  253. * <p>Note that this implementation will throw an
  254. * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is
  255. * overridden (assuming the specified collection is non-empty).
  256. *
  257. * @throws UnsupportedOperationException {@inheritDoc}
  258. * @throws ClassCastException {@inheritDoc}
  259. * @throws NullPointerException {@inheritDoc}
  260. * @throws IllegalArgumentException {@inheritDoc}
  261. * @throws IllegalStateException {@inheritDoc}
  262. *
  263. * @see #add(Object)
  264. */
  265. public boolean addAll(Collection<? extends E> c) {
  266. boolean modified = false;
  267. for (E e : c)
  268. if (add(e))
  269. modified = true;
  270. return modified;
  271. }
  272. /**
  273. * {@inheritDoc}
  274. *
  275. * <p>This implementation iterates over this collection, checking each
  276. * element returned by the iterator in turn to see if it's contained
  277. * in the specified collection. If it's so contained, it's removed from
  278. * this collection with the iterator's <tt>remove</tt> method.
  279. *
  280. * <p>Note that this implementation will throw an
  281. * <tt>UnsupportedOperationException</tt> if the iterator returned by the
  282. * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
  283. * and this collection contains one or more elements in common with the
  284. * specified collection.
  285. *
  286. * @throws UnsupportedOperationException {@inheritDoc}
  287. * @throws ClassCastException {@inheritDoc}
  288. * @throws NullPointerException {@inheritDoc}
  289. *
  290. * @see #remove(Object)
  291. * @see #contains(Object)
  292. */
  293. public boolean removeAll(Collection<?> c) {
  294. boolean modified = false;
  295. Iterator<?> it = iterator();
  296. while (it.hasNext()) {
  297. if (c.contains(it.next())) {
  298. it.remove();
  299. modified = true;
  300. }
  301. }
  302. return modified;
  303. }
  304. /**
  305. * {@inheritDoc}
  306. *
  307. * <p>This implementation iterates over this collection, checking each
  308. * element returned by the iterator in turn to see if it's contained
  309. * in the specified collection. If it's not so contained, it's removed
  310. * from this collection with the iterator's <tt>remove</tt> method.
  311. *
  312. * <p>Note that this implementation will throw an
  313. * <tt>UnsupportedOperationException</tt> if the iterator returned by the
  314. * <tt>iterator</tt> method does not implement the <tt>remove</tt> method
  315. * and this collection contains one or more elements not present in the
  316. * specified collection.
  317. *
  318. * @throws UnsupportedOperationException {@inheritDoc}
  319. * @throws ClassCastException {@inheritDoc}
  320. * @throws NullPointerException {@inheritDoc}
  321. *
  322. * @see #remove(Object)
  323. * @see #contains(Object)
  324. */
  325. public boolean retainAll(Collection<?> c) {
  326. boolean modified = false;
  327. Iterator<E> it = iterator();
  328. while (it.hasNext()) {
  329. if (!c.contains(it.next())) {
  330. it.remove();
  331. modified = true;
  332. }
  333. }
  334. return modified;
  335. }
  336. /**
  337. * {@inheritDoc}
  338. *
  339. * <p>This implementation iterates over this collection, removing each
  340. * element using the <tt>Iterator.remove</tt> operation. Most
  341. * implementations will probably choose to override this method for
  342. * efficiency.
  343. *
  344. * <p>Note that this implementation will throw an
  345. * <tt>UnsupportedOperationException</tt> if the iterator returned by this
  346. * collection's <tt>iterator</tt> method does not implement the
  347. * <tt>remove</tt> method and this collection is non-empty.
  348. *
  349. * @throws UnsupportedOperationException {@inheritDoc}
  350. */
  351. public void clear() {
  352. Iterator<E> it = iterator();
  353. while (it.hasNext()) {
  354. it.next();
  355. it.remove();
  356. }
  357. }
  358. // String conversion
  359. /**
  360. * Returns a string representation of this collection. The string
  361. * representation consists of a list of the collection's elements in the
  362. * order they are returned by its iterator, enclosed in square brackets
  363. * (<tt>"[]"</tt>). Adjacent elements are separated by the characters
  364. * <tt>", "</tt> (comma and space). Elements are converted to strings as
  365. * by {@link String#valueOf(Object)}.
  366. *
  367. * @return a string representation of this collection
  368. */
  369. public String toString() {
  370. Iterator<E> it = iterator();
  371. if (! it.hasNext())
  372. return "[]";
  373. StringBuilder sb = new StringBuilder();
  374. sb.append('[');
  375. for (;;) {
  376. E e = it.next();
  377. if(e instanceof String){
  378. sb.append(e == this ? "(this Collection)" :"'"+ e+"'");
  379. }else{
  380. sb.append(e == this ? "(this Collection)" : e);
  381. }
  382. if (! it.hasNext())
  383. return sb.append(']').toString();
  384. sb.append(',').append(' ');
  385. }
  386. }
  387. }