rt/emul/compact/src/main/java/java/util/AbstractQueue.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 597 emul/compact/src/main/java/java/util/AbstractQueue.java@ee8a922f4268
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
jaroslav@597
     1
/*
jaroslav@597
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@597
     3
 *
jaroslav@597
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@597
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@597
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@597
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@597
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@597
     9
 *
jaroslav@597
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@597
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@597
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@597
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@597
    14
 * accompanied this code).
jaroslav@597
    15
 *
jaroslav@597
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@597
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@597
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@597
    19
 *
jaroslav@597
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@597
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@597
    22
 * questions.
jaroslav@597
    23
 */
jaroslav@597
    24
jaroslav@597
    25
/*
jaroslav@597
    26
 * This file is available under and governed by the GNU General Public
jaroslav@597
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@597
    28
 * However, the following notice accompanied the original version of this
jaroslav@597
    29
 * file:
jaroslav@597
    30
 *
jaroslav@597
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@597
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@597
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@597
    34
 */
jaroslav@597
    35
jaroslav@597
    36
package java.util;
jaroslav@597
    37
jaroslav@597
    38
/**
jaroslav@597
    39
 * This class provides skeletal implementations of some {@link Queue}
jaroslav@597
    40
 * operations. The implementations in this class are appropriate when
jaroslav@597
    41
 * the base implementation does <em>not</em> allow <tt>null</tt>
jaroslav@597
    42
 * elements.  Methods {@link #add add}, {@link #remove remove}, and
jaroslav@597
    43
 * {@link #element element} are based on {@link #offer offer}, {@link
jaroslav@597
    44
 * #poll poll}, and {@link #peek peek}, respectively, but throw
jaroslav@597
    45
 * exceptions instead of indicating failure via <tt>false</tt> or
jaroslav@597
    46
 * <tt>null</tt> returns.
jaroslav@597
    47
 *
jaroslav@597
    48
 * <p>A <tt>Queue</tt> implementation that extends this class must
jaroslav@597
    49
 * minimally define a method {@link Queue#offer} which does not permit
jaroslav@597
    50
 * insertion of <tt>null</tt> elements, along with methods {@link
jaroslav@597
    51
 * Queue#peek}, {@link Queue#poll}, {@link Collection#size}, and
jaroslav@597
    52
 * {@link Collection#iterator}.  Typically, additional methods will be
jaroslav@597
    53
 * overridden as well.  If these requirements cannot be met, consider
jaroslav@597
    54
 * instead subclassing {@link AbstractCollection}.
jaroslav@597
    55
 *
jaroslav@597
    56
 * <p>This class is a member of the
jaroslav@597
    57
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@597
    58
 * Java Collections Framework</a>.
jaroslav@597
    59
 *
jaroslav@597
    60
 * @since 1.5
jaroslav@597
    61
 * @author Doug Lea
jaroslav@597
    62
 * @param <E> the type of elements held in this collection
jaroslav@597
    63
 */
jaroslav@597
    64
public abstract class AbstractQueue<E>
jaroslav@597
    65
    extends AbstractCollection<E>
jaroslav@597
    66
    implements Queue<E> {
jaroslav@597
    67
jaroslav@597
    68
    /**
jaroslav@597
    69
     * Constructor for use by subclasses.
jaroslav@597
    70
     */
jaroslav@597
    71
    protected AbstractQueue() {
jaroslav@597
    72
    }
jaroslav@597
    73
jaroslav@597
    74
    /**
jaroslav@597
    75
     * Inserts the specified element into this queue if it is possible to do so
jaroslav@597
    76
     * immediately without violating capacity restrictions, returning
jaroslav@597
    77
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
jaroslav@597
    78
     * if no space is currently available.
jaroslav@597
    79
     *
jaroslav@597
    80
     * <p>This implementation returns <tt>true</tt> if <tt>offer</tt> succeeds,
jaroslav@597
    81
     * else throws an <tt>IllegalStateException</tt>.
jaroslav@597
    82
     *
jaroslav@597
    83
     * @param e the element to add
jaroslav@597
    84
     * @return <tt>true</tt> (as specified by {@link Collection#add})
jaroslav@597
    85
     * @throws IllegalStateException if the element cannot be added at this
jaroslav@597
    86
     *         time due to capacity restrictions
jaroslav@597
    87
     * @throws ClassCastException if the class of the specified element
jaroslav@597
    88
     *         prevents it from being added to this queue
jaroslav@597
    89
     * @throws NullPointerException if the specified element is null and
jaroslav@597
    90
     *         this queue does not permit null elements
jaroslav@597
    91
     * @throws IllegalArgumentException if some property of this element
jaroslav@597
    92
     *         prevents it from being added to this queue
jaroslav@597
    93
     */
jaroslav@597
    94
    public boolean add(E e) {
jaroslav@597
    95
        if (offer(e))
jaroslav@597
    96
            return true;
jaroslav@597
    97
        else
jaroslav@597
    98
            throw new IllegalStateException("Queue full");
jaroslav@597
    99
    }
jaroslav@597
   100
jaroslav@597
   101
    /**
jaroslav@597
   102
     * Retrieves and removes the head of this queue.  This method differs
jaroslav@597
   103
     * from {@link #poll poll} only in that it throws an exception if this
jaroslav@597
   104
     * queue is empty.
jaroslav@597
   105
     *
jaroslav@597
   106
     * <p>This implementation returns the result of <tt>poll</tt>
jaroslav@597
   107
     * unless the queue is empty.
jaroslav@597
   108
     *
jaroslav@597
   109
     * @return the head of this queue
jaroslav@597
   110
     * @throws NoSuchElementException if this queue is empty
jaroslav@597
   111
     */
jaroslav@597
   112
    public E remove() {
jaroslav@597
   113
        E x = poll();
jaroslav@597
   114
        if (x != null)
jaroslav@597
   115
            return x;
jaroslav@597
   116
        else
jaroslav@597
   117
            throw new NoSuchElementException();
jaroslav@597
   118
    }
jaroslav@597
   119
jaroslav@597
   120
    /**
jaroslav@597
   121
     * Retrieves, but does not remove, the head of this queue.  This method
jaroslav@597
   122
     * differs from {@link #peek peek} only in that it throws an exception if
jaroslav@597
   123
     * this queue is empty.
jaroslav@597
   124
     *
jaroslav@597
   125
     * <p>This implementation returns the result of <tt>peek</tt>
jaroslav@597
   126
     * unless the queue is empty.
jaroslav@597
   127
     *
jaroslav@597
   128
     * @return the head of this queue
jaroslav@597
   129
     * @throws NoSuchElementException if this queue is empty
jaroslav@597
   130
     */
jaroslav@597
   131
    public E element() {
jaroslav@597
   132
        E x = peek();
jaroslav@597
   133
        if (x != null)
jaroslav@597
   134
            return x;
jaroslav@597
   135
        else
jaroslav@597
   136
            throw new NoSuchElementException();
jaroslav@597
   137
    }
jaroslav@597
   138
jaroslav@597
   139
    /**
jaroslav@597
   140
     * Removes all of the elements from this queue.
jaroslav@597
   141
     * The queue will be empty after this call returns.
jaroslav@597
   142
     *
jaroslav@597
   143
     * <p>This implementation repeatedly invokes {@link #poll poll} until it
jaroslav@597
   144
     * returns <tt>null</tt>.
jaroslav@597
   145
     */
jaroslav@597
   146
    public void clear() {
jaroslav@597
   147
        while (poll() != null)
jaroslav@597
   148
            ;
jaroslav@597
   149
    }
jaroslav@597
   150
jaroslav@597
   151
    /**
jaroslav@597
   152
     * Adds all of the elements in the specified collection to this
jaroslav@597
   153
     * queue.  Attempts to addAll of a queue to itself result in
jaroslav@597
   154
     * <tt>IllegalArgumentException</tt>. Further, the behavior of
jaroslav@597
   155
     * this operation is undefined if the specified collection is
jaroslav@597
   156
     * modified while the operation is in progress.
jaroslav@597
   157
     *
jaroslav@597
   158
     * <p>This implementation iterates over the specified collection,
jaroslav@597
   159
     * and adds each element returned by the iterator to this
jaroslav@597
   160
     * queue, in turn.  A runtime exception encountered while
jaroslav@597
   161
     * trying to add an element (including, in particular, a
jaroslav@597
   162
     * <tt>null</tt> element) may result in only some of the elements
jaroslav@597
   163
     * having been successfully added when the associated exception is
jaroslav@597
   164
     * thrown.
jaroslav@597
   165
     *
jaroslav@597
   166
     * @param c collection containing elements to be added to this queue
jaroslav@597
   167
     * @return <tt>true</tt> if this queue changed as a result of the call
jaroslav@597
   168
     * @throws ClassCastException if the class of an element of the specified
jaroslav@597
   169
     *         collection prevents it from being added to this queue
jaroslav@597
   170
     * @throws NullPointerException if the specified collection contains a
jaroslav@597
   171
     *         null element and this queue does not permit null elements,
jaroslav@597
   172
     *         or if the specified collection is null
jaroslav@597
   173
     * @throws IllegalArgumentException if some property of an element of the
jaroslav@597
   174
     *         specified collection prevents it from being added to this
jaroslav@597
   175
     *         queue, or if the specified collection is this queue
jaroslav@597
   176
     * @throws IllegalStateException if not all the elements can be added at
jaroslav@597
   177
     *         this time due to insertion restrictions
jaroslav@597
   178
     * @see #add(Object)
jaroslav@597
   179
     */
jaroslav@597
   180
    public boolean addAll(Collection<? extends E> c) {
jaroslav@597
   181
        if (c == null)
jaroslav@597
   182
            throw new NullPointerException();
jaroslav@597
   183
        if (c == this)
jaroslav@597
   184
            throw new IllegalArgumentException();
jaroslav@597
   185
        boolean modified = false;
jaroslav@597
   186
        for (E e : c)
jaroslav@597
   187
            if (add(e))
jaroslav@597
   188
                modified = true;
jaroslav@597
   189
        return modified;
jaroslav@597
   190
    }
jaroslav@597
   191
jaroslav@597
   192
}