1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.collection.Queue;
13 
14 import hunt.collection.Collection;
15 
16 /**
17  * A collection designed for holding elements prior to processing.
18  * Besides basic {@link hunt.collection.Collection Collection} operations,
19  * queues provide additional insertion, extraction, and inspection
20  * operations.  Each of these methods exists in two forms: one throws
21  * an exception if the operation fails, the other returns a special
22  * value (either {@code null} or {@code false}, depending on the
23  * operation).  The latter form of the insert operation is designed
24  * specifically for use with capacity-restricted {@code Queue}
25  * implementations; in most implementations, insert operations cannot
26  * fail.
27  *
28  * <table BORDER CELLPADDING=3 CELLSPACING=1>
29  * <caption>Summary of Queue methods</caption>
30  *  <tr>
31  *    <td></td>
32  *    <td ALIGN=CENTER><em>Throws exception</em></td>
33  *    <td ALIGN=CENTER><em>Returns special value</em></td>
34  *  </tr>
35  *  <tr>
36  *    <td><b>Insert</b></td>
37  *    <td>{@link Queue#add add(e)}</td>
38  *    <td>{@link Queue#offer offer(e)}</td>
39  *  </tr>
40  *  <tr>
41  *    <td><b>Remove</b></td>
42  *    <td>{@link Queue#remove remove()}</td>
43  *    <td>{@link Queue#poll poll()}</td>
44  *  </tr>
45  *  <tr>
46  *    <td><b>Examine</b></td>
47  *    <td>{@link Queue#element element()}</td>
48  *    <td>{@link Queue#peek peek()}</td>
49  *  </tr>
50  * </table>
51  *
52  * <p>Queues typically, but do not necessarily, order elements in a
53  * FIFO (first-in-first-out) manner.  Among the exceptions are
54  * priority queues, which order elements according to a supplied
55  * comparator, or the elements' natural ordering, and LIFO queues (or
56  * stacks) which order the elements LIFO (last-in-first-out).
57  * Whatever the ordering used, the <em>head</em> of the queue is that
58  * element which would be removed by a call to {@link #remove() } or
59  * {@link #poll()}.  In a FIFO queue, all new elements are inserted at
60  * the <em>tail</em> of the queue. Other kinds of queues may use
61  * different placement rules.  Every {@code Queue} implementation
62  * must specify its ordering properties.
63  *
64  * <p>The {@link #offer offer} method inserts an element if possible,
65  * otherwise returning {@code false}.  This differs from the {@link
66  * hunt.collection.Collection#add Collection.add} method, which can fail to
67  * add an element only by throwing an unchecked exception.  The
68  * {@code offer} method is designed for use when failure is a normal,
69  * rather than exceptional occurrence, for example, in fixed-capacity
70  * (or &quot;bounded&quot;) queues.
71  *
72  * <p>The {@link #remove()} and {@link #poll()} methods remove and
73  * return the head of the queue.
74  * Exactly which element is removed from the queue is a
75  * function of the queue's ordering policy, which differs from
76  * implementation to implementation. The {@code remove()} and
77  * {@code poll()} methods differ only in their behavior when the
78  * queue is empty: the {@code remove()} method throws an exception,
79  * while the {@code poll()} method returns {@code null}.
80  *
81  * <p>The {@link #element()} and {@link #peek()} methods return, but do
82  * not remove, the head of the queue.
83  *
84  * <p>The {@code Queue} interface does not define the <i>blocking queue
85  * methods</i>, which are common in concurrent programming.  These methods,
86  * which wait for elements to appear or for space to become available, are
87  * defined in the {@link hunt.concurrency.BlockingQueue} interface, which
88  * extends this interface.
89  *
90  * <p>{@code Queue} implementations generally do not allow insertion
91  * of {@code null} elements, although some implementations, such as
92  * {@link LinkedList}, do not prohibit insertion of {@code null}.
93  * Even in the implementations that permit it, {@code null} should
94  * not be inserted into a {@code Queue}, as {@code null} is also
95  * used as a special return value by the {@code poll} method to
96  * indicate that the queue contains no elements.
97  *
98  * <p>{@code Queue} implementations generally do not define
99  * element-based versions of methods {@code equals} and
100  * {@code hashCode} but instead inherit the identity based versions
101  * from class {@code Object}, because element-based equality is not
102  * always well-defined for queues with the same elements but different
103  * ordering properties.
104  *
105  *
106  * <p>This interface is a member of the
107  * <a href="{@docRoot}/../technotes/guides/collections/index.html">
108  * Java Collections Framework</a>.
109  *
110  * @see hunt.collection.Collection
111  * @see LinkedList
112  * @see PriorityQueue
113  * @see hunt.concurrency.LinkedBlockingQueue
114  * @see hunt.concurrency.BlockingQueue
115  * @see hunt.concurrency.ArrayBlockingQueue
116  * @see hunt.concurrency.LinkedBlockingQueue
117  * @see hunt.concurrency.PriorityBlockingQueue
118  * @since 1.5
119  * @author Doug Lea
120  * @param !E the type of elements held in this collection
121  */
122 interface Queue(E) : Collection!E {
123     /**
124      * Inserts the specified element into this queue if it is possible to do so
125      * immediately without violating capacity restrictions, returning
126      * {@code true} upon success and throwing an {@code IllegalStateException}
127      * if no space is currently available.
128      *
129      * @param e the element to add
130      * @return {@code true} (as specified by {@link Collection#add})
131      * @throws IllegalStateException if the element cannot be added at this
132      *         time due to capacity restrictions
133      * @throws ClassCastException if the class of the specified element
134      *         prevents it from being added to this queue
135      * @throws NullPointerException if the specified element is null and
136      *         this queue does not permit null elements
137      * @throws IllegalArgumentException if some property of this element
138      *         prevents it from being added to this queue
139      */
140     bool add(E e);
141 
142     /**
143      * Inserts the specified element into this queue if it is possible to do
144      * so immediately without violating capacity restrictions.
145      * When using a capacity-restricted queue, this method is generally
146      * preferable to {@link #add}, which can fail to insert an element only
147      * by throwing an exception.
148      *
149      * @param e the element to add
150      * @return {@code true} if the element was added to this queue, else
151      *         {@code false}
152      * @throws ClassCastException if the class of the specified element
153      *         prevents it from being added to this queue
154      * @throws NullPointerException if the specified element is null and
155      *         this queue does not permit null elements
156      * @throws IllegalArgumentException if some property of this element
157      *         prevents it from being added to this queue
158      */
159     bool offer(E e);
160 
161     /**
162      * Retrieves and removes the head of this queue.  This method differs
163      * from {@link #poll poll} only in that it throws an exception if this
164      * queue is empty.
165      *
166      * @return the head of this queue
167      * @throws NoSuchElementException if this queue is empty
168      */
169     E remove();
170 
171     /**
172      * Retrieves and removes the head of this queue,
173      * or returns {@code null} if this queue is empty.
174      *
175      * @return the head of this queue, or {@code null} if this queue is empty
176      */
177     E poll();
178 
179     /**
180      * Retrieves, but does not remove, the head of this queue.  This method
181      * differs from {@link #peek peek} only in that it throws an exception
182      * if this queue is empty.
183      *
184      * @return the head of this queue
185      * @throws NoSuchElementException if this queue is empty
186      */
187     E element();
188 
189     /**
190      * Retrieves, but does not remove, the head of this queue,
191      * or returns {@code null} if this queue is empty.
192      *
193      * @return the head of this queue, or {@code null} if this queue is empty
194      */
195     E peek();
196 }