Collection.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Collection.java -- Interface that represents a collection of objects
  2. Copyright (C) 1998, 2001, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.util;
  32. /**
  33. * Interface that represents a collection of objects. This interface is the
  34. * root of the collection hierarchy, and does not provide any guarantees about
  35. * the order of its elements or whether or not duplicate elements are
  36. * permitted.
  37. * <p>
  38. * All methods of this interface that are defined to modify the collection are
  39. * defined as <dfn>optional</dfn>. An optional operation may throw an
  40. * UnsupportedOperationException if the data backing this collection does not
  41. * support such a modification. This may mean that the data structure is
  42. * immutable, or that it is read-only but may change ("unmodifiable"), or
  43. * that it is modifiable but of fixed size (such as an array), or any number
  44. * of other combinations.
  45. * <p>
  46. * A class that wishes to implement this interface should consider subclassing
  47. * AbstractCollection, which provides basic implementations of most of the
  48. * methods of this interface. Classes that are prepared to make guarantees
  49. * about ordering or about absence of duplicate elements should consider
  50. * implementing List or Set respectively, both of which are subinterfaces of
  51. * Collection.
  52. * <p>
  53. * A general-purpose implementation of the Collection interface should in most
  54. * cases provide at least two constructors: One which takes no arguments and
  55. * creates an empty collection, and one which takes a Collection as an argument
  56. * and returns a collection containing the same elements (that is, creates a
  57. * copy of the argument using its own implementation).
  58. *
  59. * @author Original author unknown
  60. * @author Eric Blake (ebb9@email.byu.edu)
  61. * @author Tom Tromey (tromey@redhat.com)
  62. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  63. * @see List
  64. * @see Set
  65. * @see Map
  66. * @see SortedSet
  67. * @see SortedMap
  68. * @see HashSet
  69. * @see TreeSet
  70. * @see ArrayList
  71. * @see LinkedList
  72. * @see Vector
  73. * @see Collections
  74. * @see Arrays
  75. * @see AbstractCollection
  76. * @since 1.2
  77. * @status updated to 1.4
  78. */
  79. public interface Collection<E> extends Iterable<E>
  80. {
  81. /**
  82. * Add an element to this collection.
  83. *
  84. * @param o the object to add.
  85. * @return true if the collection was modified as a result of this action.
  86. * @throws UnsupportedOperationException if this collection does not
  87. * support the add operation.
  88. * @throws ClassCastException if o cannot be added to this collection due
  89. * to its type.
  90. * @throws NullPointerException if o is null and this collection doesn't
  91. * support the addition of null values.
  92. * @throws IllegalArgumentException if o cannot be added to this
  93. * collection for some other reason.
  94. */
  95. boolean add(E o);
  96. /**
  97. * Add the contents of a given collection to this collection.
  98. *
  99. * @param c the collection to add.
  100. * @return true if the collection was modified as a result of this action.
  101. * @throws UnsupportedOperationException if this collection does not
  102. * support the addAll operation.
  103. * @throws ClassCastException if some element of c cannot be added to this
  104. * collection due to its type.
  105. * @throws NullPointerException if some element of c is null and this
  106. * collection does not support the addition of null values.
  107. * @throws NullPointerException if c itself is null.
  108. * @throws IllegalArgumentException if some element of c cannot be added
  109. * to this collection for some other reason.
  110. */
  111. boolean addAll(Collection<? extends E> c);
  112. /**
  113. * Clear the collection, such that a subsequent call to isEmpty() would
  114. * return true.
  115. *
  116. * @throws UnsupportedOperationException if this collection does not
  117. * support the clear operation.
  118. */
  119. void clear();
  120. /**
  121. * Test whether this collection contains a given object as one of its
  122. * elements.
  123. *
  124. * @param o the element to look for.
  125. * @return true if this collection contains at least one element e such that
  126. * <code>o == null ? e == null : o.equals(e)</code>.
  127. * @throws ClassCastException if the type of o is not a valid type for this
  128. * collection.
  129. * @throws NullPointerException if o is null and this collection doesn't
  130. * support null values.
  131. */
  132. boolean contains(Object o);
  133. /**
  134. * Test whether this collection contains every element in a given collection.
  135. *
  136. * @param c the collection to test for.
  137. * @return true if for every element o in c, contains(o) would return true.
  138. * @throws ClassCastException if the type of any element in c is not a valid
  139. * type for this collection.
  140. * @throws NullPointerException if some element of c is null and this
  141. * collection does not support null values.
  142. * @throws NullPointerException if c itself is null.
  143. */
  144. boolean containsAll(Collection<?> c);
  145. /**
  146. * Test whether this collection is equal to some object. The Collection
  147. * interface does not explicitly require any behaviour from this method, and
  148. * it may be left to the default implementation provided by Object. The Set
  149. * and List interfaces do, however, require specific behaviour from this
  150. * method.
  151. * <p>
  152. * If an implementation of Collection, which is not also an implementation of
  153. * Set or List, should choose to implement this method, it should take care
  154. * to obey the contract of the equals method of Object. In particular, care
  155. * should be taken to return false when o is a Set or a List, in order to
  156. * preserve the symmetry of the relation.
  157. *
  158. * @param o the object to compare to this collection.
  159. * @return true if the o is equal to this collection.
  160. */
  161. boolean equals(Object o);
  162. /**
  163. * Obtain a hash code for this collection. The Collection interface does not
  164. * explicitly require any behaviour from this method, and it may be left to
  165. * the default implementation provided by Object. The Set and List interfaces
  166. * do, however, require specific behaviour from this method.
  167. * <p>
  168. * If an implementation of Collection, which is not also an implementation of
  169. * Set or List, should choose to implement this method, it should take care
  170. * to obey the contract of the hashCode method of Object. Note that this
  171. * method renders it impossible to correctly implement both Set and List, as
  172. * the required implementations are mutually exclusive.
  173. *
  174. * @return a hash code for this collection.
  175. */
  176. int hashCode();
  177. /**
  178. * Test whether this collection is empty, that is, if size() == 0.
  179. *
  180. * @return true if this collection contains no elements.
  181. */
  182. boolean isEmpty();
  183. /**
  184. * Obtain an Iterator over this collection.
  185. *
  186. * @return an Iterator over the elements of this collection, in any order.
  187. */
  188. Iterator<E> iterator();
  189. /**
  190. * Remove a single occurrence of an object from this collection. That is,
  191. * remove an element e, if one exists, such that <code>o == null ? e == null
  192. * : o.equals(e)</code>.
  193. *
  194. * @param o the object to remove.
  195. * @return true if the collection changed as a result of this call, that is,
  196. * if the collection contained at least one occurrence of o.
  197. * @throws UnsupportedOperationException if this collection does not
  198. * support the remove operation.
  199. * @throws ClassCastException if the type of o is not a valid type
  200. * for this collection.
  201. * @throws NullPointerException if o is null and the collection doesn't
  202. * support null values.
  203. */
  204. boolean remove(Object o);
  205. /**
  206. * Remove all elements of a given collection from this collection. That is,
  207. * remove every element e such that c.contains(e).
  208. *
  209. * @param c The collection of objects to be removed.
  210. * @return true if this collection was modified as a result of this call.
  211. * @throws UnsupportedOperationException if this collection does not
  212. * support the removeAll operation.
  213. * @throws ClassCastException if the type of any element in c is not a valid
  214. * type for this collection.
  215. * @throws NullPointerException if some element of c is null and this
  216. * collection does not support removing null values.
  217. * @throws NullPointerException if c itself is null.
  218. */
  219. boolean removeAll(Collection<?> c);
  220. /**
  221. * Remove all elements of this collection that are not contained in a given
  222. * collection. That is, remove every element e such that !c.contains(e).
  223. *
  224. * @param c The collection of objects to be retained.
  225. * @return true if this collection was modified as a result of this call.
  226. * @throws UnsupportedOperationException if this collection does not
  227. * support the retainAll operation.
  228. * @throws ClassCastException if the type of any element in c is not a valid
  229. * type for this collection.
  230. * @throws NullPointerException if some element of c is null and this
  231. * collection does not support retaining null values.
  232. * @throws NullPointerException if c itself is null.
  233. */
  234. boolean retainAll(Collection<?> c);
  235. /**
  236. * Get the number of elements in this collection.
  237. *
  238. * @return the number of elements in the collection.
  239. */
  240. int size();
  241. /**
  242. * Copy the current contents of this collection into an array.
  243. *
  244. * @return an array of type Object[] and length equal to the size of this
  245. * collection, containing the elements currently in this collection, in
  246. * any order.
  247. */
  248. Object[] toArray();
  249. /**
  250. * Copy the current contents of this collection into an array. If the array
  251. * passed as an argument has length less than the size of this collection, an
  252. * array of the same run-time type as a, and length equal to the size of this
  253. * collection, is allocated using Reflection. Otherwise, a itself is used.
  254. * The elements of this collection are copied into it, and if there is space
  255. * in the array, the following element is set to null. The resultant array is
  256. * returned.
  257. * Note: The fact that the following element is set to null is only useful
  258. * if it is known that this collection does not contain any null elements.
  259. *
  260. * @param a the array to copy this collection into.
  261. * @return an array containing the elements currently in this collection, in
  262. * any order.
  263. * @throws ArrayStoreException if the type of any element of the
  264. * collection is not a subtype of the element type of a.
  265. */
  266. <T> T[] toArray(T[] a);
  267. }