home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / util / Collection.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  16.7 KB  |  391 lines

  1. /*
  2.  * @(#)Collection.java    1.21 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.util;
  16.  
  17. /**
  18.  * The root interface in the <i>collection hierarchy</i>.  A Collection
  19.  * represents a group of Objects, known as its <i>elements</i>.  <em>Bags</em>
  20.  * (unordered collections that may contain duplicate elements) implement this
  21.  * interface directly, rather than implementing one of its "subinterfaces",
  22.  * List or Set.
  23.  * <p>
  24.  * The interfaces that comprise the collection hierarchy are designed to allow
  25.  * manipulation of collections in an implementation-independent fashion.  This
  26.  * allows for interoperability among unrelated APIs that take collections as
  27.  * input or return them as output.  It reduces the effort in learning new APIs
  28.  * that would otherwise have their own collection interfaces, reduces the
  29.  * effort in designing such APIs, and fosters software reuse.
  30.  * <p>
  31.  * The collection hierarchy consists of four interfaces, the <i>core
  32.  * collection intefaces</i>.  Two of these interfaces, Set and List, are
  33.  * children of the Collection interface; they add further constraints on the
  34.  * contracts imposed by the methods in this interface, as well as adding new
  35.  * methods.  The final core collection interface, Map, is not a child of
  36.  * Collection, as it represents a mapping rather than a true collection.
  37.  * <p>
  38.  * All of the methods in Collection and the other core interfaces that modify
  39.  * the collection are labeled <i>optional</i>.  Some implementations may not
  40.  * perform one or more of these operations, throwing a runtime exception,n
  41.  * UnsupportedOperationException, if they are attempted.  Implementations
  42.  * should specify in their documentation which optional operations they
  43.  * support.  Several terms are introduced to aid in this specification:
  44.  * <ul>
  45.  *   <li>Collections that do not support any modification operations (such
  46.  *   as add, remove, clear) are referred to as <i>unmodifiable</i>.
  47.  *   Collections that are not unmodifiable are referred to <i>modifiable.</i>
  48.  *
  49.  *   <li>Collections that additionally guarantee that no change in the
  50.  *   Collection will ever be observable via "query" operations (such as
  51.  *   iterator, size, contains) are referred to as <i>immutable</i>.
  52.  *   Collections that are not immutable are referred to as <i>mutable</i>.
  53.  *
  54.  *   <li>Lists that guarantee that their size will remain constant even though
  55.  *   the elements may change are referred to as <i>fixed-size</i>.  Lists that
  56.  *   are not fixed-size are referred to as <i>variable-size</i>.
  57.  * </ul><p>
  58.  * Some implementations may restrict what elements (or in the case of Maps,
  59.  * keys and values) may be stored.  Possible restrictions include requiring
  60.  * elements to:<ul>
  61.  *      <li>be of a particular type.
  62.  *      <li>be comparable to other elements in the collection.
  63.  *      <li>be non-null.
  64.  *      <li>obey some arbitrary predicate.
  65.  * </ul>Attempting to add an element that violates an implementation's
  66.  * restrictions will result in a runtime exception, typically a
  67.  * ClassCastException, an IllegalArgumentException or a NullPointerException.
  68.  * Attempting to remove or test for such an element may result in such an
  69.  * exception, though some "restricted collections" may permit this usage.
  70.  * <p>
  71.  * All general-purpose Collection implementation classes should provide two
  72.  * "standard" constructors:  a void (no arguments) constructor, which creates
  73.  * an empty Collection, and a constructor with a single argument of type
  74.  * Collection, which creates a new Collection with the same elements as its
  75.  * argument.  In effect, the latter constructor allows the user to copy any
  76.  * Collection, producing an equivalent Collection of the desired
  77.  * implementation type.  Similarly, all general-purpose Map implementations
  78.  * should provide a void (no arguments) constructor and a constructor that
  79.  * takes a single argument of type Map.  There is no way to enforce these
  80.  * recommendations (as interfaces cannot contain constructors) but all of the
  81.  * general-purpose Collection and Map implementations in the JDK comply.
  82.  * <p>
  83.  * Collection implementation classes typically have names of the form
  84.  * <<em>Implementation</em>><<em>Interface</em>>.
  85.  * Set implementations in the JDK include HashSet, TreeSet, and ArraySet.
  86.  * List implementations include ArrayList, LinkedList and Vector.  Map
  87.  * implementations include HashMap, TreeMap, ArrayMap, and Hashtable.
  88.  * (The JDK contains no class that implements Collection directly.)
  89.  * All of the JDK implementations with "new style"
  90.  * (<<em>Implementation</em>><<em>Interface</em>>) names
  91.  * are unsynchronized.  The Collections class contains static factories
  92.  * that may be used to add synchronization to any unsynchronized
  93.  * collection.
  94.  * <p>
  95.  * The AbstractCollection, AbstractSet, AbstractList and
  96.  * AbstractSequentialList classes provide skeletal implementations of the
  97.  * core collection interfaces, to minimize the effort required to implement
  98.  * them.  The Iterator, ListIterator, Comparable and Comparator interfaces
  99.  * provide the required "infrastructure." 
  100.  *
  101.  * @author  Josh Bloch
  102.  * @version 1.21 03/18/98
  103.  * @see        Set
  104.  * @see        List
  105.  * @see        Map
  106.  * @see        ArrayList
  107.  * @see        LinkedList
  108.  * @see        Vector
  109.  * @see        HashSet
  110.  * @see        TreeSet
  111.  * @see        HashMap
  112.  * @see        TreeMap
  113.  * @see        Hashtable
  114.  * @see     Collections
  115.  * @see        AbstractCollection
  116.  * @see        AbstractSet
  117.  * @see        AbstractList
  118.  * @see        AbstractSequentialList
  119.  * @see        AbstractMap
  120.  * @see        Iterator
  121.  * @see        ListIterator
  122.  * @see        Comparable
  123.  * @see        Comparator
  124.  * @see        UnsupportedOperationException
  125.  * @see        ConcurrentModificationException
  126.  * @since JDK1.2
  127.  */
  128.  
  129. public interface Collection {
  130.     // Query Operations
  131.  
  132.     /**
  133.      * Returns the number of elements in this Collection.
  134.      *
  135.      * @since JDK1.2
  136.      */
  137.     int size();
  138.  
  139.     /**
  140.      * Returns true if this Collection contains no elements.
  141.      *
  142.      * @since JDK1.2
  143.      */
  144.     boolean isEmpty();
  145.  
  146.     /**
  147.      * Returns true if this Collection contains the specified element.  More
  148.      * formally, returns true if and only if this Collection contains at least
  149.      * one element <code>e</code> such that <code>(o==null ? e==null :
  150.      * o.equals(e))</code>.
  151.      *
  152.      * @param o element whose presence in this Collection is to be tested.
  153.      * @since JDK1.2
  154.      */
  155.     boolean contains(Object o);
  156.  
  157.     /**
  158.      * Returns an Iterator over the elements in this Collection.  There are
  159.      * no guarantees concerning the order in which the elements are returned
  160.      * (unless this Collection is an instance of some class that provides a
  161.      * guarantee).
  162.      *
  163.      * @since JDK1.2
  164.      */
  165.     Iterator iterator();
  166.  
  167.     /**
  168.      * Returns an array containing all of the elements in this Collection.  If
  169.      * the Collection makes any guarantees as to what order its elements are
  170.      * returned by its Iterator, this method must return the elements in the
  171.      * same order.  The returned array will be "safe" in that no references to
  172.      * it are maintained by the Collection.  (In other words, this method must
  173.      * allocate a new array even if the Collection is backed by an Array).
  174.      * The caller is thus free to modify the returned array.
  175.      * <p>
  176.      * This method acts as bridge between array-based and Collection-based
  177.      * APIs.
  178.      *
  179.      * @since JDK1.2
  180.      */
  181.     Object[] toArray();
  182.  
  183.     /**
  184.      * Returns an array containing all of the elements in this Collection,
  185.      * whose runtime type is that of the specified array.  If the Collection
  186.      * fits in the specified array, it is returned therein.  Otherwise,
  187.      * a new array is allocated with the runtime type of the specified array
  188.      * and the size of this Collection.
  189.      * <p>
  190.      * If the Collection fits in the specified array with room to spare
  191.      * (i.e., the array has more elements than the Collection), 
  192.      * the element in the array immediately following the end of the
  193.      * collection is set to null.  This is useful in determining the length
  194.      * of the Collection <em>only</em> if the caller knows that the Collection
  195.      * does not contain any null elements.)
  196.      * <p>
  197.      * If this Collection makes any guarantees as to what order its elements
  198.      * are returned by its Iterator, this method must return the elements in
  199.      * the same order. 
  200.      * <p>
  201.      * Like <code>toArray()</code>, this method acts as bridge between
  202.      * array-based and Collection-based APIs.  Further, this method allows
  203.      * precise control over the runtime type of the output array, and
  204.      * may, under certain circumstances, be used to save allocation costs
  205.      * <p>
  206.      * Suppose l is a List known to contain only strings.  The following code
  207.      * can be used to dump the list into a newly allocated array of String:
  208.      * <pre>
  209.      * String[] x = (String[]) v.toArray(new String[0]);
  210.      * </pre>
  211.      * <p>
  212.      * Note that <code>toArray(new Object[0])</code> is identical in
  213.      * function to <code>toArray()</code>.
  214.      *
  215.      * @param a the array into which the elements of the Collection are to
  216.      *        be stored, if it is big enough; otherwise, a new array of the
  217.      *         same runtime type is allocated for this purpose.
  218.      * @return an array containing the elements of the Collection.
  219.      * @exception NullPointerException The specified array, a, is null.
  220.      * @exception ArrayStoreException the runtime type of a is not a supertype
  221.      *          of the runtime type of every element in this Collection.
  222.      */
  223.     Object[] toArray(Object a[]);
  224.  
  225.     // Modification Operations
  226.  
  227.     /**
  228.      * Ensures that this Collection contains the specified element (optional
  229.      * operation).  Returns true if the Collection changed as a result of the
  230.      * call.  (Returns false if this Collection does not permit duplicates and
  231.      * already contains the specified element.)  Collections that support this
  232.      * operation may place limitations on what elements may be added to the
  233.      * Collection.  In particular, some Collections will refuse to add null
  234.      * elements, and others will impose restrictions on the type of elements
  235.      * that may be added.  Collection classes should clearly specify in their
  236.      * documentation any restrictions on what elements may be added.
  237.      *
  238.      * @param o element whose presence in this Collection is to be ensured.
  239.      * @return true if the Collection changed as a result of the call.
  240.      * @exception UnsupportedOperationException add is not supported by this
  241.      *          Collection.
  242.      * @exception ClassCastException class of the specified element
  243.      *           prevents it from being added to this Collection.
  244.      * @exception IllegalArgumentException some aspect of this element prevents
  245.      *          it from being added to this Collection.
  246.      * @since JDK1.2
  247.      */
  248.     boolean add(Object o);
  249.  
  250.     /**
  251.      * Removes a single instance of the specified element from this Collection,
  252.      * if it is present (optional operation).  More formally, removes an
  253.      * element <code>e</code> such that <code>(o==null ? e==null :
  254.      * o.equals(e))</code>, if the Collection contains one or more such
  255.      * elements.  Returns true if the Collection contained the specified
  256.      * element (or equivalently, if the Collection changed as a result of the
  257.      * call).
  258.      *
  259.      * @param o element to be removed from this Collection, if present.
  260.      * @return true if the Collection changed as a result of the call.
  261.      * @exception UnsupportedOperationException remove is not supported
  262.      *          by this Collection.
  263.      * @since JDK1.2
  264.      */
  265.     boolean remove(Object o);
  266.  
  267.  
  268.     // Bulk Operations
  269.  
  270.     /**
  271.      * Returns true if this Collection contains all of the elements in the
  272.      * specified Collection.
  273.      *
  274.      * @see #contains(Object)
  275.      * @since JDK1.2
  276.      */
  277.     boolean containsAll(Collection c);
  278.  
  279.     /**
  280.      * Adds all of the elements in the specified Collection to this Collection
  281.      * (optional operation).  The behavior of this operation is undefined if
  282.      * the specified Collection is modified while the operation is in progress.
  283.      * (This implies that the behavior of this call is undefined if the the
  284.      * specified Collection is this Collection, and this Collection is
  285.      * nonempty.) 
  286.      *
  287.      * @param c elements to be inserted into this Collection.
  288.      * @return true if this Collection changed as a result of the call.
  289.      * @exception UnsupportedOperationException addAll is not supported
  290.      *           by this Collection.
  291.      * @exception ClassCastException class of an element of the specified
  292.      *           Collection prevents it from being added to this Collection.
  293.      * @exception IllegalArgumentException some aspect of an element of
  294.      *          the specified Collection prevents it from being added to
  295.      *          this Collection. 
  296.      * @see #add(Object)
  297.      * @since JDK1.2
  298.      */
  299.     boolean addAll(Collection c);
  300.  
  301.     /**
  302.      * Removes from this Collection all of its elements that are contained in
  303.      * the specified Collection (optional operation).  After this call returns,
  304.      * this Collection will contains no elements in common with the specified
  305.      * Collection.
  306.      *
  307.      * @param c elements to be removed from this Collection.
  308.      * @return true if this Collection changed as a result of the call.
  309.      * @exception UnsupportedOperationException removeAll is not supported
  310.      *           by this Collection.
  311.      * @see #remove(Object)
  312.      * @see #contains(Object)
  313.      * @since JDK1.2
  314.      */
  315.     boolean removeAll(Collection c);
  316.  
  317.     /**
  318.      * Retains only the elements in this Collection that are contained in the
  319.      * specified Collection (optional operation).  In other words, removes from
  320.      * this Collection all of its elements that are not contained in the
  321.      * specified Collection. 
  322.      *
  323.      * @param c elements to be retained in this Collection.
  324.      * @return true if this Collection changed as a result of the call.
  325.      * @exception UnsupportedOperationException retainAll is not supported
  326.      *           by this Collection.
  327.      * @see #remove(Object)
  328.      * @see #contains(Object)
  329.      * @since JDK1.2
  330.      */
  331.     boolean retainAll(Collection c);
  332.  
  333.     /**
  334.      * Removes all of the elements from this Collection (optional operation).
  335.      * The Collection will be empty after this call returns (unless it throws
  336.      * an exception).
  337.      *
  338.      * @exception UnsupportedOperationException clear is not supported
  339.      *          by this Collection.
  340.      * @since JDK1.2
  341.      */
  342.     void clear();
  343.  
  344.  
  345.     // Comparison and hashing
  346.  
  347.     /**
  348.      * Compares the specified Object with this Collection for equality.
  349.      * <p>
  350.      * While Collection adds no stipulations to the general contract for
  351.      * Object.equals, programmers who implement Collection "directly" (in
  352.      * other words, create a class that is a Collection but is not a Set or a
  353.      * List) must exercise care if they choose to override Object.equals.  It
  354.      * is not necessary to do so, and the simplest course of action is to rely
  355.      * on Object's implementation, but the implementer may wish to implement
  356.      * a "value comparison" in place of the default "reference comparison."
  357.      * (Lists and Sets mandate such value comparisons.)
  358.      * <p>
  359.      * The general contract for Object.equals states that equals
  360.      * must be reflexive (in other words, <code>a.equals(b)</code> if and only
  361.      * if <code>b.equals(a)</code>).  The contracts for List.equals and
  362.      * Set.equals state that Lists are only equal to other Lists, and
  363.      * Sets to other Sets.  Thus, a custom equals method for a Collection
  364.      * that is neither a List nor a Set must return false when this
  365.      * Collection is compared to any List or Set.
  366.      *
  367.      * @param o Object to be compared for equality with this Collection.
  368.      * @return true if the specified Object is equal to this Collection.
  369.      * @see Object#equals(Object)
  370.      * @see Set#equals(Object)
  371.      * @see List#equals(Object)
  372.      * @since JDK1.2
  373.      */
  374.     boolean equals(Object o);
  375.  
  376.     /**
  377.      * Returns the hash code value for this Collection.  While Collection
  378.      * adds no stipulations to the general contract for Object.hashCode,
  379.      * programmers should take note that any class that overrides
  380.      * Object.equals must also override Object.hashCode, in order to satisfy
  381.      * the general contract for Object.hashCode.  In particular,
  382.      * <code>c1.equals(c2)</code> implies that
  383.      * <code>c1.hashCode()==c2.hashCode()</code>.
  384.      *
  385.      * @see Object#hashCode()
  386.      * @see Object#equals(Object)
  387.      * @since JDK1.2
  388.      */
  389.     int hashCode();
  390. }
  391.