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