15.61/5.16 YES
15.96/5.19 proof of /export/starexec/sandbox/benchmark/theBenchmark.jar
15.96/5.19 # AProVE Commit ID: 48fb2092695e11cc9f56e44b17a92a5f88ffb256 marcel 20180622 unpublished dirty
15.96/5.19
15.96/5.19
15.96/5.19 termination of the given Bare JBC problem could be proven:
15.96/5.19
15.96/5.19 (0) Bare JBC problem
15.96/5.19 (1) BareJBCToJBCProof [EQUIVALENT, 94 ms]
15.96/5.19 (2) JBC problem
15.96/5.19 (3) JBCToGraph [EQUIVALENT, 2277 ms]
15.96/5.19 (4) JBCTerminationGraph
15.96/5.19 (5) TerminationGraphToSCCProof [SOUND, 0 ms]
15.96/5.19 (6) JBCTerminationSCC
15.96/5.19 (7) SCCToIRSProof [SOUND, 373 ms]
15.96/5.19 (8) IRSwT
15.96/5.19 (9) IRSFormatTransformerProof [EQUIVALENT, 0 ms]
15.96/5.19 (10) IRSwT
15.96/5.19 (11) IRSwTTerminationDigraphProof [EQUIVALENT, 129 ms]
15.96/5.19 (12) IRSwT
15.96/5.19 (13) IntTRSCompressionProof [EQUIVALENT, 0 ms]
15.96/5.19 (14) IRSwT
15.96/5.19 (15) TempFilterProof [SOUND, 107 ms]
15.96/5.19 (16) IntTRS
15.96/5.19 (17) PolynomialOrderProcessor [EQUIVALENT, 6 ms]
15.96/5.19 (18) IntTRS
15.96/5.19 (19) PolynomialOrderProcessor [EQUIVALENT, 0 ms]
15.96/5.19 (20) YES
15.96/5.19
15.96/5.19
15.96/5.19 ----------------------------------------
15.96/5.19
15.96/5.19 (0)
15.96/5.19 Obligation:
15.96/5.19 need to prove termination of the following program:
15.96/5.19 /*
15.96/5.19 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.19 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.19 *
15.96/5.19 * This code is free software; you can redistribute it and/or modify it
15.96/5.19 * under the terms of the GNU General Public License version 2 only, as
15.96/5.19 * published by the Free Software Foundation. Sun designates this
15.96/5.19 * particular file as subject to the "Classpath" exception as provided
15.96/5.19 * by Sun in the LICENSE file that accompanied this code.
15.96/5.19 *
15.96/5.19 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.19 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.19 * accompanied this code).
15.96/5.19 *
15.96/5.19 * You should have received a copy of the GNU General Public License version
15.96/5.19 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.19 *
15.96/5.19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.19 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.19 * have any questions.
15.96/5.19 */
15.96/5.19
15.96/5.19 package javaUtilEx;
15.96/5.19
15.96/5.19 /**
15.96/5.19 * This class provides a skeletal implementation of the Collection
15.96/5.19 * interface, to minimize the effort required to implement this interface.
15.96/5.19 *
15.96/5.19 * To implement an unmodifiable collection, the programmer needs only to
15.96/5.19 * extend this class and provide implementations for the iterator and
15.96/5.19 * size methods. (The iterator returned by the iterator
15.96/5.19 * method must implement hasNext and next.)
15.96/5.19 *
15.96/5.19 * To implement a modifiable collection, the programmer must additionally
15.96/5.19 * override this class's add method (which otherwise throws an
15.96/5.19 * UnsupportedOperationException), and the iterator returned by the
15.96/5.19 * iterator method must additionally implement its remove
15.96/5.19 * method.
15.96/5.19 *
15.96/5.19 * The programmer should generally provide a void (no argument) and
15.96/5.19 * Collection constructor, as per the recommendation in the
15.96/5.19 * Collection interface specification.
15.96/5.19 *
15.96/5.19 * The documentation for each non-abstract method in this class describes its
15.96/5.19 * implementation in detail. Each of these methods may be overridden if
15.96/5.19 * the collection being implemented admits a more efficient implementation.
15.96/5.19 *
15.96/5.19 * This class is a member of the
15.96/5.19 *
15.96/5.19 * Java Collections Framework.
15.96/5.19 *
15.96/5.19 * @author Josh Bloch
15.96/5.19 * @author Neal Gafter
15.96/5.19 * @see Collection
15.96/5.19 * @since 1.2
15.96/5.19 */
15.96/5.19
15.96/5.19 public abstract class AbstractCollection implements Collection {
15.96/5.19 /**
15.96/5.19 * Sole constructor. (For invocation by subclass constructors, typically
15.96/5.19 * implicit.)
15.96/5.19 */
15.96/5.19 protected AbstractCollection() {
15.96/5.19 }
15.96/5.19
15.96/5.19 // Query Operations
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Returns an iterator over the elements contained in this collection.
15.96/5.19 *
15.96/5.19 * @return an iterator over the elements contained in this collection
15.96/5.19 */
15.96/5.19 public abstract Iterator iterator();
15.96/5.19
15.96/5.19 public abstract int size();
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * This implementation returns size() == 0.
15.96/5.19 */
15.96/5.19 public boolean isEmpty() {
15.96/5.19 return size() == 0;
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation iterates over the elements in the collection,
15.96/5.19 * checking each element in turn for equality with the specified element.
15.96/5.19 *
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public boolean contains(Object o) {
15.96/5.19 Iterator e = iterator();
15.96/5.19 if (o==null) {
15.96/5.19 while (e.hasNext())
15.96/5.19 if (e.next()==null)
15.96/5.19 return true;
15.96/5.19 } else {
15.96/5.19 while (e.hasNext())
15.96/5.19 if (o.equals(e.next()))
15.96/5.19 return true;
15.96/5.19 }
15.96/5.19 return false;
15.96/5.19 }
15.96/5.19
15.96/5.19 // Modification Operations
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * This implementation always throws an
15.96/5.19 * UnsupportedOperationException.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @throws IllegalArgumentException {@inheritDoc}
15.96/5.19 * @throws IllegalStateException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public boolean add(E e) {
15.96/5.19 throw new UnsupportedOperationException();
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation iterates over the collection looking for the
15.96/5.19 * specified element. If it finds the element, it removes the element
15.96/5.19 * from the collection using the iterator's remove method.
15.96/5.19 *
15.96/5.19 *
Note that this implementation throws an
15.96/5.19 * UnsupportedOperationException if the iterator returned by this
15.96/5.19 * collection's iterator method does not implement the remove
15.96/5.19 * method and this collection contains the specified object.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public boolean remove(Object o) {
15.96/5.19 Iterator e = iterator();
15.96/5.19 if (o==null) {
15.96/5.19 while (e.hasNext()) {
15.96/5.19 if (e.next()==null) {
15.96/5.19 e.remove();
15.96/5.19 return true;
15.96/5.19 }
15.96/5.19 }
15.96/5.19 } else {
15.96/5.19 while (e.hasNext()) {
15.96/5.19 if (o.equals(e.next())) {
15.96/5.19 e.remove();
15.96/5.19 return true;
15.96/5.19 }
15.96/5.19 }
15.96/5.19 }
15.96/5.19 return false;
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 // Bulk Operations
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * This implementation iterates over the specified collection,
15.96/5.19 * checking each element returned by the iterator in turn to see
15.96/5.19 * if it's contained in this collection. If all elements are so
15.96/5.19 * contained true is returned, otherwise false.
15.96/5.19 *
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @see #contains(Object)
15.96/5.19 */
15.96/5.19 public boolean containsAll(Collection> c) {
15.96/5.19 Iterator> e = c.iterator();
15.96/5.19 while (e.hasNext())
15.96/5.19 if (!contains(e.next()))
15.96/5.19 return false;
15.96/5.19 return true;
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation iterates over the specified collection, and adds
15.96/5.19 * each object returned by the iterator to this collection, in turn.
15.96/5.19 *
15.96/5.19 *
Note that this implementation will throw an
15.96/5.19 * UnsupportedOperationException unless add is
15.96/5.19 * overridden (assuming the specified collection is non-empty).
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @throws IllegalArgumentException {@inheritDoc}
15.96/5.19 * @throws IllegalStateException {@inheritDoc}
15.96/5.19 *
15.96/5.19 * @see #add(Object)
15.96/5.19 */
15.96/5.19 public boolean addAll(Collection extends E> c) {
15.96/5.19 boolean modified = false;
15.96/5.19 Iterator extends E> e = c.iterator();
15.96/5.19 while (e.hasNext()) {
15.96/5.19 if (add(e.next()))
15.96/5.19 modified = true;
15.96/5.19 }
15.96/5.19 return modified;
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation iterates over this collection, checking each
15.96/5.19 * element returned by the iterator in turn to see if it's contained
15.96/5.19 * in the specified collection. If it's so contained, it's removed from
15.96/5.19 * this collection with the iterator's remove method.
15.96/5.19 *
15.96/5.19 *
Note that this implementation will throw an
15.96/5.19 * UnsupportedOperationException if the iterator returned by the
15.96/5.19 * iterator method does not implement the remove method
15.96/5.19 * and this collection contains one or more elements in common with the
15.96/5.19 * specified collection.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 *
15.96/5.19 * @see #remove(Object)
15.96/5.19 * @see #contains(Object)
15.96/5.19 */
15.96/5.19 public boolean removeAll(Collection> c) {
15.96/5.19 boolean modified = false;
15.96/5.19 Iterator> e = iterator();
15.96/5.19 while (e.hasNext()) {
15.96/5.19 if (c.contains(e.next())) {
15.96/5.19 e.remove();
15.96/5.19 modified = true;
15.96/5.19 }
15.96/5.19 }
15.96/5.19 return modified;
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation iterates over this collection, checking each
15.96/5.19 * element returned by the iterator in turn to see if it's contained
15.96/5.19 * in the specified collection. If it's not so contained, it's removed
15.96/5.19 * from this collection with the iterator's remove method.
15.96/5.19 *
15.96/5.19 *
Note that this implementation will throw an
15.96/5.19 * UnsupportedOperationException if the iterator returned by the
15.96/5.19 * iterator method does not implement the remove method
15.96/5.19 * and this collection contains one or more elements not present in the
15.96/5.19 * specified collection.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 *
15.96/5.19 * @see #remove(Object)
15.96/5.19 * @see #contains(Object)
15.96/5.19 */
15.96/5.19 public boolean retainAll(Collection> c) {
15.96/5.19 boolean modified = false;
15.96/5.19 Iterator e = iterator();
15.96/5.19 while (e.hasNext()) {
15.96/5.19 if (!c.contains(e.next())) {
15.96/5.19 e.remove();
15.96/5.19 modified = true;
15.96/5.19 }
15.96/5.19 }
15.96/5.19 return modified;
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * This implementation iterates over this collection, removing each
15.96/5.19 * element using the Iterator.remove operation. Most
15.96/5.19 * implementations will probably choose to override this method for
15.96/5.19 * efficiency.
15.96/5.19 *
15.96/5.19 *
Note that this implementation will throw an
15.96/5.19 * UnsupportedOperationException if the iterator returned by this
15.96/5.19 * collection's iterator method does not implement the
15.96/5.19 * remove method and this collection is non-empty.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public void clear() {
15.96/5.19 Iterator e = iterator();
15.96/5.19 while (e.hasNext()) {
15.96/5.19 e.next();
15.96/5.19 e.remove();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 // String conversion
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Returns a string representation of this collection. The string
15.96/5.19 * representation consists of a list of the collection's elements in the
15.96/5.19 * order they are returned by its iterator, enclosed in square brackets
15.96/5.19 * ("[]"). Adjacent elements are separated by the characters
15.96/5.19 * ", " (comma and space). Elements are converted to strings as
15.96/5.19 * by {@link String#valueOf(Object)}.
15.96/5.19 *
15.96/5.19 * @return a string representation of this collection
15.96/5.19 */
15.96/5.19 public String toString() {
15.96/5.19 Iterator i = iterator();
15.96/5.19 if (! i.hasNext())
15.96/5.19 return "[]";
15.96/5.19
15.96/5.19 String sb = "";
15.96/5.19 sb = sb + "[";
15.96/5.19 for (;;) {
15.96/5.19 E e = i.next();
15.96/5.19 sb = sb + (e == this ? "(this Collection)" : e);
15.96/5.19 if (! i.hasNext()) {
15.96/5.19 sb = sb + "]";
15.96/5.19 return sb;
15.96/5.19 }
15.96/5.19 sb = sb + ", ";
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 /*
15.96/5.19 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.19 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.19 *
15.96/5.19 * This code is free software; you can redistribute it and/or modify it
15.96/5.19 * under the terms of the GNU General Public License version 2 only, as
15.96/5.19 * published by the Free Software Foundation. Sun designates this
15.96/5.19 * particular file as subject to the "Classpath" exception as provided
15.96/5.19 * by Sun in the LICENSE file that accompanied this code.
15.96/5.19 *
15.96/5.19 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.19 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.19 * accompanied this code).
15.96/5.19 *
15.96/5.19 * You should have received a copy of the GNU General Public License version
15.96/5.19 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.19 *
15.96/5.19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.19 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.19 * have any questions.
15.96/5.19 */
15.96/5.19
15.96/5.19 package javaUtilEx;
15.96/5.19
15.96/5.19 /**
15.96/5.19 * This class provides a skeletal implementation of the {@link List}
15.96/5.19 * interface to minimize the effort required to implement this interface
15.96/5.19 * backed by a "random access" data store (such as an array). For sequential
15.96/5.19 * access data (such as a linked list), {@link AbstractSequentialList} should
15.96/5.19 * be used in preference to this class.
15.96/5.19 *
15.96/5.19 * To implement an unmodifiable list, the programmer needs only to extend
15.96/5.19 * this class and provide implementations for the {@link #get(int)} and
15.96/5.19 * {@link List#size() size()} methods.
15.96/5.19 *
15.96/5.19 *
To implement a modifiable list, the programmer must additionally
15.96/5.19 * override the {@link #set(int, Object) set(int, E)} method (which otherwise
15.96/5.19 * throws an {@code UnsupportedOperationException}). If the list is
15.96/5.19 * variable-size the programmer must additionally override the
15.96/5.19 * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
15.96/5.19 *
15.96/5.19 *
The programmer should generally provide a void (no argument) and collection
15.96/5.19 * constructor, as per the recommendation in the {@link Collection} interface
15.96/5.19 * specification.
15.96/5.19 *
15.96/5.19 *
Unlike the other abstract collection implementations, the programmer does
15.96/5.19 * not have to provide an iterator implementation; the iterator and
15.96/5.19 * list iterator are implemented by this class, on top of the "random access"
15.96/5.19 * methods:
15.96/5.19 * {@link #get(int)},
15.96/5.19 * {@link #set(int, Object) set(int, E)},
15.96/5.19 * {@link #add(int, Object) add(int, E)} and
15.96/5.19 * {@link #remove(int)}.
15.96/5.19 *
15.96/5.19 *
The documentation for each non-abstract method in this class describes its
15.96/5.19 * implementation in detail. Each of these methods may be overridden if the
15.96/5.19 * collection being implemented admits a more efficient implementation.
15.96/5.19 *
15.96/5.19 *
This class is a member of the
15.96/5.19 *
15.96/5.19 * Java Collections Framework.
15.96/5.19 *
15.96/5.19 * @author Josh Bloch
15.96/5.19 * @author Neal Gafter
15.96/5.19 * @since 1.2
15.96/5.19 */
15.96/5.19
15.96/5.19 public abstract class AbstractList extends AbstractCollection implements List {
15.96/5.19 /**
15.96/5.19 * Sole constructor. (For invocation by subclass constructors, typically
15.96/5.19 * implicit.)
15.96/5.19 */
15.96/5.19 protected AbstractList() {
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Appends the specified element to the end of this list (optional
15.96/5.19 * operation).
15.96/5.19 *
15.96/5.19 * Lists that support this operation may place limitations on what
15.96/5.19 * elements may be added to this list. In particular, some
15.96/5.19 * lists will refuse to add null elements, and others will impose
15.96/5.19 * restrictions on the type of elements that may be added. List
15.96/5.19 * classes should clearly specify in their documentation any restrictions
15.96/5.19 * on what elements may be added.
15.96/5.19 *
15.96/5.19 *
This implementation calls {@code add(size(), e)}.
15.96/5.19 *
15.96/5.19 *
Note that this implementation throws an
15.96/5.19 * {@code UnsupportedOperationException} unless
15.96/5.19 * {@link #add(int, Object) add(int, E)} is overridden.
15.96/5.19 *
15.96/5.19 * @param e element to be appended to this list
15.96/5.19 * @return {@code true} (as specified by {@link Collection#add})
15.96/5.19 * @throws UnsupportedOperationException if the {@code add} operation
15.96/5.19 * is not supported by this list
15.96/5.19 * @throws ClassCastException if the class of the specified element
15.96/5.19 * prevents it from being added to this list
15.96/5.19 * @throws NullPointerException if the specified element is null and this
15.96/5.19 * list does not permit null elements
15.96/5.19 * @throws IllegalArgumentException if some property of this element
15.96/5.19 * prevents it from being added to this list
15.96/5.19 */
15.96/5.19 public boolean add(E e) {
15.96/5.19 add(size(), e);
15.96/5.19 return true;
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 abstract public E get(int index);
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation always throws an
15.96/5.19 * {@code UnsupportedOperationException}.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @throws IllegalArgumentException {@inheritDoc}
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public E set(int index, E element) {
15.96/5.19 throw new UnsupportedOperationException();
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation always throws an
15.96/5.19 * {@code UnsupportedOperationException}.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @throws IllegalArgumentException {@inheritDoc}
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public void add(int index, E element) {
15.96/5.19 throw new UnsupportedOperationException();
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation always throws an
15.96/5.19 * {@code UnsupportedOperationException}.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public E remove(int index) {
15.96/5.19 throw new UnsupportedOperationException();
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 // Search Operations
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation first gets a list iterator (with
15.96/5.19 * {@code listIterator()}). Then, it iterates over the list until the
15.96/5.19 * specified element is found or the end of the list is reached.
15.96/5.19 *
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public int indexOf(Object o) {
15.96/5.19 ListIterator e = listIterator();
15.96/5.19 if (o==null) {
15.96/5.19 while (e.hasNext())
15.96/5.19 if (e.next()==null)
15.96/5.19 return e.previousIndex();
15.96/5.19 } else {
15.96/5.19 while (e.hasNext())
15.96/5.19 if (o.equals(e.next()))
15.96/5.19 return e.previousIndex();
15.96/5.19 }
15.96/5.19 return -1;
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * This implementation first gets a list iterator that points to the end
15.96/5.19 * of the list (with {@code listIterator(size())}). Then, it iterates
15.96/5.19 * backwards over the list until the specified element is found, or the
15.96/5.19 * beginning of the list is reached.
15.96/5.19 *
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public int lastIndexOf(Object o) {
15.96/5.19 ListIterator e = listIterator(size());
15.96/5.19 if (o==null) {
15.96/5.19 while (e.hasPrevious())
15.96/5.19 if (e.previous()==null)
15.96/5.19 return e.nextIndex();
15.96/5.19 } else {
15.96/5.19 while (e.hasPrevious())
15.96/5.19 if (o.equals(e.previous()))
15.96/5.19 return e.nextIndex();
15.96/5.19 }
15.96/5.19 return -1;
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 // Bulk Operations
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Removes all of the elements from this list (optional operation).
15.96/5.19 * The list will be empty after this call returns.
15.96/5.19 *
15.96/5.19 * This implementation calls {@code removeRange(0, size())}.
15.96/5.19 *
15.96/5.19 *
Note that this implementation throws an
15.96/5.19 * {@code UnsupportedOperationException} unless {@code remove(int
15.96/5.19 * index)} or {@code removeRange(int fromIndex, int toIndex)} is
15.96/5.19 * overridden.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException if the {@code clear} operation
15.96/5.19 * is not supported by this list
15.96/5.19 */
15.96/5.19 public void clear() {
15.96/5.19 removeRange(0, size());
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 *
This implementation gets an iterator over the specified collection
15.96/5.19 * and iterates over it, inserting the elements obtained from the
15.96/5.19 * iterator into this list at the appropriate position, one at a time,
15.96/5.19 * using {@code add(int, E)}.
15.96/5.19 * Many implementations will override this method for efficiency.
15.96/5.19 *
15.96/5.19 *
Note that this implementation throws an
15.96/5.19 * {@code UnsupportedOperationException} unless
15.96/5.19 * {@link #add(int, Object) add(int, E)} is overridden.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @throws IllegalArgumentException {@inheritDoc}
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public boolean addAll(int index, Collection extends E> c) {
15.96/5.19 rangeCheckForAdd(index);
15.96/5.19 boolean modified = false;
15.96/5.19 Iterator extends E> e = c.iterator();
15.96/5.19 while (e.hasNext()) {
15.96/5.19 add(index++, e.next());
15.96/5.19 modified = true;
15.96/5.19 }
15.96/5.19 return modified;
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 // Iterators
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Returns an iterator over the elements in this list in proper sequence.
15.96/5.19 *
15.96/5.19 *
This implementation returns a straightforward implementation of the
15.96/5.19 * iterator interface, relying on the backing list's {@code size()},
15.96/5.19 * {@code get(int)}, and {@code remove(int)} methods.
15.96/5.19 *
15.96/5.19 *
Note that the iterator returned by this method will throw an
15.96/5.19 * {@link UnsupportedOperationException} in response to its
15.96/5.19 * {@code remove} method unless the list's {@code remove(int)} method is
15.96/5.19 * overridden.
15.96/5.19 *
15.96/5.19 *
This implementation can be made to throw runtime exceptions in the
15.96/5.19 * face of concurrent modification, as described in the specification
15.96/5.19 * for the (protected) {@link #modCount} field.
15.96/5.19 *
15.96/5.19 * @return an iterator over the elements in this list in proper sequence
15.96/5.19 */
15.96/5.19 public Iterator iterator() {
15.96/5.19 return new Itr();
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * This implementation returns {@code listIterator(0)}.
15.96/5.19 *
15.96/5.19 * @see #listIterator(int)
15.96/5.19 */
15.96/5.19 public ListIterator listIterator() {
15.96/5.19 return listIterator(0);
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * This implementation returns a straightforward implementation of the
15.96/5.19 * {@code ListIterator} interface that extends the implementation of the
15.96/5.19 * {@code Iterator} interface returned by the {@code iterator()} method.
15.96/5.19 * The {@code ListIterator} implementation relies on the backing list's
15.96/5.19 * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
15.96/5.19 * and {@code remove(int)} methods.
15.96/5.19 *
15.96/5.19 *
Note that the list iterator returned by this implementation will
15.96/5.19 * throw an {@link UnsupportedOperationException} in response to its
15.96/5.19 * {@code remove}, {@code set} and {@code add} methods unless the
15.96/5.19 * list's {@code remove(int)}, {@code set(int, E)}, and
15.96/5.19 * {@code add(int, E)} methods are overridden.
15.96/5.19 *
15.96/5.19 *
This implementation can be made to throw runtime exceptions in the
15.96/5.19 * face of concurrent modification, as described in the specification for
15.96/5.19 * the (protected) {@link #modCount} field.
15.96/5.19 *
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public ListIterator listIterator(final int index) {
15.96/5.19 rangeCheckForAdd(index);
15.96/5.19
15.96/5.19 return new ListItr(index);
15.96/5.19 }
15.96/5.19
15.96/5.19 private class Itr implements Iterator {
15.96/5.19 /**
15.96/5.19 * Index of element to be returned by subsequent call to next.
15.96/5.19 */
15.96/5.19 int cursor = 0;
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Index of element returned by most recent call to next or
15.96/5.19 * previous. Reset to -1 if this element is deleted by a call
15.96/5.19 * to remove.
15.96/5.19 */
15.96/5.19 int lastRet = -1;
15.96/5.19
15.96/5.19 /**
15.96/5.19 * The modCount value that the iterator believes that the backing
15.96/5.19 * List should have. If this expectation is violated, the iterator
15.96/5.19 * has detected concurrent modification.
15.96/5.19 */
15.96/5.19 int expectedModCount = modCount;
15.96/5.19
15.96/5.19 public boolean hasNext() {
15.96/5.19 return cursor != size();
15.96/5.19 }
15.96/5.19
15.96/5.19 public E next() {
15.96/5.19 checkForComodification();
15.96/5.19 try {
15.96/5.19 int i = cursor;
15.96/5.19 E next = get(i);
15.96/5.19 lastRet = i;
15.96/5.19 cursor = i + 1;
15.96/5.19 return next;
15.96/5.19 } catch (IndexOutOfBoundsException e) {
15.96/5.19 checkForComodification();
15.96/5.19 throw new NoSuchElementException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 public void remove() {
15.96/5.19 if (lastRet < 0)
15.96/5.19 throw new IllegalStateException();
15.96/5.19 checkForComodification();
15.96/5.19
15.96/5.19 try {
15.96/5.19 AbstractList.this.remove(lastRet);
15.96/5.19 if (lastRet < cursor)
15.96/5.19 cursor--;
15.96/5.19 lastRet = -1;
15.96/5.19 expectedModCount = modCount;
15.96/5.19 } catch (IndexOutOfBoundsException e) {
15.96/5.19 throw new ConcurrentModificationException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 final void checkForComodification() {
15.96/5.19 if (modCount != expectedModCount)
15.96/5.19 throw new ConcurrentModificationException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 private class ListItr extends Itr implements ListIterator {
15.96/5.19 ListItr(int index) {
15.96/5.19 cursor = index;
15.96/5.19 }
15.96/5.19
15.96/5.19 public boolean hasPrevious() {
15.96/5.19 return cursor != 0;
15.96/5.19 }
15.96/5.19
15.96/5.19 public E previous() {
15.96/5.19 checkForComodification();
15.96/5.19 try {
15.96/5.19 int i = cursor - 1;
15.96/5.19 E previous = get(i);
15.96/5.19 lastRet = cursor = i;
15.96/5.19 return previous;
15.96/5.19 } catch (IndexOutOfBoundsException e) {
15.96/5.19 checkForComodification();
15.96/5.19 throw new NoSuchElementException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 public int nextIndex() {
15.96/5.19 return cursor;
15.96/5.19 }
15.96/5.19
15.96/5.19 public int previousIndex() {
15.96/5.19 return cursor-1;
15.96/5.19 }
15.96/5.19
15.96/5.19 public void set(E e) {
15.96/5.19 if (lastRet < 0)
15.96/5.19 throw new IllegalStateException();
15.96/5.19 checkForComodification();
15.96/5.19
15.96/5.19 try {
15.96/5.19 AbstractList.this.set(lastRet, e);
15.96/5.19 expectedModCount = modCount;
15.96/5.19 } catch (IndexOutOfBoundsException ex) {
15.96/5.19 throw new ConcurrentModificationException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 public void add(E e) {
15.96/5.19 checkForComodification();
15.96/5.19
15.96/5.19 try {
15.96/5.19 int i = cursor;
15.96/5.19 AbstractList.this.add(i, e);
15.96/5.19 lastRet = -1;
15.96/5.19 cursor = i + 1;
15.96/5.19 expectedModCount = modCount;
15.96/5.19 } catch (IndexOutOfBoundsException ex) {
15.96/5.19 throw new ConcurrentModificationException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * {@inheritDoc}
15.96/5.19 *
15.96/5.19 * This implementation returns a list that subclasses
15.96/5.19 * {@code AbstractList}. The subclass stores, in private fields, the
15.96/5.19 * offset of the subList within the backing list, the size of the subList
15.96/5.19 * (which can change over its lifetime), and the expected
15.96/5.19 * {@code modCount} value of the backing list. There are two variants
15.96/5.19 * of the subclass, one of which implements {@code RandomAccess}.
15.96/5.19 * If this list implements {@code RandomAccess} the returned list will
15.96/5.19 * be an instance of the subclass that implements {@code RandomAccess}.
15.96/5.19 *
15.96/5.19 *
The subclass's {@code set(int, E)}, {@code get(int)},
15.96/5.19 * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
15.96/5.19 * Collection)} and {@code removeRange(int, int)} methods all
15.96/5.19 * delegate to the corresponding methods on the backing abstract list,
15.96/5.19 * after bounds-checking the index and adjusting for the offset. The
15.96/5.19 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
15.96/5.19 * c)}.
15.96/5.19 *
15.96/5.19 *
The {@code listIterator(int)} method returns a "wrapper object"
15.96/5.19 * over a list iterator on the backing list, which is created with the
15.96/5.19 * corresponding method on the backing list. The {@code iterator} method
15.96/5.19 * merely returns {@code listIterator()}, and the {@code size} method
15.96/5.19 * merely returns the subclass's {@code size} field.
15.96/5.19 *
15.96/5.19 *
All methods first check to see if the actual {@code modCount} of
15.96/5.19 * the backing list is equal to its expected value, and throw a
15.96/5.19 * {@code ConcurrentModificationException} if it is not.
15.96/5.19 *
15.96/5.19 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
15.96/5.19 * {@code (fromIndex < 0 || toIndex > size)}
15.96/5.19 * @throws IllegalArgumentException if the endpoint indices are out of order
15.96/5.19 * {@code (fromIndex > toIndex)}
15.96/5.19 */
15.96/5.19 public List subList(int fromIndex, int toIndex) {
15.96/5.19 return (this instanceof RandomAccess ?
15.96/5.19 new RandomAccessSubList(this, fromIndex, toIndex) :
15.96/5.19 new SubList(this, fromIndex, toIndex));
15.96/5.19 }
15.96/5.19
15.96/5.19 // Comparison and hashing
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Compares the specified object with this list for equality. Returns
15.96/5.19 * {@code true} if and only if the specified object is also a list, both
15.96/5.19 * lists have the same size, and all corresponding pairs of elements in
15.96/5.19 * the two lists are equal. (Two elements {@code e1} and
15.96/5.19 * {@code e2} are equal if {@code (e1==null ? e2==null :
15.96/5.19 * e1.equals(e2))}.) In other words, two lists are defined to be
15.96/5.19 * equal if they contain the same elements in the same order.
15.96/5.19 *
15.96/5.19 * This implementation first checks if the specified object is this
15.96/5.19 * list. If so, it returns {@code true}; if not, it checks if the
15.96/5.19 * specified object is a list. If not, it returns {@code false}; if so,
15.96/5.19 * it iterates over both lists, comparing corresponding pairs of elements.
15.96/5.19 * If any comparison returns {@code false}, this method returns
15.96/5.19 * {@code false}. If either iterator runs out of elements before the
15.96/5.19 * other it returns {@code false} (as the lists are of unequal length);
15.96/5.19 * otherwise it returns {@code true} when the iterations complete.
15.96/5.19 *
15.96/5.19 * @param o the object to be compared for equality with this list
15.96/5.19 * @return {@code true} if the specified object is equal to this list
15.96/5.19 */
15.96/5.19 public boolean equals(Object o) {
15.96/5.19 if (o == this)
15.96/5.19 return true;
15.96/5.19 if (!(o instanceof List))
15.96/5.19 return false;
15.96/5.19
15.96/5.19 ListIterator e1 = listIterator();
15.96/5.19 ListIterator e2 = ((List) o).listIterator();
15.96/5.19 while(e1.hasNext() && e2.hasNext()) {
15.96/5.19 E o1 = e1.next();
15.96/5.19 Object o2 = e2.next();
15.96/5.19 if (!(o1==null ? o2==null : o1.equals(o2)))
15.96/5.19 return false;
15.96/5.19 }
15.96/5.19 return !(e1.hasNext() || e2.hasNext());
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Returns the hash code value for this list.
15.96/5.19 *
15.96/5.19 * This implementation uses exactly the code that is used to define the
15.96/5.19 * list hash function in the documentation for the {@link List#hashCode}
15.96/5.19 * method.
15.96/5.19 *
15.96/5.19 * @return the hash code value for this list
15.96/5.19 */
15.96/5.19 public int hashCode() {
15.96/5.19 int hashCode = 1;
15.96/5.19 Iterator it = this.iterator();
15.96/5.19 while (it.hasNext()) {
15.96/5.19 E e = it.next();
15.96/5.19 hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
15.96/5.19 }
15.96/5.19 return hashCode;
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Removes from this list all of the elements whose index is between
15.96/5.19 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
15.96/5.19 * Shifts any succeeding elements to the left (reduces their index).
15.96/5.19 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
15.96/5.19 * (If {@code toIndex==fromIndex}, this operation has no effect.)
15.96/5.19 *
15.96/5.19 * This method is called by the {@code clear} operation on this list
15.96/5.19 * and its subLists. Overriding this method to take advantage of
15.96/5.19 * the internals of the list implementation can substantially
15.96/5.19 * improve the performance of the {@code clear} operation on this list
15.96/5.19 * and its subLists.
15.96/5.19 *
15.96/5.19 *
This implementation gets a list iterator positioned before
15.96/5.19 * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
15.96/5.19 * followed by {@code ListIterator.remove} until the entire range has
15.96/5.19 * been removed. Note: if {@code ListIterator.remove} requires linear
15.96/5.19 * time, this implementation requires quadratic time.
15.96/5.19 *
15.96/5.19 * @param fromIndex index of first element to be removed
15.96/5.19 * @param toIndex index after last element to be removed
15.96/5.19 */
15.96/5.19 protected void removeRange(int fromIndex, int toIndex) {
15.96/5.19 ListIterator it = listIterator(fromIndex);
15.96/5.19 for (int i=0, n=toIndex-fromIndex; istructurally modified.
15.96/5.19 * Structural modifications are those that change the size of the
15.96/5.19 * list, or otherwise perturb it in such a fashion that iterations in
15.96/5.19 * progress may yield incorrect results.
15.96/5.19 *
15.96/5.19 * This field is used by the iterator and list iterator implementation
15.96/5.19 * returned by the {@code iterator} and {@code listIterator} methods.
15.96/5.19 * If the value of this field changes unexpectedly, the iterator (or list
15.96/5.19 * iterator) will throw a {@code ConcurrentModificationException} in
15.96/5.19 * response to the {@code next}, {@code remove}, {@code previous},
15.96/5.19 * {@code set} or {@code add} operations. This provides
15.96/5.19 * fail-fast behavior, rather than non-deterministic behavior in
15.96/5.19 * the face of concurrent modification during iteration.
15.96/5.19 *
15.96/5.19 *
Use of this field by subclasses is optional. If a subclass
15.96/5.19 * wishes to provide fail-fast iterators (and list iterators), then it
15.96/5.19 * merely has to increment this field in its {@code add(int, E)} and
15.96/5.19 * {@code remove(int)} methods (and any other methods that it overrides
15.96/5.19 * that result in structural modifications to the list). A single call to
15.96/5.19 * {@code add(int, E)} or {@code remove(int)} must add no more than
15.96/5.19 * one to this field, or the iterators (and list iterators) will throw
15.96/5.19 * bogus {@code ConcurrentModificationExceptions}. If an implementation
15.96/5.19 * does not wish to provide fail-fast iterators, this field may be
15.96/5.19 * ignored.
15.96/5.19 */
15.96/5.19 protected transient int modCount = 0;
15.96/5.19
15.96/5.19 private void rangeCheckForAdd(int index) {
15.96/5.19 if (index < 0 || index > size())
15.96/5.19 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
15.96/5.19 }
15.96/5.19
15.96/5.19 private String outOfBoundsMsg(int index) {
15.96/5.19 return "";
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 class SubList extends AbstractList {
15.96/5.19 private final AbstractList l;
15.96/5.19 private final int offset;
15.96/5.19 private int size;
15.96/5.19
15.96/5.19 SubList(AbstractList list, int fromIndex, int toIndex) {
15.96/5.19 if (fromIndex < 0)
15.96/5.19 throw new IndexOutOfBoundsException();
15.96/5.19 if (toIndex > list.size())
15.96/5.19 throw new IndexOutOfBoundsException();
15.96/5.19 if (fromIndex > toIndex)
15.96/5.19 throw new IllegalArgumentException();
15.96/5.19 l = list;
15.96/5.19 offset = fromIndex;
15.96/5.19 size = toIndex - fromIndex;
15.96/5.19 this.modCount = l.modCount;
15.96/5.19 }
15.96/5.19
15.96/5.19 public E set(int index, E element) {
15.96/5.19 rangeCheck(index);
15.96/5.19 checkForComodification();
15.96/5.19 return l.set(index+offset, element);
15.96/5.19 }
15.96/5.19
15.96/5.19 public E get(int index) {
15.96/5.19 rangeCheck(index);
15.96/5.19 checkForComodification();
15.96/5.19 return l.get(index+offset);
15.96/5.19 }
15.96/5.19
15.96/5.19 public int size() {
15.96/5.19 checkForComodification();
15.96/5.19 return size;
15.96/5.19 }
15.96/5.19
15.96/5.19 public void add(int index, E element) {
15.96/5.19 rangeCheckForAdd(index);
15.96/5.19 checkForComodification();
15.96/5.19 l.add(index+offset, element);
15.96/5.19 this.modCount = l.modCount;
15.96/5.19 size++;
15.96/5.19 }
15.96/5.19
15.96/5.19 public E remove(int index) {
15.96/5.19 rangeCheck(index);
15.96/5.19 checkForComodification();
15.96/5.19 E result = l.remove(index+offset);
15.96/5.19 this.modCount = l.modCount;
15.96/5.19 size--;
15.96/5.19 return result;
15.96/5.19 }
15.96/5.19
15.96/5.19 protected void removeRange(int fromIndex, int toIndex) {
15.96/5.19 checkForComodification();
15.96/5.19 l.removeRange(fromIndex+offset, toIndex+offset);
15.96/5.19 this.modCount = l.modCount;
15.96/5.19 size -= (toIndex-fromIndex);
15.96/5.19 }
15.96/5.19
15.96/5.19 public boolean addAll(Collection extends E> c) {
15.96/5.19 return addAll(size, c);
15.96/5.19 }
15.96/5.19
15.96/5.19 public boolean addAll(int index, Collection extends E> c) {
15.96/5.19 rangeCheckForAdd(index);
15.96/5.19 int cSize = c.size();
15.96/5.19 if (cSize==0)
15.96/5.19 return false;
15.96/5.19
15.96/5.19 checkForComodification();
15.96/5.19 l.addAll(offset+index, c);
15.96/5.19 this.modCount = l.modCount;
15.96/5.19 size += cSize;
15.96/5.19 return true;
15.96/5.19 }
15.96/5.19
15.96/5.19 public Iterator iterator() {
15.96/5.19 return listIterator();
15.96/5.19 }
15.96/5.19
15.96/5.19 public ListIterator listIterator(final int index) {
15.96/5.19 checkForComodification();
15.96/5.19 rangeCheckForAdd(index);
15.96/5.19
15.96/5.19 return new ListIterator() {
15.96/5.19 private final ListIterator i = l.listIterator(index+offset);
15.96/5.19
15.96/5.19 public boolean hasNext() {
15.96/5.19 return nextIndex() < size;
15.96/5.19 }
15.96/5.19
15.96/5.19 public E next() {
15.96/5.19 if (hasNext())
15.96/5.19 return i.next();
15.96/5.19 else
15.96/5.19 throw new NoSuchElementException();
15.96/5.19 }
15.96/5.19
15.96/5.19 public boolean hasPrevious() {
15.96/5.19 return previousIndex() >= 0;
15.96/5.19 }
15.96/5.19
15.96/5.19 public E previous() {
15.96/5.19 if (hasPrevious())
15.96/5.19 return i.previous();
15.96/5.19 else
15.96/5.19 throw new NoSuchElementException();
15.96/5.19 }
15.96/5.19
15.96/5.19 public int nextIndex() {
15.96/5.19 return i.nextIndex() - offset;
15.96/5.19 }
15.96/5.19
15.96/5.19 public int previousIndex() {
15.96/5.19 return i.previousIndex() - offset;
15.96/5.19 }
15.96/5.19
15.96/5.19 public void remove() {
15.96/5.19 i.remove();
15.96/5.19 SubList.this.modCount = l.modCount;
15.96/5.19 size--;
15.96/5.19 }
15.96/5.19
15.96/5.19 public void set(E e) {
15.96/5.19 i.set(e);
15.96/5.19 }
15.96/5.19
15.96/5.19 public void add(E e) {
15.96/5.19 i.add(e);
15.96/5.19 SubList.this.modCount = l.modCount;
15.96/5.19 size++;
15.96/5.19 }
15.96/5.19 };
15.96/5.19 }
15.96/5.19
15.96/5.19 public List subList(int fromIndex, int toIndex) {
15.96/5.19 return new SubList(this, fromIndex, toIndex);
15.96/5.19 }
15.96/5.19
15.96/5.19 private void rangeCheck(int index) {
15.96/5.19 if (index < 0 || index >= size)
15.96/5.19 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
15.96/5.19 }
15.96/5.19
15.96/5.19 private void rangeCheckForAdd(int index) {
15.96/5.19 if (index < 0 || index > size)
15.96/5.19 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
15.96/5.19 }
15.96/5.19
15.96/5.19 private String outOfBoundsMsg(int index) {
15.96/5.19 return "";
15.96/5.19 }
15.96/5.19
15.96/5.19 private void checkForComodification() {
15.96/5.19 if (this.modCount != l.modCount)
15.96/5.19 throw new ConcurrentModificationException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 class RandomAccessSubList extends SubList implements RandomAccess {
15.96/5.19 RandomAccessSubList(AbstractList list, int fromIndex, int toIndex) {
15.96/5.19 super(list, fromIndex, toIndex);
15.96/5.19 }
15.96/5.19
15.96/5.19 public List subList(int fromIndex, int toIndex) {
15.96/5.19 return new RandomAccessSubList(this, fromIndex, toIndex);
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 /*
15.96/5.19 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.19 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.19 *
15.96/5.19 * This code is free software; you can redistribute it and/or modify it
15.96/5.19 * under the terms of the GNU General Public License version 2 only, as
15.96/5.19 * published by the Free Software Foundation. Sun designates this
15.96/5.19 * particular file as subject to the "Classpath" exception as provided
15.96/5.19 * by Sun in the LICENSE file that accompanied this code.
15.96/5.19 *
15.96/5.19 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.19 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.19 * accompanied this code).
15.96/5.19 *
15.96/5.19 * You should have received a copy of the GNU General Public License version
15.96/5.19 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.19 *
15.96/5.19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.19 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.19 * have any questions.
15.96/5.19 */
15.96/5.19
15.96/5.19 package javaUtilEx;
15.96/5.19
15.96/5.19 /**
15.96/5.19 * This class provides a skeletal implementation of the List
15.96/5.19 * interface to minimize the effort required to implement this interface
15.96/5.19 * backed by a "sequential access" data store (such as a linked list). For
15.96/5.19 * random access data (such as an array), AbstractList should be used
15.96/5.19 * in preference to this class.
15.96/5.19 *
15.96/5.19 * This class is the opposite of the AbstractList class in the sense
15.96/5.19 * that it implements the "random access" methods (get(int index),
15.96/5.19 * set(int index, E element), add(int index, E element) and
15.96/5.19 * remove(int index)) on top of the list's list iterator, instead of
15.96/5.19 * the other way around.
15.96/5.19 *
15.96/5.19 * To implement a list the programmer needs only to extend this class and
15.96/5.19 * provide implementations for the listIterator and size
15.96/5.19 * methods. For an unmodifiable list, the programmer need only implement the
15.96/5.19 * list iterator's hasNext, next, hasPrevious,
15.96/5.19 * previous and index methods.
15.96/5.19 *
15.96/5.19 * For a modifiable list the programmer should additionally implement the list
15.96/5.19 * iterator's set method. For a variable-size list the programmer
15.96/5.19 * should additionally implement the list iterator's remove and
15.96/5.19 * add methods.
15.96/5.19 *
15.96/5.19 * The programmer should generally provide a void (no argument) and collection
15.96/5.19 * constructor, as per the recommendation in the Collection interface
15.96/5.19 * specification.
15.96/5.19 *
15.96/5.19 * This class is a member of the
15.96/5.19 *
15.96/5.19 * Java Collections Framework.
15.96/5.19 *
15.96/5.19 * @author Josh Bloch
15.96/5.19 * @author Neal Gafter
15.96/5.19 * @see Collection
15.96/5.19 * @see List
15.96/5.19 * @see AbstractList
15.96/5.19 * @see AbstractCollection
15.96/5.19 * @since 1.2
15.96/5.19 */
15.96/5.19
15.96/5.19 public abstract class AbstractSequentialList extends AbstractList {
15.96/5.19 /**
15.96/5.19 * Sole constructor. (For invocation by subclass constructors, typically
15.96/5.19 * implicit.)
15.96/5.19 */
15.96/5.19 protected AbstractSequentialList() {
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Returns the element at the specified position in this list.
15.96/5.19 *
15.96/5.19 * This implementation first gets a list iterator pointing to the
15.96/5.19 * indexed element (with listIterator(index)). Then, it gets
15.96/5.19 * the element using ListIterator.next and returns it.
15.96/5.19 *
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public E get(int index) {
15.96/5.19 try {
15.96/5.19 return listIterator(index).next();
15.96/5.19 } catch (NoSuchElementException exc) {
15.96/5.19 throw new IndexOutOfBoundsException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Replaces the element at the specified position in this list with the
15.96/5.19 * specified element (optional operation).
15.96/5.19 *
15.96/5.19 *
This implementation first gets a list iterator pointing to the
15.96/5.19 * indexed element (with listIterator(index)). Then, it gets
15.96/5.19 * the current element using ListIterator.next and replaces it
15.96/5.19 * with ListIterator.set.
15.96/5.19 *
15.96/5.19 *
Note that this implementation will throw an
15.96/5.19 * UnsupportedOperationException if the list iterator does not
15.96/5.19 * implement the set operation.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @throws IllegalArgumentException {@inheritDoc}
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public E set(int index, E element) {
15.96/5.19 try {
15.96/5.19 ListIterator e = listIterator(index);
15.96/5.19 E oldVal = e.next();
15.96/5.19 e.set(element);
15.96/5.19 return oldVal;
15.96/5.19 } catch (NoSuchElementException exc) {
15.96/5.19 throw new IndexOutOfBoundsException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Inserts the specified element at the specified position in this list
15.96/5.19 * (optional operation). Shifts the element currently at that position
15.96/5.19 * (if any) and any subsequent elements to the right (adds one to their
15.96/5.19 * indices).
15.96/5.19 *
15.96/5.19 * This implementation first gets a list iterator pointing to the
15.96/5.19 * indexed element (with listIterator(index)). Then, it
15.96/5.19 * inserts the specified element with ListIterator.add.
15.96/5.19 *
15.96/5.19 *
Note that this implementation will throw an
15.96/5.19 * UnsupportedOperationException if the list iterator does not
15.96/5.19 * implement the add operation.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @throws IllegalArgumentException {@inheritDoc}
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public void add(int index, E element) {
15.96/5.19 try {
15.96/5.19 listIterator(index).add(element);
15.96/5.19 } catch (NoSuchElementException exc) {
15.96/5.19 throw new IndexOutOfBoundsException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Removes the element at the specified position in this list (optional
15.96/5.19 * operation). Shifts any subsequent elements to the left (subtracts one
15.96/5.19 * from their indices). Returns the element that was removed from the
15.96/5.19 * list.
15.96/5.19 *
15.96/5.19 *
This implementation first gets a list iterator pointing to the
15.96/5.19 * indexed element (with listIterator(index)). Then, it removes
15.96/5.19 * the element with ListIterator.remove.
15.96/5.19 *
15.96/5.19 *
Note that this implementation will throw an
15.96/5.19 * UnsupportedOperationException if the list iterator does not
15.96/5.19 * implement the remove operation.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public E remove(int index) {
15.96/5.19 try {
15.96/5.19 ListIterator e = listIterator(index);
15.96/5.19 E outCast = e.next();
15.96/5.19 e.remove();
15.96/5.19 return outCast;
15.96/5.19 } catch (NoSuchElementException exc) {
15.96/5.19 throw new IndexOutOfBoundsException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 // Bulk Operations
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Inserts all of the elements in the specified collection into this
15.96/5.19 * list at the specified position (optional operation). Shifts the
15.96/5.19 * element currently at that position (if any) and any subsequent
15.96/5.19 * elements to the right (increases their indices). The new elements
15.96/5.19 * will appear in this list in the order that they are returned by the
15.96/5.19 * specified collection's iterator. The behavior of this operation is
15.96/5.19 * undefined if the specified collection is modified while the
15.96/5.19 * operation is in progress. (Note that this will occur if the specified
15.96/5.19 * collection is this list, and it's nonempty.)
15.96/5.19 *
15.96/5.19 * This implementation gets an iterator over the specified collection and
15.96/5.19 * a list iterator over this list pointing to the indexed element (with
15.96/5.19 * listIterator(index)). Then, it iterates over the specified
15.96/5.19 * collection, inserting the elements obtained from the iterator into this
15.96/5.19 * list, one at a time, using ListIterator.add followed by
15.96/5.19 * ListIterator.next (to skip over the added element).
15.96/5.19 *
15.96/5.19 *
Note that this implementation will throw an
15.96/5.19 * UnsupportedOperationException if the list iterator returned by
15.96/5.19 * the listIterator method does not implement the add
15.96/5.19 * operation.
15.96/5.19 *
15.96/5.19 * @throws UnsupportedOperationException {@inheritDoc}
15.96/5.19 * @throws ClassCastException {@inheritDoc}
15.96/5.19 * @throws NullPointerException {@inheritDoc}
15.96/5.19 * @throws IllegalArgumentException {@inheritDoc}
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public boolean addAll(int index, Collection extends E> c) {
15.96/5.19 try {
15.96/5.19 boolean modified = false;
15.96/5.19 ListIterator e1 = listIterator(index);
15.96/5.19 Iterator extends E> e2 = c.iterator();
15.96/5.19 while (e2.hasNext()) {
15.96/5.19 e1.add(e2.next());
15.96/5.19 modified = true;
15.96/5.19 }
15.96/5.19 return modified;
15.96/5.19 } catch (NoSuchElementException exc) {
15.96/5.19 throw new IndexOutOfBoundsException();
15.96/5.19 }
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 // Iterators
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Returns an iterator over the elements in this list (in proper
15.96/5.19 * sequence).
15.96/5.19 *
15.96/5.19 * This implementation merely returns a list iterator over the list.
15.96/5.19 *
15.96/5.19 * @return an iterator over the elements in this list (in proper sequence)
15.96/5.19 */
15.96/5.19 public Iterator iterator() {
15.96/5.19 return listIterator();
15.96/5.19 }
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Returns a list iterator over the elements in this list (in proper
15.96/5.19 * sequence).
15.96/5.19 *
15.96/5.19 * @param index index of first element to be returned from the list
15.96/5.19 * iterator (by a call to the next
method)
15.96/5.19 * @return a list iterator over the elements in this list (in proper
15.96/5.19 * sequence)
15.96/5.19 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.19 */
15.96/5.19 public abstract ListIterator listIterator(int index);
15.96/5.19 }
15.96/5.19
15.96/5.19
15.96/5.19 /*
15.96/5.19 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.19 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.19 *
15.96/5.19 * This code is free software; you can redistribute it and/or modify it
15.96/5.19 * under the terms of the GNU General Public License version 2 only, as
15.96/5.19 * published by the Free Software Foundation. Sun designates this
15.96/5.19 * particular file as subject to the "Classpath" exception as provided
15.96/5.19 * by Sun in the LICENSE file that accompanied this code.
15.96/5.19 *
15.96/5.19 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.19 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.19 * accompanied this code).
15.96/5.19 *
15.96/5.19 * You should have received a copy of the GNU General Public License version
15.96/5.19 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.19 *
15.96/5.19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.19 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.19 * have any questions.
15.96/5.19 */
15.96/5.19
15.96/5.19 package javaUtilEx;
15.96/5.19
15.96/5.19 /**
15.96/5.19 * The root interface in the collection hierarchy. A collection
15.96/5.19 * represents a group of objects, known as its elements. Some
15.96/5.19 * collections allow duplicate elements and others do not. Some are ordered
15.96/5.19 * and others unordered. The JDK does not provide any direct
15.96/5.19 * implementations of this interface: it provides implementations of more
15.96/5.19 * specific subinterfaces like Set and List. This interface
15.96/5.19 * is typically used to pass collections around and manipulate them where
15.96/5.19 * maximum generality is desired.
15.96/5.19 *
15.96/5.19 * Bags or multisets (unordered collections that may contain
15.96/5.19 * duplicate elements) should implement this interface directly.
15.96/5.19 *
15.96/5.19 *
All general-purpose Collection implementation classes (which
15.96/5.19 * typically implement Collection indirectly through one of its
15.96/5.19 * subinterfaces) should provide two "standard" constructors: a void (no
15.96/5.19 * arguments) constructor, which creates an empty collection, and a
15.96/5.19 * constructor with a single argument of type Collection, which
15.96/5.19 * creates a new collection with the same elements as its argument. In
15.96/5.19 * effect, the latter constructor allows the user to copy any collection,
15.96/5.19 * producing an equivalent collection of the desired implementation type.
15.96/5.19 * There is no way to enforce this convention (as interfaces cannot contain
15.96/5.19 * constructors) but all of the general-purpose Collection
15.96/5.19 * implementations in the Java platform libraries comply.
15.96/5.19 *
15.96/5.19 *
The "destructive" methods contained in this interface, that is, the
15.96/5.19 * methods that modify the collection on which they operate, are specified to
15.96/5.19 * throw UnsupportedOperationException if this collection does not
15.96/5.19 * support the operation. If this is the case, these methods may, but are not
15.96/5.19 * required to, throw an UnsupportedOperationException if the
15.96/5.19 * invocation would have no effect on the collection. For example, invoking
15.96/5.19 * the {@link #addAll(Collection)} method on an unmodifiable collection may,
15.96/5.19 * but is not required to, throw the exception if the collection to be added
15.96/5.19 * is empty.
15.96/5.19 *
15.96/5.19 *
Some collection implementations have restrictions on the elements that
15.96/5.19 * they may contain. For example, some implementations prohibit null elements,
15.96/5.19 * and some have restrictions on the types of their elements. Attempting to
15.96/5.19 * add an ineligible element throws an unchecked exception, typically
15.96/5.19 * NullPointerException or ClassCastException. Attempting
15.96/5.19 * to query the presence of an ineligible element may throw an exception,
15.96/5.19 * or it may simply return false; some implementations will exhibit the former
15.96/5.19 * behavior and some will exhibit the latter. More generally, attempting an
15.96/5.19 * operation on an ineligible element whose completion would not result in
15.96/5.19 * the insertion of an ineligible element into the collection may throw an
15.96/5.19 * exception or it may succeed, at the option of the implementation.
15.96/5.19 * Such exceptions are marked as "optional" in the specification for this
15.96/5.19 * interface.
15.96/5.19 *
15.96/5.19 *
It is up to each collection to determine its own synchronization
15.96/5.19 * policy. In the absence of a stronger guarantee by the
15.96/5.19 * implementation, undefined behavior may result from the invocation
15.96/5.19 * of any method on a collection that is being mutated by another
15.96/5.19 * thread; this includes direct invocations, passing the collection to
15.96/5.19 * a method that might perform invocations, and using an existing
15.96/5.19 * iterator to examine the collection.
15.96/5.19 *
15.96/5.19 *
Many methods in Collections Framework interfaces are defined in
15.96/5.19 * terms of the {@link Object#equals(Object) equals} method. For example,
15.96/5.19 * the specification for the {@link #contains(Object) contains(Object o)}
15.96/5.19 * method says: "returns true if and only if this collection
15.96/5.19 * contains at least one element e such that
15.96/5.19 * (o==null ? e==null : o.equals(e))." This specification should
15.96/5.19 * not be construed to imply that invoking Collection.contains
15.96/5.19 * with a non-null argument o will cause o.equals(e) to be
15.96/5.19 * invoked for any element e. Implementations are free to implement
15.96/5.19 * optimizations whereby the equals invocation is avoided, for
15.96/5.19 * example, by first comparing the hash codes of the two elements. (The
15.96/5.19 * {@link Object#hashCode()} specification guarantees that two objects with
15.96/5.19 * unequal hash codes cannot be equal.) More generally, implementations of
15.96/5.19 * the various Collections Framework interfaces are free to take advantage of
15.96/5.19 * the specified behavior of underlying {@link Object} methods wherever the
15.96/5.19 * implementor deems it appropriate.
15.96/5.19 *
15.96/5.19 *
This interface is a member of the
15.96/5.19 *
15.96/5.19 * Java Collections Framework.
15.96/5.19 *
15.96/5.19 * @author Josh Bloch
15.96/5.19 * @author Neal Gafter
15.96/5.19 * @see Set
15.96/5.19 * @see List
15.96/5.19 * @see Map
15.96/5.19 * @see SortedSet
15.96/5.19 * @see SortedMap
15.96/5.19 * @see HashSet
15.96/5.19 * @see TreeSet
15.96/5.19 * @see ArrayList
15.96/5.19 * @see LinkedList
15.96/5.19 * @see Vector
15.96/5.19 * @see Collections
15.96/5.19 * @see Arrays
15.96/5.19 * @see AbstractCollection
15.96/5.19 * @since 1.2
15.96/5.19 */
15.96/5.19
15.96/5.19 public interface Collection {
15.96/5.19 // Query Operations
15.96/5.19
15.96/5.19 /**
15.96/5.19 * Returns the number of elements in this collection. If this collection
15.96/5.19 * contains more than Integer.MAX_VALUE elements, returns
15.96/5.19 * Integer.MAX_VALUE.
15.96/5.19 *
15.96/5.19 * @return the number of elements in this collection
15.96/5.20 */
15.96/5.20 int size();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns true if this collection contains no elements.
15.96/5.20 *
15.96/5.20 * @return true if this collection contains no elements
15.96/5.20 */
15.96/5.20 boolean isEmpty();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns true if this collection contains the specified element.
15.96/5.20 * More formally, returns true if and only if this collection
15.96/5.20 * contains at least one element e such that
15.96/5.20 * (o==null ? e==null : o.equals(e)).
15.96/5.20 *
15.96/5.20 * @param o element whose presence in this collection is to be tested
15.96/5.20 * @return true if this collection contains the specified
15.96/5.20 * element
15.96/5.20 * @throws ClassCastException if the type of the specified element
15.96/5.20 * is incompatible with this collection (optional)
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * collection does not permit null elements (optional)
15.96/5.20 */
15.96/5.20 boolean contains(Object o);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns an iterator over the elements in this collection. There are no
15.96/5.20 * guarantees concerning the order in which the elements are returned
15.96/5.20 * (unless this collection is an instance of some class that provides a
15.96/5.20 * guarantee).
15.96/5.20 *
15.96/5.20 * @return an Iterator over the elements in this collection
15.96/5.20 */
15.96/5.20 Iterator iterator();
15.96/5.20
15.96/5.20 // Modification Operations
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Ensures that this collection contains the specified element (optional
15.96/5.20 * operation). Returns true if this collection changed as a
15.96/5.20 * result of the call. (Returns false if this collection does
15.96/5.20 * not permit duplicates and already contains the specified element.)
15.96/5.20 *
15.96/5.20 * Collections that support this operation may place limitations on what
15.96/5.20 * elements may be added to this collection. In particular, some
15.96/5.20 * collections will refuse to add null elements, and others will
15.96/5.20 * impose restrictions on the type of elements that may be added.
15.96/5.20 * Collection classes should clearly specify in their documentation any
15.96/5.20 * restrictions on what elements may be added.
15.96/5.20 *
15.96/5.20 * If a collection refuses to add a particular element for any reason
15.96/5.20 * other than that it already contains the element, it must throw
15.96/5.20 * an exception (rather than returning false). This preserves
15.96/5.20 * the invariant that a collection always contains the specified element
15.96/5.20 * after this call returns.
15.96/5.20 *
15.96/5.20 * @param e element whose presence in this collection is to be ensured
15.96/5.20 * @return true if this collection changed as a result of the
15.96/5.20 * call
15.96/5.20 * @throws UnsupportedOperationException if the add operation
15.96/5.20 * is not supported by this collection
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * prevents it from being added to this collection
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * collection does not permit null elements
15.96/5.20 * @throws IllegalArgumentException if some property of the element
15.96/5.20 * prevents it from being added to this collection
15.96/5.20 * @throws IllegalStateException if the element cannot be added at this
15.96/5.20 * time due to insertion restrictions
15.96/5.20 */
15.96/5.20 boolean add(E e);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes a single instance of the specified element from this
15.96/5.20 * collection, if it is present (optional operation). More formally,
15.96/5.20 * removes an element e such that
15.96/5.20 * (o==null ? e==null : o.equals(e)), if
15.96/5.20 * this collection contains one or more such elements. Returns
15.96/5.20 * true if this collection contained the specified element (or
15.96/5.20 * equivalently, if this collection changed as a result of the call).
15.96/5.20 *
15.96/5.20 * @param o element to be removed from this collection, if present
15.96/5.20 * @return true if an element was removed as a result of this call
15.96/5.20 * @throws ClassCastException if the type of the specified element
15.96/5.20 * is incompatible with this collection (optional)
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * collection does not permit null elements (optional)
15.96/5.20 * @throws UnsupportedOperationException if the remove operation
15.96/5.20 * is not supported by this collection
15.96/5.20 */
15.96/5.20 boolean remove(Object o);
15.96/5.20
15.96/5.20
15.96/5.20 // Bulk Operations
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns true if this collection contains all of the elements
15.96/5.20 * in the specified collection.
15.96/5.20 *
15.96/5.20 * @param c collection to be checked for containment in this collection
15.96/5.20 * @return true if this collection contains all of the elements
15.96/5.20 * in the specified collection
15.96/5.20 * @throws ClassCastException if the types of one or more elements
15.96/5.20 * in the specified collection are incompatible with this
15.96/5.20 * collection (optional)
15.96/5.20 * @throws NullPointerException if the specified collection contains one
15.96/5.20 * or more null elements and this collection does not permit null
15.96/5.20 * elements (optional), or if the specified collection is null
15.96/5.20 * @see #contains(Object)
15.96/5.20 */
15.96/5.20 boolean containsAll(Collection> c);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Adds all of the elements in the specified collection to this collection
15.96/5.20 * (optional operation). The behavior of this operation is undefined if
15.96/5.20 * the specified collection is modified while the operation is in progress.
15.96/5.20 * (This implies that the behavior of this call is undefined if the
15.96/5.20 * specified collection is this collection, and this collection is
15.96/5.20 * nonempty.)
15.96/5.20 *
15.96/5.20 * @param c collection containing elements to be added to this collection
15.96/5.20 * @return true if this collection changed as a result of the call
15.96/5.20 * @throws UnsupportedOperationException if the addAll operation
15.96/5.20 * is not supported by this collection
15.96/5.20 * @throws ClassCastException if the class of an element of the specified
15.96/5.20 * collection prevents it from being added to this collection
15.96/5.20 * @throws NullPointerException if the specified collection contains a
15.96/5.20 * null element and this collection does not permit null elements,
15.96/5.20 * or if the specified collection is null
15.96/5.20 * @throws IllegalArgumentException if some property of an element of the
15.96/5.20 * specified collection prevents it from being added to this
15.96/5.20 * collection
15.96/5.20 * @throws IllegalStateException if not all the elements can be added at
15.96/5.20 * this time due to insertion restrictions
15.96/5.20 * @see #add(Object)
15.96/5.20 */
15.96/5.20 boolean addAll(Collection extends E> c);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes all of this collection's elements that are also contained in the
15.96/5.20 * specified collection (optional operation). After this call returns,
15.96/5.20 * this collection will contain no elements in common with the specified
15.96/5.20 * collection.
15.96/5.20 *
15.96/5.20 * @param c collection containing elements to be removed from this collection
15.96/5.20 * @return true if this collection changed as a result of the
15.96/5.20 * call
15.96/5.20 * @throws UnsupportedOperationException if the removeAll method
15.96/5.20 * is not supported by this collection
15.96/5.20 * @throws ClassCastException if the types of one or more elements
15.96/5.20 * in this collection are incompatible with the specified
15.96/5.20 * collection (optional)
15.96/5.20 * @throws NullPointerException if this collection contains one or more
15.96/5.20 * null elements and the specified collection does not support
15.96/5.20 * null elements (optional), or if the specified collection is null
15.96/5.20 * @see #remove(Object)
15.96/5.20 * @see #contains(Object)
15.96/5.20 */
15.96/5.20 boolean removeAll(Collection> c);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retains only the elements in this collection that are contained in the
15.96/5.20 * specified collection (optional operation). In other words, removes from
15.96/5.20 * this collection all of its elements that are not contained in the
15.96/5.20 * specified collection.
15.96/5.20 *
15.96/5.20 * @param c collection containing elements to be retained in this collection
15.96/5.20 * @return true if this collection changed as a result of the call
15.96/5.20 * @throws UnsupportedOperationException if the retainAll operation
15.96/5.20 * is not supported by this collection
15.96/5.20 * @throws ClassCastException if the types of one or more elements
15.96/5.20 * in this collection are incompatible with the specified
15.96/5.20 * collection (optional)
15.96/5.20 * @throws NullPointerException if this collection contains one or more
15.96/5.20 * null elements and the specified collection does not permit null
15.96/5.20 * elements (optional), or if the specified collection is null
15.96/5.20 * @see #remove(Object)
15.96/5.20 * @see #contains(Object)
15.96/5.20 */
15.96/5.20 boolean retainAll(Collection> c);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes all of the elements from this collection (optional operation).
15.96/5.20 * The collection will be empty after this method returns.
15.96/5.20 *
15.96/5.20 * @throws UnsupportedOperationException if the clear operation
15.96/5.20 * is not supported by this collection
15.96/5.20 */
15.96/5.20 void clear();
15.96/5.20
15.96/5.20
15.96/5.20 // Comparison and hashing
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Compares the specified object with this collection for equality.
15.96/5.20 *
15.96/5.20 * While the Collection interface adds no stipulations to the
15.96/5.20 * general contract for the Object.equals, programmers who
15.96/5.20 * implement the Collection interface "directly" (in other words,
15.96/5.20 * create a class that is a Collection but is not a Set
15.96/5.20 * or a List) must exercise care if they choose to override the
15.96/5.20 * Object.equals. It is not necessary to do so, and the simplest
15.96/5.20 * course of action is to rely on Object's implementation, but
15.96/5.20 * the implementor may wish to implement a "value comparison" in place of
15.96/5.20 * the default "reference comparison." (The List and
15.96/5.20 * Set interfaces mandate such value comparisons.)
15.96/5.20 *
15.96/5.20 * The general contract for the Object.equals method states that
15.96/5.20 * equals must be symmetric (in other words, a.equals(b) if and
15.96/5.20 * only if b.equals(a)). The contracts for List.equals
15.96/5.20 * and Set.equals state that lists are only equal to other lists,
15.96/5.20 * and sets to other sets. Thus, a custom equals method for a
15.96/5.20 * collection class that implements neither the List nor
15.96/5.20 * Set interface must return false when this collection
15.96/5.20 * is compared to any list or set. (By the same logic, it is not possible
15.96/5.20 * to write a class that correctly implements both the Set and
15.96/5.20 * List interfaces.)
15.96/5.20 *
15.96/5.20 * @param o object to be compared for equality with this collection
15.96/5.20 * @return true if the specified object is equal to this
15.96/5.20 * collection
15.96/5.20 *
15.96/5.20 * @see Object#equals(Object)
15.96/5.20 * @see Set#equals(Object)
15.96/5.20 * @see List#equals(Object)
15.96/5.20 */
15.96/5.20 boolean equals(Object o);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the hash code value for this collection. While the
15.96/5.20 * Collection interface adds no stipulations to the general
15.96/5.20 * contract for the Object.hashCode method, programmers should
15.96/5.20 * take note that any class that overrides the Object.equals
15.96/5.20 * method must also override the Object.hashCode method in order
15.96/5.20 * to satisfy the general contract for the Object.hashCodemethod.
15.96/5.20 * In particular, c1.equals(c2) implies that
15.96/5.20 * c1.hashCode()==c2.hashCode().
15.96/5.20 *
15.96/5.20 * @return the hash code value for this collection
15.96/5.20 *
15.96/5.20 * @see Object#hashCode()
15.96/5.20 * @see Object#equals(Object)
15.96/5.20 */
15.96/5.20 int hashCode();
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 /*
15.96/5.20 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.20 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.20 *
15.96/5.20 * This code is free software; you can redistribute it and/or modify it
15.96/5.20 * under the terms of the GNU General Public License version 2 only, as
15.96/5.20 * published by the Free Software Foundation. Sun designates this
15.96/5.20 * particular file as subject to the "Classpath" exception as provided
15.96/5.20 * by Sun in the LICENSE file that accompanied this code.
15.96/5.20 *
15.96/5.20 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.20 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.20 * accompanied this code).
15.96/5.20 *
15.96/5.20 * You should have received a copy of the GNU General Public License version
15.96/5.20 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.20 *
15.96/5.20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.20 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.20 * have any questions.
15.96/5.20 */
15.96/5.20
15.96/5.20 package javaUtilEx;
15.96/5.20
15.96/5.20 /**
15.96/5.20 * This exception may be thrown by methods that have detected concurrent
15.96/5.20 * modification of an object when such modification is not permissible.
15.96/5.20 *
15.96/5.20 * For example, it is not generally permissible for one thread to modify a Collection
15.96/5.20 * while another thread is iterating over it. In general, the results of the
15.96/5.20 * iteration are undefined under these circumstances. Some Iterator
15.96/5.20 * implementations (including those of all the general purpose collection implementations
15.96/5.20 * provided by the JRE) may choose to throw this exception if this behavior is
15.96/5.20 * detected. Iterators that do this are known as fail-fast iterators,
15.96/5.20 * as they fail quickly and cleanly, rather that risking arbitrary,
15.96/5.20 * non-deterministic behavior at an undetermined time in the future.
15.96/5.20 *
15.96/5.20 * Note that this exception does not always indicate that an object has
15.96/5.20 * been concurrently modified by a different thread. If a single
15.96/5.20 * thread issues a sequence of method invocations that violates the
15.96/5.20 * contract of an object, the object may throw this exception. For
15.96/5.20 * example, if a thread modifies a collection directly while it is
15.96/5.20 * iterating over the collection with a fail-fast iterator, the iterator
15.96/5.20 * will throw this exception.
15.96/5.20 *
15.96/5.20 *
Note that fail-fast behavior cannot be guaranteed as it is, generally
15.96/5.20 * speaking, impossible to make any hard guarantees in the presence of
15.96/5.20 * unsynchronized concurrent modification. Fail-fast operations
15.96/5.20 * throw ConcurrentModificationException on a best-effort basis.
15.96/5.20 * Therefore, it would be wrong to write a program that depended on this
15.96/5.20 * exception for its correctness: ConcurrentModificationException
15.96/5.20 * should be used only to detect bugs.
15.96/5.20 *
15.96/5.20 * @author Josh Bloch
15.96/5.20 * @see Collection
15.96/5.20 * @see Iterator
15.96/5.20 * @see ListIterator
15.96/5.20 * @see Vector
15.96/5.20 * @see LinkedList
15.96/5.20 * @see HashSet
15.96/5.20 * @see Hashtable
15.96/5.20 * @see TreeMap
15.96/5.20 * @see AbstractList
15.96/5.20 * @since 1.2
15.96/5.20 */
15.96/5.20 public class ConcurrentModificationException extends RuntimeException {
15.96/5.20 /**
15.96/5.20 * Constructs a ConcurrentModificationException with no
15.96/5.20 * detail message.
15.96/5.20 */
15.96/5.20 public ConcurrentModificationException() {
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs a ConcurrentModificationException with the
15.96/5.20 * specified detail message.
15.96/5.20 *
15.96/5.20 * @param message the detail message pertaining to this exception.
15.96/5.20 */
15.96/5.20 public ConcurrentModificationException(String message) {
15.96/5.20 super(message);
15.96/5.20 }
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 /*
15.96/5.20 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.20 *
15.96/5.20 * This code is free software; you can redistribute it and/or modify it
15.96/5.20 * under the terms of the GNU General Public License version 2 only, as
15.96/5.20 * published by the Free Software Foundation. Sun designates this
15.96/5.20 * particular file as subject to the "Classpath" exception as provided
15.96/5.20 * by Sun in the LICENSE file that accompanied this code.
15.96/5.20 *
15.96/5.20 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.20 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.20 * accompanied this code).
15.96/5.20 *
15.96/5.20 * You should have received a copy of the GNU General Public License version
15.96/5.20 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.20 *
15.96/5.20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.20 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.20 * have any questions.
15.96/5.20 */
15.96/5.20
15.96/5.20 /*
15.96/5.20 * This file is available under and governed by the GNU General Public
15.96/5.20 * License version 2 only, as published by the Free Software Foundation.
15.96/5.20 * However, the following notice accompanied the original version of this
15.96/5.20 * file:
15.96/5.20 *
15.96/5.20 * Written by Doug Lea and Josh Bloch with assistance from members of
15.96/5.20 * JCP JSR-166 Expert Group and released to the public domain, as explained
15.96/5.20 * at http://creativecommons.org/licenses/publicdomain
15.96/5.20 */
15.96/5.20
15.96/5.20 package javaUtilEx;
15.96/5.20
15.96/5.20 /**
15.96/5.20 * A linear collection that supports element insertion and removal at
15.96/5.20 * both ends. The name deque is short for "double ended queue"
15.96/5.20 * and is usually pronounced "deck". Most Deque
15.96/5.20 * implementations place no fixed limits on the number of elements
15.96/5.20 * they may contain, but this interface supports capacity-restricted
15.96/5.20 * deques as well as those with no fixed size limit.
15.96/5.20 *
15.96/5.20 *
This interface defines methods to access the elements at both
15.96/5.20 * ends of the deque. Methods are provided to insert, remove, and
15.96/5.20 * examine the element. Each of these methods exists in two forms:
15.96/5.20 * one throws an exception if the operation fails, the other returns a
15.96/5.20 * special value (either null or false, depending on
15.96/5.20 * the operation). The latter form of the insert operation is
15.96/5.20 * designed specifically for use with capacity-restricted
15.96/5.20 * Deque implementations; in most implementations, insert
15.96/5.20 * operations cannot fail.
15.96/5.20 *
15.96/5.20 *
The twelve methods described above are summarized in the
15.96/5.20 * following table:
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * |
15.96/5.20 * First Element (Head) |
15.96/5.20 * Last Element (Tail) |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * |
15.96/5.20 * Throws exception |
15.96/5.20 * Special value |
15.96/5.20 * Throws exception |
15.96/5.20 * Special value |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * Insert |
15.96/5.20 * {@link #addFirst addFirst(e)} |
15.96/5.20 * {@link #offerFirst offerFirst(e)} |
15.96/5.20 * {@link #addLast addLast(e)} |
15.96/5.20 * {@link #offerLast offerLast(e)} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * Remove |
15.96/5.20 * {@link #removeFirst removeFirst()} |
15.96/5.20 * {@link #pollFirst pollFirst()} |
15.96/5.20 * {@link #removeLast removeLast()} |
15.96/5.20 * {@link #pollLast pollLast()} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * Examine |
15.96/5.20 * {@link #getFirst getFirst()} |
15.96/5.20 * {@link #peekFirst peekFirst()} |
15.96/5.20 * {@link #getLast getLast()} |
15.96/5.20 * {@link #peekLast peekLast()} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * This interface extends the {@link Queue} interface. When a deque is
15.96/5.20 * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are
15.96/5.20 * added at the end of the deque and removed from the beginning. The methods
15.96/5.20 * inherited from the Queue interface are precisely equivalent to
15.96/5.20 * Deque methods as indicated in the following table:
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * Queue Method |
15.96/5.20 * Equivalent Deque Method |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link java.util.Queue#add add(e)} |
15.96/5.20 * {@link #addLast addLast(e)} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link java.util.Queue#offer offer(e)} |
15.96/5.20 * {@link #offerLast offerLast(e)} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link java.util.Queue#remove remove()} |
15.96/5.20 * {@link #removeFirst removeFirst()} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link java.util.Queue#poll poll()} |
15.96/5.20 * {@link #pollFirst pollFirst()} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link java.util.Queue#element element()} |
15.96/5.20 * {@link #getFirst getFirst()} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link java.util.Queue#peek peek()} |
15.96/5.20 * {@link #peek peekFirst()} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * Deques can also be used as LIFO (Last-In-First-Out) stacks. This
15.96/5.20 * interface should be used in preference to the legacy {@link Stack} class.
15.96/5.20 * When a deque is used as a stack, elements are pushed and popped from the
15.96/5.20 * beginning of the deque. Stack methods are precisely equivalent to
15.96/5.20 * Deque methods as indicated in the table below:
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * Stack Method |
15.96/5.20 * Equivalent Deque Method |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link #push push(e)} |
15.96/5.20 * {@link #addFirst addFirst(e)} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link #pop pop()} |
15.96/5.20 * {@link #removeFirst removeFirst()} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * {@link #peek peek()} |
15.96/5.20 * {@link #peekFirst peekFirst()} |
15.96/5.20 *
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * Note that the {@link #peek peek} method works equally well when
15.96/5.20 * a deque is used as a queue or a stack; in either case, elements are
15.96/5.20 * drawn from the beginning of the deque.
15.96/5.20 *
15.96/5.20 *
This interface provides two methods to remove interior
15.96/5.20 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
15.96/5.20 * {@link #removeLastOccurrence removeLastOccurrence}.
15.96/5.20 *
15.96/5.20 *
Unlike the {@link List} interface, this interface does not
15.96/5.20 * provide support for indexed access to elements.
15.96/5.20 *
15.96/5.20 *
While Deque implementations are not strictly required
15.96/5.20 * to prohibit the insertion of null elements, they are strongly
15.96/5.20 * encouraged to do so. Users of any Deque implementations
15.96/5.20 * that do allow null elements are strongly encouraged not to
15.96/5.20 * take advantage of the ability to insert nulls. This is so because
15.96/5.20 * null is used as a special return value by various methods
15.96/5.20 * to indicated that the deque is empty.
15.96/5.20 *
15.96/5.20 *
Deque implementations generally do not define
15.96/5.20 * element-based versions of the equals and hashCode
15.96/5.20 * methods, but instead inherit the identity-based versions from class
15.96/5.20 * Object.
15.96/5.20 *
15.96/5.20 *
This interface is a member of the Java Collections
15.96/5.20 * Framework.
15.96/5.20 *
15.96/5.20 * @author Doug Lea
15.96/5.20 * @author Josh Bloch
15.96/5.20 * @since 1.6
15.96/5.20 * @param the type of elements held in this collection
15.96/5.20 */
15.96/5.20
15.96/5.20 public interface Deque extends Queue {
15.96/5.20 /**
15.96/5.20 * Inserts the specified element at the front of this deque if it is
15.96/5.20 * possible to do so immediately without violating capacity restrictions.
15.96/5.20 * When using a capacity-restricted deque, it is generally preferable to
15.96/5.20 * use method {@link #offerFirst}.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 * @throws IllegalStateException if the element cannot be added at this
15.96/5.20 * time due to capacity restrictions
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * prevents it from being added to this deque
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements
15.96/5.20 * @throws IllegalArgumentException if some property of the specified
15.96/5.20 * element prevents it from being added to this deque
15.96/5.20 */
15.96/5.20 void addFirst(E e);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Inserts the specified element at the end of this deque if it is
15.96/5.20 * possible to do so immediately without violating capacity restrictions.
15.96/5.20 * When using a capacity-restricted deque, it is generally preferable to
15.96/5.20 * use method {@link #offerLast}.
15.96/5.20 *
15.96/5.20 * This method is equivalent to {@link #add}.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 * @throws IllegalStateException if the element cannot be added at this
15.96/5.20 * time due to capacity restrictions
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * prevents it from being added to this deque
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements
15.96/5.20 * @throws IllegalArgumentException if some property of the specified
15.96/5.20 * element prevents it from being added to this deque
15.96/5.20 */
15.96/5.20 void addLast(E e);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Inserts the specified element at the front of this deque unless it would
15.96/5.20 * violate capacity restrictions. When using a capacity-restricted deque,
15.96/5.20 * this method is generally preferable to the {@link #addFirst} method,
15.96/5.20 * which can fail to insert an element only by throwing an exception.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 * @return true if the element was added to this deque, else
15.96/5.20 * false
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * prevents it from being added to this deque
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements
15.96/5.20 * @throws IllegalArgumentException if some property of the specified
15.96/5.20 * element prevents it from being added to this deque
15.96/5.20 */
15.96/5.20 boolean offerFirst(E e);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Inserts the specified element at the end of this deque unless it would
15.96/5.20 * violate capacity restrictions. When using a capacity-restricted deque,
15.96/5.20 * this method is generally preferable to the {@link #addLast} method,
15.96/5.20 * which can fail to insert an element only by throwing an exception.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 * @return true if the element was added to this deque, else
15.96/5.20 * false
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * prevents it from being added to this deque
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements
15.96/5.20 * @throws IllegalArgumentException if some property of the specified
15.96/5.20 * element prevents it from being added to this deque
15.96/5.20 */
15.96/5.20 boolean offerLast(E e);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the first element of this deque. This method
15.96/5.20 * differs from {@link #pollFirst pollFirst} only in that it throws an
15.96/5.20 * exception if this deque is empty.
15.96/5.20 *
15.96/5.20 * @return the head of this deque
15.96/5.20 * @throws NoSuchElementException if this deque is empty
15.96/5.20 */
15.96/5.20 E removeFirst();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the last element of this deque. This method
15.96/5.20 * differs from {@link #pollLast pollLast} only in that it throws an
15.96/5.20 * exception if this deque is empty.
15.96/5.20 *
15.96/5.20 * @return the tail of this deque
15.96/5.20 * @throws NoSuchElementException if this deque is empty
15.96/5.20 */
15.96/5.20 E removeLast();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the first element of this deque,
15.96/5.20 * or returns null if this deque is empty.
15.96/5.20 *
15.96/5.20 * @return the head of this deque, or null if this deque is empty
15.96/5.20 */
15.96/5.20 E pollFirst();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the last element of this deque,
15.96/5.20 * or returns null if this deque is empty.
15.96/5.20 *
15.96/5.20 * @return the tail of this deque, or null if this deque is empty
15.96/5.20 */
15.96/5.20 E pollLast();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the first element of this deque.
15.96/5.20 *
15.96/5.20 * This method differs from {@link #peekFirst peekFirst} only in that it
15.96/5.20 * throws an exception if this deque is empty.
15.96/5.20 *
15.96/5.20 * @return the head of this deque
15.96/5.20 * @throws NoSuchElementException if this deque is empty
15.96/5.20 */
15.96/5.20 E getFirst();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the last element of this deque.
15.96/5.20 * This method differs from {@link #peekLast peekLast} only in that it
15.96/5.20 * throws an exception if this deque is empty.
15.96/5.20 *
15.96/5.20 * @return the tail of this deque
15.96/5.20 * @throws NoSuchElementException if this deque is empty
15.96/5.20 */
15.96/5.20 E getLast();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the first element of this deque,
15.96/5.20 * or returns null if this deque is empty.
15.96/5.20 *
15.96/5.20 * @return the head of this deque, or null if this deque is empty
15.96/5.20 */
15.96/5.20 E peekFirst();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the last element of this deque,
15.96/5.20 * or returns null if this deque is empty.
15.96/5.20 *
15.96/5.20 * @return the tail of this deque, or null if this deque is empty
15.96/5.20 */
15.96/5.20 E peekLast();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes the first occurrence of the specified element from this deque.
15.96/5.20 * If the deque does not contain the element, it is unchanged.
15.96/5.20 * More formally, removes the first element e such that
15.96/5.20 * (o==null ? e==null : o.equals(e))
15.96/5.20 * (if such an element exists).
15.96/5.20 * Returns true if this deque contained the specified element
15.96/5.20 * (or equivalently, if this deque changed as a result of the call).
15.96/5.20 *
15.96/5.20 * @param o element to be removed from this deque, if present
15.96/5.20 * @return true if an element was removed as a result of this call
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * is incompatible with this deque (optional)
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements (optional)
15.96/5.20 */
15.96/5.20 boolean removeFirstOccurrence(Object o);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes the last occurrence of the specified element from this deque.
15.96/5.20 * If the deque does not contain the element, it is unchanged.
15.96/5.20 * More formally, removes the last element e such that
15.96/5.20 * (o==null ? e==null : o.equals(e))
15.96/5.20 * (if such an element exists).
15.96/5.20 * Returns true if this deque contained the specified element
15.96/5.20 * (or equivalently, if this deque changed as a result of the call).
15.96/5.20 *
15.96/5.20 * @param o element to be removed from this deque, if present
15.96/5.20 * @return true if an element was removed as a result of this call
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * is incompatible with this deque (optional)
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements (optional)
15.96/5.20 */
15.96/5.20 boolean removeLastOccurrence(Object o);
15.96/5.20
15.96/5.20 // *** Queue methods ***
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Inserts the specified element into the queue represented by this deque
15.96/5.20 * (in other words, at the tail of this deque) if it is possible to do so
15.96/5.20 * immediately without violating capacity restrictions, returning
15.96/5.20 * true upon success and throwing an
15.96/5.20 * IllegalStateException if no space is currently available.
15.96/5.20 * When using a capacity-restricted deque, it is generally preferable to
15.96/5.20 * use {@link #offer(Object) offer}.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #addLast}.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 * @return true (as specified by {@link Collection#add})
15.96/5.20 * @throws IllegalStateException if the element cannot be added at this
15.96/5.20 * time due to capacity restrictions
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * prevents it from being added to this deque
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements
15.96/5.20 * @throws IllegalArgumentException if some property of the specified
15.96/5.20 * element prevents it from being added to this deque
15.96/5.20 */
15.96/5.20 boolean add(E e);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Inserts the specified element into the queue represented by this deque
15.96/5.20 * (in other words, at the tail of this deque) if it is possible to do so
15.96/5.20 * immediately without violating capacity restrictions, returning
15.96/5.20 * true upon success and false if no space is currently
15.96/5.20 * available. When using a capacity-restricted deque, this method is
15.96/5.20 * generally preferable to the {@link #add} method, which can fail to
15.96/5.20 * insert an element only by throwing an exception.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #offerLast}.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 * @return true if the element was added to this deque, else
15.96/5.20 * false
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * prevents it from being added to this deque
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements
15.96/5.20 * @throws IllegalArgumentException if some property of the specified
15.96/5.20 * element prevents it from being added to this deque
15.96/5.20 */
15.96/5.20 boolean offer(E e);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the head of the queue represented by this deque
15.96/5.20 * (in other words, the first element of this deque).
15.96/5.20 * This method differs from {@link #poll poll} only in that it throws an
15.96/5.20 * exception if this deque is empty.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #removeFirst()}.
15.96/5.20 *
15.96/5.20 * @return the head of the queue represented by this deque
15.96/5.20 * @throws NoSuchElementException if this deque is empty
15.96/5.20 */
15.96/5.20 E remove();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the head of the queue represented by this deque
15.96/5.20 * (in other words, the first element of this deque), or returns
15.96/5.20 * null if this deque is empty.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #pollFirst()}.
15.96/5.20 *
15.96/5.20 * @return the first element of this deque, or null if
15.96/5.20 * this deque is empty
15.96/5.20 */
15.96/5.20 E poll();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the head of the queue represented by
15.96/5.20 * this deque (in other words, the first element of this deque).
15.96/5.20 * This method differs from {@link #peek peek} only in that it throws an
15.96/5.20 * exception if this deque is empty.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #getFirst()}.
15.96/5.20 *
15.96/5.20 * @return the head of the queue represented by this deque
15.96/5.20 * @throws NoSuchElementException if this deque is empty
15.96/5.20 */
15.96/5.20 E element();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the head of the queue represented by
15.96/5.20 * this deque (in other words, the first element of this deque), or
15.96/5.20 * returns null if this deque is empty.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #peekFirst()}.
15.96/5.20 *
15.96/5.20 * @return the head of the queue represented by this deque, or
15.96/5.20 * null if this deque is empty
15.96/5.20 */
15.96/5.20 E peek();
15.96/5.20
15.96/5.20
15.96/5.20 // *** Stack methods ***
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Pushes an element onto the stack represented by this deque (in other
15.96/5.20 * words, at the head of this deque) if it is possible to do so
15.96/5.20 * immediately without violating capacity restrictions, returning
15.96/5.20 * true upon success and throwing an
15.96/5.20 * IllegalStateException if no space is currently available.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #addFirst}.
15.96/5.20 *
15.96/5.20 * @param e the element to push
15.96/5.20 * @throws IllegalStateException if the element cannot be added at this
15.96/5.20 * time due to capacity restrictions
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * prevents it from being added to this deque
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements
15.96/5.20 * @throws IllegalArgumentException if some property of the specified
15.96/5.20 * element prevents it from being added to this deque
15.96/5.20 */
15.96/5.20 void push(E e);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Pops an element from the stack represented by this deque. In other
15.96/5.20 * words, removes and returns the first element of this deque.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #removeFirst()}.
15.96/5.20 *
15.96/5.20 * @return the element at the front of this deque (which is the top
15.96/5.20 * of the stack represented by this deque)
15.96/5.20 * @throws NoSuchElementException if this deque is empty
15.96/5.20 */
15.96/5.20 E pop();
15.96/5.20
15.96/5.20
15.96/5.20 // *** Collection methods ***
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes the first occurrence of the specified element from this deque.
15.96/5.20 * If the deque does not contain the element, it is unchanged.
15.96/5.20 * More formally, removes the first element e such that
15.96/5.20 * (o==null ? e==null : o.equals(e))
15.96/5.20 * (if such an element exists).
15.96/5.20 * Returns true if this deque contained the specified element
15.96/5.20 * (or equivalently, if this deque changed as a result of the call).
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #removeFirstOccurrence}.
15.96/5.20 *
15.96/5.20 * @param o element to be removed from this deque, if present
15.96/5.20 * @return true if an element was removed as a result of this call
15.96/5.20 * @throws ClassCastException if the class of the specified element
15.96/5.20 * is incompatible with this deque (optional)
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements (optional)
15.96/5.20 */
15.96/5.20 boolean remove(Object o);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns true if this deque contains the specified element.
15.96/5.20 * More formally, returns true if and only if this deque contains
15.96/5.20 * at least one element e such that
15.96/5.20 * (o==null ? e==null : o.equals(e)).
15.96/5.20 *
15.96/5.20 * @param o element whose presence in this deque is to be tested
15.96/5.20 * @return true if this deque contains the specified element
15.96/5.20 * @throws ClassCastException if the type of the specified element
15.96/5.20 * is incompatible with this deque (optional)
15.96/5.20 * @throws NullPointerException if the specified element is null and this
15.96/5.20 * deque does not permit null elements (optional)
15.96/5.20 */
15.96/5.20 boolean contains(Object o);
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the number of elements in this deque.
15.96/5.20 *
15.96/5.20 * @return the number of elements in this deque
15.96/5.20 */
15.96/5.20 public int size();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns an iterator over the elements in this deque in proper sequence.
15.96/5.20 * The elements will be returned in order from first (head) to last (tail).
15.96/5.20 *
15.96/5.20 * @return an iterator over the elements in this deque in proper sequence
15.96/5.20 */
15.96/5.20 Iterator iterator();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns an iterator over the elements in this deque in reverse
15.96/5.20 * sequential order. The elements will be returned in order from
15.96/5.20 * last (tail) to first (head).
15.96/5.20 *
15.96/5.20 * @return an iterator over the elements in this deque in reverse
15.96/5.20 * sequence
15.96/5.20 */
15.96/5.20 Iterator descendingIterator();
15.96/5.20
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 /*
15.96/5.20 * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.20 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.20 *
15.96/5.20 * This code is free software; you can redistribute it and/or modify it
15.96/5.20 * under the terms of the GNU General Public License version 2 only, as
15.96/5.20 * published by the Free Software Foundation. Sun designates this
15.96/5.20 * particular file as subject to the "Classpath" exception as provided
15.96/5.20 * by Sun in the LICENSE file that accompanied this code.
15.96/5.20 *
15.96/5.20 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.20 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.20 * accompanied this code).
15.96/5.20 *
15.96/5.20 * You should have received a copy of the GNU General Public License version
15.96/5.20 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.20 *
15.96/5.20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.20 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.20 * have any questions.
15.96/5.20 */
15.96/5.20
15.96/5.20 package javaUtilEx;
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Thrown to indicate that a method has been passed an illegal or
15.96/5.20 * inappropriate argument.
15.96/5.20 *
15.96/5.20 * @author unascribed
15.96/5.20 * @see java.lang.Thread#setPriority(int)
15.96/5.20 * @since JDK1.0
15.96/5.20 */
15.96/5.20 public
15.96/5.20 class IllegalArgumentException extends RuntimeException {
15.96/5.20 /**
15.96/5.20 * Constructs an IllegalArgumentException
with no
15.96/5.20 * detail message.
15.96/5.20 */
15.96/5.20 public IllegalArgumentException() {
15.96/5.20 super();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs an IllegalArgumentException
with the
15.96/5.20 * specified detail message.
15.96/5.20 *
15.96/5.20 * @param s the detail message.
15.96/5.20 */
15.96/5.20 public IllegalArgumentException(String s) {
15.96/5.20 super(s);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs a new exception with the specified detail message and
15.96/5.20 * cause.
15.96/5.20 *
15.96/5.20 * Note that the detail message associated with cause
is
15.96/5.20 * not automatically incorporated in this exception's detail
15.96/5.20 * message.
15.96/5.20 *
15.96/5.20 * @param message the detail message (which is saved for later retrieval
15.96/5.20 * by the {@link Throwable#getMessage()} method).
15.96/5.20 * @param cause the cause (which is saved for later retrieval by the
15.96/5.20 * {@link Throwable#getCause()} method). (A null value
15.96/5.20 * is permitted, and indicates that the cause is nonexistent or
15.96/5.20 * unknown.)
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public IllegalArgumentException(String message, Throwable cause) {
15.96/5.20 super(message, cause);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs a new exception with the specified cause and a detail
15.96/5.20 * message of (cause==null ? null : cause.toString()) (which
15.96/5.20 * typically contains the class and detail message of cause).
15.96/5.20 * This constructor is useful for exceptions that are little more than
15.96/5.20 * wrappers for other throwables (for example, {@link
15.96/5.20 * java.security.PrivilegedActionException}).
15.96/5.20 *
15.96/5.20 * @param cause the cause (which is saved for later retrieval by the
15.96/5.20 * {@link Throwable#getCause()} method). (A null value is
15.96/5.20 * permitted, and indicates that the cause is nonexistent or
15.96/5.20 * unknown.)
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public IllegalArgumentException(Throwable cause) {
15.96/5.20 super(cause);
15.96/5.20 }
15.96/5.20
15.96/5.20 private static final long serialVersionUID = -5365630128856068164L;
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 /*
15.96/5.20 * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.20 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.20 *
15.96/5.20 * This code is free software; you can redistribute it and/or modify it
15.96/5.20 * under the terms of the GNU General Public License version 2 only, as
15.96/5.20 * published by the Free Software Foundation. Sun designates this
15.96/5.20 * particular file as subject to the "Classpath" exception as provided
15.96/5.20 * by Sun in the LICENSE file that accompanied this code.
15.96/5.20 *
15.96/5.20 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.20 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.20 * accompanied this code).
15.96/5.20 *
15.96/5.20 * You should have received a copy of the GNU General Public License version
15.96/5.20 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.20 *
15.96/5.20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.20 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.20 * have any questions.
15.96/5.20 */
15.96/5.20
15.96/5.20 package javaUtilEx;
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Signals that a method has been invoked at an illegal or
15.96/5.20 * inappropriate time. In other words, the Java environment or
15.96/5.20 * Java application is not in an appropriate state for the requested
15.96/5.20 * operation.
15.96/5.20 *
15.96/5.20 * @author Jonni Kanerva
15.96/5.20 * @since JDK1.1
15.96/5.20 */
15.96/5.20 public
15.96/5.20 class IllegalStateException extends RuntimeException {
15.96/5.20 /**
15.96/5.20 * Constructs an IllegalStateException with no detail message.
15.96/5.20 * A detail message is a String that describes this particular exception.
15.96/5.20 */
15.96/5.20 public IllegalStateException() {
15.96/5.20 super();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs an IllegalStateException with the specified detail
15.96/5.20 * message. A detail message is a String that describes this particular
15.96/5.20 * exception.
15.96/5.20 *
15.96/5.20 * @param s the String that contains a detailed message
15.96/5.20 */
15.96/5.20 public IllegalStateException(String s) {
15.96/5.20 super(s);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs a new exception with the specified detail message and
15.96/5.20 * cause.
15.96/5.20 *
15.96/5.20 *
Note that the detail message associated with cause
is
15.96/5.20 * not automatically incorporated in this exception's detail
15.96/5.20 * message.
15.96/5.20 *
15.96/5.20 * @param message the detail message (which is saved for later retrieval
15.96/5.20 * by the {@link Throwable#getMessage()} method).
15.96/5.20 * @param cause the cause (which is saved for later retrieval by the
15.96/5.20 * {@link Throwable#getCause()} method). (A null value
15.96/5.20 * is permitted, and indicates that the cause is nonexistent or
15.96/5.20 * unknown.)
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public IllegalStateException(String message, Throwable cause) {
15.96/5.20 super(message, cause);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs a new exception with the specified cause and a detail
15.96/5.20 * message of (cause==null ? null : cause.toString()) (which
15.96/5.20 * typically contains the class and detail message of cause).
15.96/5.20 * This constructor is useful for exceptions that are little more than
15.96/5.20 * wrappers for other throwables (for example, {@link
15.96/5.20 * java.security.PrivilegedActionException}).
15.96/5.20 *
15.96/5.20 * @param cause the cause (which is saved for later retrieval by the
15.96/5.20 * {@link Throwable#getCause()} method). (A null value is
15.96/5.20 * permitted, and indicates that the cause is nonexistent or
15.96/5.20 * unknown.)
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public IllegalStateException(Throwable cause) {
15.96/5.20 super(cause);
15.96/5.20 }
15.96/5.20
15.96/5.20 static final long serialVersionUID = -1848914673093119416L;
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 /*
15.96/5.20 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.20 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.20 *
15.96/5.20 * This code is free software; you can redistribute it and/or modify it
15.96/5.20 * under the terms of the GNU General Public License version 2 only, as
15.96/5.20 * published by the Free Software Foundation. Sun designates this
15.96/5.20 * particular file as subject to the "Classpath" exception as provided
15.96/5.20 * by Sun in the LICENSE file that accompanied this code.
15.96/5.20 *
15.96/5.20 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.20 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.20 * accompanied this code).
15.96/5.20 *
15.96/5.20 * You should have received a copy of the GNU General Public License version
15.96/5.20 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.20 *
15.96/5.20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.20 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.20 * have any questions.
15.96/5.20 */
15.96/5.20
15.96/5.20 package javaUtilEx;
15.96/5.20
15.96/5.20 /**
15.96/5.20 * An iterator over a collection. {@code Iterator} takes the place of
15.96/5.20 * {@link Enumeration} in the Java Collections Framework. Iterators
15.96/5.20 * differ from enumerations in two ways:
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * - Iterators allow the caller to remove elements from the
15.96/5.20 * underlying collection during the iteration with well-defined
15.96/5.20 * semantics.
15.96/5.20 *
- Method names have been improved.
15.96/5.20 *
15.96/5.20 *
15.96/5.20 * This interface is a member of the
15.96/5.20 *
15.96/5.20 * Java Collections Framework.
15.96/5.20 *
15.96/5.20 * @author Josh Bloch
15.96/5.20 * @see Collection
15.96/5.20 * @see ListIterator
15.96/5.20 * @see Iterable
15.96/5.20 * @since 1.2
15.96/5.20 */
15.96/5.20 public interface Iterator {
15.96/5.20 /**
15.96/5.20 * Returns {@code true} if the iteration has more elements.
15.96/5.20 * (In other words, returns {@code true} if {@link #next} would
15.96/5.20 * return an element rather than throwing an exception.)
15.96/5.20 *
15.96/5.20 * @return {@code true} if the iteration has more elements
15.96/5.20 */
15.96/5.20 boolean hasNext();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the next element in the iteration.
15.96/5.20 *
15.96/5.20 * @return the next element in the iteration
15.96/5.20 * @throws NoSuchElementException if the iteration has no more elements
15.96/5.20 */
15.96/5.20 E next();
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes from the underlying collection the last element returned
15.96/5.20 * by this iterator (optional operation). This method can be called
15.96/5.20 * only once per call to {@link #next}. The behavior of an iterator
15.96/5.20 * is unspecified if the underlying collection is modified while the
15.96/5.20 * iteration is in progress in any way other than by calling this
15.96/5.20 * method.
15.96/5.20 *
15.96/5.20 * @throws UnsupportedOperationException if the {@code remove}
15.96/5.20 * operation is not supported by this iterator
15.96/5.20 *
15.96/5.20 * @throws IllegalStateException if the {@code next} method has not
15.96/5.20 * yet been called, or the {@code remove} method has already
15.96/5.20 * been called after the last call to the {@code next}
15.96/5.20 * method
15.96/5.20 */
15.96/5.20 void remove();
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 package javaUtilEx;
15.96/5.20
15.96/5.20 public class juLinkedListCreateAdd {
15.96/5.20 public static void main(String[] args) {
15.96/5.20 Random.args = args;
15.96/5.20
15.96/5.20 LinkedList l = createList(Random.random());
15.96/5.20 l.add(new Content(Random.random()));
15.96/5.20 }
15.96/5.20
15.96/5.20 public static LinkedList createList(int n) {
15.96/5.20 LinkedList l = new LinkedList();
15.96/5.20 while (n > 0) {
15.96/5.20 l.addLast(new Content(Random.random()));
15.96/5.20 n--;
15.96/5.20 }
15.96/5.20 return l;
15.96/5.20 }
15.96/5.20 }
15.96/5.20
15.96/5.20 final class Content {
15.96/5.20 int val;
15.96/5.20
15.96/5.20 public Content(int v) {
15.96/5.20 this.val = v;
15.96/5.20 }
15.96/5.20
15.96/5.20 public int hashCode() {
15.96/5.20 return val^31;
15.96/5.20 }
15.96/5.20
15.96/5.20 public boolean equals(Object o) {
15.96/5.20 if (o instanceof Content) {
15.96/5.20 return this.val == ((Content) o).val;
15.96/5.20 }
15.96/5.20 return false;
15.96/5.20 }
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 /*
15.96/5.20 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.20 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.20 *
15.96/5.20 * This code is free software; you can redistribute it and/or modify it
15.96/5.20 * under the terms of the GNU General Public License version 2 only, as
15.96/5.20 * published by the Free Software Foundation. Sun designates this
15.96/5.20 * particular file as subject to the "Classpath" exception as provided
15.96/5.20 * by Sun in the LICENSE file that accompanied this code.
15.96/5.20 *
15.96/5.20 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.20 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.20 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.20 * accompanied this code).
15.96/5.20 *
15.96/5.20 * You should have received a copy of the GNU General Public License version
15.96/5.20 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.20 *
15.96/5.20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.20 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.20 * have any questions.
15.96/5.20 */
15.96/5.20
15.96/5.20 package javaUtilEx;
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Linked list implementation of the List interface. Implements all
15.96/5.20 * optional list operations, and permits all elements (including
15.96/5.20 * null). In addition to implementing the List interface,
15.96/5.20 * the LinkedList class provides uniformly named methods to
15.96/5.20 * get, remove and insert an element at the
15.96/5.20 * beginning and end of the list. These operations allow linked lists to be
15.96/5.20 * used as a stack, {@linkplain Queue queue}, or {@linkplain Deque
15.96/5.20 * double-ended queue}.
15.96/5.20 *
15.96/5.20 * The class implements the Deque interface, providing
15.96/5.20 * first-in-first-out queue operations for add,
15.96/5.20 * poll, along with other stack and deque operations.
15.96/5.20 *
15.96/5.20 * All of the operations perform as could be expected for a doubly-linked
15.96/5.20 * list. Operations that index into the list will traverse the list from
15.96/5.20 * the beginning or the end, whichever is closer to the specified index.
15.96/5.20 *
15.96/5.20 *
Note that this implementation is not synchronized.
15.96/5.20 * If multiple threads access a linked list concurrently, and at least
15.96/5.20 * one of the threads modifies the list structurally, it must be
15.96/5.20 * synchronized externally. (A structural modification is any operation
15.96/5.20 * that adds or deletes one or more elements; merely setting the value of
15.96/5.20 * an element is not a structural modification.) This is typically
15.96/5.20 * accomplished by synchronizing on some object that naturally
15.96/5.20 * encapsulates the list.
15.96/5.20 *
15.96/5.20 * If no such object exists, the list should be "wrapped" using the
15.96/5.20 * {@link Collections#synchronizedList Collections.synchronizedList}
15.96/5.20 * method. This is best done at creation time, to prevent accidental
15.96/5.20 * unsynchronized access to the list:
15.96/5.20 * List list = Collections.synchronizedList(new LinkedList(...));
15.96/5.20 *
15.96/5.20 * The iterators returned by this class's iterator and
15.96/5.20 * listIterator methods are fail-fast: if the list is
15.96/5.20 * structurally modified at any time after the iterator is created, in
15.96/5.20 * any way except through the Iterator's own remove or
15.96/5.20 * add methods, the iterator will throw a {@link
15.96/5.20 * ConcurrentModificationException}. Thus, in the face of concurrent
15.96/5.20 * modification, the iterator fails quickly and cleanly, rather than
15.96/5.20 * risking arbitrary, non-deterministic behavior at an undetermined
15.96/5.20 * time in the future.
15.96/5.20 *
15.96/5.20 *
Note that the fail-fast behavior of an iterator cannot be guaranteed
15.96/5.20 * as it is, generally speaking, impossible to make any hard guarantees in the
15.96/5.20 * presence of unsynchronized concurrent modification. Fail-fast iterators
15.96/5.20 * throw ConcurrentModificationException on a best-effort basis.
15.96/5.20 * Therefore, it would be wrong to write a program that depended on this
15.96/5.20 * exception for its correctness: the fail-fast behavior of iterators
15.96/5.20 * should be used only to detect bugs.
15.96/5.20 *
15.96/5.20 *
This class is a member of the
15.96/5.20 *
15.96/5.20 * Java Collections Framework.
15.96/5.20 *
15.96/5.20 * @author Josh Bloch
15.96/5.20 * @see List
15.96/5.20 * @see ArrayList
15.96/5.20 * @see Vector
15.96/5.20 * @since 1.2
15.96/5.20 * @param the type of elements held in this collection
15.96/5.20 */
15.96/5.20
15.96/5.20 public class LinkedList
15.96/5.20 extends AbstractSequentialList
15.96/5.20 implements List, Deque
15.96/5.20 {
15.96/5.20 private transient Entry header = new Entry(null, null, null);
15.96/5.20 private transient int size = 0;
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs an empty list.
15.96/5.20 */
15.96/5.20 public LinkedList() {
15.96/5.20 header.next = header.previous = header;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Constructs a list containing the elements of the specified
15.96/5.20 * collection, in the order they are returned by the collection's
15.96/5.20 * iterator.
15.96/5.20 *
15.96/5.20 * @param c the collection whose elements are to be placed into this list
15.96/5.20 * @throws NullPointerException if the specified collection is null
15.96/5.20 */
15.96/5.20 public LinkedList(Collection extends E> c) {
15.96/5.20 this();
15.96/5.20 addAll(c);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the first element in this list.
15.96/5.20 *
15.96/5.20 * @return the first element in this list
15.96/5.20 * @throws NoSuchElementException if this list is empty
15.96/5.20 */
15.96/5.20 public E getFirst() {
15.96/5.20 if (size==0)
15.96/5.20 throw new NoSuchElementException();
15.96/5.20
15.96/5.20 return header.next.element;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the last element in this list.
15.96/5.20 *
15.96/5.20 * @return the last element in this list
15.96/5.20 * @throws NoSuchElementException if this list is empty
15.96/5.20 */
15.96/5.20 public E getLast() {
15.96/5.20 if (size==0)
15.96/5.20 throw new NoSuchElementException();
15.96/5.20
15.96/5.20 return header.previous.element;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes and returns the first element from this list.
15.96/5.20 *
15.96/5.20 * @return the first element from this list
15.96/5.20 * @throws NoSuchElementException if this list is empty
15.96/5.20 */
15.96/5.20 public E removeFirst() {
15.96/5.20 return remove(header.next);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes and returns the last element from this list.
15.96/5.20 *
15.96/5.20 * @return the last element from this list
15.96/5.20 * @throws NoSuchElementException if this list is empty
15.96/5.20 */
15.96/5.20 public E removeLast() {
15.96/5.20 return remove(header.previous);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Inserts the specified element at the beginning of this list.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 */
15.96/5.20 public void addFirst(E e) {
15.96/5.20 addBefore(e, header.next);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Appends the specified element to the end of this list.
15.96/5.20 *
15.96/5.20 * This method is equivalent to {@link #add}.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 */
15.96/5.20 public void addLast(E e) {
15.96/5.20 addBefore(e, header);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns true if this list contains the specified element.
15.96/5.20 * More formally, returns true if and only if this list contains
15.96/5.20 * at least one element e such that
15.96/5.20 * (o==null ? e==null : o.equals(e)).
15.96/5.20 *
15.96/5.20 * @param o element whose presence in this list is to be tested
15.96/5.20 * @return true if this list contains the specified element
15.96/5.20 */
15.96/5.20 public boolean contains(Object o) {
15.96/5.20 return indexOf(o) != -1;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the number of elements in this list.
15.96/5.20 *
15.96/5.20 * @return the number of elements in this list
15.96/5.20 */
15.96/5.20 public int size() {
15.96/5.20 return size;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Appends the specified element to the end of this list.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #addLast}.
15.96/5.20 *
15.96/5.20 * @param e element to be appended to this list
15.96/5.20 * @return true (as specified by {@link Collection#add})
15.96/5.20 */
15.96/5.20 public boolean add(E e) {
15.96/5.20 addBefore(e, header);
15.96/5.20 return true;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes the first occurrence of the specified element from this list,
15.96/5.20 * if it is present. If this list does not contain the element, it is
15.96/5.20 * unchanged. More formally, removes the element with the lowest index
15.96/5.20 * i such that
15.96/5.20 * (o==null ? get(i)==null : o.equals(get(i)))
15.96/5.20 * (if such an element exists). Returns true if this list
15.96/5.20 * contained the specified element (or equivalently, if this list
15.96/5.20 * changed as a result of the call).
15.96/5.20 *
15.96/5.20 * @param o element to be removed from this list, if present
15.96/5.20 * @return true if this list contained the specified element
15.96/5.20 */
15.96/5.20 public boolean remove(Object o) {
15.96/5.20 if (o==null) {
15.96/5.20 for (Entry e = header.next; e != header; e = e.next) {
15.96/5.20 if (e.element==null) {
15.96/5.20 remove(e);
15.96/5.20 return true;
15.96/5.20 }
15.96/5.20 }
15.96/5.20 } else {
15.96/5.20 for (Entry e = header.next; e != header; e = e.next) {
15.96/5.20 if (o.equals(e.element)) {
15.96/5.20 remove(e);
15.96/5.20 return true;
15.96/5.20 }
15.96/5.20 }
15.96/5.20 }
15.96/5.20 return false;
15.96/5.20 }
15.96/5.20 /**
15.96/5.20 * Removes all of the elements from this list.
15.96/5.20 */
15.96/5.20 public void clear() {
15.96/5.20 Entry e = header.next;
15.96/5.20 while (e != header) {
15.96/5.20 Entry next = e.next;
15.96/5.20 e.next = e.previous = null;
15.96/5.20 e.element = null;
15.96/5.20 e = next;
15.96/5.20 }
15.96/5.20 header.next = header.previous = header;
15.96/5.20 size = 0;
15.96/5.20 modCount++;
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 // Positional Access Operations
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the element at the specified position in this list.
15.96/5.20 *
15.96/5.20 * @param index index of the element to return
15.96/5.20 * @return the element at the specified position in this list
15.96/5.20 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.20 */
15.96/5.20 public E get(int index) {
15.96/5.20 return entry(index).element;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Replaces the element at the specified position in this list with the
15.96/5.20 * specified element.
15.96/5.20 *
15.96/5.20 * @param index index of the element to replace
15.96/5.20 * @param element element to be stored at the specified position
15.96/5.20 * @return the element previously at the specified position
15.96/5.20 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.20 */
15.96/5.20 public E set(int index, E element) {
15.96/5.20 Entry e = entry(index);
15.96/5.20 E oldVal = e.element;
15.96/5.20 e.element = element;
15.96/5.20 return oldVal;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Inserts the specified element at the specified position in this list.
15.96/5.20 * Shifts the element currently at that position (if any) and any
15.96/5.20 * subsequent elements to the right (adds one to their indices).
15.96/5.20 *
15.96/5.20 * @param index index at which the specified element is to be inserted
15.96/5.20 * @param element element to be inserted
15.96/5.20 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.20 */
15.96/5.20 public void add(int index, E element) {
15.96/5.20 addBefore(element, (index==size ? header : entry(index)));
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes the element at the specified position in this list. Shifts any
15.96/5.20 * subsequent elements to the left (subtracts one from their indices).
15.96/5.20 * Returns the element that was removed from the list.
15.96/5.20 *
15.96/5.20 * @param index the index of the element to be removed
15.96/5.20 * @return the element previously at the specified position
15.96/5.20 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.20 */
15.96/5.20 public E remove(int index) {
15.96/5.20 return remove(entry(index));
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the indexed entry.
15.96/5.20 */
15.96/5.20 private Entry entry(int index) {
15.96/5.20 if (index < 0 || index >= size)
15.96/5.20 throw new IndexOutOfBoundsException();
15.96/5.20 Entry e = header;
15.96/5.20 if (index < (size >> 1)) {
15.96/5.20 for (int i = 0; i <= index; i++)
15.96/5.20 e = e.next;
15.96/5.20 } else {
15.96/5.20 for (int i = size; i > index; i--)
15.96/5.20 e = e.previous;
15.96/5.20 }
15.96/5.20 return e;
15.96/5.20 }
15.96/5.20
15.96/5.20
15.96/5.20 // Search Operations
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the index of the first occurrence of the specified element
15.96/5.20 * in this list, or -1 if this list does not contain the element.
15.96/5.20 * More formally, returns the lowest index i such that
15.96/5.20 * (o==null ? get(i)==null : o.equals(get(i))),
15.96/5.20 * or -1 if there is no such index.
15.96/5.20 *
15.96/5.20 * @param o element to search for
15.96/5.20 * @return the index of the first occurrence of the specified element in
15.96/5.20 * this list, or -1 if this list does not contain the element
15.96/5.20 */
15.96/5.20 public int indexOf(Object o) {
15.96/5.20 int index = 0;
15.96/5.20 if (o==null) {
15.96/5.20 for (Entry e = header.next; e != header; e = e.next) {
15.96/5.20 if (e.element==null)
15.96/5.20 return index;
15.96/5.20 index++;
15.96/5.20 }
15.96/5.20 } else {
15.96/5.20 for (Entry e = header.next; e != header; e = e.next) {
15.96/5.20 if (o.equals(e.element))
15.96/5.20 return index;
15.96/5.20 index++;
15.96/5.20 }
15.96/5.20 }
15.96/5.20 return -1;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns the index of the last occurrence of the specified element
15.96/5.20 * in this list, or -1 if this list does not contain the element.
15.96/5.20 * More formally, returns the highest index i such that
15.96/5.20 * (o==null ? get(i)==null : o.equals(get(i))),
15.96/5.20 * or -1 if there is no such index.
15.96/5.20 *
15.96/5.20 * @param o element to search for
15.96/5.20 * @return the index of the last occurrence of the specified element in
15.96/5.20 * this list, or -1 if this list does not contain the element
15.96/5.20 */
15.96/5.20 public int lastIndexOf(Object o) {
15.96/5.20 int index = size;
15.96/5.20 if (o==null) {
15.96/5.20 for (Entry e = header.previous; e != header; e = e.previous) {
15.96/5.20 index--;
15.96/5.20 if (e.element==null)
15.96/5.20 return index;
15.96/5.20 }
15.96/5.20 } else {
15.96/5.20 for (Entry e = header.previous; e != header; e = e.previous) {
15.96/5.20 index--;
15.96/5.20 if (o.equals(e.element))
15.96/5.20 return index;
15.96/5.20 }
15.96/5.20 }
15.96/5.20 return -1;
15.96/5.20 }
15.96/5.20
15.96/5.20 // Queue operations.
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the head (first element) of this list.
15.96/5.20 * @return the head of this list, or null if this list is empty
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public E peek() {
15.96/5.20 if (size==0)
15.96/5.20 return null;
15.96/5.20 return getFirst();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the head (first element) of this list.
15.96/5.20 * @return the head of this list
15.96/5.20 * @throws NoSuchElementException if this list is empty
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public E element() {
15.96/5.20 return getFirst();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the head (first element) of this list
15.96/5.20 * @return the head of this list, or null if this list is empty
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public E poll() {
15.96/5.20 if (size==0)
15.96/5.20 return null;
15.96/5.20 return removeFirst();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the head (first element) of this list.
15.96/5.20 *
15.96/5.20 * @return the head of this list
15.96/5.20 * @throws NoSuchElementException if this list is empty
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public E remove() {
15.96/5.20 return removeFirst();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Adds the specified element as the tail (last element) of this list.
15.96/5.20 *
15.96/5.20 * @param e the element to add
15.96/5.20 * @return true (as specified by {@link Queue#offer})
15.96/5.20 * @since 1.5
15.96/5.20 */
15.96/5.20 public boolean offer(E e) {
15.96/5.20 return add(e);
15.96/5.20 }
15.96/5.20
15.96/5.20 // Deque operations
15.96/5.20 /**
15.96/5.20 * Inserts the specified element at the front of this list.
15.96/5.20 *
15.96/5.20 * @param e the element to insert
15.96/5.20 * @return true (as specified by {@link Deque#offerFirst})
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public boolean offerFirst(E e) {
15.96/5.20 addFirst(e);
15.96/5.20 return true;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Inserts the specified element at the end of this list.
15.96/5.20 *
15.96/5.20 * @param e the element to insert
15.96/5.20 * @return true (as specified by {@link Deque#offerLast})
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public boolean offerLast(E e) {
15.96/5.20 addLast(e);
15.96/5.20 return true;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the first element of this list,
15.96/5.20 * or returns null if this list is empty.
15.96/5.20 *
15.96/5.20 * @return the first element of this list, or null
15.96/5.20 * if this list is empty
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public E peekFirst() {
15.96/5.20 if (size==0)
15.96/5.20 return null;
15.96/5.20 return getFirst();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves, but does not remove, the last element of this list,
15.96/5.20 * or returns null if this list is empty.
15.96/5.20 *
15.96/5.20 * @return the last element of this list, or null
15.96/5.20 * if this list is empty
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public E peekLast() {
15.96/5.20 if (size==0)
15.96/5.20 return null;
15.96/5.20 return getLast();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the first element of this list,
15.96/5.20 * or returns null if this list is empty.
15.96/5.20 *
15.96/5.20 * @return the first element of this list, or null if
15.96/5.20 * this list is empty
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public E pollFirst() {
15.96/5.20 if (size==0)
15.96/5.20 return null;
15.96/5.20 return removeFirst();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Retrieves and removes the last element of this list,
15.96/5.20 * or returns null if this list is empty.
15.96/5.20 *
15.96/5.20 * @return the last element of this list, or null if
15.96/5.20 * this list is empty
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public E pollLast() {
15.96/5.20 if (size==0)
15.96/5.20 return null;
15.96/5.20 return removeLast();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Pushes an element onto the stack represented by this list. In other
15.96/5.20 * words, inserts the element at the front of this list.
15.96/5.20 *
15.96/5.20 * This method is equivalent to {@link #addFirst}.
15.96/5.20 *
15.96/5.20 * @param e the element to push
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public void push(E e) {
15.96/5.20 addFirst(e);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Pops an element from the stack represented by this list. In other
15.96/5.20 * words, removes and returns the first element of this list.
15.96/5.20 *
15.96/5.20 *
This method is equivalent to {@link #removeFirst()}.
15.96/5.20 *
15.96/5.20 * @return the element at the front of this list (which is the top
15.96/5.20 * of the stack represented by this list)
15.96/5.20 * @throws NoSuchElementException if this list is empty
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public E pop() {
15.96/5.20 return removeFirst();
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes the first occurrence of the specified element in this
15.96/5.20 * list (when traversing the list from head to tail). If the list
15.96/5.20 * does not contain the element, it is unchanged.
15.96/5.20 *
15.96/5.20 * @param o element to be removed from this list, if present
15.96/5.20 * @return true if the list contained the specified element
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public boolean removeFirstOccurrence(Object o) {
15.96/5.20 return remove(o);
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Removes the last occurrence of the specified element in this
15.96/5.20 * list (when traversing the list from head to tail). If the list
15.96/5.20 * does not contain the element, it is unchanged.
15.96/5.20 *
15.96/5.20 * @param o element to be removed from this list, if present
15.96/5.20 * @return true if the list contained the specified element
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public boolean removeLastOccurrence(Object o) {
15.96/5.20 if (o==null) {
15.96/5.20 for (Entry e = header.previous; e != header; e = e.previous) {
15.96/5.20 if (e.element==null) {
15.96/5.20 remove(e);
15.96/5.20 return true;
15.96/5.20 }
15.96/5.20 }
15.96/5.20 } else {
15.96/5.20 for (Entry e = header.previous; e != header; e = e.previous) {
15.96/5.20 if (o.equals(e.element)) {
15.96/5.20 remove(e);
15.96/5.20 return true;
15.96/5.20 }
15.96/5.20 }
15.96/5.20 }
15.96/5.20 return false;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns a list-iterator of the elements in this list (in proper
15.96/5.20 * sequence), starting at the specified position in the list.
15.96/5.20 * Obeys the general contract of List.listIterator(int).
15.96/5.20 *
15.96/5.20 * The list-iterator is fail-fast: if the list is structurally
15.96/5.20 * modified at any time after the Iterator is created, in any way except
15.96/5.20 * through the list-iterator's own remove or add
15.96/5.20 * methods, the list-iterator will throw a
15.96/5.20 * ConcurrentModificationException. Thus, in the face of
15.96/5.20 * concurrent modification, the iterator fails quickly and cleanly, rather
15.96/5.20 * than risking arbitrary, non-deterministic behavior at an undetermined
15.96/5.20 * time in the future.
15.96/5.20 *
15.96/5.20 * @param index index of the first element to be returned from the
15.96/5.20 * list-iterator (by a call to next)
15.96/5.20 * @return a ListIterator of the elements in this list (in proper
15.96/5.20 * sequence), starting at the specified position in the list
15.96/5.20 * @throws IndexOutOfBoundsException {@inheritDoc}
15.96/5.20 * @see List#listIterator(int)
15.96/5.20 */
15.96/5.20 public ListIterator listIterator(int index) {
15.96/5.20 return new ListItr(index);
15.96/5.20 }
15.96/5.20
15.96/5.20 private class ListItr implements ListIterator {
15.96/5.20 private Entry lastReturned = header;
15.96/5.20 private Entry next;
15.96/5.20 private int nextIndex;
15.96/5.20 private int expectedModCount = modCount;
15.96/5.20
15.96/5.20 ListItr(int index) {
15.96/5.20 if (index < 0 || index > size)
15.96/5.20 throw new IndexOutOfBoundsException();
15.96/5.20 if (index < (size >> 1)) {
15.96/5.20 next = header.next;
15.96/5.20 for (nextIndex=0; nextIndexindex; nextIndex--)
15.96/5.20 next = next.previous;
15.96/5.20 }
15.96/5.20 }
15.96/5.20
15.96/5.20 public boolean hasNext() {
15.96/5.20 return nextIndex != size;
15.96/5.20 }
15.96/5.20
15.96/5.20 public E next() {
15.96/5.20 checkForComodification();
15.96/5.20 if (nextIndex == size)
15.96/5.20 throw new NoSuchElementException();
15.96/5.20
15.96/5.20 lastReturned = next;
15.96/5.20 next = next.next;
15.96/5.20 nextIndex++;
15.96/5.20 return lastReturned.element;
15.96/5.20 }
15.96/5.20
15.96/5.20 public boolean hasPrevious() {
15.96/5.20 return nextIndex != 0;
15.96/5.20 }
15.96/5.20
15.96/5.20 public E previous() {
15.96/5.20 if (nextIndex == 0)
15.96/5.20 throw new NoSuchElementException();
15.96/5.20
15.96/5.20 lastReturned = next = next.previous;
15.96/5.20 nextIndex--;
15.96/5.20 checkForComodification();
15.96/5.20 return lastReturned.element;
15.96/5.20 }
15.96/5.20
15.96/5.20 public int nextIndex() {
15.96/5.20 return nextIndex;
15.96/5.20 }
15.96/5.20
15.96/5.20 public int previousIndex() {
15.96/5.20 return nextIndex-1;
15.96/5.20 }
15.96/5.20
15.96/5.20 public void remove() {
15.96/5.20 checkForComodification();
15.96/5.20 Entry lastNext = lastReturned.next;
15.96/5.20 try {
15.96/5.20 LinkedList.this.remove(lastReturned);
15.96/5.20 } catch (NoSuchElementException e) {
15.96/5.20 throw new IllegalStateException();
15.96/5.20 }
15.96/5.20 if (next==lastReturned)
15.96/5.20 next = lastNext;
15.96/5.20 else
15.96/5.20 nextIndex--;
15.96/5.20 lastReturned = header;
15.96/5.20 expectedModCount++;
15.96/5.20 }
15.96/5.20
15.96/5.20 public void set(E e) {
15.96/5.20 if (lastReturned == header)
15.96/5.20 throw new IllegalStateException();
15.96/5.20 checkForComodification();
15.96/5.20 lastReturned.element = e;
15.96/5.20 }
15.96/5.20
15.96/5.20 public void add(E e) {
15.96/5.20 checkForComodification();
15.96/5.20 lastReturned = header;
15.96/5.20 addBefore(e, next);
15.96/5.20 nextIndex++;
15.96/5.20 expectedModCount++;
15.96/5.20 }
15.96/5.20
15.96/5.20 final void checkForComodification() {
15.96/5.20 if (modCount != expectedModCount)
15.96/5.20 throw new ConcurrentModificationException();
15.96/5.20 }
15.96/5.20 }
15.96/5.20
15.96/5.20 private static class Entry {
15.96/5.20 E element;
15.96/5.20 Entry next;
15.96/5.20 Entry previous;
15.96/5.20
15.96/5.20 Entry(E element, Entry next, Entry previous) {
15.96/5.20 this.element = element;
15.96/5.20 this.next = next;
15.96/5.20 this.previous = previous;
15.96/5.20 }
15.96/5.20 }
15.96/5.20
15.96/5.20 private Entry addBefore(E e, Entry entry) {
15.96/5.20 Entry newEntry = new Entry(e, entry, entry.previous);
15.96/5.20 newEntry.previous.next = newEntry;
15.96/5.20 newEntry.next.previous = newEntry;
15.96/5.20 size++;
15.96/5.20 modCount++;
15.96/5.20 return newEntry;
15.96/5.20 }
15.96/5.20
15.96/5.20 private E remove(Entry e) {
15.96/5.20 if (e == header)
15.96/5.20 throw new NoSuchElementException();
15.96/5.20
15.96/5.20 E result = e.element;
15.96/5.20 e.previous.next = e.next;
15.96/5.20 e.next.previous = e.previous;
15.96/5.20 e.next = e.previous = null;
15.96/5.20 e.element = null;
15.96/5.20 size--;
15.96/5.20 modCount++;
15.96/5.20 return result;
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * @since 1.6
15.96/5.20 */
15.96/5.20 public Iterator descendingIterator() {
15.96/5.20 return new DescendingIterator();
15.96/5.20 }
15.96/5.20
15.96/5.20 /** Adapter to provide descending iterators via ListItr.previous */
15.96/5.20 private class DescendingIterator implements Iterator {
15.96/5.20 final ListItr itr = new ListItr(size());
15.96/5.20 public boolean hasNext() {
15.96/5.20 return itr.hasPrevious();
15.96/5.20 }
15.96/5.20 public E next() {
15.96/5.20 return itr.previous();
15.96/5.20 }
15.96/5.20 public void remove() {
15.96/5.20 itr.remove();
15.96/5.20 }
15.96/5.20 }
15.96/5.20
15.96/5.20 /**
15.96/5.20 * Returns an array containing all of the elements in this list
15.96/5.20 * in proper sequence (from first to last element).
15.96/5.20 *
15.96/5.20 * The returned array will be "safe" in that no references to it are
15.96/5.20 * maintained by this list. (In other words, this method must allocate
15.96/5.20 * a new array). The caller is thus free to modify the returned array.
15.96/5.20 *
15.96/5.20 *
This method acts as bridge between array-based and collection-based
15.96/5.20 * APIs.
15.96/5.20 *
15.96/5.20 * @return an array containing all of the elements in this list
15.96/5.21 * in proper sequence
15.96/5.21 */
15.96/5.21 public Object[] toArray() {
15.96/5.21 Object[] result = new Object[size];
15.96/5.21 int i = 0;
15.96/5.21 for (Entry e = header.next; e != header; e = e.next)
15.96/5.21 result[i++] = e.element;
15.96/5.21 return result;
15.96/5.21 }
15.96/5.21
15.96/5.21 private static final long serialVersionUID = 876323262645176354L;
15.96/5.21 }
15.96/5.21
15.96/5.21
15.96/5.21 /*
15.96/5.21 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.21 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.21 *
15.96/5.21 * This code is free software; you can redistribute it and/or modify it
15.96/5.21 * under the terms of the GNU General Public License version 2 only, as
15.96/5.21 * published by the Free Software Foundation. Sun designates this
15.96/5.21 * particular file as subject to the "Classpath" exception as provided
15.96/5.21 * by Sun in the LICENSE file that accompanied this code.
15.96/5.21 *
15.96/5.21 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.21 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.21 * accompanied this code).
15.96/5.21 *
15.96/5.21 * You should have received a copy of the GNU General Public License version
15.96/5.21 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.21 *
15.96/5.21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.21 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.21 * have any questions.
15.96/5.21 */
15.96/5.21
15.96/5.21 package javaUtilEx;
15.96/5.21
15.96/5.21 /**
15.96/5.21 * An iterator for lists that allows the programmer
15.96/5.21 * to traverse the list in either direction, modify
15.96/5.21 * the list during iteration, and obtain the iterator's
15.96/5.21 * current position in the list. A {@code ListIterator}
15.96/5.21 * has no current element; its cursor position always
15.96/5.21 * lies between the element that would be returned by a call
15.96/5.21 * to {@code previous()} and the element that would be
15.96/5.21 * returned by a call to {@code next()}.
15.96/5.21 * An iterator for a list of length {@code n} has {@code n+1} possible
15.96/5.21 * cursor positions, as illustrated by the carets ({@code ^}) below:
15.96/5.21 *
15.96/5.21 * Element(0) Element(1) Element(2) ... Element(n-1)
15.96/5.21 * cursor positions: ^ ^ ^ ^ ^
15.96/5.21 *
15.96/5.21 * Note that the {@link #remove} and {@link #set(Object)} methods are
15.96/5.21 * not defined in terms of the cursor position; they are defined to
15.96/5.21 * operate on the last element returned by a call to {@link #next} or
15.96/5.21 * {@link #previous()}.
15.96/5.21 *
15.96/5.21 * This interface is a member of the
15.96/5.21 *
15.96/5.21 * Java Collections Framework.
15.96/5.21 *
15.96/5.21 * @author Josh Bloch
15.96/5.21 * @see Collection
15.96/5.21 * @see List
15.96/5.21 * @see Iterator
15.96/5.21 * @see Enumeration
15.96/5.21 * @see List#listIterator()
15.96/5.21 * @since 1.2
15.96/5.21 */
15.96/5.21 public interface ListIterator extends Iterator {
15.96/5.21 // Query Operations
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns {@code true} if this list iterator has more elements when
15.96/5.21 * traversing the list in the forward direction. (In other words,
15.96/5.21 * returns {@code true} if {@link #next} would return an element rather
15.96/5.21 * than throwing an exception.)
15.96/5.21 *
15.96/5.21 * @return {@code true} if the list iterator has more elements when
15.96/5.21 * traversing the list in the forward direction
15.96/5.21 */
15.96/5.21 boolean hasNext();
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns the next element in the list and advances the cursor position.
15.96/5.21 * This method may be called repeatedly to iterate through the list,
15.96/5.21 * or intermixed with calls to {@link #previous} to go back and forth.
15.96/5.21 * (Note that alternating calls to {@code next} and {@code previous}
15.96/5.21 * will return the same element repeatedly.)
15.96/5.21 *
15.96/5.21 * @return the next element in the list
15.96/5.21 * @throws NoSuchElementException if the iteration has no next element
15.96/5.21 */
15.96/5.21 E next();
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns {@code true} if this list iterator has more elements when
15.96/5.21 * traversing the list in the reverse direction. (In other words,
15.96/5.21 * returns {@code true} if {@link #previous} would return an element
15.96/5.21 * rather than throwing an exception.)
15.96/5.21 *
15.96/5.21 * @return {@code true} if the list iterator has more elements when
15.96/5.21 * traversing the list in the reverse direction
15.96/5.21 */
15.96/5.21 boolean hasPrevious();
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns the previous element in the list and moves the cursor
15.96/5.21 * position backwards. This method may be called repeatedly to
15.96/5.21 * iterate through the list backwards, or intermixed with calls to
15.96/5.21 * {@link #next} to go back and forth. (Note that alternating calls
15.96/5.21 * to {@code next} and {@code previous} will return the same
15.96/5.21 * element repeatedly.)
15.96/5.21 *
15.96/5.21 * @return the previous element in the list
15.96/5.21 * @throws NoSuchElementException if the iteration has no previous
15.96/5.21 * element
15.96/5.21 */
15.96/5.21 E previous();
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns the index of the element that would be returned by a
15.96/5.21 * subsequent call to {@link #next}. (Returns list size if the list
15.96/5.21 * iterator is at the end of the list.)
15.96/5.21 *
15.96/5.21 * @return the index of the element that would be returned by a
15.96/5.21 * subsequent call to {@code next}, or list size if the list
15.96/5.21 * iterator is at the end of the list
15.96/5.21 */
15.96/5.21 int nextIndex();
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns the index of the element that would be returned by a
15.96/5.21 * subsequent call to {@link #previous}. (Returns -1 if the list
15.96/5.21 * iterator is at the beginning of the list.)
15.96/5.21 *
15.96/5.21 * @return the index of the element that would be returned by a
15.96/5.21 * subsequent call to {@code previous}, or -1 if the list
15.96/5.21 * iterator is at the beginning of the list
15.96/5.21 */
15.96/5.21 int previousIndex();
15.96/5.21
15.96/5.21
15.96/5.21 // Modification Operations
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Removes from the list the last element that was returned by {@link
15.96/5.21 * #next} or {@link #previous} (optional operation). This call can
15.96/5.21 * only be made once per call to {@code next} or {@code previous}.
15.96/5.21 * It can be made only if {@link #add} has not been
15.96/5.21 * called after the last call to {@code next} or {@code previous}.
15.96/5.21 *
15.96/5.21 * @throws UnsupportedOperationException if the {@code remove}
15.96/5.21 * operation is not supported by this list iterator
15.96/5.21 * @throws IllegalStateException if neither {@code next} nor
15.96/5.21 * {@code previous} have been called, or {@code remove} or
15.96/5.21 * {@code add} have been called after the last call to
15.96/5.21 * {@code next} or {@code previous}
15.96/5.21 */
15.96/5.21 void remove();
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Replaces the last element returned by {@link #next} or
15.96/5.21 * {@link #previous} with the specified element (optional operation).
15.96/5.21 * This call can be made only if neither {@link #remove} nor {@link
15.96/5.21 * #add} have been called after the last call to {@code next} or
15.96/5.21 * {@code previous}.
15.96/5.21 *
15.96/5.21 * @param e the element with which to replace the last element returned by
15.96/5.21 * {@code next} or {@code previous}
15.96/5.21 * @throws UnsupportedOperationException if the {@code set} operation
15.96/5.21 * is not supported by this list iterator
15.96/5.21 * @throws ClassCastException if the class of the specified element
15.96/5.21 * prevents it from being added to this list
15.96/5.21 * @throws IllegalArgumentException if some aspect of the specified
15.96/5.21 * element prevents it from being added to this list
15.96/5.21 * @throws IllegalStateException if neither {@code next} nor
15.96/5.21 * {@code previous} have been called, or {@code remove} or
15.96/5.21 * {@code add} have been called after the last call to
15.96/5.21 * {@code next} or {@code previous}
15.96/5.21 */
15.96/5.21 void set(E e);
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Inserts the specified element into the list (optional operation).
15.96/5.21 * The element is inserted immediately before the next element that
15.96/5.21 * would be returned by {@link #next}, if any, and after the next
15.96/5.21 * element that would be returned by {@link #previous}, if any. (If the
15.96/5.21 * list contains no elements, the new element becomes the sole element
15.96/5.21 * on the list.) The new element is inserted before the implicit
15.96/5.21 * cursor: a subsequent call to {@code next} would be unaffected, and a
15.96/5.21 * subsequent call to {@code previous} would return the new element.
15.96/5.21 * (This call increases by one the value that would be returned by a
15.96/5.21 * call to {@code nextIndex} or {@code previousIndex}.)
15.96/5.21 *
15.96/5.21 * @param e the element to insert
15.96/5.21 * @throws UnsupportedOperationException if the {@code add} method is
15.96/5.21 * not supported by this list iterator
15.96/5.21 * @throws ClassCastException if the class of the specified element
15.96/5.21 * prevents it from being added to this list
15.96/5.21 * @throws IllegalArgumentException if some aspect of this element
15.96/5.21 * prevents it from being added to this list
15.96/5.21 */
15.96/5.21 void add(E e);
15.96/5.21 }
15.96/5.21
15.96/5.21
15.96/5.21 /*
15.96/5.21 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
15.96/5.21 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
15.96/5.21 *
15.96/5.21 * This code is free software; you can redistribute it and/or modify it
15.96/5.21 * under the terms of the GNU General Public License version 2 only, as
15.96/5.21 * published by the Free Software Foundation. Sun designates this
15.96/5.21 * particular file as subject to the "Classpath" exception as provided
15.96/5.21 * by Sun in the LICENSE file that accompanied this code.
15.96/5.21 *
15.96/5.21 * This code is distributed in the hope that it will be useful, but WITHOUT
15.96/5.21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15.96/5.21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15.96/5.21 * version 2 for more details (a copy is included in the LICENSE file that
15.96/5.21 * accompanied this code).
15.96/5.21 *
15.96/5.21 * You should have received a copy of the GNU General Public License version
15.96/5.21 * 2 along with this work; if not, write to the Free Software Foundation,
15.96/5.21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
15.96/5.21 *
15.96/5.21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
15.96/5.21 * CA 95054 USA or visit www.sun.com if you need additional information or
15.96/5.21 * have any questions.
15.96/5.21 */
15.96/5.21
15.96/5.21 package javaUtilEx;
15.96/5.21
15.96/5.21 /**
15.96/5.21 * An ordered collection (also known as a sequence). The user of this
15.96/5.21 * interface has precise control over where in the list each element is
15.96/5.21 * inserted. The user can access elements by their integer index (position in
15.96/5.21 * the list), and search for elements in the list.
15.96/5.21 *
15.96/5.21 * Unlike sets, lists typically allow duplicate elements. More formally,
15.96/5.21 * lists typically allow pairs of elements e1 and e2
15.96/5.21 * such that e1.equals(e2), and they typically allow multiple
15.96/5.21 * null elements if they allow null elements at all. It is not inconceivable
15.96/5.21 * that someone might wish to implement a list that prohibits duplicates, by
15.96/5.21 * throwing runtime exceptions when the user attempts to insert them, but we
15.96/5.21 * expect this usage to be rare.
15.96/5.21 *
15.96/5.21 * The List interface places additional stipulations, beyond those
15.96/5.21 * specified in the Collection interface, on the contracts of the
15.96/5.21 * iterator, add, remove, equals, and
15.96/5.21 * hashCode methods. Declarations for other inherited methods are
15.96/5.21 * also included here for convenience.
15.96/5.21 *
15.96/5.21 * The List interface provides four methods for positional (indexed)
15.96/5.21 * access to list elements. Lists (like Java arrays) are zero based. Note
15.96/5.21 * that these operations may execute in time proportional to the index value
15.96/5.21 * for some implementations (the LinkedList class, for
15.96/5.21 * example). Thus, iterating over the elements in a list is typically
15.96/5.21 * preferable to indexing through it if the caller does not know the
15.96/5.21 * implementation.
15.96/5.21 *
15.96/5.21 * The List interface provides a special iterator, called a
15.96/5.21 * ListIterator, that allows element insertion and replacement, and
15.96/5.21 * bidirectional access in addition to the normal operations that the
15.96/5.21 * Iterator interface provides. A method is provided to obtain a
15.96/5.21 * list iterator that starts at a specified position in the list.
15.96/5.21 *
15.96/5.21 * The List interface provides two methods to search for a specified
15.96/5.21 * object. From a performance standpoint, these methods should be used with
15.96/5.21 * caution. In many implementations they will perform costly linear
15.96/5.21 * searches.
15.96/5.21 *
15.96/5.21 * The List interface provides two methods to efficiently insert and
15.96/5.21 * remove multiple elements at an arbitrary point in the list.
15.96/5.21 *
15.96/5.21 * Note: While it is permissible for lists to contain themselves as elements,
15.96/5.21 * extreme caution is advised: the equals and hashCode
15.96/5.21 * methods are no longer well defined on such a list.
15.96/5.21 *
15.96/5.21 *
Some list implementations have restrictions on the elements that
15.96/5.21 * they may contain. For example, some implementations prohibit null elements,
15.96/5.21 * and some have restrictions on the types of their elements. Attempting to
15.96/5.21 * add an ineligible element throws an unchecked exception, typically
15.96/5.21 * NullPointerException or ClassCastException. Attempting
15.96/5.21 * to query the presence of an ineligible element may throw an exception,
15.96/5.21 * or it may simply return false; some implementations will exhibit the former
15.96/5.21 * behavior and some will exhibit the latter. More generally, attempting an
15.96/5.21 * operation on an ineligible element whose completion would not result in
15.96/5.21 * the insertion of an ineligible element into the list may throw an
15.96/5.21 * exception or it may succeed, at the option of the implementation.
15.96/5.21 * Such exceptions are marked as "optional" in the specification for this
15.96/5.21 * interface.
15.96/5.21 *
15.96/5.21 *
This interface is a member of the
15.96/5.21 *
15.96/5.21 * Java Collections Framework.
15.96/5.21 *
15.96/5.21 * @author Josh Bloch
15.96/5.21 * @author Neal Gafter
15.96/5.21 * @see Collection
15.96/5.21 * @see Set
15.96/5.21 * @see ArrayList
15.96/5.21 * @see LinkedList
15.96/5.21 * @see Vector
15.96/5.21 * @see Arrays#asList(Object[])
15.96/5.21 * @see Collections#nCopies(int, Object)
15.96/5.21 * @see Collections#EMPTY_LIST
15.96/5.21 * @see AbstractList
15.96/5.21 * @see AbstractSequentialList
15.96/5.21 * @since 1.2
15.96/5.21 */
15.96/5.21
15.96/5.21 public interface List extends Collection {
15.96/5.21 // Query Operations
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns the number of elements in this list. If this list contains
15.96/5.21 * more than Integer.MAX_VALUE elements, returns
15.96/5.21 * Integer.MAX_VALUE.
15.96/5.21 *
15.96/5.21 * @return the number of elements in this list
15.96/5.21 */
15.96/5.21 int size();
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns true if this list contains no elements.
15.96/5.21 *
15.96/5.21 * @return true if this list contains no elements
15.96/5.21 */
15.96/5.21 boolean isEmpty();
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns true if this list contains the specified element.
15.96/5.21 * More formally, returns true if and only if this list contains
15.96/5.21 * at least one element e such that
15.96/5.21 * (o==null ? e==null : o.equals(e)).
15.96/5.21 *
15.96/5.21 * @param o element whose presence in this list is to be tested
15.96/5.21 * @return true if this list contains the specified element
15.96/5.21 * @throws ClassCastException if the type of the specified element
15.96/5.21 * is incompatible with this list (optional)
15.96/5.21 * @throws NullPointerException if the specified element is null and this
15.96/5.21 * list does not permit null elements (optional)
15.96/5.21 */
15.96/5.21 boolean contains(Object o);
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns an iterator over the elements in this list in proper sequence.
15.96/5.21 *
15.96/5.21 * @return an iterator over the elements in this list in proper sequence
15.96/5.21 */
15.96/5.21 Iterator iterator();
15.96/5.21
15.96/5.21 // Modification Operations
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Appends the specified element to the end of this list (optional
15.96/5.21 * operation).
15.96/5.21 *
15.96/5.21 * Lists that support this operation may place limitations on what
15.96/5.21 * elements may be added to this list. In particular, some
15.96/5.21 * lists will refuse to add null elements, and others will impose
15.96/5.21 * restrictions on the type of elements that may be added. List
15.96/5.21 * classes should clearly specify in their documentation any restrictions
15.96/5.21 * on what elements may be added.
15.96/5.21 *
15.96/5.21 * @param e element to be appended to this list
15.96/5.21 * @return true (as specified by {@link Collection#add})
15.96/5.21 * @throws UnsupportedOperationException if the add operation
15.96/5.21 * is not supported by this list
15.96/5.21 * @throws ClassCastException if the class of the specified element
15.96/5.21 * prevents it from being added to this list
15.96/5.21 * @throws NullPointerException if the specified element is null and this
15.96/5.21 * list does not permit null elements
15.96/5.21 * @throws IllegalArgumentException if some property of this element
15.96/5.21 * prevents it from being added to this list
15.96/5.21 */
15.96/5.21 boolean add(E e);
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Removes the first occurrence of the specified element from this list,
15.96/5.21 * if it is present (optional operation). If this list does not contain
15.96/5.21 * the element, it is unchanged. More formally, removes the element with
15.96/5.21 * the lowest index i such that
15.96/5.21 * (o==null ? get(i)==null : o.equals(get(i)))
15.96/5.21 * (if such an element exists). Returns true if this list
15.96/5.21 * contained the specified element (or equivalently, if this list changed
15.96/5.21 * as a result of the call).
15.96/5.21 *
15.96/5.21 * @param o element to be removed from this list, if present
15.96/5.21 * @return true if this list contained the specified element
15.96/5.21 * @throws ClassCastException if the type of the specified element
15.96/5.21 * is incompatible with this list (optional)
15.96/5.21 * @throws NullPointerException if the specified element is null and this
15.96/5.21 * list does not permit null elements (optional)
15.96/5.21 * @throws UnsupportedOperationException if the remove operation
15.96/5.21 * is not supported by this list
15.96/5.21 */
15.96/5.21 boolean remove(Object o);
15.96/5.21
15.96/5.21
15.96/5.21 // Bulk Modification Operations
15.96/5.21
15.96/5.21 /**
15.96/5.21 * Returns true if this list contains all of the elements of the
15.96/5.21 * specified collection.
15.96/5.21 *
15.96/5.21 * @param c collection to be checked for containment in this list
15.96/5.21 * @return true if this list contains all of the elements of the
15.96/5.21 * specified collection
15.96/5.21 * @throws ClassCastException if the types of one or more elements
15.96/5.21 * in the specified collection are incompatible with this
15.96/5.21 * list (optional)
15.96/5.21 * @throws NullPointerException if the specified collection contains one
15.96/5.21 * or more null elements and this list does not permit null
16.02/5.21 * elements (optional), or if the specified collection is null
16.02/5.21 * @see #contains(Object)
16.02/5.21 */
16.02/5.21 boolean containsAll(Collection> c);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Appends all of the elements in the specified collection to the end of
16.02/5.21 * this list, in the order that they are returned by the specified
16.02/5.21 * collection's iterator (optional operation). The behavior of this
16.02/5.21 * operation is undefined if the specified collection is modified while
16.02/5.21 * the operation is in progress. (Note that this will occur if the
16.02/5.21 * specified collection is this list, and it's nonempty.)
16.02/5.21 *
16.02/5.21 * @param c collection containing elements to be added to this list
16.02/5.21 * @return true if this list changed as a result of the call
16.02/5.21 * @throws UnsupportedOperationException if the addAll operation
16.02/5.21 * is not supported by this list
16.02/5.21 * @throws ClassCastException if the class of an element of the specified
16.02/5.21 * collection prevents it from being added to this list
16.02/5.21 * @throws NullPointerException if the specified collection contains one
16.02/5.21 * or more null elements and this list does not permit null
16.02/5.21 * elements, or if the specified collection is null
16.02/5.21 * @throws IllegalArgumentException if some property of an element of the
16.02/5.21 * specified collection prevents it from being added to this list
16.02/5.21 * @see #add(Object)
16.02/5.21 */
16.02/5.21 boolean addAll(Collection extends E> c);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Inserts all of the elements in the specified collection into this
16.02/5.21 * list at the specified position (optional operation). Shifts the
16.02/5.21 * element currently at that position (if any) and any subsequent
16.02/5.21 * elements to the right (increases their indices). The new elements
16.02/5.21 * will appear in this list in the order that they are returned by the
16.02/5.21 * specified collection's iterator. The behavior of this operation is
16.02/5.21 * undefined if the specified collection is modified while the
16.02/5.21 * operation is in progress. (Note that this will occur if the specified
16.02/5.21 * collection is this list, and it's nonempty.)
16.02/5.21 *
16.02/5.21 * @param index index at which to insert the first element from the
16.02/5.21 * specified collection
16.02/5.21 * @param c collection containing elements to be added to this list
16.02/5.21 * @return true if this list changed as a result of the call
16.02/5.21 * @throws UnsupportedOperationException if the addAll operation
16.02/5.21 * is not supported by this list
16.02/5.21 * @throws ClassCastException if the class of an element of the specified
16.02/5.21 * collection prevents it from being added to this list
16.02/5.21 * @throws NullPointerException if the specified collection contains one
16.02/5.21 * or more null elements and this list does not permit null
16.02/5.21 * elements, or if the specified collection is null
16.02/5.21 * @throws IllegalArgumentException if some property of an element of the
16.02/5.21 * specified collection prevents it from being added to this list
16.02/5.21 * @throws IndexOutOfBoundsException if the index is out of range
16.02/5.21 * (index < 0 || index > size())
16.02/5.21 */
16.02/5.21 boolean addAll(int index, Collection extends E> c);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Removes from this list all of its elements that are contained in the
16.02/5.21 * specified collection (optional operation).
16.02/5.21 *
16.02/5.21 * @param c collection containing elements to be removed from this list
16.02/5.21 * @return true if this list changed as a result of the call
16.02/5.21 * @throws UnsupportedOperationException if the removeAll operation
16.02/5.21 * is not supported by this list
16.02/5.21 * @throws ClassCastException if the class of an element of this list
16.02/5.21 * is incompatible with the specified collection (optional)
16.02/5.21 * @throws NullPointerException if this list contains a null element and the
16.02/5.21 * specified collection does not permit null elements (optional),
16.02/5.21 * or if the specified collection is null
16.02/5.21 * @see #remove(Object)
16.02/5.21 * @see #contains(Object)
16.02/5.21 */
16.02/5.21 boolean removeAll(Collection> c);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Retains only the elements in this list that are contained in the
16.02/5.21 * specified collection (optional operation). In other words, removes
16.02/5.21 * from this list all of its elements that are not contained in the
16.02/5.21 * specified collection.
16.02/5.21 *
16.02/5.21 * @param c collection containing elements to be retained in this list
16.02/5.21 * @return true if this list changed as a result of the call
16.02/5.21 * @throws UnsupportedOperationException if the retainAll operation
16.02/5.21 * is not supported by this list
16.02/5.21 * @throws ClassCastException if the class of an element of this list
16.02/5.21 * is incompatible with the specified collection (optional)
16.02/5.21 * @throws NullPointerException if this list contains a null element and the
16.02/5.21 * specified collection does not permit null elements (optional),
16.02/5.21 * or if the specified collection is null
16.02/5.21 * @see #remove(Object)
16.02/5.21 * @see #contains(Object)
16.02/5.21 */
16.02/5.21 boolean retainAll(Collection> c);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Removes all of the elements from this list (optional operation).
16.02/5.21 * The list will be empty after this call returns.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException if the clear operation
16.02/5.21 * is not supported by this list
16.02/5.21 */
16.02/5.21 void clear();
16.02/5.21
16.02/5.21
16.02/5.21 // Comparison and hashing
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Compares the specified object with this list for equality. Returns
16.02/5.21 * true if and only if the specified object is also a list, both
16.02/5.21 * lists have the same size, and all corresponding pairs of elements in
16.02/5.21 * the two lists are equal. (Two elements e1 and
16.02/5.21 * e2 are equal if (e1==null ? e2==null :
16.02/5.21 * e1.equals(e2)).) In other words, two lists are defined to be
16.02/5.21 * equal if they contain the same elements in the same order. This
16.02/5.21 * definition ensures that the equals method works properly across
16.02/5.21 * different implementations of the List interface.
16.02/5.21 *
16.02/5.21 * @param o the object to be compared for equality with this list
16.02/5.21 * @return true if the specified object is equal to this list
16.02/5.21 */
16.02/5.21 boolean equals(Object o);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns the hash code value for this list. The hash code of a list
16.02/5.21 * is defined to be the result of the following calculation:
16.02/5.21 *
16.02/5.21 * int hashCode = 1;
16.02/5.21 * for (E e : list)
16.02/5.21 * hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
16.02/5.21 *
16.02/5.21 * This ensures that list1.equals(list2) implies that
16.02/5.21 * list1.hashCode()==list2.hashCode() for any two lists,
16.02/5.21 * list1 and list2, as required by the general
16.02/5.21 * contract of {@link Object#hashCode}.
16.02/5.21 *
16.02/5.21 * @return the hash code value for this list
16.02/5.21 * @see Object#equals(Object)
16.02/5.21 * @see #equals(Object)
16.02/5.21 */
16.02/5.21 int hashCode();
16.02/5.21
16.02/5.21
16.02/5.21 // Positional Access Operations
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns the element at the specified position in this list.
16.02/5.21 *
16.02/5.21 * @param index index of the element to return
16.02/5.21 * @return the element at the specified position in this list
16.02/5.21 * @throws IndexOutOfBoundsException if the index is out of range
16.02/5.21 * (index < 0 || index >= size())
16.02/5.21 */
16.02/5.21 E get(int index);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Replaces the element at the specified position in this list with the
16.02/5.21 * specified element (optional operation).
16.02/5.21 *
16.02/5.21 * @param index index of the element to replace
16.02/5.21 * @param element element to be stored at the specified position
16.02/5.21 * @return the element previously at the specified position
16.02/5.21 * @throws UnsupportedOperationException if the set operation
16.02/5.21 * is not supported by this list
16.02/5.21 * @throws ClassCastException if the class of the specified element
16.02/5.21 * prevents it from being added to this list
16.02/5.21 * @throws NullPointerException if the specified element is null and
16.02/5.21 * this list does not permit null elements
16.02/5.21 * @throws IllegalArgumentException if some property of the specified
16.02/5.21 * element prevents it from being added to this list
16.02/5.21 * @throws IndexOutOfBoundsException if the index is out of range
16.02/5.21 * (index < 0 || index >= size())
16.02/5.21 */
16.02/5.21 E set(int index, E element);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Inserts the specified element at the specified position in this list
16.02/5.21 * (optional operation). Shifts the element currently at that position
16.02/5.21 * (if any) and any subsequent elements to the right (adds one to their
16.02/5.21 * indices).
16.02/5.21 *
16.02/5.21 * @param index index at which the specified element is to be inserted
16.02/5.21 * @param element element to be inserted
16.02/5.21 * @throws UnsupportedOperationException if the add operation
16.02/5.21 * is not supported by this list
16.02/5.21 * @throws ClassCastException if the class of the specified element
16.02/5.21 * prevents it from being added to this list
16.02/5.21 * @throws NullPointerException if the specified element is null and
16.02/5.21 * this list does not permit null elements
16.02/5.21 * @throws IllegalArgumentException if some property of the specified
16.02/5.21 * element prevents it from being added to this list
16.02/5.21 * @throws IndexOutOfBoundsException if the index is out of range
16.02/5.21 * (index < 0 || index > size())
16.02/5.21 */
16.02/5.21 void add(int index, E element);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Removes the element at the specified position in this list (optional
16.02/5.21 * operation). Shifts any subsequent elements to the left (subtracts one
16.02/5.21 * from their indices). Returns the element that was removed from the
16.02/5.21 * list.
16.02/5.21 *
16.02/5.21 * @param index the index of the element to be removed
16.02/5.21 * @return the element previously at the specified position
16.02/5.21 * @throws UnsupportedOperationException if the remove operation
16.02/5.21 * is not supported by this list
16.02/5.21 * @throws IndexOutOfBoundsException if the index is out of range
16.02/5.21 * (index < 0 || index >= size())
16.02/5.21 */
16.02/5.21 E remove(int index);
16.02/5.21
16.02/5.21
16.02/5.21 // Search Operations
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns the index of the first occurrence of the specified element
16.02/5.21 * in this list, or -1 if this list does not contain the element.
16.02/5.21 * More formally, returns the lowest index i such that
16.02/5.21 * (o==null ? get(i)==null : o.equals(get(i))),
16.02/5.21 * or -1 if there is no such index.
16.02/5.21 *
16.02/5.21 * @param o element to search for
16.02/5.21 * @return the index of the first occurrence of the specified element in
16.02/5.21 * this list, or -1 if this list does not contain the element
16.02/5.21 * @throws ClassCastException if the type of the specified element
16.02/5.21 * is incompatible with this list (optional)
16.02/5.21 * @throws NullPointerException if the specified element is null and this
16.02/5.21 * list does not permit null elements (optional)
16.02/5.21 */
16.02/5.21 int indexOf(Object o);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns the index of the last occurrence of the specified element
16.02/5.21 * in this list, or -1 if this list does not contain the element.
16.02/5.21 * More formally, returns the highest index i such that
16.02/5.21 * (o==null ? get(i)==null : o.equals(get(i))),
16.02/5.21 * or -1 if there is no such index.
16.02/5.21 *
16.02/5.21 * @param o element to search for
16.02/5.21 * @return the index of the last occurrence of the specified element in
16.02/5.21 * this list, or -1 if this list does not contain the element
16.02/5.21 * @throws ClassCastException if the type of the specified element
16.02/5.21 * is incompatible with this list (optional)
16.02/5.21 * @throws NullPointerException if the specified element is null and this
16.02/5.21 * list does not permit null elements (optional)
16.02/5.21 */
16.02/5.21 int lastIndexOf(Object o);
16.02/5.21
16.02/5.21
16.02/5.21 // List Iterators
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns a list iterator over the elements in this list (in proper
16.02/5.21 * sequence).
16.02/5.21 *
16.02/5.21 * @return a list iterator over the elements in this list (in proper
16.02/5.21 * sequence)
16.02/5.21 */
16.02/5.21 ListIterator listIterator();
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns a list iterator over the elements in this list (in proper
16.02/5.21 * sequence), starting at the specified position in the list.
16.02/5.21 * The specified index indicates the first element that would be
16.02/5.21 * returned by an initial call to {@link ListIterator#next next}.
16.02/5.21 * An initial call to {@link ListIterator#previous previous} would
16.02/5.21 * return the element with the specified index minus one.
16.02/5.21 *
16.02/5.21 * @param index index of the first element to be returned from the
16.02/5.21 * list iterator (by a call to {@link ListIterator#next next})
16.02/5.21 * @return a list iterator over the elements in this list (in proper
16.02/5.21 * sequence), starting at the specified position in the list
16.02/5.21 * @throws IndexOutOfBoundsException if the index is out of range
16.02/5.21 * ({@code index < 0 || index > size()})
16.02/5.21 */
16.02/5.21 ListIterator listIterator(int index);
16.02/5.21
16.02/5.21 // View
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns a view of the portion of this list between the specified
16.02/5.21 * fromIndex, inclusive, and toIndex, exclusive. (If
16.02/5.21 * fromIndex and toIndex are equal, the returned list is
16.02/5.21 * empty.) The returned list is backed by this list, so non-structural
16.02/5.21 * changes in the returned list are reflected in this list, and vice-versa.
16.02/5.21 * The returned list supports all of the optional list operations supported
16.02/5.21 * by this list.
16.02/5.21 *
16.02/5.21 * This method eliminates the need for explicit range operations (of
16.02/5.21 * the sort that commonly exist for arrays). Any operation that expects
16.02/5.21 * a list can be used as a range operation by passing a subList view
16.02/5.21 * instead of a whole list. For example, the following idiom
16.02/5.21 * removes a range of elements from a list:
16.02/5.21 *
16.02/5.21 * list.subList(from, to).clear();
16.02/5.21 *
16.02/5.21 * Similar idioms may be constructed for indexOf and
16.02/5.21 * lastIndexOf, and all of the algorithms in the
16.02/5.21 * Collections class can be applied to a subList.
16.02/5.21 *
16.02/5.21 * The semantics of the list returned by this method become undefined if
16.02/5.21 * the backing list (i.e., this list) is structurally modified in
16.02/5.21 * any way other than via the returned list. (Structural modifications are
16.02/5.21 * those that change the size of this list, or otherwise perturb it in such
16.02/5.21 * a fashion that iterations in progress may yield incorrect results.)
16.02/5.21 *
16.02/5.21 * @param fromIndex low endpoint (inclusive) of the subList
16.02/5.21 * @param toIndex high endpoint (exclusive) of the subList
16.02/5.21 * @return a view of the specified range within this list
16.02/5.21 * @throws IndexOutOfBoundsException for an illegal endpoint index value
16.02/5.21 * (fromIndex < 0 || toIndex > size ||
16.02/5.21 * fromIndex > toIndex)
16.02/5.21 */
16.02/5.21 List subList(int fromIndex, int toIndex);
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 /*
16.02/5.21 * Copyright 1994-1998 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.21 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.21 *
16.02/5.21 * This code is free software; you can redistribute it and/or modify it
16.02/5.21 * under the terms of the GNU General Public License version 2 only, as
16.02/5.21 * published by the Free Software Foundation. Sun designates this
16.02/5.21 * particular file as subject to the "Classpath" exception as provided
16.02/5.21 * by Sun in the LICENSE file that accompanied this code.
16.02/5.21 *
16.02/5.21 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.21 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.21 * accompanied this code).
16.02/5.21 *
16.02/5.21 * You should have received a copy of the GNU General Public License version
16.02/5.21 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.21 *
16.02/5.21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.21 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.21 * have any questions.
16.02/5.21 */
16.02/5.21
16.02/5.21 package javaUtilEx;
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Thrown by the nextElement
method of an
16.02/5.21 * Enumeration
to indicate that there are no more
16.02/5.21 * elements in the enumeration.
16.02/5.21 *
16.02/5.21 * @author unascribed
16.02/5.21 * @see java.util.Enumeration
16.02/5.21 * @see java.util.Enumeration#nextElement()
16.02/5.21 * @since JDK1.0
16.02/5.21 */
16.02/5.21 public
16.02/5.21 class NoSuchElementException extends RuntimeException {
16.02/5.21 /**
16.02/5.21 * Constructs a NoSuchElementException
with null
16.02/5.21 * as its error message string.
16.02/5.21 */
16.02/5.21 public NoSuchElementException() {
16.02/5.21 super();
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Constructs a NoSuchElementException
, saving a reference
16.02/5.21 * to the error message string s for later retrieval by the
16.02/5.21 * getMessage method.
16.02/5.21 *
16.02/5.21 * @param s the detail message.
16.02/5.21 */
16.02/5.21 public NoSuchElementException(String s) {
16.02/5.21 super(s);
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 /*
16.02/5.21 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.21 *
16.02/5.21 * This code is free software; you can redistribute it and/or modify it
16.02/5.21 * under the terms of the GNU General Public License version 2 only, as
16.02/5.21 * published by the Free Software Foundation. Sun designates this
16.02/5.21 * particular file as subject to the "Classpath" exception as provided
16.02/5.21 * by Sun in the LICENSE file that accompanied this code.
16.02/5.21 *
16.02/5.21 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.21 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.21 * accompanied this code).
16.02/5.21 *
16.02/5.21 * You should have received a copy of the GNU General Public License version
16.02/5.21 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.21 *
16.02/5.21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.21 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.21 * have any questions.
16.02/5.21 */
16.02/5.21
16.02/5.21 /*
16.02/5.21 * This file is available under and governed by the GNU General Public
16.02/5.21 * License version 2 only, as published by the Free Software Foundation.
16.02/5.21 * However, the following notice accompanied the original version of this
16.02/5.21 * file:
16.02/5.21 *
16.02/5.21 * Written by Doug Lea with assistance from members of JCP JSR-166
16.02/5.21 * Expert Group and released to the public domain, as explained at
16.02/5.21 * http://creativecommons.org/licenses/publicdomain
16.02/5.21 */
16.02/5.21
16.02/5.21 package javaUtilEx;
16.02/5.21
16.02/5.21 /**
16.02/5.21 * A collection designed for holding elements prior to processing.
16.02/5.21 * Besides basic {@link java.util.Collection Collection} operations,
16.02/5.21 * queues provide additional insertion, extraction, and inspection
16.02/5.21 * operations. Each of these methods exists in two forms: one throws
16.02/5.21 * an exception if the operation fails, the other returns a special
16.02/5.21 * value (either null or false, depending on the
16.02/5.21 * operation). The latter form of the insert operation is designed
16.02/5.21 * specifically for use with capacity-restricted Queue
16.02/5.21 * implementations; in most implementations, insert operations cannot
16.02/5.21 * fail.
16.02/5.21 *
16.02/5.21 *
16.02/5.21 *
16.02/5.21 *
16.02/5.21 * |
16.02/5.21 * Throws exception |
16.02/5.21 * Returns special value |
16.02/5.21 *
16.02/5.21 *
16.02/5.21 * Insert |
16.02/5.21 * {@link #add add(e)} |
16.02/5.21 * {@link #offer offer(e)} |
16.02/5.21 *
16.02/5.21 *
16.02/5.21 * Remove |
16.02/5.21 * {@link #remove remove()} |
16.02/5.21 * {@link #poll poll()} |
16.02/5.21 *
16.02/5.21 *
16.02/5.21 * Examine |
16.02/5.21 * {@link #element element()} |
16.02/5.21 * {@link #peek peek()} |
16.02/5.21 *
16.02/5.21 *
16.02/5.21 *
16.02/5.21 * Queues typically, but do not necessarily, order elements in a
16.02/5.21 * FIFO (first-in-first-out) manner. Among the exceptions are
16.02/5.21 * priority queues, which order elements according to a supplied
16.02/5.21 * comparator, or the elements' natural ordering, and LIFO queues (or
16.02/5.21 * stacks) which order the elements LIFO (last-in-first-out).
16.02/5.21 * Whatever the ordering used, the head of the queue is that
16.02/5.21 * element which would be removed by a call to {@link #remove() } or
16.02/5.21 * {@link #poll()}. In a FIFO queue, all new elements are inserted at
16.02/5.21 * the tail of the queue. Other kinds of queues may use
16.02/5.21 * different placement rules. Every Queue implementation
16.02/5.21 * must specify its ordering properties.
16.02/5.21 *
16.02/5.21 *
The {@link #offer offer} method inserts an element if possible,
16.02/5.21 * otherwise returning false. This differs from the {@link
16.02/5.21 * java.util.Collection#add Collection.add} method, which can fail to
16.02/5.21 * add an element only by throwing an unchecked exception. The
16.02/5.21 * offer method is designed for use when failure is a normal,
16.02/5.21 * rather than exceptional occurrence, for example, in fixed-capacity
16.02/5.21 * (or "bounded") queues.
16.02/5.21 *
16.02/5.21 *
The {@link #remove()} and {@link #poll()} methods remove and
16.02/5.21 * return the head of the queue.
16.02/5.21 * Exactly which element is removed from the queue is a
16.02/5.21 * function of the queue's ordering policy, which differs from
16.02/5.21 * implementation to implementation. The remove() and
16.02/5.21 * poll() methods differ only in their behavior when the
16.02/5.21 * queue is empty: the remove() method throws an exception,
16.02/5.21 * while the poll() method returns null.
16.02/5.21 *
16.02/5.21 *
The {@link #element()} and {@link #peek()} methods return, but do
16.02/5.21 * not remove, the head of the queue.
16.02/5.21 *
16.02/5.21 *
The Queue interface does not define the blocking queue
16.02/5.21 * methods, which are common in concurrent programming. These methods,
16.02/5.21 * which wait for elements to appear or for space to become available, are
16.02/5.21 * defined in the {@link java.util.concurrent.BlockingQueue} interface, which
16.02/5.21 * extends this interface.
16.02/5.21 *
16.02/5.21 *
Queue implementations generally do not allow insertion
16.02/5.21 * of null elements, although some implementations, such as
16.02/5.21 * {@link LinkedList}, do not prohibit insertion of null.
16.02/5.21 * Even in the implementations that permit it, null should
16.02/5.21 * not be inserted into a Queue, as null is also
16.02/5.21 * used as a special return value by the poll method to
16.02/5.21 * indicate that the queue contains no elements.
16.02/5.21 *
16.02/5.21 *
Queue implementations generally do not define
16.02/5.21 * element-based versions of methods equals and
16.02/5.21 * hashCode but instead inherit the identity based versions
16.02/5.21 * from class Object, because element-based equality is not
16.02/5.21 * always well-defined for queues with the same elements but different
16.02/5.21 * ordering properties.
16.02/5.21 *
16.02/5.21 *
16.02/5.21 *
This interface is a member of the
16.02/5.21 *
16.02/5.21 * Java Collections Framework.
16.02/5.21 *
16.02/5.21 * @see java.util.Collection
16.02/5.21 * @see LinkedList
16.02/5.21 * @see PriorityQueue
16.02/5.21 * @see java.util.concurrent.LinkedBlockingQueue
16.02/5.21 * @see java.util.concurrent.BlockingQueue
16.02/5.21 * @see java.util.concurrent.ArrayBlockingQueue
16.02/5.21 * @see java.util.concurrent.LinkedBlockingQueue
16.02/5.21 * @see java.util.concurrent.PriorityBlockingQueue
16.02/5.21 * @since 1.5
16.02/5.21 * @author Doug Lea
16.02/5.21 * @param the type of elements held in this collection
16.02/5.21 */
16.02/5.21 public interface Queue extends Collection {
16.02/5.21 /**
16.02/5.21 * Inserts the specified element into this queue if it is possible to do so
16.02/5.21 * immediately without violating capacity restrictions, returning
16.02/5.21 * true upon success and throwing an IllegalStateException
16.02/5.21 * if no space is currently available.
16.02/5.21 *
16.02/5.21 * @param e the element to add
16.02/5.21 * @return true (as specified by {@link Collection#add})
16.02/5.21 * @throws IllegalStateException if the element cannot be added at this
16.02/5.21 * time due to capacity restrictions
16.02/5.21 * @throws ClassCastException if the class of the specified element
16.02/5.21 * prevents it from being added to this queue
16.02/5.21 * @throws NullPointerException if the specified element is null and
16.02/5.21 * this queue does not permit null elements
16.02/5.21 * @throws IllegalArgumentException if some property of this element
16.02/5.21 * prevents it from being added to this queue
16.02/5.21 */
16.02/5.21 boolean add(E e);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Inserts the specified element into this queue if it is possible to do
16.02/5.21 * so immediately without violating capacity restrictions.
16.02/5.21 * When using a capacity-restricted queue, this method is generally
16.02/5.21 * preferable to {@link #add}, which can fail to insert an element only
16.02/5.21 * by throwing an exception.
16.02/5.21 *
16.02/5.21 * @param e the element to add
16.02/5.21 * @return true if the element was added to this queue, else
16.02/5.21 * false
16.02/5.21 * @throws ClassCastException if the class of the specified element
16.02/5.21 * prevents it from being added to this queue
16.02/5.21 * @throws NullPointerException if the specified element is null and
16.02/5.21 * this queue does not permit null elements
16.02/5.21 * @throws IllegalArgumentException if some property of this element
16.02/5.21 * prevents it from being added to this queue
16.02/5.21 */
16.02/5.21 boolean offer(E e);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Retrieves and removes the head of this queue. This method differs
16.02/5.21 * from {@link #poll poll} only in that it throws an exception if this
16.02/5.21 * queue is empty.
16.02/5.21 *
16.02/5.21 * @return the head of this queue
16.02/5.21 * @throws NoSuchElementException if this queue is empty
16.02/5.21 */
16.02/5.21 E remove();
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Retrieves and removes the head of this queue,
16.02/5.21 * or returns null if this queue is empty.
16.02/5.21 *
16.02/5.21 * @return the head of this queue, or null if this queue is empty
16.02/5.21 */
16.02/5.21 E poll();
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Retrieves, but does not remove, the head of this queue. This method
16.02/5.21 * differs from {@link #peek peek} only in that it throws an exception
16.02/5.21 * if this queue is empty.
16.02/5.21 *
16.02/5.21 * @return the head of this queue
16.02/5.21 * @throws NoSuchElementException if this queue is empty
16.02/5.21 */
16.02/5.21 E element();
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Retrieves, but does not remove, the head of this queue,
16.02/5.21 * or returns null if this queue is empty.
16.02/5.21 *
16.02/5.21 * @return the head of this queue, or null if this queue is empty
16.02/5.21 */
16.02/5.21 E peek();
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 /*
16.02/5.21 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.21 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.21 *
16.02/5.21 * This code is free software; you can redistribute it and/or modify it
16.02/5.21 * under the terms of the GNU General Public License version 2 only, as
16.02/5.21 * published by the Free Software Foundation. Sun designates this
16.02/5.21 * particular file as subject to the "Classpath" exception as provided
16.02/5.21 * by Sun in the LICENSE file that accompanied this code.
16.02/5.21 *
16.02/5.21 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.21 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.21 * accompanied this code).
16.02/5.21 *
16.02/5.21 * You should have received a copy of the GNU General Public License version
16.02/5.21 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.21 *
16.02/5.21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.21 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.21 * have any questions.
16.02/5.21 */
16.02/5.21
16.02/5.21 package javaUtilEx;
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Marker interface used by List implementations to indicate that
16.02/5.21 * they support fast (generally constant time) random access. The primary
16.02/5.21 * purpose of this interface is to allow generic algorithms to alter their
16.02/5.21 * behavior to provide good performance when applied to either random or
16.02/5.21 * sequential access lists.
16.02/5.21 *
16.02/5.21 * The best algorithms for manipulating random access lists (such as
16.02/5.21 * ArrayList) can produce quadratic behavior when applied to
16.02/5.21 * sequential access lists (such as LinkedList). Generic list
16.02/5.21 * algorithms are encouraged to check whether the given list is an
16.02/5.21 * instanceof this interface before applying an algorithm that would
16.02/5.21 * provide poor performance if it were applied to a sequential access list,
16.02/5.21 * and to alter their behavior if necessary to guarantee acceptable
16.02/5.21 * performance.
16.02/5.21 *
16.02/5.21 *
It is recognized that the distinction between random and sequential
16.02/5.21 * access is often fuzzy. For example, some List implementations
16.02/5.21 * provide asymptotically linear access times if they get huge, but constant
16.02/5.21 * access times in practice. Such a List implementation
16.02/5.21 * should generally implement this interface. As a rule of thumb, a
16.02/5.21 * List implementation should implement this interface if,
16.02/5.21 * for typical instances of the class, this loop:
16.02/5.21 *
16.02/5.21 * for (int i=0, n=list.size(); i < n; i++)
16.02/5.21 * list.get(i);
16.02/5.21 *
16.02/5.21 * runs faster than this loop:
16.02/5.21 *
16.02/5.21 * for (Iterator i=list.iterator(); i.hasNext(); )
16.02/5.21 * i.next();
16.02/5.21 *
16.02/5.21 *
16.02/5.21 * This interface is a member of the
16.02/5.21 *
16.02/5.21 * Java Collections Framework.
16.02/5.21 *
16.02/5.21 * @since 1.4
16.02/5.21 */
16.02/5.21 public interface RandomAccess {
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 package javaUtilEx;
16.02/5.21
16.02/5.21 public class Random {
16.02/5.21 static String[] args;
16.02/5.21 static int index = 0;
16.02/5.21
16.02/5.21 public static int random() {
16.02/5.21 String string = args[index];
16.02/5.21 index++;
16.02/5.21 return string.length();
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 /*
16.02/5.21 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.21 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.21 *
16.02/5.21 * This code is free software; you can redistribute it and/or modify it
16.02/5.21 * under the terms of the GNU General Public License version 2 only, as
16.02/5.21 * published by the Free Software Foundation. Sun designates this
16.02/5.21 * particular file as subject to the "Classpath" exception as provided
16.02/5.21 * by Sun in the LICENSE file that accompanied this code.
16.02/5.21 *
16.02/5.21 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.21 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.21 * accompanied this code).
16.02/5.21 *
16.02/5.21 * You should have received a copy of the GNU General Public License version
16.02/5.21 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.21 *
16.02/5.21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.21 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.21 * have any questions.
16.02/5.21 */
16.02/5.21
16.02/5.21 package javaUtilEx;
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Thrown to indicate that the requested operation is not supported.
16.02/5.21 *
16.02/5.21 * This class is a member of the
16.02/5.21 *
16.02/5.21 * Java Collections Framework.
16.02/5.21 *
16.02/5.21 * @author Josh Bloch
16.02/5.21 * @since 1.2
16.02/5.21 */
16.02/5.21 public class UnsupportedOperationException extends RuntimeException {
16.02/5.21 /**
16.02/5.21 * Constructs an UnsupportedOperationException with no detail message.
16.02/5.21 */
16.02/5.21 public UnsupportedOperationException() {
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Constructs an UnsupportedOperationException with the specified
16.02/5.21 * detail message.
16.02/5.21 *
16.02/5.21 * @param message the detail message
16.02/5.21 */
16.02/5.21 public UnsupportedOperationException(String message) {
16.02/5.21 super(message);
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Constructs a new exception with the specified detail message and
16.02/5.21 * cause.
16.02/5.21 *
16.02/5.21 *
Note that the detail message associated with cause
is
16.02/5.21 * not automatically incorporated in this exception's detail
16.02/5.21 * message.
16.02/5.21 *
16.02/5.21 * @param message the detail message (which is saved for later retrieval
16.02/5.21 * by the {@link Throwable#getMessage()} method).
16.02/5.21 * @param cause the cause (which is saved for later retrieval by the
16.02/5.21 * {@link Throwable#getCause()} method). (A null value
16.02/5.21 * is permitted, and indicates that the cause is nonexistent or
16.02/5.21 * unknown.)
16.02/5.21 * @since 1.5
16.02/5.21 */
16.02/5.21 public UnsupportedOperationException(String message, Throwable cause) {
16.02/5.21 super(message, cause);
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Constructs a new exception with the specified cause and a detail
16.02/5.21 * message of (cause==null ? null : cause.toString()) (which
16.02/5.21 * typically contains the class and detail message of cause).
16.02/5.21 * This constructor is useful for exceptions that are little more than
16.02/5.21 * wrappers for other throwables (for example, {@link
16.02/5.21 * java.security.PrivilegedActionException}).
16.02/5.21 *
16.02/5.21 * @param cause the cause (which is saved for later retrieval by the
16.02/5.21 * {@link Throwable#getCause()} method). (A null value is
16.02/5.21 * permitted, and indicates that the cause is nonexistent or
16.02/5.21 * unknown.)
16.02/5.21 * @since 1.5
16.02/5.21 */
16.02/5.21 public UnsupportedOperationException(Throwable cause) {
16.02/5.21 super(cause);
16.02/5.21 }
16.02/5.21
16.02/5.21 static final long serialVersionUID = -1242599979055084673L;
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21
16.02/5.21 ----------------------------------------
16.02/5.21
16.02/5.21 (1) BareJBCToJBCProof (EQUIVALENT)
16.02/5.21 initialized classpath
16.02/5.21 ----------------------------------------
16.02/5.21
16.02/5.21 (2)
16.02/5.21 Obligation:
16.02/5.21 need to prove termination of the following program:
16.02/5.21 /*
16.02/5.21 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.21 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.21 *
16.02/5.21 * This code is free software; you can redistribute it and/or modify it
16.02/5.21 * under the terms of the GNU General Public License version 2 only, as
16.02/5.21 * published by the Free Software Foundation. Sun designates this
16.02/5.21 * particular file as subject to the "Classpath" exception as provided
16.02/5.21 * by Sun in the LICENSE file that accompanied this code.
16.02/5.21 *
16.02/5.21 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.21 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.21 * accompanied this code).
16.02/5.21 *
16.02/5.21 * You should have received a copy of the GNU General Public License version
16.02/5.21 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.21 *
16.02/5.21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.21 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.21 * have any questions.
16.02/5.21 */
16.02/5.21
16.02/5.21 package javaUtilEx;
16.02/5.21
16.02/5.21 /**
16.02/5.21 * This class provides a skeletal implementation of the Collection
16.02/5.21 * interface, to minimize the effort required to implement this interface.
16.02/5.21 *
16.02/5.21 * To implement an unmodifiable collection, the programmer needs only to
16.02/5.21 * extend this class and provide implementations for the iterator and
16.02/5.21 * size methods. (The iterator returned by the iterator
16.02/5.21 * method must implement hasNext and next.)
16.02/5.21 *
16.02/5.21 * To implement a modifiable collection, the programmer must additionally
16.02/5.21 * override this class's add method (which otherwise throws an
16.02/5.21 * UnsupportedOperationException), and the iterator returned by the
16.02/5.21 * iterator method must additionally implement its remove
16.02/5.21 * method.
16.02/5.21 *
16.02/5.21 * The programmer should generally provide a void (no argument) and
16.02/5.21 * Collection constructor, as per the recommendation in the
16.02/5.21 * Collection interface specification.
16.02/5.21 *
16.02/5.21 * The documentation for each non-abstract method in this class describes its
16.02/5.21 * implementation in detail. Each of these methods may be overridden if
16.02/5.21 * the collection being implemented admits a more efficient implementation.
16.02/5.21 *
16.02/5.21 * This class is a member of the
16.02/5.21 *
16.02/5.21 * Java Collections Framework.
16.02/5.21 *
16.02/5.21 * @author Josh Bloch
16.02/5.21 * @author Neal Gafter
16.02/5.21 * @see Collection
16.02/5.21 * @since 1.2
16.02/5.21 */
16.02/5.21
16.02/5.21 public abstract class AbstractCollection implements Collection {
16.02/5.21 /**
16.02/5.21 * Sole constructor. (For invocation by subclass constructors, typically
16.02/5.21 * implicit.)
16.02/5.21 */
16.02/5.21 protected AbstractCollection() {
16.02/5.21 }
16.02/5.21
16.02/5.21 // Query Operations
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns an iterator over the elements contained in this collection.
16.02/5.21 *
16.02/5.21 * @return an iterator over the elements contained in this collection
16.02/5.21 */
16.02/5.21 public abstract Iterator iterator();
16.02/5.21
16.02/5.21 public abstract int size();
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * This implementation returns size() == 0.
16.02/5.21 */
16.02/5.21 public boolean isEmpty() {
16.02/5.21 return size() == 0;
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation iterates over the elements in the collection,
16.02/5.21 * checking each element in turn for equality with the specified element.
16.02/5.21 *
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public boolean contains(Object o) {
16.02/5.21 Iterator e = iterator();
16.02/5.21 if (o==null) {
16.02/5.21 while (e.hasNext())
16.02/5.21 if (e.next()==null)
16.02/5.21 return true;
16.02/5.21 } else {
16.02/5.21 while (e.hasNext())
16.02/5.21 if (o.equals(e.next()))
16.02/5.21 return true;
16.02/5.21 }
16.02/5.21 return false;
16.02/5.21 }
16.02/5.21
16.02/5.21 // Modification Operations
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * This implementation always throws an
16.02/5.21 * UnsupportedOperationException.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 * @throws IllegalArgumentException {@inheritDoc}
16.02/5.21 * @throws IllegalStateException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public boolean add(E e) {
16.02/5.21 throw new UnsupportedOperationException();
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation iterates over the collection looking for the
16.02/5.21 * specified element. If it finds the element, it removes the element
16.02/5.21 * from the collection using the iterator's remove method.
16.02/5.21 *
16.02/5.21 *
Note that this implementation throws an
16.02/5.21 * UnsupportedOperationException if the iterator returned by this
16.02/5.21 * collection's iterator method does not implement the remove
16.02/5.21 * method and this collection contains the specified object.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public boolean remove(Object o) {
16.02/5.21 Iterator e = iterator();
16.02/5.21 if (o==null) {
16.02/5.21 while (e.hasNext()) {
16.02/5.21 if (e.next()==null) {
16.02/5.21 e.remove();
16.02/5.21 return true;
16.02/5.21 }
16.02/5.21 }
16.02/5.21 } else {
16.02/5.21 while (e.hasNext()) {
16.02/5.21 if (o.equals(e.next())) {
16.02/5.21 e.remove();
16.02/5.21 return true;
16.02/5.21 }
16.02/5.21 }
16.02/5.21 }
16.02/5.21 return false;
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 // Bulk Operations
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * This implementation iterates over the specified collection,
16.02/5.21 * checking each element returned by the iterator in turn to see
16.02/5.21 * if it's contained in this collection. If all elements are so
16.02/5.21 * contained true is returned, otherwise false.
16.02/5.21 *
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 * @see #contains(Object)
16.02/5.21 */
16.02/5.21 public boolean containsAll(Collection> c) {
16.02/5.21 Iterator> e = c.iterator();
16.02/5.21 while (e.hasNext())
16.02/5.21 if (!contains(e.next()))
16.02/5.21 return false;
16.02/5.21 return true;
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation iterates over the specified collection, and adds
16.02/5.21 * each object returned by the iterator to this collection, in turn.
16.02/5.21 *
16.02/5.21 *
Note that this implementation will throw an
16.02/5.21 * UnsupportedOperationException unless add is
16.02/5.21 * overridden (assuming the specified collection is non-empty).
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 * @throws IllegalArgumentException {@inheritDoc}
16.02/5.21 * @throws IllegalStateException {@inheritDoc}
16.02/5.21 *
16.02/5.21 * @see #add(Object)
16.02/5.21 */
16.02/5.21 public boolean addAll(Collection extends E> c) {
16.02/5.21 boolean modified = false;
16.02/5.21 Iterator extends E> e = c.iterator();
16.02/5.21 while (e.hasNext()) {
16.02/5.21 if (add(e.next()))
16.02/5.21 modified = true;
16.02/5.21 }
16.02/5.21 return modified;
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation iterates over this collection, checking each
16.02/5.21 * element returned by the iterator in turn to see if it's contained
16.02/5.21 * in the specified collection. If it's so contained, it's removed from
16.02/5.21 * this collection with the iterator's remove method.
16.02/5.21 *
16.02/5.21 *
Note that this implementation will throw an
16.02/5.21 * UnsupportedOperationException if the iterator returned by the
16.02/5.21 * iterator method does not implement the remove method
16.02/5.21 * and this collection contains one or more elements in common with the
16.02/5.21 * specified collection.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 *
16.02/5.21 * @see #remove(Object)
16.02/5.21 * @see #contains(Object)
16.02/5.21 */
16.02/5.21 public boolean removeAll(Collection> c) {
16.02/5.21 boolean modified = false;
16.02/5.21 Iterator> e = iterator();
16.02/5.21 while (e.hasNext()) {
16.02/5.21 if (c.contains(e.next())) {
16.02/5.21 e.remove();
16.02/5.21 modified = true;
16.02/5.21 }
16.02/5.21 }
16.02/5.21 return modified;
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation iterates over this collection, checking each
16.02/5.21 * element returned by the iterator in turn to see if it's contained
16.02/5.21 * in the specified collection. If it's not so contained, it's removed
16.02/5.21 * from this collection with the iterator's remove method.
16.02/5.21 *
16.02/5.21 *
Note that this implementation will throw an
16.02/5.21 * UnsupportedOperationException if the iterator returned by the
16.02/5.21 * iterator method does not implement the remove method
16.02/5.21 * and this collection contains one or more elements not present in the
16.02/5.21 * specified collection.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 *
16.02/5.21 * @see #remove(Object)
16.02/5.21 * @see #contains(Object)
16.02/5.21 */
16.02/5.21 public boolean retainAll(Collection> c) {
16.02/5.21 boolean modified = false;
16.02/5.21 Iterator e = iterator();
16.02/5.21 while (e.hasNext()) {
16.02/5.21 if (!c.contains(e.next())) {
16.02/5.21 e.remove();
16.02/5.21 modified = true;
16.02/5.21 }
16.02/5.21 }
16.02/5.21 return modified;
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * This implementation iterates over this collection, removing each
16.02/5.21 * element using the Iterator.remove operation. Most
16.02/5.21 * implementations will probably choose to override this method for
16.02/5.21 * efficiency.
16.02/5.21 *
16.02/5.21 *
Note that this implementation will throw an
16.02/5.21 * UnsupportedOperationException if the iterator returned by this
16.02/5.21 * collection's iterator method does not implement the
16.02/5.21 * remove method and this collection is non-empty.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public void clear() {
16.02/5.21 Iterator e = iterator();
16.02/5.21 while (e.hasNext()) {
16.02/5.21 e.next();
16.02/5.21 e.remove();
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 // String conversion
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns a string representation of this collection. The string
16.02/5.21 * representation consists of a list of the collection's elements in the
16.02/5.21 * order they are returned by its iterator, enclosed in square brackets
16.02/5.21 * ("[]"). Adjacent elements are separated by the characters
16.02/5.21 * ", " (comma and space). Elements are converted to strings as
16.02/5.21 * by {@link String#valueOf(Object)}.
16.02/5.21 *
16.02/5.21 * @return a string representation of this collection
16.02/5.21 */
16.02/5.21 public String toString() {
16.02/5.21 Iterator i = iterator();
16.02/5.21 if (! i.hasNext())
16.02/5.21 return "[]";
16.02/5.21
16.02/5.21 String sb = "";
16.02/5.21 sb = sb + "[";
16.02/5.21 for (;;) {
16.02/5.21 E e = i.next();
16.02/5.21 sb = sb + (e == this ? "(this Collection)" : e);
16.02/5.21 if (! i.hasNext()) {
16.02/5.21 sb = sb + "]";
16.02/5.21 return sb;
16.02/5.21 }
16.02/5.21 sb = sb + ", ";
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 /*
16.02/5.21 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.21 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.21 *
16.02/5.21 * This code is free software; you can redistribute it and/or modify it
16.02/5.21 * under the terms of the GNU General Public License version 2 only, as
16.02/5.21 * published by the Free Software Foundation. Sun designates this
16.02/5.21 * particular file as subject to the "Classpath" exception as provided
16.02/5.21 * by Sun in the LICENSE file that accompanied this code.
16.02/5.21 *
16.02/5.21 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.21 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.21 * accompanied this code).
16.02/5.21 *
16.02/5.21 * You should have received a copy of the GNU General Public License version
16.02/5.21 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.21 *
16.02/5.21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.21 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.21 * have any questions.
16.02/5.21 */
16.02/5.21
16.02/5.21 package javaUtilEx;
16.02/5.21
16.02/5.21 /**
16.02/5.21 * This class provides a skeletal implementation of the {@link List}
16.02/5.21 * interface to minimize the effort required to implement this interface
16.02/5.21 * backed by a "random access" data store (such as an array). For sequential
16.02/5.21 * access data (such as a linked list), {@link AbstractSequentialList} should
16.02/5.21 * be used in preference to this class.
16.02/5.21 *
16.02/5.21 * To implement an unmodifiable list, the programmer needs only to extend
16.02/5.21 * this class and provide implementations for the {@link #get(int)} and
16.02/5.21 * {@link List#size() size()} methods.
16.02/5.21 *
16.02/5.21 *
To implement a modifiable list, the programmer must additionally
16.02/5.21 * override the {@link #set(int, Object) set(int, E)} method (which otherwise
16.02/5.21 * throws an {@code UnsupportedOperationException}). If the list is
16.02/5.21 * variable-size the programmer must additionally override the
16.02/5.21 * {@link #add(int, Object) add(int, E)} and {@link #remove(int)} methods.
16.02/5.21 *
16.02/5.21 *
The programmer should generally provide a void (no argument) and collection
16.02/5.21 * constructor, as per the recommendation in the {@link Collection} interface
16.02/5.21 * specification.
16.02/5.21 *
16.02/5.21 *
Unlike the other abstract collection implementations, the programmer does
16.02/5.21 * not have to provide an iterator implementation; the iterator and
16.02/5.21 * list iterator are implemented by this class, on top of the "random access"
16.02/5.21 * methods:
16.02/5.21 * {@link #get(int)},
16.02/5.21 * {@link #set(int, Object) set(int, E)},
16.02/5.21 * {@link #add(int, Object) add(int, E)} and
16.02/5.21 * {@link #remove(int)}.
16.02/5.21 *
16.02/5.21 *
The documentation for each non-abstract method in this class describes its
16.02/5.21 * implementation in detail. Each of these methods may be overridden if the
16.02/5.21 * collection being implemented admits a more efficient implementation.
16.02/5.21 *
16.02/5.21 *
This class is a member of the
16.02/5.21 *
16.02/5.21 * Java Collections Framework.
16.02/5.21 *
16.02/5.21 * @author Josh Bloch
16.02/5.21 * @author Neal Gafter
16.02/5.21 * @since 1.2
16.02/5.21 */
16.02/5.21
16.02/5.21 public abstract class AbstractList extends AbstractCollection implements List {
16.02/5.21 /**
16.02/5.21 * Sole constructor. (For invocation by subclass constructors, typically
16.02/5.21 * implicit.)
16.02/5.21 */
16.02/5.21 protected AbstractList() {
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Appends the specified element to the end of this list (optional
16.02/5.21 * operation).
16.02/5.21 *
16.02/5.21 * Lists that support this operation may place limitations on what
16.02/5.21 * elements may be added to this list. In particular, some
16.02/5.21 * lists will refuse to add null elements, and others will impose
16.02/5.21 * restrictions on the type of elements that may be added. List
16.02/5.21 * classes should clearly specify in their documentation any restrictions
16.02/5.21 * on what elements may be added.
16.02/5.21 *
16.02/5.21 *
This implementation calls {@code add(size(), e)}.
16.02/5.21 *
16.02/5.21 *
Note that this implementation throws an
16.02/5.21 * {@code UnsupportedOperationException} unless
16.02/5.21 * {@link #add(int, Object) add(int, E)} is overridden.
16.02/5.21 *
16.02/5.21 * @param e element to be appended to this list
16.02/5.21 * @return {@code true} (as specified by {@link Collection#add})
16.02/5.21 * @throws UnsupportedOperationException if the {@code add} operation
16.02/5.21 * is not supported by this list
16.02/5.21 * @throws ClassCastException if the class of the specified element
16.02/5.21 * prevents it from being added to this list
16.02/5.21 * @throws NullPointerException if the specified element is null and this
16.02/5.21 * list does not permit null elements
16.02/5.21 * @throws IllegalArgumentException if some property of this element
16.02/5.21 * prevents it from being added to this list
16.02/5.21 */
16.02/5.21 public boolean add(E e) {
16.02/5.21 add(size(), e);
16.02/5.21 return true;
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.21 */
16.02/5.21 abstract public E get(int index);
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation always throws an
16.02/5.21 * {@code UnsupportedOperationException}.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 * @throws IllegalArgumentException {@inheritDoc}
16.02/5.21 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public E set(int index, E element) {
16.02/5.21 throw new UnsupportedOperationException();
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation always throws an
16.02/5.21 * {@code UnsupportedOperationException}.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 * @throws IllegalArgumentException {@inheritDoc}
16.02/5.21 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public void add(int index, E element) {
16.02/5.21 throw new UnsupportedOperationException();
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation always throws an
16.02/5.21 * {@code UnsupportedOperationException}.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public E remove(int index) {
16.02/5.21 throw new UnsupportedOperationException();
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 // Search Operations
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation first gets a list iterator (with
16.02/5.21 * {@code listIterator()}). Then, it iterates over the list until the
16.02/5.21 * specified element is found or the end of the list is reached.
16.02/5.21 *
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public int indexOf(Object o) {
16.02/5.21 ListIterator e = listIterator();
16.02/5.21 if (o==null) {
16.02/5.21 while (e.hasNext())
16.02/5.21 if (e.next()==null)
16.02/5.21 return e.previousIndex();
16.02/5.21 } else {
16.02/5.21 while (e.hasNext())
16.02/5.21 if (o.equals(e.next()))
16.02/5.21 return e.previousIndex();
16.02/5.21 }
16.02/5.21 return -1;
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * This implementation first gets a list iterator that points to the end
16.02/5.21 * of the list (with {@code listIterator(size())}). Then, it iterates
16.02/5.21 * backwards over the list until the specified element is found, or the
16.02/5.21 * beginning of the list is reached.
16.02/5.21 *
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public int lastIndexOf(Object o) {
16.02/5.21 ListIterator e = listIterator(size());
16.02/5.21 if (o==null) {
16.02/5.21 while (e.hasPrevious())
16.02/5.21 if (e.previous()==null)
16.02/5.21 return e.nextIndex();
16.02/5.21 } else {
16.02/5.21 while (e.hasPrevious())
16.02/5.21 if (o.equals(e.previous()))
16.02/5.21 return e.nextIndex();
16.02/5.21 }
16.02/5.21 return -1;
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 // Bulk Operations
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Removes all of the elements from this list (optional operation).
16.02/5.21 * The list will be empty after this call returns.
16.02/5.21 *
16.02/5.21 * This implementation calls {@code removeRange(0, size())}.
16.02/5.21 *
16.02/5.21 *
Note that this implementation throws an
16.02/5.21 * {@code UnsupportedOperationException} unless {@code remove(int
16.02/5.21 * index)} or {@code removeRange(int fromIndex, int toIndex)} is
16.02/5.21 * overridden.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException if the {@code clear} operation
16.02/5.21 * is not supported by this list
16.02/5.21 */
16.02/5.21 public void clear() {
16.02/5.21 removeRange(0, size());
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 *
This implementation gets an iterator over the specified collection
16.02/5.21 * and iterates over it, inserting the elements obtained from the
16.02/5.21 * iterator into this list at the appropriate position, one at a time,
16.02/5.21 * using {@code add(int, E)}.
16.02/5.21 * Many implementations will override this method for efficiency.
16.02/5.21 *
16.02/5.21 *
Note that this implementation throws an
16.02/5.21 * {@code UnsupportedOperationException} unless
16.02/5.21 * {@link #add(int, Object) add(int, E)} is overridden.
16.02/5.21 *
16.02/5.21 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.21 * @throws ClassCastException {@inheritDoc}
16.02/5.21 * @throws NullPointerException {@inheritDoc}
16.02/5.21 * @throws IllegalArgumentException {@inheritDoc}
16.02/5.21 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public boolean addAll(int index, Collection extends E> c) {
16.02/5.21 rangeCheckForAdd(index);
16.02/5.21 boolean modified = false;
16.02/5.21 Iterator extends E> e = c.iterator();
16.02/5.21 while (e.hasNext()) {
16.02/5.21 add(index++, e.next());
16.02/5.21 modified = true;
16.02/5.21 }
16.02/5.21 return modified;
16.02/5.21 }
16.02/5.21
16.02/5.21
16.02/5.21 // Iterators
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Returns an iterator over the elements in this list in proper sequence.
16.02/5.21 *
16.02/5.21 *
This implementation returns a straightforward implementation of the
16.02/5.21 * iterator interface, relying on the backing list's {@code size()},
16.02/5.21 * {@code get(int)}, and {@code remove(int)} methods.
16.02/5.21 *
16.02/5.21 *
Note that the iterator returned by this method will throw an
16.02/5.21 * {@link UnsupportedOperationException} in response to its
16.02/5.21 * {@code remove} method unless the list's {@code remove(int)} method is
16.02/5.21 * overridden.
16.02/5.21 *
16.02/5.21 *
This implementation can be made to throw runtime exceptions in the
16.02/5.21 * face of concurrent modification, as described in the specification
16.02/5.21 * for the (protected) {@link #modCount} field.
16.02/5.21 *
16.02/5.21 * @return an iterator over the elements in this list in proper sequence
16.02/5.21 */
16.02/5.21 public Iterator iterator() {
16.02/5.21 return new Itr();
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * This implementation returns {@code listIterator(0)}.
16.02/5.21 *
16.02/5.21 * @see #listIterator(int)
16.02/5.21 */
16.02/5.21 public ListIterator listIterator() {
16.02/5.21 return listIterator(0);
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * This implementation returns a straightforward implementation of the
16.02/5.21 * {@code ListIterator} interface that extends the implementation of the
16.02/5.21 * {@code Iterator} interface returned by the {@code iterator()} method.
16.02/5.21 * The {@code ListIterator} implementation relies on the backing list's
16.02/5.21 * {@code get(int)}, {@code set(int, E)}, {@code add(int, E)}
16.02/5.21 * and {@code remove(int)} methods.
16.02/5.21 *
16.02/5.21 *
Note that the list iterator returned by this implementation will
16.02/5.21 * throw an {@link UnsupportedOperationException} in response to its
16.02/5.21 * {@code remove}, {@code set} and {@code add} methods unless the
16.02/5.21 * list's {@code remove(int)}, {@code set(int, E)}, and
16.02/5.21 * {@code add(int, E)} methods are overridden.
16.02/5.21 *
16.02/5.21 *
This implementation can be made to throw runtime exceptions in the
16.02/5.21 * face of concurrent modification, as described in the specification for
16.02/5.21 * the (protected) {@link #modCount} field.
16.02/5.21 *
16.02/5.21 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.21 */
16.02/5.21 public ListIterator listIterator(final int index) {
16.02/5.21 rangeCheckForAdd(index);
16.02/5.21
16.02/5.21 return new ListItr(index);
16.02/5.21 }
16.02/5.21
16.02/5.21 private class Itr implements Iterator {
16.02/5.21 /**
16.02/5.21 * Index of element to be returned by subsequent call to next.
16.02/5.21 */
16.02/5.21 int cursor = 0;
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Index of element returned by most recent call to next or
16.02/5.21 * previous. Reset to -1 if this element is deleted by a call
16.02/5.21 * to remove.
16.02/5.21 */
16.02/5.21 int lastRet = -1;
16.02/5.21
16.02/5.21 /**
16.02/5.21 * The modCount value that the iterator believes that the backing
16.02/5.21 * List should have. If this expectation is violated, the iterator
16.02/5.21 * has detected concurrent modification.
16.02/5.21 */
16.02/5.21 int expectedModCount = modCount;
16.02/5.21
16.02/5.21 public boolean hasNext() {
16.02/5.21 return cursor != size();
16.02/5.21 }
16.02/5.21
16.02/5.21 public E next() {
16.02/5.21 checkForComodification();
16.02/5.21 try {
16.02/5.21 int i = cursor;
16.02/5.21 E next = get(i);
16.02/5.21 lastRet = i;
16.02/5.21 cursor = i + 1;
16.02/5.21 return next;
16.02/5.21 } catch (IndexOutOfBoundsException e) {
16.02/5.21 checkForComodification();
16.02/5.21 throw new NoSuchElementException();
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21 public void remove() {
16.02/5.21 if (lastRet < 0)
16.02/5.21 throw new IllegalStateException();
16.02/5.21 checkForComodification();
16.02/5.21
16.02/5.21 try {
16.02/5.21 AbstractList.this.remove(lastRet);
16.02/5.21 if (lastRet < cursor)
16.02/5.21 cursor--;
16.02/5.21 lastRet = -1;
16.02/5.21 expectedModCount = modCount;
16.02/5.21 } catch (IndexOutOfBoundsException e) {
16.02/5.21 throw new ConcurrentModificationException();
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21 final void checkForComodification() {
16.02/5.21 if (modCount != expectedModCount)
16.02/5.21 throw new ConcurrentModificationException();
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21 private class ListItr extends Itr implements ListIterator {
16.02/5.21 ListItr(int index) {
16.02/5.21 cursor = index;
16.02/5.21 }
16.02/5.21
16.02/5.21 public boolean hasPrevious() {
16.02/5.21 return cursor != 0;
16.02/5.21 }
16.02/5.21
16.02/5.21 public E previous() {
16.02/5.21 checkForComodification();
16.02/5.21 try {
16.02/5.21 int i = cursor - 1;
16.02/5.21 E previous = get(i);
16.02/5.21 lastRet = cursor = i;
16.02/5.21 return previous;
16.02/5.21 } catch (IndexOutOfBoundsException e) {
16.02/5.21 checkForComodification();
16.02/5.21 throw new NoSuchElementException();
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21 public int nextIndex() {
16.02/5.21 return cursor;
16.02/5.21 }
16.02/5.21
16.02/5.21 public int previousIndex() {
16.02/5.21 return cursor-1;
16.02/5.21 }
16.02/5.21
16.02/5.21 public void set(E e) {
16.02/5.21 if (lastRet < 0)
16.02/5.21 throw new IllegalStateException();
16.02/5.21 checkForComodification();
16.02/5.21
16.02/5.21 try {
16.02/5.21 AbstractList.this.set(lastRet, e);
16.02/5.21 expectedModCount = modCount;
16.02/5.21 } catch (IndexOutOfBoundsException ex) {
16.02/5.21 throw new ConcurrentModificationException();
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21 public void add(E e) {
16.02/5.21 checkForComodification();
16.02/5.21
16.02/5.21 try {
16.02/5.21 int i = cursor;
16.02/5.21 AbstractList.this.add(i, e);
16.02/5.21 lastRet = -1;
16.02/5.21 cursor = i + 1;
16.02/5.21 expectedModCount = modCount;
16.02/5.21 } catch (IndexOutOfBoundsException ex) {
16.02/5.21 throw new ConcurrentModificationException();
16.02/5.21 }
16.02/5.21 }
16.02/5.21 }
16.02/5.21
16.02/5.21 /**
16.02/5.21 * {@inheritDoc}
16.02/5.21 *
16.02/5.21 * This implementation returns a list that subclasses
16.02/5.21 * {@code AbstractList}. The subclass stores, in private fields, the
16.02/5.21 * offset of the subList within the backing list, the size of the subList
16.02/5.21 * (which can change over its lifetime), and the expected
16.02/5.21 * {@code modCount} value of the backing list. There are two variants
16.02/5.21 * of the subclass, one of which implements {@code RandomAccess}.
16.02/5.21 * If this list implements {@code RandomAccess} the returned list will
16.02/5.21 * be an instance of the subclass that implements {@code RandomAccess}.
16.02/5.21 *
16.02/5.21 *
The subclass's {@code set(int, E)}, {@code get(int)},
16.02/5.21 * {@code add(int, E)}, {@code remove(int)}, {@code addAll(int,
16.02/5.21 * Collection)} and {@code removeRange(int, int)} methods all
16.02/5.21 * delegate to the corresponding methods on the backing abstract list,
16.02/5.21 * after bounds-checking the index and adjusting for the offset. The
16.02/5.21 * {@code addAll(Collection c)} method merely returns {@code addAll(size,
16.02/5.21 * c)}.
16.02/5.21 *
16.02/5.21 *
The {@code listIterator(int)} method returns a "wrapper object"
16.02/5.21 * over a list iterator on the backing list, which is created with the
16.02/5.21 * corresponding method on the backing list. The {@code iterator} method
16.02/5.21 * merely returns {@code listIterator()}, and the {@code size} method
16.02/5.21 * merely returns the subclass's {@code size} field.
16.02/5.21 *
16.02/5.21 *
All methods first check to see if the actual {@code modCount} of
16.02/5.21 * the backing list is equal to its expected value, and throw a
16.02/5.21 * {@code ConcurrentModificationException} if it is not.
16.02/5.21 *
16.02/5.21 * @throws IndexOutOfBoundsException if an endpoint index value is out of range
16.02/5.21 * {@code (fromIndex < 0 || toIndex > size)}
16.02/5.21 * @throws IllegalArgumentException if the endpoint indices are out of order
16.02/5.21 * {@code (fromIndex > toIndex)}
16.02/5.21 */
16.02/5.21 public List subList(int fromIndex, int toIndex) {
16.02/5.21 return (this instanceof RandomAccess ?
16.02/5.21 new RandomAccessSubList(this, fromIndex, toIndex) :
16.02/5.21 new SubList(this, fromIndex, toIndex));
16.02/5.21 }
16.02/5.21
16.02/5.21 // Comparison and hashing
16.02/5.21
16.02/5.21 /**
16.02/5.21 * Compares the specified object with this list for equality. Returns
16.02/5.21 * {@code true} if and only if the specified object is also a list, both
16.02/5.21 * lists have the same size, and all corresponding pairs of elements in
16.02/5.21 * the two lists are equal. (Two elements {@code e1} and
16.02/5.21 * {@code e2} are equal if {@code (e1==null ? e2==null :
16.02/5.21 * e1.equals(e2))}.) In other words, two lists are defined to be
16.02/5.21 * equal if they contain the same elements in the same order.
16.02/5.21 *
16.02/5.21 * This implementation first checks if the specified object is this
16.02/5.21 * list. If so, it returns {@code true}; if not, it checks if the
16.02/5.21 * specified object is a list. If not, it returns {@code false}; if so,
16.02/5.21 * it iterates over both lists, comparing corresponding pairs of elements.
16.02/5.21 * If any comparison returns {@code false}, this method returns
16.02/5.22 * {@code false}. If either iterator runs out of elements before the
16.02/5.22 * other it returns {@code false} (as the lists are of unequal length);
16.02/5.22 * otherwise it returns {@code true} when the iterations complete.
16.02/5.22 *
16.02/5.22 * @param o the object to be compared for equality with this list
16.02/5.22 * @return {@code true} if the specified object is equal to this list
16.02/5.22 */
16.02/5.22 public boolean equals(Object o) {
16.02/5.22 if (o == this)
16.02/5.22 return true;
16.02/5.22 if (!(o instanceof List))
16.02/5.22 return false;
16.02/5.22
16.02/5.22 ListIterator e1 = listIterator();
16.02/5.22 ListIterator e2 = ((List) o).listIterator();
16.02/5.22 while(e1.hasNext() && e2.hasNext()) {
16.02/5.22 E o1 = e1.next();
16.02/5.22 Object o2 = e2.next();
16.02/5.22 if (!(o1==null ? o2==null : o1.equals(o2)))
16.02/5.22 return false;
16.02/5.22 }
16.02/5.22 return !(e1.hasNext() || e2.hasNext());
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns the hash code value for this list.
16.02/5.22 *
16.02/5.22 * This implementation uses exactly the code that is used to define the
16.02/5.22 * list hash function in the documentation for the {@link List#hashCode}
16.02/5.22 * method.
16.02/5.22 *
16.02/5.22 * @return the hash code value for this list
16.02/5.22 */
16.02/5.22 public int hashCode() {
16.02/5.22 int hashCode = 1;
16.02/5.22 Iterator it = this.iterator();
16.02/5.22 while (it.hasNext()) {
16.02/5.22 E e = it.next();
16.02/5.22 hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
16.02/5.22 }
16.02/5.22 return hashCode;
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes from this list all of the elements whose index is between
16.02/5.22 * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.
16.02/5.22 * Shifts any succeeding elements to the left (reduces their index).
16.02/5.22 * This call shortens the list by {@code (toIndex - fromIndex)} elements.
16.02/5.22 * (If {@code toIndex==fromIndex}, this operation has no effect.)
16.02/5.22 *
16.02/5.22 * This method is called by the {@code clear} operation on this list
16.02/5.22 * and its subLists. Overriding this method to take advantage of
16.02/5.22 * the internals of the list implementation can substantially
16.02/5.22 * improve the performance of the {@code clear} operation on this list
16.02/5.22 * and its subLists.
16.02/5.22 *
16.02/5.22 *
This implementation gets a list iterator positioned before
16.02/5.22 * {@code fromIndex}, and repeatedly calls {@code ListIterator.next}
16.02/5.22 * followed by {@code ListIterator.remove} until the entire range has
16.02/5.22 * been removed. Note: if {@code ListIterator.remove} requires linear
16.02/5.22 * time, this implementation requires quadratic time.
16.02/5.22 *
16.02/5.22 * @param fromIndex index of first element to be removed
16.02/5.22 * @param toIndex index after last element to be removed
16.02/5.22 */
16.02/5.22 protected void removeRange(int fromIndex, int toIndex) {
16.02/5.22 ListIterator it = listIterator(fromIndex);
16.02/5.22 for (int i=0, n=toIndex-fromIndex; istructurally modified.
16.02/5.22 * Structural modifications are those that change the size of the
16.02/5.22 * list, or otherwise perturb it in such a fashion that iterations in
16.02/5.22 * progress may yield incorrect results.
16.02/5.22 *
16.02/5.22 * This field is used by the iterator and list iterator implementation
16.02/5.22 * returned by the {@code iterator} and {@code listIterator} methods.
16.02/5.22 * If the value of this field changes unexpectedly, the iterator (or list
16.02/5.22 * iterator) will throw a {@code ConcurrentModificationException} in
16.02/5.22 * response to the {@code next}, {@code remove}, {@code previous},
16.02/5.22 * {@code set} or {@code add} operations. This provides
16.02/5.22 * fail-fast behavior, rather than non-deterministic behavior in
16.02/5.22 * the face of concurrent modification during iteration.
16.02/5.22 *
16.02/5.22 *
Use of this field by subclasses is optional. If a subclass
16.02/5.22 * wishes to provide fail-fast iterators (and list iterators), then it
16.02/5.22 * merely has to increment this field in its {@code add(int, E)} and
16.02/5.22 * {@code remove(int)} methods (and any other methods that it overrides
16.02/5.22 * that result in structural modifications to the list). A single call to
16.02/5.22 * {@code add(int, E)} or {@code remove(int)} must add no more than
16.02/5.22 * one to this field, or the iterators (and list iterators) will throw
16.02/5.22 * bogus {@code ConcurrentModificationExceptions}. If an implementation
16.02/5.22 * does not wish to provide fail-fast iterators, this field may be
16.02/5.22 * ignored.
16.02/5.22 */
16.02/5.22 protected transient int modCount = 0;
16.02/5.22
16.02/5.22 private void rangeCheckForAdd(int index) {
16.02/5.22 if (index < 0 || index > size())
16.02/5.22 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
16.02/5.22 }
16.02/5.22
16.02/5.22 private String outOfBoundsMsg(int index) {
16.02/5.22 return "";
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22 class SubList extends AbstractList {
16.02/5.22 private final AbstractList l;
16.02/5.22 private final int offset;
16.02/5.22 private int size;
16.02/5.22
16.02/5.22 SubList(AbstractList list, int fromIndex, int toIndex) {
16.02/5.22 if (fromIndex < 0)
16.02/5.22 throw new IndexOutOfBoundsException();
16.02/5.22 if (toIndex > list.size())
16.02/5.22 throw new IndexOutOfBoundsException();
16.02/5.22 if (fromIndex > toIndex)
16.02/5.22 throw new IllegalArgumentException();
16.02/5.22 l = list;
16.02/5.22 offset = fromIndex;
16.02/5.22 size = toIndex - fromIndex;
16.02/5.22 this.modCount = l.modCount;
16.02/5.22 }
16.02/5.22
16.02/5.22 public E set(int index, E element) {
16.02/5.22 rangeCheck(index);
16.02/5.22 checkForComodification();
16.02/5.22 return l.set(index+offset, element);
16.02/5.22 }
16.02/5.22
16.02/5.22 public E get(int index) {
16.02/5.22 rangeCheck(index);
16.02/5.22 checkForComodification();
16.02/5.22 return l.get(index+offset);
16.02/5.22 }
16.02/5.22
16.02/5.22 public int size() {
16.02/5.22 checkForComodification();
16.02/5.22 return size;
16.02/5.22 }
16.02/5.22
16.02/5.22 public void add(int index, E element) {
16.02/5.22 rangeCheckForAdd(index);
16.02/5.22 checkForComodification();
16.02/5.22 l.add(index+offset, element);
16.02/5.22 this.modCount = l.modCount;
16.02/5.22 size++;
16.02/5.22 }
16.02/5.22
16.02/5.22 public E remove(int index) {
16.02/5.22 rangeCheck(index);
16.02/5.22 checkForComodification();
16.02/5.22 E result = l.remove(index+offset);
16.02/5.22 this.modCount = l.modCount;
16.02/5.22 size--;
16.02/5.22 return result;
16.02/5.22 }
16.02/5.22
16.02/5.22 protected void removeRange(int fromIndex, int toIndex) {
16.02/5.22 checkForComodification();
16.02/5.22 l.removeRange(fromIndex+offset, toIndex+offset);
16.02/5.22 this.modCount = l.modCount;
16.02/5.22 size -= (toIndex-fromIndex);
16.02/5.22 }
16.02/5.22
16.02/5.22 public boolean addAll(Collection extends E> c) {
16.02/5.22 return addAll(size, c);
16.02/5.22 }
16.02/5.22
16.02/5.22 public boolean addAll(int index, Collection extends E> c) {
16.02/5.22 rangeCheckForAdd(index);
16.02/5.22 int cSize = c.size();
16.02/5.22 if (cSize==0)
16.02/5.22 return false;
16.02/5.22
16.02/5.22 checkForComodification();
16.02/5.22 l.addAll(offset+index, c);
16.02/5.22 this.modCount = l.modCount;
16.02/5.22 size += cSize;
16.02/5.22 return true;
16.02/5.22 }
16.02/5.22
16.02/5.22 public Iterator iterator() {
16.02/5.22 return listIterator();
16.02/5.22 }
16.02/5.22
16.02/5.22 public ListIterator listIterator(final int index) {
16.02/5.22 checkForComodification();
16.02/5.22 rangeCheckForAdd(index);
16.02/5.22
16.02/5.22 return new ListIterator() {
16.02/5.22 private final ListIterator i = l.listIterator(index+offset);
16.02/5.22
16.02/5.22 public boolean hasNext() {
16.02/5.22 return nextIndex() < size;
16.02/5.22 }
16.02/5.22
16.02/5.22 public E next() {
16.02/5.22 if (hasNext())
16.02/5.22 return i.next();
16.02/5.22 else
16.02/5.22 throw new NoSuchElementException();
16.02/5.22 }
16.02/5.22
16.02/5.22 public boolean hasPrevious() {
16.02/5.22 return previousIndex() >= 0;
16.02/5.22 }
16.02/5.22
16.02/5.22 public E previous() {
16.02/5.22 if (hasPrevious())
16.02/5.22 return i.previous();
16.02/5.22 else
16.02/5.22 throw new NoSuchElementException();
16.02/5.22 }
16.02/5.22
16.02/5.22 public int nextIndex() {
16.02/5.22 return i.nextIndex() - offset;
16.02/5.22 }
16.02/5.22
16.02/5.22 public int previousIndex() {
16.02/5.22 return i.previousIndex() - offset;
16.02/5.22 }
16.02/5.22
16.02/5.22 public void remove() {
16.02/5.22 i.remove();
16.02/5.22 SubList.this.modCount = l.modCount;
16.02/5.22 size--;
16.02/5.22 }
16.02/5.22
16.02/5.22 public void set(E e) {
16.02/5.22 i.set(e);
16.02/5.22 }
16.02/5.22
16.02/5.22 public void add(E e) {
16.02/5.22 i.add(e);
16.02/5.22 SubList.this.modCount = l.modCount;
16.02/5.22 size++;
16.02/5.22 }
16.02/5.22 };
16.02/5.22 }
16.02/5.22
16.02/5.22 public List subList(int fromIndex, int toIndex) {
16.02/5.22 return new SubList(this, fromIndex, toIndex);
16.02/5.22 }
16.02/5.22
16.02/5.22 private void rangeCheck(int index) {
16.02/5.22 if (index < 0 || index >= size)
16.02/5.22 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
16.02/5.22 }
16.02/5.22
16.02/5.22 private void rangeCheckForAdd(int index) {
16.02/5.22 if (index < 0 || index > size)
16.02/5.22 throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
16.02/5.22 }
16.02/5.22
16.02/5.22 private String outOfBoundsMsg(int index) {
16.02/5.22 return "";
16.02/5.22 }
16.02/5.22
16.02/5.22 private void checkForComodification() {
16.02/5.22 if (this.modCount != l.modCount)
16.02/5.22 throw new ConcurrentModificationException();
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22 class RandomAccessSubList extends SubList implements RandomAccess {
16.02/5.22 RandomAccessSubList(AbstractList list, int fromIndex, int toIndex) {
16.02/5.22 super(list, fromIndex, toIndex);
16.02/5.22 }
16.02/5.22
16.02/5.22 public List subList(int fromIndex, int toIndex) {
16.02/5.22 return new RandomAccessSubList(this, fromIndex, toIndex);
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 /*
16.02/5.22 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.22 *
16.02/5.22 * This code is free software; you can redistribute it and/or modify it
16.02/5.22 * under the terms of the GNU General Public License version 2 only, as
16.02/5.22 * published by the Free Software Foundation. Sun designates this
16.02/5.22 * particular file as subject to the "Classpath" exception as provided
16.02/5.22 * by Sun in the LICENSE file that accompanied this code.
16.02/5.22 *
16.02/5.22 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.22 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.22 * accompanied this code).
16.02/5.22 *
16.02/5.22 * You should have received a copy of the GNU General Public License version
16.02/5.22 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.22 *
16.02/5.22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.22 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.22 * have any questions.
16.02/5.22 */
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 /**
16.02/5.22 * This class provides a skeletal implementation of the List
16.02/5.22 * interface to minimize the effort required to implement this interface
16.02/5.22 * backed by a "sequential access" data store (such as a linked list). For
16.02/5.22 * random access data (such as an array), AbstractList should be used
16.02/5.22 * in preference to this class.
16.02/5.22 *
16.02/5.22 * This class is the opposite of the AbstractList class in the sense
16.02/5.22 * that it implements the "random access" methods (get(int index),
16.02/5.22 * set(int index, E element), add(int index, E element) and
16.02/5.22 * remove(int index)) on top of the list's list iterator, instead of
16.02/5.22 * the other way around.
16.02/5.22 *
16.02/5.22 * To implement a list the programmer needs only to extend this class and
16.02/5.22 * provide implementations for the listIterator and size
16.02/5.22 * methods. For an unmodifiable list, the programmer need only implement the
16.02/5.22 * list iterator's hasNext, next, hasPrevious,
16.02/5.22 * previous and index methods.
16.02/5.22 *
16.02/5.22 * For a modifiable list the programmer should additionally implement the list
16.02/5.22 * iterator's set method. For a variable-size list the programmer
16.02/5.22 * should additionally implement the list iterator's remove and
16.02/5.22 * add methods.
16.02/5.22 *
16.02/5.22 * The programmer should generally provide a void (no argument) and collection
16.02/5.22 * constructor, as per the recommendation in the Collection interface
16.02/5.22 * specification.
16.02/5.22 *
16.02/5.22 * This class is a member of the
16.02/5.22 *
16.02/5.22 * Java Collections Framework.
16.02/5.22 *
16.02/5.22 * @author Josh Bloch
16.02/5.22 * @author Neal Gafter
16.02/5.22 * @see Collection
16.02/5.22 * @see List
16.02/5.22 * @see AbstractList
16.02/5.22 * @see AbstractCollection
16.02/5.22 * @since 1.2
16.02/5.22 */
16.02/5.22
16.02/5.22 public abstract class AbstractSequentialList extends AbstractList {
16.02/5.22 /**
16.02/5.22 * Sole constructor. (For invocation by subclass constructors, typically
16.02/5.22 * implicit.)
16.02/5.22 */
16.02/5.22 protected AbstractSequentialList() {
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns the element at the specified position in this list.
16.02/5.22 *
16.02/5.22 * This implementation first gets a list iterator pointing to the
16.02/5.22 * indexed element (with listIterator(index)). Then, it gets
16.02/5.22 * the element using ListIterator.next and returns it.
16.02/5.22 *
16.02/5.22 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.22 */
16.02/5.22 public E get(int index) {
16.02/5.22 try {
16.02/5.22 return listIterator(index).next();
16.02/5.22 } catch (NoSuchElementException exc) {
16.02/5.22 throw new IndexOutOfBoundsException();
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Replaces the element at the specified position in this list with the
16.02/5.22 * specified element (optional operation).
16.02/5.22 *
16.02/5.22 *
This implementation first gets a list iterator pointing to the
16.02/5.22 * indexed element (with listIterator(index)). Then, it gets
16.02/5.22 * the current element using ListIterator.next and replaces it
16.02/5.22 * with ListIterator.set.
16.02/5.22 *
16.02/5.22 *
Note that this implementation will throw an
16.02/5.22 * UnsupportedOperationException if the list iterator does not
16.02/5.22 * implement the set operation.
16.02/5.22 *
16.02/5.22 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.22 * @throws ClassCastException {@inheritDoc}
16.02/5.22 * @throws NullPointerException {@inheritDoc}
16.02/5.22 * @throws IllegalArgumentException {@inheritDoc}
16.02/5.22 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.22 */
16.02/5.22 public E set(int index, E element) {
16.02/5.22 try {
16.02/5.22 ListIterator e = listIterator(index);
16.02/5.22 E oldVal = e.next();
16.02/5.22 e.set(element);
16.02/5.22 return oldVal;
16.02/5.22 } catch (NoSuchElementException exc) {
16.02/5.22 throw new IndexOutOfBoundsException();
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Inserts the specified element at the specified position in this list
16.02/5.22 * (optional operation). Shifts the element currently at that position
16.02/5.22 * (if any) and any subsequent elements to the right (adds one to their
16.02/5.22 * indices).
16.02/5.22 *
16.02/5.22 * This implementation first gets a list iterator pointing to the
16.02/5.22 * indexed element (with listIterator(index)). Then, it
16.02/5.22 * inserts the specified element with ListIterator.add.
16.02/5.22 *
16.02/5.22 *
Note that this implementation will throw an
16.02/5.22 * UnsupportedOperationException if the list iterator does not
16.02/5.22 * implement the add operation.
16.02/5.22 *
16.02/5.22 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.22 * @throws ClassCastException {@inheritDoc}
16.02/5.22 * @throws NullPointerException {@inheritDoc}
16.02/5.22 * @throws IllegalArgumentException {@inheritDoc}
16.02/5.22 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.22 */
16.02/5.22 public void add(int index, E element) {
16.02/5.22 try {
16.02/5.22 listIterator(index).add(element);
16.02/5.22 } catch (NoSuchElementException exc) {
16.02/5.22 throw new IndexOutOfBoundsException();
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes the element at the specified position in this list (optional
16.02/5.22 * operation). Shifts any subsequent elements to the left (subtracts one
16.02/5.22 * from their indices). Returns the element that was removed from the
16.02/5.22 * list.
16.02/5.22 *
16.02/5.22 *
This implementation first gets a list iterator pointing to the
16.02/5.22 * indexed element (with listIterator(index)). Then, it removes
16.02/5.22 * the element with ListIterator.remove.
16.02/5.22 *
16.02/5.22 *
Note that this implementation will throw an
16.02/5.22 * UnsupportedOperationException if the list iterator does not
16.02/5.22 * implement the remove operation.
16.02/5.22 *
16.02/5.22 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.22 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.22 */
16.02/5.22 public E remove(int index) {
16.02/5.22 try {
16.02/5.22 ListIterator e = listIterator(index);
16.02/5.22 E outCast = e.next();
16.02/5.22 e.remove();
16.02/5.22 return outCast;
16.02/5.22 } catch (NoSuchElementException exc) {
16.02/5.22 throw new IndexOutOfBoundsException();
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 // Bulk Operations
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Inserts all of the elements in the specified collection into this
16.02/5.22 * list at the specified position (optional operation). Shifts the
16.02/5.22 * element currently at that position (if any) and any subsequent
16.02/5.22 * elements to the right (increases their indices). The new elements
16.02/5.22 * will appear in this list in the order that they are returned by the
16.02/5.22 * specified collection's iterator. The behavior of this operation is
16.02/5.22 * undefined if the specified collection is modified while the
16.02/5.22 * operation is in progress. (Note that this will occur if the specified
16.02/5.22 * collection is this list, and it's nonempty.)
16.02/5.22 *
16.02/5.22 * This implementation gets an iterator over the specified collection and
16.02/5.22 * a list iterator over this list pointing to the indexed element (with
16.02/5.22 * listIterator(index)). Then, it iterates over the specified
16.02/5.22 * collection, inserting the elements obtained from the iterator into this
16.02/5.22 * list, one at a time, using ListIterator.add followed by
16.02/5.22 * ListIterator.next (to skip over the added element).
16.02/5.22 *
16.02/5.22 *
Note that this implementation will throw an
16.02/5.22 * UnsupportedOperationException if the list iterator returned by
16.02/5.22 * the listIterator method does not implement the add
16.02/5.22 * operation.
16.02/5.22 *
16.02/5.22 * @throws UnsupportedOperationException {@inheritDoc}
16.02/5.22 * @throws ClassCastException {@inheritDoc}
16.02/5.22 * @throws NullPointerException {@inheritDoc}
16.02/5.22 * @throws IllegalArgumentException {@inheritDoc}
16.02/5.22 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.22 */
16.02/5.22 public boolean addAll(int index, Collection extends E> c) {
16.02/5.22 try {
16.02/5.22 boolean modified = false;
16.02/5.22 ListIterator e1 = listIterator(index);
16.02/5.22 Iterator extends E> e2 = c.iterator();
16.02/5.22 while (e2.hasNext()) {
16.02/5.22 e1.add(e2.next());
16.02/5.22 modified = true;
16.02/5.22 }
16.02/5.22 return modified;
16.02/5.22 } catch (NoSuchElementException exc) {
16.02/5.22 throw new IndexOutOfBoundsException();
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 // Iterators
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns an iterator over the elements in this list (in proper
16.02/5.22 * sequence).
16.02/5.22 *
16.02/5.22 * This implementation merely returns a list iterator over the list.
16.02/5.22 *
16.02/5.22 * @return an iterator over the elements in this list (in proper sequence)
16.02/5.22 */
16.02/5.22 public Iterator iterator() {
16.02/5.22 return listIterator();
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns a list iterator over the elements in this list (in proper
16.02/5.22 * sequence).
16.02/5.22 *
16.02/5.22 * @param index index of first element to be returned from the list
16.02/5.22 * iterator (by a call to the next
method)
16.02/5.22 * @return a list iterator over the elements in this list (in proper
16.02/5.22 * sequence)
16.02/5.22 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.22 */
16.02/5.22 public abstract ListIterator listIterator(int index);
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 /*
16.02/5.22 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.22 *
16.02/5.22 * This code is free software; you can redistribute it and/or modify it
16.02/5.22 * under the terms of the GNU General Public License version 2 only, as
16.02/5.22 * published by the Free Software Foundation. Sun designates this
16.02/5.22 * particular file as subject to the "Classpath" exception as provided
16.02/5.22 * by Sun in the LICENSE file that accompanied this code.
16.02/5.22 *
16.02/5.22 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.22 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.22 * accompanied this code).
16.02/5.22 *
16.02/5.22 * You should have received a copy of the GNU General Public License version
16.02/5.22 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.22 *
16.02/5.22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.22 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.22 * have any questions.
16.02/5.22 */
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 /**
16.02/5.22 * The root interface in the collection hierarchy. A collection
16.02/5.22 * represents a group of objects, known as its elements. Some
16.02/5.22 * collections allow duplicate elements and others do not. Some are ordered
16.02/5.22 * and others unordered. The JDK does not provide any direct
16.02/5.22 * implementations of this interface: it provides implementations of more
16.02/5.22 * specific subinterfaces like Set and List. This interface
16.02/5.22 * is typically used to pass collections around and manipulate them where
16.02/5.22 * maximum generality is desired.
16.02/5.22 *
16.02/5.22 * Bags or multisets (unordered collections that may contain
16.02/5.22 * duplicate elements) should implement this interface directly.
16.02/5.22 *
16.02/5.22 *
All general-purpose Collection implementation classes (which
16.02/5.22 * typically implement Collection indirectly through one of its
16.02/5.22 * subinterfaces) should provide two "standard" constructors: a void (no
16.02/5.22 * arguments) constructor, which creates an empty collection, and a
16.02/5.22 * constructor with a single argument of type Collection, which
16.02/5.22 * creates a new collection with the same elements as its argument. In
16.02/5.22 * effect, the latter constructor allows the user to copy any collection,
16.02/5.22 * producing an equivalent collection of the desired implementation type.
16.02/5.22 * There is no way to enforce this convention (as interfaces cannot contain
16.02/5.22 * constructors) but all of the general-purpose Collection
16.02/5.22 * implementations in the Java platform libraries comply.
16.02/5.22 *
16.02/5.22 *
The "destructive" methods contained in this interface, that is, the
16.02/5.22 * methods that modify the collection on which they operate, are specified to
16.02/5.22 * throw UnsupportedOperationException if this collection does not
16.02/5.22 * support the operation. If this is the case, these methods may, but are not
16.02/5.22 * required to, throw an UnsupportedOperationException if the
16.02/5.22 * invocation would have no effect on the collection. For example, invoking
16.02/5.22 * the {@link #addAll(Collection)} method on an unmodifiable collection may,
16.02/5.22 * but is not required to, throw the exception if the collection to be added
16.02/5.22 * is empty.
16.02/5.22 *
16.02/5.22 *
Some collection implementations have restrictions on the elements that
16.02/5.22 * they may contain. For example, some implementations prohibit null elements,
16.02/5.22 * and some have restrictions on the types of their elements. Attempting to
16.02/5.22 * add an ineligible element throws an unchecked exception, typically
16.02/5.22 * NullPointerException or ClassCastException. Attempting
16.02/5.22 * to query the presence of an ineligible element may throw an exception,
16.02/5.22 * or it may simply return false; some implementations will exhibit the former
16.02/5.22 * behavior and some will exhibit the latter. More generally, attempting an
16.02/5.22 * operation on an ineligible element whose completion would not result in
16.02/5.22 * the insertion of an ineligible element into the collection may throw an
16.02/5.22 * exception or it may succeed, at the option of the implementation.
16.02/5.22 * Such exceptions are marked as "optional" in the specification for this
16.02/5.22 * interface.
16.02/5.22 *
16.02/5.22 *
It is up to each collection to determine its own synchronization
16.02/5.22 * policy. In the absence of a stronger guarantee by the
16.02/5.22 * implementation, undefined behavior may result from the invocation
16.02/5.22 * of any method on a collection that is being mutated by another
16.02/5.22 * thread; this includes direct invocations, passing the collection to
16.02/5.22 * a method that might perform invocations, and using an existing
16.02/5.22 * iterator to examine the collection.
16.02/5.22 *
16.02/5.22 *
Many methods in Collections Framework interfaces are defined in
16.02/5.22 * terms of the {@link Object#equals(Object) equals} method. For example,
16.02/5.22 * the specification for the {@link #contains(Object) contains(Object o)}
16.02/5.22 * method says: "returns true if and only if this collection
16.02/5.22 * contains at least one element e such that
16.02/5.22 * (o==null ? e==null : o.equals(e))." This specification should
16.02/5.22 * not be construed to imply that invoking Collection.contains
16.02/5.22 * with a non-null argument o will cause o.equals(e) to be
16.02/5.22 * invoked for any element e. Implementations are free to implement
16.02/5.22 * optimizations whereby the equals invocation is avoided, for
16.02/5.22 * example, by first comparing the hash codes of the two elements. (The
16.02/5.22 * {@link Object#hashCode()} specification guarantees that two objects with
16.02/5.22 * unequal hash codes cannot be equal.) More generally, implementations of
16.02/5.22 * the various Collections Framework interfaces are free to take advantage of
16.02/5.22 * the specified behavior of underlying {@link Object} methods wherever the
16.02/5.22 * implementor deems it appropriate.
16.02/5.22 *
16.02/5.22 *
This interface is a member of the
16.02/5.22 *
16.02/5.22 * Java Collections Framework.
16.02/5.22 *
16.02/5.22 * @author Josh Bloch
16.02/5.22 * @author Neal Gafter
16.02/5.22 * @see Set
16.02/5.22 * @see List
16.02/5.22 * @see Map
16.02/5.22 * @see SortedSet
16.02/5.22 * @see SortedMap
16.02/5.22 * @see HashSet
16.02/5.22 * @see TreeSet
16.02/5.22 * @see ArrayList
16.02/5.22 * @see LinkedList
16.02/5.22 * @see Vector
16.02/5.22 * @see Collections
16.02/5.22 * @see Arrays
16.02/5.22 * @see AbstractCollection
16.02/5.22 * @since 1.2
16.02/5.22 */
16.02/5.22
16.02/5.22 public interface Collection {
16.02/5.22 // Query Operations
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns the number of elements in this collection. If this collection
16.02/5.22 * contains more than Integer.MAX_VALUE elements, returns
16.02/5.22 * Integer.MAX_VALUE.
16.02/5.22 *
16.02/5.22 * @return the number of elements in this collection
16.02/5.22 */
16.02/5.22 int size();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns true if this collection contains no elements.
16.02/5.22 *
16.02/5.22 * @return true if this collection contains no elements
16.02/5.22 */
16.02/5.22 boolean isEmpty();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns true if this collection contains the specified element.
16.02/5.22 * More formally, returns true if and only if this collection
16.02/5.22 * contains at least one element e such that
16.02/5.22 * (o==null ? e==null : o.equals(e)).
16.02/5.22 *
16.02/5.22 * @param o element whose presence in this collection is to be tested
16.02/5.22 * @return true if this collection contains the specified
16.02/5.22 * element
16.02/5.22 * @throws ClassCastException if the type of the specified element
16.02/5.22 * is incompatible with this collection (optional)
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * collection does not permit null elements (optional)
16.02/5.22 */
16.02/5.22 boolean contains(Object o);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns an iterator over the elements in this collection. There are no
16.02/5.22 * guarantees concerning the order in which the elements are returned
16.02/5.22 * (unless this collection is an instance of some class that provides a
16.02/5.22 * guarantee).
16.02/5.22 *
16.02/5.22 * @return an Iterator over the elements in this collection
16.02/5.22 */
16.02/5.22 Iterator iterator();
16.02/5.22
16.02/5.22 // Modification Operations
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Ensures that this collection contains the specified element (optional
16.02/5.22 * operation). Returns true if this collection changed as a
16.02/5.22 * result of the call. (Returns false if this collection does
16.02/5.22 * not permit duplicates and already contains the specified element.)
16.02/5.22 *
16.02/5.22 * Collections that support this operation may place limitations on what
16.02/5.22 * elements may be added to this collection. In particular, some
16.02/5.22 * collections will refuse to add null elements, and others will
16.02/5.22 * impose restrictions on the type of elements that may be added.
16.02/5.22 * Collection classes should clearly specify in their documentation any
16.02/5.22 * restrictions on what elements may be added.
16.02/5.22 *
16.02/5.22 * If a collection refuses to add a particular element for any reason
16.02/5.22 * other than that it already contains the element, it must throw
16.02/5.22 * an exception (rather than returning false). This preserves
16.02/5.22 * the invariant that a collection always contains the specified element
16.02/5.22 * after this call returns.
16.02/5.22 *
16.02/5.22 * @param e element whose presence in this collection is to be ensured
16.02/5.22 * @return true if this collection changed as a result of the
16.02/5.22 * call
16.02/5.22 * @throws UnsupportedOperationException if the add operation
16.02/5.22 * is not supported by this collection
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * prevents it from being added to this collection
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * collection does not permit null elements
16.02/5.22 * @throws IllegalArgumentException if some property of the element
16.02/5.22 * prevents it from being added to this collection
16.02/5.22 * @throws IllegalStateException if the element cannot be added at this
16.02/5.22 * time due to insertion restrictions
16.02/5.22 */
16.02/5.22 boolean add(E e);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes a single instance of the specified element from this
16.02/5.22 * collection, if it is present (optional operation). More formally,
16.02/5.22 * removes an element e such that
16.02/5.22 * (o==null ? e==null : o.equals(e)), if
16.02/5.22 * this collection contains one or more such elements. Returns
16.02/5.22 * true if this collection contained the specified element (or
16.02/5.22 * equivalently, if this collection changed as a result of the call).
16.02/5.22 *
16.02/5.22 * @param o element to be removed from this collection, if present
16.02/5.22 * @return true if an element was removed as a result of this call
16.02/5.22 * @throws ClassCastException if the type of the specified element
16.02/5.22 * is incompatible with this collection (optional)
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * collection does not permit null elements (optional)
16.02/5.22 * @throws UnsupportedOperationException if the remove operation
16.02/5.22 * is not supported by this collection
16.02/5.22 */
16.02/5.22 boolean remove(Object o);
16.02/5.22
16.02/5.22
16.02/5.22 // Bulk Operations
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns true if this collection contains all of the elements
16.02/5.22 * in the specified collection.
16.02/5.22 *
16.02/5.22 * @param c collection to be checked for containment in this collection
16.02/5.22 * @return true if this collection contains all of the elements
16.02/5.22 * in the specified collection
16.02/5.22 * @throws ClassCastException if the types of one or more elements
16.02/5.22 * in the specified collection are incompatible with this
16.02/5.22 * collection (optional)
16.02/5.22 * @throws NullPointerException if the specified collection contains one
16.02/5.22 * or more null elements and this collection does not permit null
16.02/5.22 * elements (optional), or if the specified collection is null
16.02/5.22 * @see #contains(Object)
16.02/5.22 */
16.02/5.22 boolean containsAll(Collection> c);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Adds all of the elements in the specified collection to this collection
16.02/5.22 * (optional operation). The behavior of this operation is undefined if
16.02/5.22 * the specified collection is modified while the operation is in progress.
16.02/5.22 * (This implies that the behavior of this call is undefined if the
16.02/5.22 * specified collection is this collection, and this collection is
16.02/5.22 * nonempty.)
16.02/5.22 *
16.02/5.22 * @param c collection containing elements to be added to this collection
16.02/5.22 * @return true if this collection changed as a result of the call
16.02/5.22 * @throws UnsupportedOperationException if the addAll operation
16.02/5.22 * is not supported by this collection
16.02/5.22 * @throws ClassCastException if the class of an element of the specified
16.02/5.22 * collection prevents it from being added to this collection
16.02/5.22 * @throws NullPointerException if the specified collection contains a
16.02/5.22 * null element and this collection does not permit null elements,
16.02/5.22 * or if the specified collection is null
16.02/5.22 * @throws IllegalArgumentException if some property of an element of the
16.02/5.22 * specified collection prevents it from being added to this
16.02/5.22 * collection
16.02/5.22 * @throws IllegalStateException if not all the elements can be added at
16.02/5.22 * this time due to insertion restrictions
16.02/5.22 * @see #add(Object)
16.02/5.22 */
16.02/5.22 boolean addAll(Collection extends E> c);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes all of this collection's elements that are also contained in the
16.02/5.22 * specified collection (optional operation). After this call returns,
16.02/5.22 * this collection will contain no elements in common with the specified
16.02/5.22 * collection.
16.02/5.22 *
16.02/5.22 * @param c collection containing elements to be removed from this collection
16.02/5.22 * @return true if this collection changed as a result of the
16.02/5.22 * call
16.02/5.22 * @throws UnsupportedOperationException if the removeAll method
16.02/5.22 * is not supported by this collection
16.02/5.22 * @throws ClassCastException if the types of one or more elements
16.02/5.22 * in this collection are incompatible with the specified
16.02/5.22 * collection (optional)
16.02/5.22 * @throws NullPointerException if this collection contains one or more
16.02/5.22 * null elements and the specified collection does not support
16.02/5.22 * null elements (optional), or if the specified collection is null
16.02/5.22 * @see #remove(Object)
16.02/5.22 * @see #contains(Object)
16.02/5.22 */
16.02/5.22 boolean removeAll(Collection> c);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retains only the elements in this collection that are contained in the
16.02/5.22 * specified collection (optional operation). In other words, removes from
16.02/5.22 * this collection all of its elements that are not contained in the
16.02/5.22 * specified collection.
16.02/5.22 *
16.02/5.22 * @param c collection containing elements to be retained in this collection
16.02/5.22 * @return true if this collection changed as a result of the call
16.02/5.22 * @throws UnsupportedOperationException if the retainAll operation
16.02/5.22 * is not supported by this collection
16.02/5.22 * @throws ClassCastException if the types of one or more elements
16.02/5.22 * in this collection are incompatible with the specified
16.02/5.22 * collection (optional)
16.02/5.22 * @throws NullPointerException if this collection contains one or more
16.02/5.22 * null elements and the specified collection does not permit null
16.02/5.22 * elements (optional), or if the specified collection is null
16.02/5.22 * @see #remove(Object)
16.02/5.22 * @see #contains(Object)
16.02/5.22 */
16.02/5.22 boolean retainAll(Collection> c);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes all of the elements from this collection (optional operation).
16.02/5.22 * The collection will be empty after this method returns.
16.02/5.22 *
16.02/5.22 * @throws UnsupportedOperationException if the clear operation
16.02/5.22 * is not supported by this collection
16.02/5.22 */
16.02/5.22 void clear();
16.02/5.22
16.02/5.22
16.02/5.22 // Comparison and hashing
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Compares the specified object with this collection for equality.
16.02/5.22 *
16.02/5.22 * While the Collection interface adds no stipulations to the
16.02/5.22 * general contract for the Object.equals, programmers who
16.02/5.22 * implement the Collection interface "directly" (in other words,
16.02/5.22 * create a class that is a Collection but is not a Set
16.02/5.22 * or a List) must exercise care if they choose to override the
16.02/5.22 * Object.equals. It is not necessary to do so, and the simplest
16.02/5.22 * course of action is to rely on Object's implementation, but
16.02/5.22 * the implementor may wish to implement a "value comparison" in place of
16.02/5.22 * the default "reference comparison." (The List and
16.02/5.22 * Set interfaces mandate such value comparisons.)
16.02/5.22 *
16.02/5.22 * The general contract for the Object.equals method states that
16.02/5.22 * equals must be symmetric (in other words, a.equals(b) if and
16.02/5.22 * only if b.equals(a)). The contracts for List.equals
16.02/5.22 * and Set.equals state that lists are only equal to other lists,
16.02/5.22 * and sets to other sets. Thus, a custom equals method for a
16.02/5.22 * collection class that implements neither the List nor
16.02/5.22 * Set interface must return false when this collection
16.02/5.22 * is compared to any list or set. (By the same logic, it is not possible
16.02/5.22 * to write a class that correctly implements both the Set and
16.02/5.22 * List interfaces.)
16.02/5.22 *
16.02/5.22 * @param o object to be compared for equality with this collection
16.02/5.22 * @return true if the specified object is equal to this
16.02/5.22 * collection
16.02/5.22 *
16.02/5.22 * @see Object#equals(Object)
16.02/5.22 * @see Set#equals(Object)
16.02/5.22 * @see List#equals(Object)
16.02/5.22 */
16.02/5.22 boolean equals(Object o);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns the hash code value for this collection. While the
16.02/5.22 * Collection interface adds no stipulations to the general
16.02/5.22 * contract for the Object.hashCode method, programmers should
16.02/5.22 * take note that any class that overrides the Object.equals
16.02/5.22 * method must also override the Object.hashCode method in order
16.02/5.22 * to satisfy the general contract for the Object.hashCodemethod.
16.02/5.22 * In particular, c1.equals(c2) implies that
16.02/5.22 * c1.hashCode()==c2.hashCode().
16.02/5.22 *
16.02/5.22 * @return the hash code value for this collection
16.02/5.22 *
16.02/5.22 * @see Object#hashCode()
16.02/5.22 * @see Object#equals(Object)
16.02/5.22 */
16.02/5.22 int hashCode();
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 /*
16.02/5.22 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.22 *
16.02/5.22 * This code is free software; you can redistribute it and/or modify it
16.02/5.22 * under the terms of the GNU General Public License version 2 only, as
16.02/5.22 * published by the Free Software Foundation. Sun designates this
16.02/5.22 * particular file as subject to the "Classpath" exception as provided
16.02/5.22 * by Sun in the LICENSE file that accompanied this code.
16.02/5.22 *
16.02/5.22 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.22 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.22 * accompanied this code).
16.02/5.22 *
16.02/5.22 * You should have received a copy of the GNU General Public License version
16.02/5.22 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.22 *
16.02/5.22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.22 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.22 * have any questions.
16.02/5.22 */
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 /**
16.02/5.22 * This exception may be thrown by methods that have detected concurrent
16.02/5.22 * modification of an object when such modification is not permissible.
16.02/5.22 *
16.02/5.22 * For example, it is not generally permissible for one thread to modify a Collection
16.02/5.22 * while another thread is iterating over it. In general, the results of the
16.02/5.22 * iteration are undefined under these circumstances. Some Iterator
16.02/5.22 * implementations (including those of all the general purpose collection implementations
16.02/5.22 * provided by the JRE) may choose to throw this exception if this behavior is
16.02/5.22 * detected. Iterators that do this are known as fail-fast iterators,
16.02/5.22 * as they fail quickly and cleanly, rather that risking arbitrary,
16.02/5.22 * non-deterministic behavior at an undetermined time in the future.
16.02/5.22 *
16.02/5.22 * Note that this exception does not always indicate that an object has
16.02/5.22 * been concurrently modified by a different thread. If a single
16.02/5.22 * thread issues a sequence of method invocations that violates the
16.02/5.22 * contract of an object, the object may throw this exception. For
16.02/5.22 * example, if a thread modifies a collection directly while it is
16.02/5.22 * iterating over the collection with a fail-fast iterator, the iterator
16.02/5.22 * will throw this exception.
16.02/5.22 *
16.02/5.22 *
Note that fail-fast behavior cannot be guaranteed as it is, generally
16.02/5.22 * speaking, impossible to make any hard guarantees in the presence of
16.02/5.22 * unsynchronized concurrent modification. Fail-fast operations
16.02/5.22 * throw ConcurrentModificationException on a best-effort basis.
16.02/5.22 * Therefore, it would be wrong to write a program that depended on this
16.02/5.22 * exception for its correctness: ConcurrentModificationException
16.02/5.22 * should be used only to detect bugs.
16.02/5.22 *
16.02/5.22 * @author Josh Bloch
16.02/5.22 * @see Collection
16.02/5.22 * @see Iterator
16.02/5.22 * @see ListIterator
16.02/5.22 * @see Vector
16.02/5.22 * @see LinkedList
16.02/5.22 * @see HashSet
16.02/5.22 * @see Hashtable
16.02/5.22 * @see TreeMap
16.02/5.22 * @see AbstractList
16.02/5.22 * @since 1.2
16.02/5.22 */
16.02/5.22 public class ConcurrentModificationException extends RuntimeException {
16.02/5.22 /**
16.02/5.22 * Constructs a ConcurrentModificationException with no
16.02/5.22 * detail message.
16.02/5.22 */
16.02/5.22 public ConcurrentModificationException() {
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Constructs a ConcurrentModificationException with the
16.02/5.22 * specified detail message.
16.02/5.22 *
16.02/5.22 * @param message the detail message pertaining to this exception.
16.02/5.22 */
16.02/5.22 public ConcurrentModificationException(String message) {
16.02/5.22 super(message);
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 /*
16.02/5.22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.22 *
16.02/5.22 * This code is free software; you can redistribute it and/or modify it
16.02/5.22 * under the terms of the GNU General Public License version 2 only, as
16.02/5.22 * published by the Free Software Foundation. Sun designates this
16.02/5.22 * particular file as subject to the "Classpath" exception as provided
16.02/5.22 * by Sun in the LICENSE file that accompanied this code.
16.02/5.22 *
16.02/5.22 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.22 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.22 * accompanied this code).
16.02/5.22 *
16.02/5.22 * You should have received a copy of the GNU General Public License version
16.02/5.22 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.22 *
16.02/5.22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.22 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.22 * have any questions.
16.02/5.22 */
16.02/5.22
16.02/5.22 /*
16.02/5.22 * This file is available under and governed by the GNU General Public
16.02/5.22 * License version 2 only, as published by the Free Software Foundation.
16.02/5.22 * However, the following notice accompanied the original version of this
16.02/5.22 * file:
16.02/5.22 *
16.02/5.22 * Written by Doug Lea and Josh Bloch with assistance from members of
16.02/5.22 * JCP JSR-166 Expert Group and released to the public domain, as explained
16.02/5.22 * at http://creativecommons.org/licenses/publicdomain
16.02/5.22 */
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 /**
16.02/5.22 * A linear collection that supports element insertion and removal at
16.02/5.22 * both ends. The name deque is short for "double ended queue"
16.02/5.22 * and is usually pronounced "deck". Most Deque
16.02/5.22 * implementations place no fixed limits on the number of elements
16.02/5.22 * they may contain, but this interface supports capacity-restricted
16.02/5.22 * deques as well as those with no fixed size limit.
16.02/5.22 *
16.02/5.22 *
This interface defines methods to access the elements at both
16.02/5.22 * ends of the deque. Methods are provided to insert, remove, and
16.02/5.22 * examine the element. Each of these methods exists in two forms:
16.02/5.22 * one throws an exception if the operation fails, the other returns a
16.02/5.22 * special value (either null or false, depending on
16.02/5.22 * the operation). The latter form of the insert operation is
16.02/5.22 * designed specifically for use with capacity-restricted
16.02/5.22 * Deque implementations; in most implementations, insert
16.02/5.22 * operations cannot fail.
16.02/5.22 *
16.02/5.22 *
The twelve methods described above are summarized in the
16.02/5.22 * following table:
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * |
16.02/5.22 * First Element (Head) |
16.02/5.22 * Last Element (Tail) |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * |
16.02/5.22 * Throws exception |
16.02/5.22 * Special value |
16.02/5.22 * Throws exception |
16.02/5.22 * Special value |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * Insert |
16.02/5.22 * {@link #addFirst addFirst(e)} |
16.02/5.22 * {@link #offerFirst offerFirst(e)} |
16.02/5.22 * {@link #addLast addLast(e)} |
16.02/5.22 * {@link #offerLast offerLast(e)} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * Remove |
16.02/5.22 * {@link #removeFirst removeFirst()} |
16.02/5.22 * {@link #pollFirst pollFirst()} |
16.02/5.22 * {@link #removeLast removeLast()} |
16.02/5.22 * {@link #pollLast pollLast()} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * Examine |
16.02/5.22 * {@link #getFirst getFirst()} |
16.02/5.22 * {@link #peekFirst peekFirst()} |
16.02/5.22 * {@link #getLast getLast()} |
16.02/5.22 * {@link #peekLast peekLast()} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * This interface extends the {@link Queue} interface. When a deque is
16.02/5.22 * used as a queue, FIFO (First-In-First-Out) behavior results. Elements are
16.02/5.22 * added at the end of the deque and removed from the beginning. The methods
16.02/5.22 * inherited from the Queue interface are precisely equivalent to
16.02/5.22 * Deque methods as indicated in the following table:
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * Queue Method |
16.02/5.22 * Equivalent Deque Method |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link java.util.Queue#add add(e)} |
16.02/5.22 * {@link #addLast addLast(e)} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link java.util.Queue#offer offer(e)} |
16.02/5.22 * {@link #offerLast offerLast(e)} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link java.util.Queue#remove remove()} |
16.02/5.22 * {@link #removeFirst removeFirst()} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link java.util.Queue#poll poll()} |
16.02/5.22 * {@link #pollFirst pollFirst()} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link java.util.Queue#element element()} |
16.02/5.22 * {@link #getFirst getFirst()} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link java.util.Queue#peek peek()} |
16.02/5.22 * {@link #peek peekFirst()} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * Deques can also be used as LIFO (Last-In-First-Out) stacks. This
16.02/5.22 * interface should be used in preference to the legacy {@link Stack} class.
16.02/5.22 * When a deque is used as a stack, elements are pushed and popped from the
16.02/5.22 * beginning of the deque. Stack methods are precisely equivalent to
16.02/5.22 * Deque methods as indicated in the table below:
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * Stack Method |
16.02/5.22 * Equivalent Deque Method |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link #push push(e)} |
16.02/5.22 * {@link #addFirst addFirst(e)} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link #pop pop()} |
16.02/5.22 * {@link #removeFirst removeFirst()} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * {@link #peek peek()} |
16.02/5.22 * {@link #peekFirst peekFirst()} |
16.02/5.22 *
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * Note that the {@link #peek peek} method works equally well when
16.02/5.22 * a deque is used as a queue or a stack; in either case, elements are
16.02/5.22 * drawn from the beginning of the deque.
16.02/5.22 *
16.02/5.22 *
This interface provides two methods to remove interior
16.02/5.22 * elements, {@link #removeFirstOccurrence removeFirstOccurrence} and
16.02/5.22 * {@link #removeLastOccurrence removeLastOccurrence}.
16.02/5.22 *
16.02/5.22 *
Unlike the {@link List} interface, this interface does not
16.02/5.22 * provide support for indexed access to elements.
16.02/5.22 *
16.02/5.22 *
While Deque implementations are not strictly required
16.02/5.22 * to prohibit the insertion of null elements, they are strongly
16.02/5.22 * encouraged to do so. Users of any Deque implementations
16.02/5.22 * that do allow null elements are strongly encouraged not to
16.02/5.22 * take advantage of the ability to insert nulls. This is so because
16.02/5.22 * null is used as a special return value by various methods
16.02/5.22 * to indicated that the deque is empty.
16.02/5.22 *
16.02/5.22 *
Deque implementations generally do not define
16.02/5.22 * element-based versions of the equals and hashCode
16.02/5.22 * methods, but instead inherit the identity-based versions from class
16.02/5.22 * Object.
16.02/5.22 *
16.02/5.22 *
This interface is a member of the Java Collections
16.02/5.22 * Framework.
16.02/5.22 *
16.02/5.22 * @author Doug Lea
16.02/5.22 * @author Josh Bloch
16.02/5.22 * @since 1.6
16.02/5.22 * @param the type of elements held in this collection
16.02/5.22 */
16.02/5.22
16.02/5.22 public interface Deque extends Queue {
16.02/5.22 /**
16.02/5.22 * Inserts the specified element at the front of this deque if it is
16.02/5.22 * possible to do so immediately without violating capacity restrictions.
16.02/5.22 * When using a capacity-restricted deque, it is generally preferable to
16.02/5.22 * use method {@link #offerFirst}.
16.02/5.22 *
16.02/5.22 * @param e the element to add
16.02/5.22 * @throws IllegalStateException if the element cannot be added at this
16.02/5.22 * time due to capacity restrictions
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * prevents it from being added to this deque
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements
16.02/5.22 * @throws IllegalArgumentException if some property of the specified
16.02/5.22 * element prevents it from being added to this deque
16.02/5.22 */
16.02/5.22 void addFirst(E e);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Inserts the specified element at the end of this deque if it is
16.02/5.22 * possible to do so immediately without violating capacity restrictions.
16.02/5.22 * When using a capacity-restricted deque, it is generally preferable to
16.02/5.22 * use method {@link #offerLast}.
16.02/5.22 *
16.02/5.22 * This method is equivalent to {@link #add}.
16.02/5.22 *
16.02/5.22 * @param e the element to add
16.02/5.22 * @throws IllegalStateException if the element cannot be added at this
16.02/5.22 * time due to capacity restrictions
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * prevents it from being added to this deque
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements
16.02/5.22 * @throws IllegalArgumentException if some property of the specified
16.02/5.22 * element prevents it from being added to this deque
16.02/5.22 */
16.02/5.22 void addLast(E e);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Inserts the specified element at the front of this deque unless it would
16.02/5.22 * violate capacity restrictions. When using a capacity-restricted deque,
16.02/5.22 * this method is generally preferable to the {@link #addFirst} method,
16.02/5.22 * which can fail to insert an element only by throwing an exception.
16.02/5.22 *
16.02/5.22 * @param e the element to add
16.02/5.22 * @return true if the element was added to this deque, else
16.02/5.22 * false
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * prevents it from being added to this deque
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements
16.02/5.22 * @throws IllegalArgumentException if some property of the specified
16.02/5.22 * element prevents it from being added to this deque
16.02/5.22 */
16.02/5.22 boolean offerFirst(E e);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Inserts the specified element at the end of this deque unless it would
16.02/5.22 * violate capacity restrictions. When using a capacity-restricted deque,
16.02/5.22 * this method is generally preferable to the {@link #addLast} method,
16.02/5.22 * which can fail to insert an element only by throwing an exception.
16.02/5.22 *
16.02/5.22 * @param e the element to add
16.02/5.22 * @return true if the element was added to this deque, else
16.02/5.22 * false
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * prevents it from being added to this deque
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements
16.02/5.22 * @throws IllegalArgumentException if some property of the specified
16.02/5.22 * element prevents it from being added to this deque
16.02/5.22 */
16.02/5.22 boolean offerLast(E e);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves and removes the first element of this deque. This method
16.02/5.22 * differs from {@link #pollFirst pollFirst} only in that it throws an
16.02/5.22 * exception if this deque is empty.
16.02/5.22 *
16.02/5.22 * @return the head of this deque
16.02/5.22 * @throws NoSuchElementException if this deque is empty
16.02/5.22 */
16.02/5.22 E removeFirst();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves and removes the last element of this deque. This method
16.02/5.22 * differs from {@link #pollLast pollLast} only in that it throws an
16.02/5.22 * exception if this deque is empty.
16.02/5.22 *
16.02/5.22 * @return the tail of this deque
16.02/5.22 * @throws NoSuchElementException if this deque is empty
16.02/5.22 */
16.02/5.22 E removeLast();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves and removes the first element of this deque,
16.02/5.22 * or returns null if this deque is empty.
16.02/5.22 *
16.02/5.22 * @return the head of this deque, or null if this deque is empty
16.02/5.22 */
16.02/5.22 E pollFirst();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves and removes the last element of this deque,
16.02/5.22 * or returns null if this deque is empty.
16.02/5.22 *
16.02/5.22 * @return the tail of this deque, or null if this deque is empty
16.02/5.22 */
16.02/5.22 E pollLast();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves, but does not remove, the first element of this deque.
16.02/5.22 *
16.02/5.22 * This method differs from {@link #peekFirst peekFirst} only in that it
16.02/5.22 * throws an exception if this deque is empty.
16.02/5.22 *
16.02/5.22 * @return the head of this deque
16.02/5.22 * @throws NoSuchElementException if this deque is empty
16.02/5.22 */
16.02/5.22 E getFirst();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves, but does not remove, the last element of this deque.
16.02/5.22 * This method differs from {@link #peekLast peekLast} only in that it
16.02/5.22 * throws an exception if this deque is empty.
16.02/5.22 *
16.02/5.22 * @return the tail of this deque
16.02/5.22 * @throws NoSuchElementException if this deque is empty
16.02/5.22 */
16.02/5.22 E getLast();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves, but does not remove, the first element of this deque,
16.02/5.22 * or returns null if this deque is empty.
16.02/5.22 *
16.02/5.22 * @return the head of this deque, or null if this deque is empty
16.02/5.22 */
16.02/5.22 E peekFirst();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves, but does not remove, the last element of this deque,
16.02/5.22 * or returns null if this deque is empty.
16.02/5.22 *
16.02/5.22 * @return the tail of this deque, or null if this deque is empty
16.02/5.22 */
16.02/5.22 E peekLast();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes the first occurrence of the specified element from this deque.
16.02/5.22 * If the deque does not contain the element, it is unchanged.
16.02/5.22 * More formally, removes the first element e such that
16.02/5.22 * (o==null ? e==null : o.equals(e))
16.02/5.22 * (if such an element exists).
16.02/5.22 * Returns true if this deque contained the specified element
16.02/5.22 * (or equivalently, if this deque changed as a result of the call).
16.02/5.22 *
16.02/5.22 * @param o element to be removed from this deque, if present
16.02/5.22 * @return true if an element was removed as a result of this call
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * is incompatible with this deque (optional)
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements (optional)
16.02/5.22 */
16.02/5.22 boolean removeFirstOccurrence(Object o);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes the last occurrence of the specified element from this deque.
16.02/5.22 * If the deque does not contain the element, it is unchanged.
16.02/5.22 * More formally, removes the last element e such that
16.02/5.22 * (o==null ? e==null : o.equals(e))
16.02/5.22 * (if such an element exists).
16.02/5.22 * Returns true if this deque contained the specified element
16.02/5.22 * (or equivalently, if this deque changed as a result of the call).
16.02/5.22 *
16.02/5.22 * @param o element to be removed from this deque, if present
16.02/5.22 * @return true if an element was removed as a result of this call
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * is incompatible with this deque (optional)
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements (optional)
16.02/5.22 */
16.02/5.22 boolean removeLastOccurrence(Object o);
16.02/5.22
16.02/5.22 // *** Queue methods ***
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Inserts the specified element into the queue represented by this deque
16.02/5.22 * (in other words, at the tail of this deque) if it is possible to do so
16.02/5.22 * immediately without violating capacity restrictions, returning
16.02/5.22 * true upon success and throwing an
16.02/5.22 * IllegalStateException if no space is currently available.
16.02/5.22 * When using a capacity-restricted deque, it is generally preferable to
16.02/5.22 * use {@link #offer(Object) offer}.
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #addLast}.
16.02/5.22 *
16.02/5.22 * @param e the element to add
16.02/5.22 * @return true (as specified by {@link Collection#add})
16.02/5.22 * @throws IllegalStateException if the element cannot be added at this
16.02/5.22 * time due to capacity restrictions
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * prevents it from being added to this deque
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements
16.02/5.22 * @throws IllegalArgumentException if some property of the specified
16.02/5.22 * element prevents it from being added to this deque
16.02/5.22 */
16.02/5.22 boolean add(E e);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Inserts the specified element into the queue represented by this deque
16.02/5.22 * (in other words, at the tail of this deque) if it is possible to do so
16.02/5.22 * immediately without violating capacity restrictions, returning
16.02/5.22 * true upon success and false if no space is currently
16.02/5.22 * available. When using a capacity-restricted deque, this method is
16.02/5.22 * generally preferable to the {@link #add} method, which can fail to
16.02/5.22 * insert an element only by throwing an exception.
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #offerLast}.
16.02/5.22 *
16.02/5.22 * @param e the element to add
16.02/5.22 * @return true if the element was added to this deque, else
16.02/5.22 * false
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * prevents it from being added to this deque
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements
16.02/5.22 * @throws IllegalArgumentException if some property of the specified
16.02/5.22 * element prevents it from being added to this deque
16.02/5.22 */
16.02/5.22 boolean offer(E e);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves and removes the head of the queue represented by this deque
16.02/5.22 * (in other words, the first element of this deque).
16.02/5.22 * This method differs from {@link #poll poll} only in that it throws an
16.02/5.22 * exception if this deque is empty.
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #removeFirst()}.
16.02/5.22 *
16.02/5.22 * @return the head of the queue represented by this deque
16.02/5.22 * @throws NoSuchElementException if this deque is empty
16.02/5.22 */
16.02/5.22 E remove();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves and removes the head of the queue represented by this deque
16.02/5.22 * (in other words, the first element of this deque), or returns
16.02/5.22 * null if this deque is empty.
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #pollFirst()}.
16.02/5.22 *
16.02/5.22 * @return the first element of this deque, or null if
16.02/5.22 * this deque is empty
16.02/5.22 */
16.02/5.22 E poll();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves, but does not remove, the head of the queue represented by
16.02/5.22 * this deque (in other words, the first element of this deque).
16.02/5.22 * This method differs from {@link #peek peek} only in that it throws an
16.02/5.22 * exception if this deque is empty.
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #getFirst()}.
16.02/5.22 *
16.02/5.22 * @return the head of the queue represented by this deque
16.02/5.22 * @throws NoSuchElementException if this deque is empty
16.02/5.22 */
16.02/5.22 E element();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Retrieves, but does not remove, the head of the queue represented by
16.02/5.22 * this deque (in other words, the first element of this deque), or
16.02/5.22 * returns null if this deque is empty.
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #peekFirst()}.
16.02/5.22 *
16.02/5.22 * @return the head of the queue represented by this deque, or
16.02/5.22 * null if this deque is empty
16.02/5.22 */
16.02/5.22 E peek();
16.02/5.22
16.02/5.22
16.02/5.22 // *** Stack methods ***
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Pushes an element onto the stack represented by this deque (in other
16.02/5.22 * words, at the head of this deque) if it is possible to do so
16.02/5.22 * immediately without violating capacity restrictions, returning
16.02/5.22 * true upon success and throwing an
16.02/5.22 * IllegalStateException if no space is currently available.
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #addFirst}.
16.02/5.22 *
16.02/5.22 * @param e the element to push
16.02/5.22 * @throws IllegalStateException if the element cannot be added at this
16.02/5.22 * time due to capacity restrictions
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * prevents it from being added to this deque
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements
16.02/5.22 * @throws IllegalArgumentException if some property of the specified
16.02/5.22 * element prevents it from being added to this deque
16.02/5.22 */
16.02/5.22 void push(E e);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Pops an element from the stack represented by this deque. In other
16.02/5.22 * words, removes and returns the first element of this deque.
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #removeFirst()}.
16.02/5.22 *
16.02/5.22 * @return the element at the front of this deque (which is the top
16.02/5.22 * of the stack represented by this deque)
16.02/5.22 * @throws NoSuchElementException if this deque is empty
16.02/5.22 */
16.02/5.22 E pop();
16.02/5.22
16.02/5.22
16.02/5.22 // *** Collection methods ***
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes the first occurrence of the specified element from this deque.
16.02/5.22 * If the deque does not contain the element, it is unchanged.
16.02/5.22 * More formally, removes the first element e such that
16.02/5.22 * (o==null ? e==null : o.equals(e))
16.02/5.22 * (if such an element exists).
16.02/5.22 * Returns true if this deque contained the specified element
16.02/5.22 * (or equivalently, if this deque changed as a result of the call).
16.02/5.22 *
16.02/5.22 *
This method is equivalent to {@link #removeFirstOccurrence}.
16.02/5.22 *
16.02/5.22 * @param o element to be removed from this deque, if present
16.02/5.22 * @return true if an element was removed as a result of this call
16.02/5.22 * @throws ClassCastException if the class of the specified element
16.02/5.22 * is incompatible with this deque (optional)
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements (optional)
16.02/5.22 */
16.02/5.22 boolean remove(Object o);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns true if this deque contains the specified element.
16.02/5.22 * More formally, returns true if and only if this deque contains
16.02/5.22 * at least one element e such that
16.02/5.22 * (o==null ? e==null : o.equals(e)).
16.02/5.22 *
16.02/5.22 * @param o element whose presence in this deque is to be tested
16.02/5.22 * @return true if this deque contains the specified element
16.02/5.22 * @throws ClassCastException if the type of the specified element
16.02/5.22 * is incompatible with this deque (optional)
16.02/5.22 * @throws NullPointerException if the specified element is null and this
16.02/5.22 * deque does not permit null elements (optional)
16.02/5.22 */
16.02/5.22 boolean contains(Object o);
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns the number of elements in this deque.
16.02/5.22 *
16.02/5.22 * @return the number of elements in this deque
16.02/5.22 */
16.02/5.22 public int size();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns an iterator over the elements in this deque in proper sequence.
16.02/5.22 * The elements will be returned in order from first (head) to last (tail).
16.02/5.22 *
16.02/5.22 * @return an iterator over the elements in this deque in proper sequence
16.02/5.22 */
16.02/5.22 Iterator iterator();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns an iterator over the elements in this deque in reverse
16.02/5.22 * sequential order. The elements will be returned in order from
16.02/5.22 * last (tail) to first (head).
16.02/5.22 *
16.02/5.22 * @return an iterator over the elements in this deque in reverse
16.02/5.22 * sequence
16.02/5.22 */
16.02/5.22 Iterator descendingIterator();
16.02/5.22
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 /*
16.02/5.22 * Copyright 1994-2003 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.22 *
16.02/5.22 * This code is free software; you can redistribute it and/or modify it
16.02/5.22 * under the terms of the GNU General Public License version 2 only, as
16.02/5.22 * published by the Free Software Foundation. Sun designates this
16.02/5.22 * particular file as subject to the "Classpath" exception as provided
16.02/5.22 * by Sun in the LICENSE file that accompanied this code.
16.02/5.22 *
16.02/5.22 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.22 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.22 * accompanied this code).
16.02/5.22 *
16.02/5.22 * You should have received a copy of the GNU General Public License version
16.02/5.22 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.22 *
16.02/5.22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.22 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.22 * have any questions.
16.02/5.22 */
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Thrown to indicate that a method has been passed an illegal or
16.02/5.22 * inappropriate argument.
16.02/5.22 *
16.02/5.22 * @author unascribed
16.02/5.22 * @see java.lang.Thread#setPriority(int)
16.02/5.22 * @since JDK1.0
16.02/5.22 */
16.02/5.22 public
16.02/5.22 class IllegalArgumentException extends RuntimeException {
16.02/5.22 /**
16.02/5.22 * Constructs an IllegalArgumentException
with no
16.02/5.22 * detail message.
16.02/5.22 */
16.02/5.22 public IllegalArgumentException() {
16.02/5.22 super();
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Constructs an IllegalArgumentException
with the
16.02/5.22 * specified detail message.
16.02/5.22 *
16.02/5.22 * @param s the detail message.
16.02/5.22 */
16.02/5.22 public IllegalArgumentException(String s) {
16.02/5.22 super(s);
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Constructs a new exception with the specified detail message and
16.02/5.22 * cause.
16.02/5.22 *
16.02/5.22 * Note that the detail message associated with cause
is
16.02/5.22 * not automatically incorporated in this exception's detail
16.02/5.22 * message.
16.02/5.22 *
16.02/5.22 * @param message the detail message (which is saved for later retrieval
16.02/5.22 * by the {@link Throwable#getMessage()} method).
16.02/5.22 * @param cause the cause (which is saved for later retrieval by the
16.02/5.22 * {@link Throwable#getCause()} method). (A null value
16.02/5.22 * is permitted, and indicates that the cause is nonexistent or
16.02/5.22 * unknown.)
16.02/5.22 * @since 1.5
16.02/5.22 */
16.02/5.22 public IllegalArgumentException(String message, Throwable cause) {
16.02/5.22 super(message, cause);
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Constructs a new exception with the specified cause and a detail
16.02/5.22 * message of (cause==null ? null : cause.toString()) (which
16.02/5.22 * typically contains the class and detail message of cause).
16.02/5.22 * This constructor is useful for exceptions that are little more than
16.02/5.22 * wrappers for other throwables (for example, {@link
16.02/5.22 * java.security.PrivilegedActionException}).
16.02/5.22 *
16.02/5.22 * @param cause the cause (which is saved for later retrieval by the
16.02/5.22 * {@link Throwable#getCause()} method). (A null value is
16.02/5.22 * permitted, and indicates that the cause is nonexistent or
16.02/5.22 * unknown.)
16.02/5.22 * @since 1.5
16.02/5.22 */
16.02/5.22 public IllegalArgumentException(Throwable cause) {
16.02/5.22 super(cause);
16.02/5.22 }
16.02/5.22
16.02/5.22 private static final long serialVersionUID = -5365630128856068164L;
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 /*
16.02/5.22 * Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.22 *
16.02/5.22 * This code is free software; you can redistribute it and/or modify it
16.02/5.22 * under the terms of the GNU General Public License version 2 only, as
16.02/5.22 * published by the Free Software Foundation. Sun designates this
16.02/5.22 * particular file as subject to the "Classpath" exception as provided
16.02/5.22 * by Sun in the LICENSE file that accompanied this code.
16.02/5.22 *
16.02/5.22 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.22 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.22 * accompanied this code).
16.02/5.22 *
16.02/5.22 * You should have received a copy of the GNU General Public License version
16.02/5.22 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.22 *
16.02/5.22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.22 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.22 * have any questions.
16.02/5.22 */
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Signals that a method has been invoked at an illegal or
16.02/5.22 * inappropriate time. In other words, the Java environment or
16.02/5.22 * Java application is not in an appropriate state for the requested
16.02/5.22 * operation.
16.02/5.22 *
16.02/5.22 * @author Jonni Kanerva
16.02/5.22 * @since JDK1.1
16.02/5.22 */
16.02/5.22 public
16.02/5.22 class IllegalStateException extends RuntimeException {
16.02/5.22 /**
16.02/5.22 * Constructs an IllegalStateException with no detail message.
16.02/5.22 * A detail message is a String that describes this particular exception.
16.02/5.22 */
16.02/5.22 public IllegalStateException() {
16.02/5.22 super();
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Constructs an IllegalStateException with the specified detail
16.02/5.22 * message. A detail message is a String that describes this particular
16.02/5.22 * exception.
16.02/5.22 *
16.02/5.22 * @param s the String that contains a detailed message
16.02/5.22 */
16.02/5.22 public IllegalStateException(String s) {
16.02/5.22 super(s);
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Constructs a new exception with the specified detail message and
16.02/5.22 * cause.
16.02/5.22 *
16.02/5.22 *
Note that the detail message associated with cause
is
16.02/5.22 * not automatically incorporated in this exception's detail
16.02/5.22 * message.
16.02/5.22 *
16.02/5.22 * @param message the detail message (which is saved for later retrieval
16.02/5.22 * by the {@link Throwable#getMessage()} method).
16.02/5.22 * @param cause the cause (which is saved for later retrieval by the
16.02/5.22 * {@link Throwable#getCause()} method). (A null value
16.02/5.22 * is permitted, and indicates that the cause is nonexistent or
16.02/5.22 * unknown.)
16.02/5.22 * @since 1.5
16.02/5.22 */
16.02/5.22 public IllegalStateException(String message, Throwable cause) {
16.02/5.22 super(message, cause);
16.02/5.22 }
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Constructs a new exception with the specified cause and a detail
16.02/5.22 * message of (cause==null ? null : cause.toString()) (which
16.02/5.22 * typically contains the class and detail message of cause).
16.02/5.22 * This constructor is useful for exceptions that are little more than
16.02/5.22 * wrappers for other throwables (for example, {@link
16.02/5.22 * java.security.PrivilegedActionException}).
16.02/5.22 *
16.02/5.22 * @param cause the cause (which is saved for later retrieval by the
16.02/5.22 * {@link Throwable#getCause()} method). (A null value is
16.02/5.22 * permitted, and indicates that the cause is nonexistent or
16.02/5.22 * unknown.)
16.02/5.22 * @since 1.5
16.02/5.22 */
16.02/5.22 public IllegalStateException(Throwable cause) {
16.02/5.22 super(cause);
16.02/5.22 }
16.02/5.22
16.02/5.22 static final long serialVersionUID = -1848914673093119416L;
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 /*
16.02/5.22 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.22 *
16.02/5.22 * This code is free software; you can redistribute it and/or modify it
16.02/5.22 * under the terms of the GNU General Public License version 2 only, as
16.02/5.22 * published by the Free Software Foundation. Sun designates this
16.02/5.22 * particular file as subject to the "Classpath" exception as provided
16.02/5.22 * by Sun in the LICENSE file that accompanied this code.
16.02/5.22 *
16.02/5.22 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.22 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.22 * accompanied this code).
16.02/5.22 *
16.02/5.22 * You should have received a copy of the GNU General Public License version
16.02/5.22 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.22 *
16.02/5.22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.22 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.22 * have any questions.
16.02/5.22 */
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 /**
16.02/5.22 * An iterator over a collection. {@code Iterator} takes the place of
16.02/5.22 * {@link Enumeration} in the Java Collections Framework. Iterators
16.02/5.22 * differ from enumerations in two ways:
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * - Iterators allow the caller to remove elements from the
16.02/5.22 * underlying collection during the iteration with well-defined
16.02/5.22 * semantics.
16.02/5.22 *
- Method names have been improved.
16.02/5.22 *
16.02/5.22 *
16.02/5.22 * This interface is a member of the
16.02/5.22 *
16.02/5.22 * Java Collections Framework.
16.02/5.22 *
16.02/5.22 * @author Josh Bloch
16.02/5.22 * @see Collection
16.02/5.22 * @see ListIterator
16.02/5.22 * @see Iterable
16.02/5.22 * @since 1.2
16.02/5.22 */
16.02/5.22 public interface Iterator {
16.02/5.22 /**
16.02/5.22 * Returns {@code true} if the iteration has more elements.
16.02/5.22 * (In other words, returns {@code true} if {@link #next} would
16.02/5.22 * return an element rather than throwing an exception.)
16.02/5.22 *
16.02/5.22 * @return {@code true} if the iteration has more elements
16.02/5.22 */
16.02/5.22 boolean hasNext();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Returns the next element in the iteration.
16.02/5.22 *
16.02/5.22 * @return the next element in the iteration
16.02/5.22 * @throws NoSuchElementException if the iteration has no more elements
16.02/5.22 */
16.02/5.22 E next();
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Removes from the underlying collection the last element returned
16.02/5.22 * by this iterator (optional operation). This method can be called
16.02/5.22 * only once per call to {@link #next}. The behavior of an iterator
16.02/5.22 * is unspecified if the underlying collection is modified while the
16.02/5.22 * iteration is in progress in any way other than by calling this
16.02/5.22 * method.
16.02/5.22 *
16.02/5.22 * @throws UnsupportedOperationException if the {@code remove}
16.02/5.22 * operation is not supported by this iterator
16.02/5.22 *
16.02/5.22 * @throws IllegalStateException if the {@code next} method has not
16.02/5.22 * yet been called, or the {@code remove} method has already
16.02/5.22 * been called after the last call to the {@code next}
16.02/5.22 * method
16.02/5.22 */
16.02/5.22 void remove();
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 public class juLinkedListCreateAdd {
16.02/5.22 public static void main(String[] args) {
16.02/5.22 Random.args = args;
16.02/5.22
16.02/5.22 LinkedList l = createList(Random.random());
16.02/5.22 l.add(new Content(Random.random()));
16.02/5.22 }
16.02/5.22
16.02/5.22 public static LinkedList createList(int n) {
16.02/5.22 LinkedList l = new LinkedList();
16.02/5.22 while (n > 0) {
16.02/5.22 l.addLast(new Content(Random.random()));
16.02/5.22 n--;
16.02/5.22 }
16.02/5.22 return l;
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22 final class Content {
16.02/5.22 int val;
16.02/5.22
16.02/5.22 public Content(int v) {
16.02/5.22 this.val = v;
16.02/5.22 }
16.02/5.22
16.02/5.22 public int hashCode() {
16.02/5.22 return val^31;
16.02/5.22 }
16.02/5.22
16.02/5.22 public boolean equals(Object o) {
16.02/5.22 if (o instanceof Content) {
16.02/5.22 return this.val == ((Content) o).val;
16.02/5.22 }
16.02/5.22 return false;
16.02/5.22 }
16.02/5.22 }
16.02/5.22
16.02/5.22
16.02/5.22 /*
16.02/5.22 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
16.02/5.22 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
16.02/5.22 *
16.02/5.22 * This code is free software; you can redistribute it and/or modify it
16.02/5.22 * under the terms of the GNU General Public License version 2 only, as
16.02/5.22 * published by the Free Software Foundation. Sun designates this
16.02/5.22 * particular file as subject to the "Classpath" exception as provided
16.02/5.22 * by Sun in the LICENSE file that accompanied this code.
16.02/5.22 *
16.02/5.22 * This code is distributed in the hope that it will be useful, but WITHOUT
16.02/5.22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16.02/5.22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16.02/5.22 * version 2 for more details (a copy is included in the LICENSE file that
16.02/5.22 * accompanied this code).
16.02/5.22 *
16.02/5.22 * You should have received a copy of the GNU General Public License version
16.02/5.22 * 2 along with this work; if not, write to the Free Software Foundation,
16.02/5.22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
16.02/5.22 *
16.02/5.22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
16.02/5.22 * CA 95054 USA or visit www.sun.com if you need additional information or
16.02/5.22 * have any questions.
16.02/5.22 */
16.02/5.22
16.02/5.22 package javaUtilEx;
16.02/5.22
16.02/5.22 /**
16.02/5.22 * Linked list implementation of the List interface. Implements all
16.02/5.22 * optional list operations, and permits all elements (including
16.02/5.22 * null). In addition to implementing the List interface,
16.02/5.22 * the LinkedList class provides uniformly named methods to
16.02/5.22 * get, remove and insert an element at the
16.02/5.22 * beginning and end of the list. These operations allow linked lists to be
16.02/5.22 * used as a stack, {@linkplain Queue queue}, or {@linkplain Deque
16.02/5.22 * double-ended queue}.
16.02/5.22 *
16.02/5.22 * The class implements the Deque interface, providing
16.02/5.22 * first-in-first-out queue operations for add,
16.02/5.22 * poll, along with other stack and deque operations.
16.02/5.22 *
16.02/5.22 * All of the operations perform as could be expected for a doubly-linked
16.02/5.22 * list. Operations that index into the list will traverse the list from
16.02/5.22 * the beginning or the end, whichever is closer to the specified index.
16.02/5.22 *
16.02/5.22 *
Note that this implementation is not synchronized.
16.02/5.22 * If multiple threads access a linked list concurrently, and at least
16.02/5.23 * one of the threads modifies the list structurally, it must be
16.02/5.23 * synchronized externally. (A structural modification is any operation
16.02/5.23 * that adds or deletes one or more elements; merely setting the value of
16.02/5.23 * an element is not a structural modification.) This is typically
16.02/5.23 * accomplished by synchronizing on some object that naturally
16.02/5.23 * encapsulates the list.
16.02/5.23 *
16.02/5.23 * If no such object exists, the list should be "wrapped" using the
16.02/5.23 * {@link Collections#synchronizedList Collections.synchronizedList}
16.02/5.23 * method. This is best done at creation time, to prevent accidental
16.02/5.23 * unsynchronized access to the list:
16.02/5.23 * List list = Collections.synchronizedList(new LinkedList(...));
16.02/5.23 *
16.02/5.23 * The iterators returned by this class's iterator and
16.02/5.23 * listIterator methods are fail-fast: if the list is
16.02/5.23 * structurally modified at any time after the iterator is created, in
16.02/5.23 * any way except through the Iterator's own remove or
16.02/5.23 * add methods, the iterator will throw a {@link
16.02/5.23 * ConcurrentModificationException}. Thus, in the face of concurrent
16.02/5.23 * modification, the iterator fails quickly and cleanly, rather than
16.02/5.23 * risking arbitrary, non-deterministic behavior at an undetermined
16.02/5.23 * time in the future.
16.02/5.23 *
16.02/5.23 *
Note that the fail-fast behavior of an iterator cannot be guaranteed
16.02/5.23 * as it is, generally speaking, impossible to make any hard guarantees in the
16.02/5.23 * presence of unsynchronized concurrent modification. Fail-fast iterators
16.02/5.23 * throw ConcurrentModificationException on a best-effort basis.
16.02/5.23 * Therefore, it would be wrong to write a program that depended on this
16.02/5.23 * exception for its correctness: the fail-fast behavior of iterators
16.02/5.23 * should be used only to detect bugs.
16.02/5.23 *
16.02/5.23 *
This class is a member of the
16.02/5.23 *
16.02/5.23 * Java Collections Framework.
16.02/5.23 *
16.02/5.23 * @author Josh Bloch
16.02/5.23 * @see List
16.02/5.23 * @see ArrayList
16.02/5.23 * @see Vector
16.02/5.23 * @since 1.2
16.02/5.23 * @param the type of elements held in this collection
16.02/5.23 */
16.02/5.23
16.02/5.23 public class LinkedList
16.02/5.23 extends AbstractSequentialList
16.02/5.23 implements List, Deque
16.02/5.23 {
16.02/5.23 private transient Entry header = new Entry(null, null, null);
16.02/5.23 private transient int size = 0;
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Constructs an empty list.
16.02/5.23 */
16.02/5.23 public LinkedList() {
16.02/5.23 header.next = header.previous = header;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Constructs a list containing the elements of the specified
16.02/5.23 * collection, in the order they are returned by the collection's
16.02/5.23 * iterator.
16.02/5.23 *
16.02/5.23 * @param c the collection whose elements are to be placed into this list
16.02/5.23 * @throws NullPointerException if the specified collection is null
16.02/5.23 */
16.02/5.23 public LinkedList(Collection extends E> c) {
16.02/5.23 this();
16.02/5.23 addAll(c);
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns the first element in this list.
16.02/5.23 *
16.02/5.23 * @return the first element in this list
16.02/5.23 * @throws NoSuchElementException if this list is empty
16.02/5.23 */
16.02/5.23 public E getFirst() {
16.02/5.23 if (size==0)
16.02/5.23 throw new NoSuchElementException();
16.02/5.23
16.02/5.23 return header.next.element;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns the last element in this list.
16.02/5.23 *
16.02/5.23 * @return the last element in this list
16.02/5.23 * @throws NoSuchElementException if this list is empty
16.02/5.23 */
16.02/5.23 public E getLast() {
16.02/5.23 if (size==0)
16.02/5.23 throw new NoSuchElementException();
16.02/5.23
16.02/5.23 return header.previous.element;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Removes and returns the first element from this list.
16.02/5.23 *
16.02/5.23 * @return the first element from this list
16.02/5.23 * @throws NoSuchElementException if this list is empty
16.02/5.23 */
16.02/5.23 public E removeFirst() {
16.02/5.23 return remove(header.next);
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Removes and returns the last element from this list.
16.02/5.23 *
16.02/5.23 * @return the last element from this list
16.02/5.23 * @throws NoSuchElementException if this list is empty
16.02/5.23 */
16.02/5.23 public E removeLast() {
16.02/5.23 return remove(header.previous);
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Inserts the specified element at the beginning of this list.
16.02/5.23 *
16.02/5.23 * @param e the element to add
16.02/5.23 */
16.02/5.23 public void addFirst(E e) {
16.02/5.23 addBefore(e, header.next);
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Appends the specified element to the end of this list.
16.02/5.23 *
16.02/5.23 * This method is equivalent to {@link #add}.
16.02/5.23 *
16.02/5.23 * @param e the element to add
16.02/5.23 */
16.02/5.23 public void addLast(E e) {
16.02/5.23 addBefore(e, header);
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns true if this list contains the specified element.
16.02/5.23 * More formally, returns true if and only if this list contains
16.02/5.23 * at least one element e such that
16.02/5.23 * (o==null ? e==null : o.equals(e)).
16.02/5.23 *
16.02/5.23 * @param o element whose presence in this list is to be tested
16.02/5.23 * @return true if this list contains the specified element
16.02/5.23 */
16.02/5.23 public boolean contains(Object o) {
16.02/5.23 return indexOf(o) != -1;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns the number of elements in this list.
16.02/5.23 *
16.02/5.23 * @return the number of elements in this list
16.02/5.23 */
16.02/5.23 public int size() {
16.02/5.23 return size;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Appends the specified element to the end of this list.
16.02/5.23 *
16.02/5.23 *
This method is equivalent to {@link #addLast}.
16.02/5.23 *
16.02/5.23 * @param e element to be appended to this list
16.02/5.23 * @return true (as specified by {@link Collection#add})
16.02/5.23 */
16.02/5.23 public boolean add(E e) {
16.02/5.23 addBefore(e, header);
16.02/5.23 return true;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Removes the first occurrence of the specified element from this list,
16.02/5.23 * if it is present. If this list does not contain the element, it is
16.02/5.23 * unchanged. More formally, removes the element with the lowest index
16.02/5.23 * i such that
16.02/5.23 * (o==null ? get(i)==null : o.equals(get(i)))
16.02/5.23 * (if such an element exists). Returns true if this list
16.02/5.23 * contained the specified element (or equivalently, if this list
16.02/5.23 * changed as a result of the call).
16.02/5.23 *
16.02/5.23 * @param o element to be removed from this list, if present
16.02/5.23 * @return true if this list contained the specified element
16.02/5.23 */
16.02/5.23 public boolean remove(Object o) {
16.02/5.23 if (o==null) {
16.02/5.23 for (Entry e = header.next; e != header; e = e.next) {
16.02/5.23 if (e.element==null) {
16.02/5.23 remove(e);
16.02/5.23 return true;
16.02/5.23 }
16.02/5.23 }
16.02/5.23 } else {
16.02/5.23 for (Entry e = header.next; e != header; e = e.next) {
16.02/5.23 if (o.equals(e.element)) {
16.02/5.23 remove(e);
16.02/5.23 return true;
16.02/5.23 }
16.02/5.23 }
16.02/5.23 }
16.02/5.23 return false;
16.02/5.23 }
16.02/5.23 /**
16.02/5.23 * Removes all of the elements from this list.
16.02/5.23 */
16.02/5.23 public void clear() {
16.02/5.23 Entry e = header.next;
16.02/5.23 while (e != header) {
16.02/5.23 Entry next = e.next;
16.02/5.23 e.next = e.previous = null;
16.02/5.23 e.element = null;
16.02/5.23 e = next;
16.02/5.23 }
16.02/5.23 header.next = header.previous = header;
16.02/5.23 size = 0;
16.02/5.23 modCount++;
16.02/5.23 }
16.02/5.23
16.02/5.23
16.02/5.23 // Positional Access Operations
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns the element at the specified position in this list.
16.02/5.23 *
16.02/5.23 * @param index index of the element to return
16.02/5.23 * @return the element at the specified position in this list
16.02/5.23 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.23 */
16.02/5.23 public E get(int index) {
16.02/5.23 return entry(index).element;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Replaces the element at the specified position in this list with the
16.02/5.23 * specified element.
16.02/5.23 *
16.02/5.23 * @param index index of the element to replace
16.02/5.23 * @param element element to be stored at the specified position
16.02/5.23 * @return the element previously at the specified position
16.02/5.23 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.23 */
16.02/5.23 public E set(int index, E element) {
16.02/5.23 Entry e = entry(index);
16.02/5.23 E oldVal = e.element;
16.02/5.23 e.element = element;
16.02/5.23 return oldVal;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Inserts the specified element at the specified position in this list.
16.02/5.23 * Shifts the element currently at that position (if any) and any
16.02/5.23 * subsequent elements to the right (adds one to their indices).
16.02/5.23 *
16.02/5.23 * @param index index at which the specified element is to be inserted
16.02/5.23 * @param element element to be inserted
16.02/5.23 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.23 */
16.02/5.23 public void add(int index, E element) {
16.02/5.23 addBefore(element, (index==size ? header : entry(index)));
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Removes the element at the specified position in this list. Shifts any
16.02/5.23 * subsequent elements to the left (subtracts one from their indices).
16.02/5.23 * Returns the element that was removed from the list.
16.02/5.23 *
16.02/5.23 * @param index the index of the element to be removed
16.02/5.23 * @return the element previously at the specified position
16.02/5.23 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.23 */
16.02/5.23 public E remove(int index) {
16.02/5.23 return remove(entry(index));
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns the indexed entry.
16.02/5.23 */
16.02/5.23 private Entry entry(int index) {
16.02/5.23 if (index < 0 || index >= size)
16.02/5.23 throw new IndexOutOfBoundsException();
16.02/5.23 Entry e = header;
16.02/5.23 if (index < (size >> 1)) {
16.02/5.23 for (int i = 0; i <= index; i++)
16.02/5.23 e = e.next;
16.02/5.23 } else {
16.02/5.23 for (int i = size; i > index; i--)
16.02/5.23 e = e.previous;
16.02/5.23 }
16.02/5.23 return e;
16.02/5.23 }
16.02/5.23
16.02/5.23
16.02/5.23 // Search Operations
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns the index of the first occurrence of the specified element
16.02/5.23 * in this list, or -1 if this list does not contain the element.
16.02/5.23 * More formally, returns the lowest index i such that
16.02/5.23 * (o==null ? get(i)==null : o.equals(get(i))),
16.02/5.23 * or -1 if there is no such index.
16.02/5.23 *
16.02/5.23 * @param o element to search for
16.02/5.23 * @return the index of the first occurrence of the specified element in
16.02/5.23 * this list, or -1 if this list does not contain the element
16.02/5.23 */
16.02/5.23 public int indexOf(Object o) {
16.02/5.23 int index = 0;
16.02/5.23 if (o==null) {
16.02/5.23 for (Entry e = header.next; e != header; e = e.next) {
16.02/5.23 if (e.element==null)
16.02/5.23 return index;
16.02/5.23 index++;
16.02/5.23 }
16.02/5.23 } else {
16.02/5.23 for (Entry e = header.next; e != header; e = e.next) {
16.02/5.23 if (o.equals(e.element))
16.02/5.23 return index;
16.02/5.23 index++;
16.02/5.23 }
16.02/5.23 }
16.02/5.23 return -1;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns the index of the last occurrence of the specified element
16.02/5.23 * in this list, or -1 if this list does not contain the element.
16.02/5.23 * More formally, returns the highest index i such that
16.02/5.23 * (o==null ? get(i)==null : o.equals(get(i))),
16.02/5.23 * or -1 if there is no such index.
16.02/5.23 *
16.02/5.23 * @param o element to search for
16.02/5.23 * @return the index of the last occurrence of the specified element in
16.02/5.23 * this list, or -1 if this list does not contain the element
16.02/5.23 */
16.02/5.23 public int lastIndexOf(Object o) {
16.02/5.23 int index = size;
16.02/5.23 if (o==null) {
16.02/5.23 for (Entry e = header.previous; e != header; e = e.previous) {
16.02/5.23 index--;
16.02/5.23 if (e.element==null)
16.02/5.23 return index;
16.02/5.23 }
16.02/5.23 } else {
16.02/5.23 for (Entry e = header.previous; e != header; e = e.previous) {
16.02/5.23 index--;
16.02/5.23 if (o.equals(e.element))
16.02/5.23 return index;
16.02/5.23 }
16.02/5.23 }
16.02/5.23 return -1;
16.02/5.23 }
16.02/5.23
16.02/5.23 // Queue operations.
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Retrieves, but does not remove, the head (first element) of this list.
16.02/5.23 * @return the head of this list, or null if this list is empty
16.02/5.23 * @since 1.5
16.02/5.23 */
16.02/5.23 public E peek() {
16.02/5.23 if (size==0)
16.02/5.23 return null;
16.02/5.23 return getFirst();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Retrieves, but does not remove, the head (first element) of this list.
16.02/5.23 * @return the head of this list
16.02/5.23 * @throws NoSuchElementException if this list is empty
16.02/5.23 * @since 1.5
16.02/5.23 */
16.02/5.23 public E element() {
16.02/5.23 return getFirst();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Retrieves and removes the head (first element) of this list
16.02/5.23 * @return the head of this list, or null if this list is empty
16.02/5.23 * @since 1.5
16.02/5.23 */
16.02/5.23 public E poll() {
16.02/5.23 if (size==0)
16.02/5.23 return null;
16.02/5.23 return removeFirst();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Retrieves and removes the head (first element) of this list.
16.02/5.23 *
16.02/5.23 * @return the head of this list
16.02/5.23 * @throws NoSuchElementException if this list is empty
16.02/5.23 * @since 1.5
16.02/5.23 */
16.02/5.23 public E remove() {
16.02/5.23 return removeFirst();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Adds the specified element as the tail (last element) of this list.
16.02/5.23 *
16.02/5.23 * @param e the element to add
16.02/5.23 * @return true (as specified by {@link Queue#offer})
16.02/5.23 * @since 1.5
16.02/5.23 */
16.02/5.23 public boolean offer(E e) {
16.02/5.23 return add(e);
16.02/5.23 }
16.02/5.23
16.02/5.23 // Deque operations
16.02/5.23 /**
16.02/5.23 * Inserts the specified element at the front of this list.
16.02/5.23 *
16.02/5.23 * @param e the element to insert
16.02/5.23 * @return true (as specified by {@link Deque#offerFirst})
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public boolean offerFirst(E e) {
16.02/5.23 addFirst(e);
16.02/5.23 return true;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Inserts the specified element at the end of this list.
16.02/5.23 *
16.02/5.23 * @param e the element to insert
16.02/5.23 * @return true (as specified by {@link Deque#offerLast})
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public boolean offerLast(E e) {
16.02/5.23 addLast(e);
16.02/5.23 return true;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Retrieves, but does not remove, the first element of this list,
16.02/5.23 * or returns null if this list is empty.
16.02/5.23 *
16.02/5.23 * @return the first element of this list, or null
16.02/5.23 * if this list is empty
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public E peekFirst() {
16.02/5.23 if (size==0)
16.02/5.23 return null;
16.02/5.23 return getFirst();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Retrieves, but does not remove, the last element of this list,
16.02/5.23 * or returns null if this list is empty.
16.02/5.23 *
16.02/5.23 * @return the last element of this list, or null
16.02/5.23 * if this list is empty
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public E peekLast() {
16.02/5.23 if (size==0)
16.02/5.23 return null;
16.02/5.23 return getLast();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Retrieves and removes the first element of this list,
16.02/5.23 * or returns null if this list is empty.
16.02/5.23 *
16.02/5.23 * @return the first element of this list, or null if
16.02/5.23 * this list is empty
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public E pollFirst() {
16.02/5.23 if (size==0)
16.02/5.23 return null;
16.02/5.23 return removeFirst();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Retrieves and removes the last element of this list,
16.02/5.23 * or returns null if this list is empty.
16.02/5.23 *
16.02/5.23 * @return the last element of this list, or null if
16.02/5.23 * this list is empty
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public E pollLast() {
16.02/5.23 if (size==0)
16.02/5.23 return null;
16.02/5.23 return removeLast();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Pushes an element onto the stack represented by this list. In other
16.02/5.23 * words, inserts the element at the front of this list.
16.02/5.23 *
16.02/5.23 * This method is equivalent to {@link #addFirst}.
16.02/5.23 *
16.02/5.23 * @param e the element to push
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public void push(E e) {
16.02/5.23 addFirst(e);
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Pops an element from the stack represented by this list. In other
16.02/5.23 * words, removes and returns the first element of this list.
16.02/5.23 *
16.02/5.23 *
This method is equivalent to {@link #removeFirst()}.
16.02/5.23 *
16.02/5.23 * @return the element at the front of this list (which is the top
16.02/5.23 * of the stack represented by this list)
16.02/5.23 * @throws NoSuchElementException if this list is empty
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public E pop() {
16.02/5.23 return removeFirst();
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Removes the first occurrence of the specified element in this
16.02/5.23 * list (when traversing the list from head to tail). If the list
16.02/5.23 * does not contain the element, it is unchanged.
16.02/5.23 *
16.02/5.23 * @param o element to be removed from this list, if present
16.02/5.23 * @return true if the list contained the specified element
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public boolean removeFirstOccurrence(Object o) {
16.02/5.23 return remove(o);
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Removes the last occurrence of the specified element in this
16.02/5.23 * list (when traversing the list from head to tail). If the list
16.02/5.23 * does not contain the element, it is unchanged.
16.02/5.23 *
16.02/5.23 * @param o element to be removed from this list, if present
16.02/5.23 * @return true if the list contained the specified element
16.02/5.23 * @since 1.6
16.02/5.23 */
16.02/5.23 public boolean removeLastOccurrence(Object o) {
16.02/5.23 if (o==null) {
16.02/5.23 for (Entry e = header.previous; e != header; e = e.previous) {
16.02/5.23 if (e.element==null) {
16.02/5.23 remove(e);
16.02/5.23 return true;
16.02/5.23 }
16.02/5.23 }
16.02/5.23 } else {
16.02/5.23 for (Entry e = header.previous; e != header; e = e.previous) {
16.02/5.23 if (o.equals(e.element)) {
16.02/5.23 remove(e);
16.02/5.23 return true;
16.02/5.23 }
16.02/5.23 }
16.02/5.23 }
16.02/5.23 return false;
16.02/5.23 }
16.02/5.23
16.02/5.23 /**
16.02/5.23 * Returns a list-iterator of the elements in this list (in proper
16.02/5.23 * sequence), starting at the specified position in the list.
16.02/5.23 * Obeys the general contract of List.listIterator(int).
16.02/5.23 *
16.02/5.23 * The list-iterator is fail-fast: if the list is structurally
16.02/5.23 * modified at any time after the Iterator is created, in any way except
16.02/5.23 * through the list-iterator's own remove or add
16.02/5.23 * methods, the list-iterator will throw a
16.02/5.23 * ConcurrentModificationException. Thus, in the face of
16.02/5.23 * concurrent modification, the iterator fails quickly and cleanly, rather
16.02/5.23 * than risking arbitrary, non-deterministic behavior at an undetermined
16.02/5.23 * time in the future.
16.02/5.23 *
16.02/5.23 * @param index index of the first element to be returned from the
16.02/5.23 * list-iterator (by a call to next)
16.02/5.23 * @return a ListIterator of the elements in this list (in proper
16.02/5.23 * sequence), starting at the specified position in the list
16.02/5.23 * @throws IndexOutOfBoundsException {@inheritDoc}
16.02/5.23 * @see List#listIterator(int)
16.02/5.23 */
16.02/5.23 public ListIterator listIterator(int index) {
16.02/5.23 return new ListItr(index);
16.02/5.23 }
16.02/5.23
16.02/5.23 private class ListItr implements ListIterator {
16.02/5.23 private Entry lastReturned = header;
16.02/5.23 private Entry next;
16.02/5.23 private int nextIndex;
16.02/5.23 private int expectedModCount = modCount;
16.02/5.23
16.02/5.23 ListItr(int index) {
16.02/5.23 if (index < 0 || index > size)
16.02/5.23 throw new IndexOutOfBoundsException();
16.02/5.23 if (index < (size >> 1)) {
16.02/5.23 next = header.next;
16.02/5.23 for (nextIndex=0; nextIndexindex; nextIndex--)
16.02/5.23 next = next.previous;
16.02/5.23 }
16.02/5.23 }
16.02/5.23
16.02/5.23 public boolean hasNext() {
16.02/5.23 return nextIndex != size;
16.02/5.23 }
16.02/5.23
16.02/5.23 public E next() {
16.02/5.23 checkForComodification();
16.02/5.23 if (nextIndex == size)
16.02/5.23 throw new NoSuchElementException();
16.02/5.23
16.02/5.23 lastReturned = next;
16.02/5.23 next = next.next;
16.02/5.23 nextIndex++;
16.02/5.23 return lastReturned.element;
16.02/5.23 }
16.02/5.23
16.02/5.23 public boolean hasPrevious() {
16.02/5.23 return nextIndex != 0;
16.02/5.23 }
16.02/5.23
16.02/5.23 public E previous() {
16.02/5.23 if (nextIndex == 0)
16.02/5.23 throw new NoSuchElementException();
16.02/5.23
16.02/5.23 lastReturned = next = next.previous;
16.02/5.23 nextIndex--;
16.02/5.23 checkForComodification();
16.02/5.23 return lastReturned.element;
16.02/5.23 }
16.02/5.23
16.02/5.23 public int nextIndex() {
16.02/5.23 return nextIndex;
16.02/5.23 }
16.02/5.23
16.02/5.23 public int previousIndex() {
16.02/5.23 return nextIndex-1;
16.02/5.23 }
16.02/5.23
16.02/5.23 public void remove() {
16.02/5.23 checkForComodification();
16.02/5.23 Entry lastNext = lastReturned.next;
16.02/5.23 try {
16.02/5.23 LinkedList.this.remove(lastReturned);
16.02/5.23 } catch (NoSuchElementException e) {
16.02/5.23 throw new IllegalStateException();
16.02/5.23 }
16.02/5.23 if (next==lastReturned)
16.02/5.23 next = lastNext;
16.02/5.23 else
16.02/5.23 nextIndex--;
16.02/5.23 lastReturned = header;
16.02/5.23 expectedModCount++;
16.02/5.23 }
16.02/5.23
16.02/5.23 public void set(E e) {
16.02/5.23 if (lastReturned == header)
16.02/5.23 throw new IllegalStateException();
16.02/5.23 checkForComodification();
16.02/5.23 lastReturned.element = e;
16.02/5.23 }
16.02/5.23
16.02/5.23 public void add(E e) {
16.02/5.23 checkForComodification();
16.02/5.23 lastReturned = header;
16.02/5.23 addBefore(e, next);
16.02/5.23 nextIndex++;
16.02/5.23 expectedModCount++;
16.02/5.23 }
16.02/5.23
16.02/5.23 final void checkForComodification() {
16.02/5.23 if (modCount != expectedModCount)
16.02/5.23 throw new ConcurrentModificationException();
16.02/5.23 }
16.02/5.23 }
16.02/5.23
16.02/5.23 private static class Entry {
16.02/5.23 E element;
16.02/5.23 Entry next;
16.02/5.23 Entry previous;
16.02/5.23
16.02/5.23 Entry(E element, Entry next, Entry previous) {
16.02/5.23 this.element = element;
16.02/5.23 this.next = next;
16.02/5.23 this.previous = previous;
16.02/5.23 }
16.02/5.23 }
16.02/5.23
16.02/5.23 private Entry