rt/emul/compact/src/main/java/java/util/AbstractQueue.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/AbstractQueue.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,192 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.6 + *
     1.7 + * This code is free software; you can redistribute it and/or modify it
     1.8 + * under the terms of the GNU General Public License version 2 only, as
     1.9 + * published by the Free Software Foundation.  Oracle designates this
    1.10 + * particular file as subject to the "Classpath" exception as provided
    1.11 + * by Oracle in the LICENSE file that accompanied this code.
    1.12 + *
    1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.16 + * version 2 for more details (a copy is included in the LICENSE file that
    1.17 + * accompanied this code).
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License version
    1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.22 + *
    1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.24 + * or visit www.oracle.com if you need additional information or have any
    1.25 + * questions.
    1.26 + */
    1.27 +
    1.28 +/*
    1.29 + * This file is available under and governed by the GNU General Public
    1.30 + * License version 2 only, as published by the Free Software Foundation.
    1.31 + * However, the following notice accompanied the original version of this
    1.32 + * file:
    1.33 + *
    1.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
    1.35 + * Expert Group and released to the public domain, as explained at
    1.36 + * http://creativecommons.org/publicdomain/zero/1.0/
    1.37 + */
    1.38 +
    1.39 +package java.util;
    1.40 +
    1.41 +/**
    1.42 + * This class provides skeletal implementations of some {@link Queue}
    1.43 + * operations. The implementations in this class are appropriate when
    1.44 + * the base implementation does <em>not</em> allow <tt>null</tt>
    1.45 + * elements.  Methods {@link #add add}, {@link #remove remove}, and
    1.46 + * {@link #element element} are based on {@link #offer offer}, {@link
    1.47 + * #poll poll}, and {@link #peek peek}, respectively, but throw
    1.48 + * exceptions instead of indicating failure via <tt>false</tt> or
    1.49 + * <tt>null</tt> returns.
    1.50 + *
    1.51 + * <p>A <tt>Queue</tt> implementation that extends this class must
    1.52 + * minimally define a method {@link Queue#offer} which does not permit
    1.53 + * insertion of <tt>null</tt> elements, along with methods {@link
    1.54 + * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and
    1.55 + * {@link Collection#iterator}.  Typically, additional methods will be
    1.56 + * overridden as well.  If these requirements cannot be met, consider
    1.57 + * instead subclassing {@link AbstractCollection}.
    1.58 + *
    1.59 + * <p>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 + * @since 1.5
    1.64 + * @author Doug Lea
    1.65 + * @param <E> the type of elements held in this collection
    1.66 + */
    1.67 +public abstract class AbstractQueue<E>
    1.68 +    extends AbstractCollection<E>
    1.69 +    implements Queue<E> {
    1.70 +
    1.71 +    /**
    1.72 +     * Constructor for use by subclasses.
    1.73 +     */
    1.74 +    protected AbstractQueue() {
    1.75 +    }
    1.76 +
    1.77 +    /**
    1.78 +     * Inserts the specified element into this queue if it is possible to do so
    1.79 +     * immediately without violating capacity restrictions, returning
    1.80 +     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
    1.81 +     * if no space is currently available.
    1.82 +     *
    1.83 +     * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
    1.84 +     * else throws an <tt>IllegalStateException</tt>.
    1.85 +     *
    1.86 +     * @param e the element to add
    1.87 +     * @return <tt>true</tt> (as specified by {@link Collection#add})
    1.88 +     * @throws IllegalStateException if the element cannot be added at this
    1.89 +     *         time due to capacity restrictions
    1.90 +     * @throws ClassCastException if the class of the specified element
    1.91 +     *         prevents it from being added to this queue
    1.92 +     * @throws NullPointerException if the specified element is null and
    1.93 +     *         this queue does not permit null elements
    1.94 +     * @throws IllegalArgumentException if some property of this element
    1.95 +     *         prevents it from being added to this queue
    1.96 +     */
    1.97 +    public boolean add(E e) {
    1.98 +        if (offer(e))
    1.99 +            return true;
   1.100 +        else
   1.101 +            throw new IllegalStateException("Queue full");
   1.102 +    }
   1.103 +
   1.104 +    /**
   1.105 +     * Retrieves and removes the head of this queue.  This method differs
   1.106 +     * from {@link #poll poll} only in that it throws an exception if this
   1.107 +     * queue is empty.
   1.108 +     *
   1.109 +     * <p>This implementation returns the result of <tt>poll</tt>
   1.110 +     * unless the queue is empty.
   1.111 +     *
   1.112 +     * @return the head of this queue
   1.113 +     * @throws NoSuchElementException if this queue is empty
   1.114 +     */
   1.115 +    public E remove() {
   1.116 +        E x = poll();
   1.117 +        if (x != null)
   1.118 +            return x;
   1.119 +        else
   1.120 +            throw new NoSuchElementException();
   1.121 +    }
   1.122 +
   1.123 +    /**
   1.124 +     * Retrieves, but does not remove, the head of this queue.  This method
   1.125 +     * differs from {@link #peek peek} only in that it throws an exception if
   1.126 +     * this queue is empty.
   1.127 +     *
   1.128 +     * <p>This implementation returns the result of <tt>peek</tt>
   1.129 +     * unless the queue is empty.
   1.130 +     *
   1.131 +     * @return the head of this queue
   1.132 +     * @throws NoSuchElementException if this queue is empty
   1.133 +     */
   1.134 +    public E element() {
   1.135 +        E x = peek();
   1.136 +        if (x != null)
   1.137 +            return x;
   1.138 +        else
   1.139 +            throw new NoSuchElementException();
   1.140 +    }
   1.141 +
   1.142 +    /**
   1.143 +     * Removes all of the elements from this queue.
   1.144 +     * The queue will be empty after this call returns.
   1.145 +     *
   1.146 +     * <p>This implementation repeatedly invokes {@link #poll poll} until it
   1.147 +     * returns <tt>null</tt>.
   1.148 +     */
   1.149 +    public void clear() {
   1.150 +        while (poll() != null)
   1.151 +            ;
   1.152 +    }
   1.153 +
   1.154 +    /**
   1.155 +     * Adds all of the elements in the specified collection to this
   1.156 +     * queue.  Attempts to addAll of a queue to itself result in
   1.157 +     * <tt>IllegalArgumentException</tt>. Further, the behavior of
   1.158 +     * this operation is undefined if the specified collection is
   1.159 +     * modified while the operation is in progress.
   1.160 +     *
   1.161 +     * <p>This implementation iterates over the specified collection,
   1.162 +     * and adds each element returned by the iterator to this
   1.163 +     * queue, in turn.  A runtime exception encountered while
   1.164 +     * trying to add an element (including, in particular, a
   1.165 +     * <tt>null</tt> element) may result in only some of the elements
   1.166 +     * having been successfully added when the associated exception is
   1.167 +     * thrown.
   1.168 +     *
   1.169 +     * @param c collection containing elements to be added to this queue
   1.170 +     * @return <tt>true</tt> if this queue changed as a result of the call
   1.171 +     * @throws ClassCastException if the class of an element of the specified
   1.172 +     *         collection prevents it from being added to this queue
   1.173 +     * @throws NullPointerException if the specified collection contains a
   1.174 +     *         null element and this queue does not permit null elements,
   1.175 +     *         or if the specified collection is null
   1.176 +     * @throws IllegalArgumentException if some property of an element of the
   1.177 +     *         specified collection prevents it from being added to this
   1.178 +     *         queue, or if the specified collection is this queue
   1.179 +     * @throws IllegalStateException if not all the elements can be added at
   1.180 +     *         this time due to insertion restrictions
   1.181 +     * @see #add(Object)
   1.182 +     */
   1.183 +    public boolean addAll(Collection<? extends E> c) {
   1.184 +        if (c == null)
   1.185 +            throw new NullPointerException();
   1.186 +        if (c == this)
   1.187 +            throw new IllegalArgumentException();
   1.188 +        boolean modified = false;
   1.189 +        for (E e : c)
   1.190 +            if (add(e))
   1.191 +                modified = true;
   1.192 +        return modified;
   1.193 +    }
   1.194 +
   1.195 +}