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