1 module hunt.collection.AbstractDeque;
2 
3 import hunt.collection.AbstractCollection;
4 import hunt.collection.AbstractQueue;
5 import hunt.collection.Collection;
6 import hunt.collection.Deque;
7 import hunt.collection.Queue;
8 import hunt.Exceptions;
9 import hunt.Object;
10 
11 import core.time;
12 
13 
14 /**
15 */
16 abstract class AbstractDeque(E) : AbstractQueue!(E), Deque!(E) {
17 
18 
19     /**
20      * {@inheritDoc}
21      */
22     override
23     void addFirst(E e) {
24         if (!offerFirst(e)) {
25             throw new IllegalStateException("Deque full");
26         }
27     }
28 
29     /**
30      * {@inheritDoc}
31      */
32     override
33     void addLast(E e) {
34         if (!offerLast(e)) {
35             throw new IllegalStateException("Deque full");
36         }
37     }
38 
39     /**
40      * {@inheritDoc}
41      */
42     override
43     bool add(E e) {
44         addLast(e);
45         return true;
46     }
47 
48     /**
49      * {@inheritDoc}
50      */
51     override
52     bool offer(E e) {
53         return offerLast(e);
54     }
55 
56     /**
57      * Links the provided element as the last in the queue, waiting until there
58      * is space to do so if the queue is full.
59      *
60      * <p>This method is equivalent to {@link #putLast(Object)}.
61      *
62      * @param e element to link
63      *
64      * @throws NullPointerException if e is null
65      * @throws InterruptedException if the thread is interrupted whilst waiting
66      *         for space
67      */
68     void put(E e){
69         putLast(e);
70     }
71 
72     /**
73      * Links the provided element as the last in the queue, waiting up to the
74      * specified time to do so if the queue is full.
75      * <p>
76      * This method is equivalent to {@link #offerLast(Object, long, TimeUnit)}
77      *
78      * @param e         element to link
79      * @param timeout   length of time to wait
80      * @param unit      units that timeout is expressed in
81      *
82      * @return {@code true} if successful, otherwise {@code false}
83      *
84      * @throws NullPointerException if e is null
85      * @throws InterruptedException if the thread is interrupted whilst waiting
86      *         for space
87      */
88     bool offer(E e, Duration timeout) {
89         return offerLast(e, timeout);
90     }
91 
92     /**
93      * Retrieves and removes the head of the queue represented by this deque.
94      * This method differs from {@link #poll poll} only in that it throws an
95      * exception if this deque is empty.
96      *
97      * <p>This method is equivalent to {@link #removeFirst() removeFirst}.
98      *
99      * @return the head of the queue represented by this deque
100      * @throws NoSuchElementException if this deque is empty
101      */
102     override
103     E remove() {
104         return removeFirst();
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     override
111     E removeFirst() {
112         E x = pollFirst();
113         if (x is null) {
114             throw new NoSuchElementException();
115         }
116         return x;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     override
123     E removeLast() {
124         E x = pollLast();
125         if (x is null) {
126             throw new NoSuchElementException();
127         }
128         return x;
129     }
130 
131     /**
132      * Removes the first occurrence of the specified element from this deque.
133      * If the deque does not contain the element, it is unchanged.
134      * More formally, removes the first element {@code e} such that
135      * {@code o == e} (if such an element exists).
136      * Returns {@code true} if this deque contained the specified element
137      * (or equivalently, if this deque changed as a result of the call).
138      *
139      * <p>This method is equivalent to
140      * {@link #removeFirstOccurrence(Object) removeFirstOccurrence}.
141      *
142      * @param o element to be removed from this deque, if present
143      * @return {@code true} if this deque changed as a result of the call
144      */
145     override
146     bool remove(E o) {
147         return removeFirstOccurrence(o);
148     }
149 
150     override
151     E poll() {
152         return pollFirst();
153     }
154 
155     /**
156      * Unlinks the first element in the queue, waiting until there is an element
157      * to unlink if the queue is empty.
158      *
159      * <p>This method is equivalent to {@link #takeFirst()}.
160      *
161      * @return the unlinked element
162      * @throws InterruptedException if the current thread is interrupted
163      */
164     E take() {
165         return takeFirst();
166     }
167 
168     /**
169      * Unlinks the first element in the queue, waiting up to the specified time
170      * to do so if the queue is empty.
171      *
172      * <p>This method is equivalent to {@link #pollFirst(long, TimeUnit)}.
173      *
174      * @param timeout   length of time to wait
175      * @param unit      units that timeout is expressed in
176      *
177      * @return the unlinked element
178      * @throws InterruptedException if the current thread is interrupted
179      */
180     E poll(Duration timeout) {
181         return pollFirst(timeout);
182     }
183 
184     /**
185      * Retrieves, but does not remove, the head of the queue represented by
186      * this deque.  This method differs from {@link #peek peek} only in that
187      * itempty.
188      *
189      * <p>This method is equivalent to {@link #getFirst() getFirst}.
190      *
191      * @return the head of the queue represented by this deque
192      * @throws NoSuchElementException if this deque is empty
193      */
194     override
195     E element() {
196         return getFirst();
197     }
198 
199     override
200     E peek() {
201         return peekFirst();
202     }
203 
204 
205     override bool contains(E o) {
206         throw new NotSupportedException();
207     }
208 
209     // Stack methods
210 
211     /**
212      * {@inheritDoc}
213      */
214     override
215     void push(E e) {
216         addFirst(e);
217     }
218 
219     /**
220      * {@inheritDoc}
221      */
222     override
223     E pop() {
224         return removeFirst();
225     }
226 
227 
228     bool offerFirst(E e) {
229         throw new NotSupportedException();
230     }
231 
232     bool offerLast(E e) {
233         throw new NotSupportedException();
234     }
235     
236     bool offerLast(E e, Duration timeout) {
237         throw new NotSupportedException();
238     }
239 
240     E pollFirst() {
241         throw new NotSupportedException();
242     }
243 
244     E pollFirst(Duration timeout) {
245         throw new NotSupportedException();
246     }
247 
248     E pollLast() {
249         throw new NotSupportedException();
250     }
251     
252     E getFirst() {
253         throw new NotSupportedException();
254     }
255 
256     E takeFirst() {
257         throw new NotSupportedException();
258     }
259 
260     E getLast() {
261         throw new NotSupportedException();
262     }
263 
264     E peekFirst() {
265         throw new NotSupportedException();
266     }
267 
268     void putLast(E e) {
269         throw new NotSupportedException();
270     }
271 
272     E peekLast() {
273         throw new NotSupportedException();
274     }
275 
276     bool removeFirstOccurrence(E o) {
277         throw new NotSupportedException();
278     }
279 
280     override bool opEquals(IObject o) {
281         return opEquals(cast(Object) o);
282     }
283     
284     override bool opEquals(Object o) {
285         return super.opEquals(o);
286     }
287 
288     override size_t toHash() @trusted nothrow {
289         return super.toHash();
290     }
291 
292     override string toString() {
293         return super.toString();
294     }
295 }