Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9de1eab2e3822cd4be91b9fd54e39398e057ff8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*******************************************************************************
 * Copyright (c) 2013, 2015 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.utility.internal.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.eclipse.jpt.common.utility.collection.Queue;
import org.eclipse.jpt.common.utility.collection.Stack;

/**
 * {@link Queue} utility methods.
 */
public final class QueueTools {

	// ********** enqueue all **********

	/**
	 * Enqueue all the elements returned by the specified iterable
	 * on the specified queue.
	 * Return the queue.
	 */
	public static <Q extends Queue<? super E>, E> Q enqueueAll(Q queue, Iterable<? extends E> iterable) {
		return enqueueAll(queue, iterable.iterator());
	}

	/**
	 * Enqueue all the elements returned by the specified iterator
	 * on the specified queue.
	 * Return the queue.
	 */
	public static <Q extends Queue<? super E>, E> Q enqueueAll(Q queue, Iterator<? extends E> iterator) {
		while (iterator.hasNext()) {
			queue.enqueue(iterator.next());
		}
		return queue;
	}

	/**
	 * Enqueue all the elements in the specified array
	 * on the specified queue.
	 * Return the queue.
	 */
	public static <Q extends Queue<? super E>, E> Q enqueueAll(Q queue, E... array) {
		for (E element : array) {
			queue.enqueue(element);
		}
		return queue;
	}

	/**
	 * Pop all the elements from the specified stack and enqueue them
	 * on the specified queue.
	 * Return the queue.
	 */
	public static <Q extends Queue<? super E>, E> Q enqueueAll(Q queue, Stack<? extends E> stack) {
		while ( ! stack.isEmpty()) {
			queue.enqueue(stack.pop());
		}
		return queue;
	}

	/**
	 * Dequeue all the elements from the second specified queue and enqueue them
	 * on the first specified queue.
	 * Return the first queue.
	 * @see #drainTo(Queue, Queue)
	 */
	public static <Q extends Queue<? super E>, E> Q enqueueAll(Q queue1, Queue<? extends E> queue2) {
		while ( ! queue2.isEmpty()) {
			queue1.enqueue(queue2.dequeue());
		}
		return queue1;
	}


	// ********** drain **********

	/**
	 * Drain all the elements from the specified queue and return them in a
	 * list.
	 */
	public static <E> ArrayList<E> drain(Queue<? extends E> queue) {
		return drainTo(queue, new ArrayList<E>());
	}

	/**
	 * Drain all the elements from the specified queue and add them to the
	 * specified collection.
	 * Return the collection.
	 */
	public static <C extends Collection<? super E>, E> C drainTo(Queue<? extends E> queue, C collection) {
		while ( ! queue.isEmpty()) {
			collection.add(queue.dequeue());
		}
		return collection;
	}

	/**
	 * Drain all the elements from the specified queue and push them on the
	 * specified stack.
	 * Return the stack.
	 */
	public static <S extends Stack<? super E>, E> S drainTo(Queue<? extends E> queue, S stack) {
		while ( ! queue.isEmpty()) {
			stack.push(queue.dequeue());
		}
		return stack;
	}

	/**
	 * Drain all the elements from the first specified queue and enqueue them
	 * on the second specified queue.
	 * Return the second queue.
	 * @see #enqueueAll(Queue, Queue)
	 */
	public static <Q extends Queue<? super E>, E> Q drainTo(Queue<? extends E> queue1, Q queue2) {
		while ( ! queue1.isEmpty()) {
			queue2.enqueue(queue1.dequeue());
		}
		return queue2;
	}


	// ********** factory methods **********

	/**
	 * Return an empty array-based FIFO queue.
	 */
	public static <E> ArrayQueue<E> queue() {
		return arrayQueue();
	}

	/**
	 * Return an empty array-based FIFO queue with specified initial capacity.
	 */
	public static <E> ArrayQueue<E> queue(int initialCapacity) {
		return arrayQueue(initialCapacity);
	}

	/**
	 * Return a FIFO queue corresponding to the specified iterable.
	 */
	public static <E> ArrayQueue<E> queue(Iterable<? extends E> iterable) {
		return arrayQueue(iterable);
	}

	/**
	 * Return a FIFO queue corresponding to the specified iterable.
	 * The specified iterable size is a performance hint.
	 */
	public static <E> ArrayQueue<E> queue(Iterable<? extends E> iterable, int iterableSize) {
		return arrayQueue(iterable, iterableSize);
	}

	/**
	 * Return a FIFO queue corresponding to the specified iterator.
	 */
	public static <E> ArrayQueue<E> queue(Iterator<? extends E> iterator) {
		return arrayQueue(iterator);
	}

	/**
	 * Return a FIFO queue corresponding to the specified iterator.
	 * The specified iterator size is a performance hint.
	 */
	public static <E> ArrayQueue<E> queue(Iterator<? extends E> iterator, int iteratorSize) {
		return arrayQueue(iterator, iteratorSize);
	}

	/**
	 * Return a FIFO queue corresponding to the specified array.
	 */
	public static <E> ArrayQueue<E> queue(E... array) {
		return arrayQueue(array);
	}

	/**
	 * Return an empty array-based FIFO queue.
	 */
	public static <E> ArrayQueue<E> arrayQueue() {
		return arrayQueue(10);
	}

	/**
	 * Return an empty array-based FIFO queue with specified initial capacity.
	 */
	public static <E> ArrayQueue<E> arrayQueue(int initialCapacity) {
		return new ArrayQueue<E>(initialCapacity);
	}

	/**
	 * Return an array-based FIFO queue corresponding to the specified iterable.
	 */
	public static <E> ArrayQueue<E> arrayQueue(Iterable<? extends E> iterable) {
		return arrayQueue(iterable.iterator());
	}

	/**
	 * Return an array-based FIFO queue corresponding to the specified iterable.
	 * The specified iterable size is a performance hint.
	 */
	public static <E> ArrayQueue<E> arrayQueue(Iterable<? extends E> iterable, int iterableSize) {
		return arrayQueue(iterable.iterator(), iterableSize);
	}

	/**
	 * Return an array-based FIFO queue corresponding to the specified iterator.
	 */
	public static <E> ArrayQueue<E> arrayQueue(Iterator<? extends E> iterator) {
		return enqueueAll(QueueTools.<E>arrayQueue(), iterator);
	}

	/**
	 * Return an array-based FIFO queue corresponding to the specified iterator.
	 * The specified iterator size is a performance hint.
	 */
	public static <E> ArrayQueue<E> arrayQueue(Iterator<? extends E> iterator, int iteratorSize) {
		return enqueueAll(QueueTools.<E>arrayQueue(iteratorSize), iterator);
	}

	/**
	 * Return an array-based FIFO queue corresponding to the specified array.
	 */
	public static <E> ArrayQueue<E> arrayQueue(E... array) {
		return enqueueAll(QueueTools.<E>arrayQueue(array.length), array);
	}

	/**
	 * Return an empty link-based FIFO queue with no node cache.
	 */
	public static <E> LinkedQueue<E> linkedQueue() {
		return linkedQueue(0);
	}

	/**
	 * Return an empty link-based FIFO queue
	 * with the specified node cache size.
	 * Specify a cache size of -1 for an unlimited cache.
	 */
	public static <E> LinkedQueue<E> linkedQueue(int cacheSize) {
		return new LinkedQueue<E>(cacheSize);
	}

	/**
	 * Return a link-based FIFO queue corresponding to the specified iterable.
	 */
	public static <E> LinkedQueue<E> linkedQueue(Iterable<? extends E> iterable) {
		return linkedQueue(iterable, 0);
	}

	/**
	 * Return a link-based FIFO queue corresponding to the specified iterable
	 * with the specified node cache size.
	 * Specify a cache size of -1 for an unlimited cache.
	 */
	public static <E> LinkedQueue<E> linkedQueue(Iterable<? extends E> iterable, int cacheSize) {
		return linkedQueue(iterable.iterator(), cacheSize);
	}

	/**
	 * Return a link-based FIFO queue corresponding to the specified iterator.
	 */
	public static <E> LinkedQueue<E> linkedQueue(Iterator<? extends E> iterator) {
		return linkedQueue(iterator, 0);
	}

	/**
	 * Return a link-based FIFO queue corresponding to the specified iterator
	 * with the specified node cache size.
	 * Specify a cache size of -1 for an unlimited cache.
	 */
	public static <E> LinkedQueue<E> linkedQueue(Iterator<? extends E> iterator, int cacheSize) {
		return enqueueAll(QueueTools.<E>linkedQueue(cacheSize), iterator);
	}

	/**
	 * Return a link-based FIFO queue corresponding to the specified array.
	 */
	public static <E> LinkedQueue<E> linkedQueue(E... array) {
		return linkedQueue(array, 0);
	}

	/**
	 * Return a link-based FIFO queue corresponding to the specified array
	 * with the specified node cache size.
	 * Specify a cache size of -1 for an unlimited cache.
	 */
	public static <E> LinkedQueue<E> linkedQueue(E[] array, int cacheSize) {
		return enqueueAll(QueueTools.<E>linkedQueue(cacheSize), array);
	}

	/**
	 * Return a fixed-size queue with the specified capacity.
	 */
	public static <E> FixedSizeArrayQueue<E> fixedSizeQueue(int capacity) {
		return new FixedSizeArrayQueue<E>(capacity);
	}

	/**
	 * Return a fized-size queue containing the elements of the specified
	 * collection. The queue will dequeue its elements in the same
	 * order they are returned by the collection's iterator (i.e. the
	 * first element returned by the collection's iterator will be the
	 * first element returned by {@link Queue#dequeue()}).
	 * The queue's capacity will be match the collection's size.
	 */
	public static <E> FixedSizeArrayQueue<E> fixedSizeQueue(Collection<? extends E> collection) {
		return enqueueAll(QueueTools.<E>fixedSizeQueue(collection.size()), collection);
	}

	/**
	 * Return a LIFO queue.
	 */
	public static <E> StackQueue<E> stackQueue() {
		return queue(new ArrayStack<E>());
	}

	/**
	 * Adapt the specified stack to the {@link Queue} interface,
	 * implementing a LIFO queue.
	 */
	public static <E> StackQueue<E> queue(Stack<E> stack) {
		return new StackQueue<E>(stack);
	}

	/**
	 * Return a priority queue that returns its elements in
	 * {@linkplain Comparable natural order}.
	 */
	public static <E> PriorityQueue<E> priorityQueue() {
		return queue((Comparator<? super E>) null);
	}

	/**
	 * Return a priority queue whose elements are returned in
	 * the order determined by the specified comparator.
	 * If the specified comparator is <code>null</code>, the elements will be
	 * returned in {@linkplain Comparable natural order}.
	 */
	public static <E> PriorityQueue<E> queue(Comparator<? super E> comparator) {
		return queue(new TreeSet<E>(comparator));
	}

	/**
	 * Adapt the specified sorted set to the {@link Queue} interface,
	 * implementing a priority queue.
	 */
	public static <E> PriorityQueue<E> queue(SortedSet<E> elements) {
		return new PriorityQueue<E>(elements);
	}

	/**
	 * Adapt the specified list to the {@link Queue} interface.
	 */
	public static <E> ListQueue<E> wrap(List<E> list) {
		return new ListQueue<E>(list);
	}

	/**
	 * Return a queue that synchronizes the specified queue
	 * with specified mutex.
	 */
	public static <E> SynchronizedQueue<E> synchronizedQueue(Queue<E> queue, Object mutex) {
		return new SynchronizedQueue<E>(queue, mutex);
	}

	/**
	 * Return a queue that synchronizes the specified queue.
	 */
	public static <E> SynchronizedQueue<E> synchronizedQueue(Queue<E> queue) {
		return new SynchronizedQueue<E>(queue);
	}

	/**
	 * Return an unmodifiable empty queue.
	 */
	public static <E> Queue<E> emptyQueue() {
		return EmptyQueue.<E>instance();
	}


	// ********** constructor **********

	/**
	 * Suppress default constructor, ensuring non-instantiability.
	 */
	private QueueTools() {
		super();
		throw new UnsupportedOperationException();
	}
}

Back to the top