rt/emul/compact/src/main/java/java/util/AbstractSequentialList.java
changeset 772 d382dacfd73f
parent 597 ee8a922f4268
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/AbstractSequentialList.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,253 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.util;
    1.30 +
    1.31 +/**
    1.32 + * This class provides a skeletal implementation of the <tt>List</tt>
    1.33 + * interface to minimize the effort required to implement this interface
    1.34 + * backed by a "sequential access" data store (such as a linked list).  For
    1.35 + * random access data (such as an array), <tt>AbstractList</tt> should be used
    1.36 + * in preference to this class.<p>
    1.37 + *
    1.38 + * This class is the opposite of the <tt>AbstractList</tt> class in the sense
    1.39 + * that it implements the "random access" methods (<tt>get(int index)</tt>,
    1.40 + * <tt>set(int index, E element)</tt>, <tt>add(int index, E element)</tt> and
    1.41 + * <tt>remove(int index)</tt>) on top of the list's list iterator, instead of
    1.42 + * the other way around.<p>
    1.43 + *
    1.44 + * To implement a list the programmer needs only to extend this class and
    1.45 + * provide implementations for the <tt>listIterator</tt> and <tt>size</tt>
    1.46 + * methods.  For an unmodifiable list, the programmer need only implement the
    1.47 + * list iterator's <tt>hasNext</tt>, <tt>next</tt>, <tt>hasPrevious</tt>,
    1.48 + * <tt>previous</tt> and <tt>index</tt> methods.<p>
    1.49 + *
    1.50 + * For a modifiable list the programmer should additionally implement the list
    1.51 + * iterator's <tt>set</tt> method.  For a variable-size list the programmer
    1.52 + * should additionally implement the list iterator's <tt>remove</tt> and
    1.53 + * <tt>add</tt> methods.<p>
    1.54 + *
    1.55 + * The programmer should generally provide a void (no argument) and collection
    1.56 + * constructor, as per the recommendation in the <tt>Collection</tt> interface
    1.57 + * specification.<p>
    1.58 + *
    1.59 + * This class is a member of the
    1.60 + * <a href="{@docRoot}/../technotes/guides/collections/index.html">
    1.61 + * Java Collections Framework</a>.
    1.62 + *
    1.63 + * @author  Josh Bloch
    1.64 + * @author  Neal Gafter
    1.65 + * @see Collection
    1.66 + * @see List
    1.67 + * @see AbstractList
    1.68 + * @see AbstractCollection
    1.69 + * @since 1.2
    1.70 + */
    1.71 +
    1.72 +public abstract class AbstractSequentialList<E> extends AbstractList<E> {
    1.73 +    /**
    1.74 +     * Sole constructor.  (For invocation by subclass constructors, typically
    1.75 +     * implicit.)
    1.76 +     */
    1.77 +    protected AbstractSequentialList() {
    1.78 +    }
    1.79 +
    1.80 +    /**
    1.81 +     * Returns the element at the specified position in this list.
    1.82 +     *
    1.83 +     * <p>This implementation first gets a list iterator pointing to the
    1.84 +     * indexed element (with <tt>listIterator(index)</tt>).  Then, it gets
    1.85 +     * the element using <tt>ListIterator.next</tt> and returns it.
    1.86 +     *
    1.87 +     * @throws IndexOutOfBoundsException {@inheritDoc}
    1.88 +     */
    1.89 +    public E get(int index) {
    1.90 +        try {
    1.91 +            return listIterator(index).next();
    1.92 +        } catch (NoSuchElementException exc) {
    1.93 +            throw new IndexOutOfBoundsException("Index: "+index);
    1.94 +        }
    1.95 +    }
    1.96 +
    1.97 +    /**
    1.98 +     * Replaces the element at the specified position in this list with the
    1.99 +     * specified element (optional operation).
   1.100 +     *
   1.101 +     * <p>This implementation first gets a list iterator pointing to the
   1.102 +     * indexed element (with <tt>listIterator(index)</tt>).  Then, it gets
   1.103 +     * the current element using <tt>ListIterator.next</tt> and replaces it
   1.104 +     * with <tt>ListIterator.set</tt>.
   1.105 +     *
   1.106 +     * <p>Note that this implementation will throw an
   1.107 +     * <tt>UnsupportedOperationException</tt> if the list iterator does not
   1.108 +     * implement the <tt>set</tt> operation.
   1.109 +     *
   1.110 +     * @throws UnsupportedOperationException {@inheritDoc}
   1.111 +     * @throws ClassCastException            {@inheritDoc}
   1.112 +     * @throws NullPointerException          {@inheritDoc}
   1.113 +     * @throws IllegalArgumentException      {@inheritDoc}
   1.114 +     * @throws IndexOutOfBoundsException     {@inheritDoc}
   1.115 +     */
   1.116 +    public E set(int index, E element) {
   1.117 +        try {
   1.118 +            ListIterator<E> e = listIterator(index);
   1.119 +            E oldVal = e.next();
   1.120 +            e.set(element);
   1.121 +            return oldVal;
   1.122 +        } catch (NoSuchElementException exc) {
   1.123 +            throw new IndexOutOfBoundsException("Index: "+index);
   1.124 +        }
   1.125 +    }
   1.126 +
   1.127 +    /**
   1.128 +     * Inserts the specified element at the specified position in this list
   1.129 +     * (optional operation).  Shifts the element currently at that position
   1.130 +     * (if any) and any subsequent elements to the right (adds one to their
   1.131 +     * indices).
   1.132 +     *
   1.133 +     * <p>This implementation first gets a list iterator pointing to the
   1.134 +     * indexed element (with <tt>listIterator(index)</tt>).  Then, it
   1.135 +     * inserts the specified element with <tt>ListIterator.add</tt>.
   1.136 +     *
   1.137 +     * <p>Note that this implementation will throw an
   1.138 +     * <tt>UnsupportedOperationException</tt> if the list iterator does not
   1.139 +     * implement the <tt>add</tt> operation.
   1.140 +     *
   1.141 +     * @throws UnsupportedOperationException {@inheritDoc}
   1.142 +     * @throws ClassCastException            {@inheritDoc}
   1.143 +     * @throws NullPointerException          {@inheritDoc}
   1.144 +     * @throws IllegalArgumentException      {@inheritDoc}
   1.145 +     * @throws IndexOutOfBoundsException     {@inheritDoc}
   1.146 +     */
   1.147 +    public void add(int index, E element) {
   1.148 +        try {
   1.149 +            listIterator(index).add(element);
   1.150 +        } catch (NoSuchElementException exc) {
   1.151 +            throw new IndexOutOfBoundsException("Index: "+index);
   1.152 +        }
   1.153 +    }
   1.154 +
   1.155 +    /**
   1.156 +     * Removes the element at the specified position in this list (optional
   1.157 +     * operation).  Shifts any subsequent elements to the left (subtracts one
   1.158 +     * from their indices).  Returns the element that was removed from the
   1.159 +     * list.
   1.160 +     *
   1.161 +     * <p>This implementation first gets a list iterator pointing to the
   1.162 +     * indexed element (with <tt>listIterator(index)</tt>).  Then, it removes
   1.163 +     * the element with <tt>ListIterator.remove</tt>.
   1.164 +     *
   1.165 +     * <p>Note that this implementation will throw an
   1.166 +     * <tt>UnsupportedOperationException</tt> if the list iterator does not
   1.167 +     * implement the <tt>remove</tt> operation.
   1.168 +     *
   1.169 +     * @throws UnsupportedOperationException {@inheritDoc}
   1.170 +     * @throws IndexOutOfBoundsException     {@inheritDoc}
   1.171 +     */
   1.172 +    public E remove(int index) {
   1.173 +        try {
   1.174 +            ListIterator<E> e = listIterator(index);
   1.175 +            E outCast = e.next();
   1.176 +            e.remove();
   1.177 +            return outCast;
   1.178 +        } catch (NoSuchElementException exc) {
   1.179 +            throw new IndexOutOfBoundsException("Index: "+index);
   1.180 +        }
   1.181 +    }
   1.182 +
   1.183 +
   1.184 +    // Bulk Operations
   1.185 +
   1.186 +    /**
   1.187 +     * Inserts all of the elements in the specified collection into this
   1.188 +     * list at the specified position (optional operation).  Shifts the
   1.189 +     * element currently at that position (if any) and any subsequent
   1.190 +     * elements to the right (increases their indices).  The new elements
   1.191 +     * will appear in this list in the order that they are returned by the
   1.192 +     * specified collection's iterator.  The behavior of this operation is
   1.193 +     * undefined if the specified collection is modified while the
   1.194 +     * operation is in progress.  (Note that this will occur if the specified
   1.195 +     * collection is this list, and it's nonempty.)
   1.196 +     *
   1.197 +     * <p>This implementation gets an iterator over the specified collection and
   1.198 +     * a list iterator over this list pointing to the indexed element (with
   1.199 +     * <tt>listIterator(index)</tt>).  Then, it iterates over the specified
   1.200 +     * collection, inserting the elements obtained from the iterator into this
   1.201 +     * list, one at a time, using <tt>ListIterator.add</tt> followed by
   1.202 +     * <tt>ListIterator.next</tt> (to skip over the added element).
   1.203 +     *
   1.204 +     * <p>Note that this implementation will throw an
   1.205 +     * <tt>UnsupportedOperationException</tt> if the list iterator returned by
   1.206 +     * the <tt>listIterator</tt> method does not implement the <tt>add</tt>
   1.207 +     * operation.
   1.208 +     *
   1.209 +     * @throws UnsupportedOperationException {@inheritDoc}
   1.210 +     * @throws ClassCastException            {@inheritDoc}
   1.211 +     * @throws NullPointerException          {@inheritDoc}
   1.212 +     * @throws IllegalArgumentException      {@inheritDoc}
   1.213 +     * @throws IndexOutOfBoundsException     {@inheritDoc}
   1.214 +     */
   1.215 +    public boolean addAll(int index, Collection<? extends E> c) {
   1.216 +        try {
   1.217 +            boolean modified = false;
   1.218 +            ListIterator<E> e1 = listIterator(index);
   1.219 +            Iterator<? extends E> e2 = c.iterator();
   1.220 +            while (e2.hasNext()) {
   1.221 +                e1.add(e2.next());
   1.222 +                modified = true;
   1.223 +            }
   1.224 +            return modified;
   1.225 +        } catch (NoSuchElementException exc) {
   1.226 +            throw new IndexOutOfBoundsException("Index: "+index);
   1.227 +        }
   1.228 +    }
   1.229 +
   1.230 +
   1.231 +    // Iterators
   1.232 +
   1.233 +    /**
   1.234 +     * Returns an iterator over the elements in this list (in proper
   1.235 +     * sequence).<p>
   1.236 +     *
   1.237 +     * This implementation merely returns a list iterator over the list.
   1.238 +     *
   1.239 +     * @return an iterator over the elements in this list (in proper sequence)
   1.240 +     */
   1.241 +    public Iterator<E> iterator() {
   1.242 +        return listIterator();
   1.243 +    }
   1.244 +
   1.245 +    /**
   1.246 +     * Returns a list iterator over the elements in this list (in proper
   1.247 +     * sequence).
   1.248 +     *
   1.249 +     * @param  index index of first element to be returned from the list
   1.250 +     *         iterator (by a call to the <code>next</code> method)
   1.251 +     * @return a list iterator over the elements in this list (in proper
   1.252 +     *         sequence)
   1.253 +     * @throws IndexOutOfBoundsException {@inheritDoc}
   1.254 +     */
   1.255 +    public abstract ListIterator<E> listIterator(int index);
   1.256 +}