⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractcollection.java

📁 this gcc-g++-3.3.1.tar.gz is a source file of gcc, you can learn more about gcc through this codes f
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* AbstractCollection.java -- Abstract implementation of most of Collection   Copyright (C) 1998, 2000, 2001 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.util;import java.lang.reflect.Array;/** * A basic implementation of most of the methods in the Collection interface to * make it easier to create a collection. To create an unmodifiable Collection, * just subclass AbstractCollection and provide implementations of the * iterator() and size() methods. The Iterator returned by iterator() need only * provide implementations of hasNext() and next() (that is, it may throw an * UnsupportedOperationException if remove() is called). To create a modifiable * Collection, you must in addition provide an implementation of the * add(Object) method and the Iterator returned by iterator() must provide an * implementation of remove(). Other methods should be overridden if the * backing data structure allows for a more efficient implementation. The * precise implementation used by AbstractCollection is documented, so that * subclasses can tell which methods could be implemented more efficiently. * <p> * * The programmer should provide a no-argument constructor, and one that * accepts another Collection, as recommended by the Collection interface. * Unfortunately, there is no way to enforce this in Java. * * @author Original author unknown * @author Bryce McKinlay * @author Eric Blake <ebb9@email.byu.edu> * @see Collection * @see AbstractSet * @see AbstractList * @since 1.2 * @status updated to 1.4 */public abstract class AbstractCollection implements Collection{  /**   * The main constructor, for use by subclasses.   */  protected AbstractCollection()  {  }  /**   * Return an Iterator over this collection. The iterator must provide the   * hasNext and next methods and should in addition provide remove if the   * collection is modifiable.   *   * @return an iterator   */  public abstract Iterator iterator();  /**   * Return the number of elements in this collection. If there are more than   * Integer.MAX_VALUE elements, return Integer.MAX_VALUE.   *   * @return the size   */  public abstract int size();  /**   * Add an object to the collection (optional operation). This implementation   * always throws an UnsupportedOperationException - it should be   * overridden if the collection is to be modifiable. If the collection   * does not accept duplicates, simply return false. Collections may specify   * limitations on what may be added.   *   * @param o the object to add   * @return true if the add operation caused the Collection to change   * @throws UnsupportedOperationException if the add operation is not   *         supported on this collection   * @throws NullPointerException if the collection does not support null   * @throws ClassCastException if the object is of the wrong type   * @throws IllegalArgumentException if some aspect of the object prevents   *         it from being added   */  public boolean add(Object o)  {    throw new UnsupportedOperationException();  }  /**   * Add all the elements of a given collection to this collection (optional   * operation). This implementation obtains an Iterator over the given   * collection and iterates over it, adding each element with the   * add(Object) method (thus this method will fail with an   * UnsupportedOperationException if the add method does). The behavior is   * unspecified if the specified collection is modified during the iteration,   * including the special case of trying addAll(this) on a non-empty   * collection.   *   * @param c the collection to add the elements of to this collection   * @return true if the add operation caused the Collection to change   * @throws UnsupportedOperationException if the add operation is not   *         supported on this collection   * @throws NullPointerException if this collection does not support null,   *         or if the specified collection is null   * @throws ClassCastException if an object in c is of the wrong type   * @throws IllegalArgumentException if some aspect of an object in c prevents   *         it from being added   * @see #add(Object)   */  public boolean addAll(Collection c)  {    Iterator itr = c.iterator();    boolean modified = false;    int pos = c.size();    while (--pos >= 0)      modified |= add(itr.next());    return modified;  }  /**   * Remove all elements from the collection (optional operation). This   * implementation obtains an iterator over the collection and calls next   * and remove on it repeatedly (thus this method will fail with an   * UnsupportedOperationException if the Iterator's remove method does)   * until there are no more elements to remove.   * Many implementations will have a faster way of doing this.   *   * @throws UnsupportedOperationException if the Iterator returned by   *         iterator does not provide an implementation of remove   * @see Iterator#remove()   */  public void clear()  {    Iterator itr = iterator();    int pos = size();    while (--pos >= 0)      {        itr.next();        itr.remove();      }  }  /**   * Test whether this collection contains a given object. That is, if the   * collection has an element e such that (o == null ? e == null :   * o.equals(e)). This implementation obtains an iterator over the collection   * and iterates over it, testing each element for equality with the given   * object. If it is equal, true is returned. Otherwise false is returned when   * the end of the collection is reached.   *   * @param o the object to remove from this collection   * @return true if this collection contains an object equal to o   */  public boolean contains(Object o)  {    Iterator itr = iterator();    int pos = size();    while (--pos >= 0)      if (equals(o, itr.next()))        return true;    return false;  }  /**   * Tests whether this collection contains all the elements in a given   * collection. This implementation iterates over the given collection,   * testing whether each element is contained in this collection. If any one   * is not, false is returned. Otherwise true is returned.   *   * @param c the collection to test against   * @return true if this collection contains all the elements in the given   *         collection   * @throws NullPointerException if the given collection is null   * @see #contains(Object)   */  public boolean containsAll(Collection c)  {    Iterator itr = c.iterator();    int pos = c.size();    while (--pos >= 0)      if (!contains(itr.next()))        return false;    return true;  }  /**   * Test whether this collection is empty. This implementation returns   * size() == 0.   *   * @return true if this collection is empty.   * @see #size()   */  public boolean isEmpty()  {    return size() == 0;  }  /**   * Remove a single instance of an object from this collection (optional   * operation). That is, remove one element e such that   * <code>(o == null ? e == null : o.equals(e))</code>, if such an element   * exists. This implementation obtains an iterator over the collection   * and iterates over it, testing each element for equality with the given

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -