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