Skip to main content
summaryrefslogtreecommitdiffstats
blob: 02e026c7db1692bc31bfa71bee9aac1815133d19 (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
/*******************************************************************************
 * Copyright (c) 2009 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;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;

/**
 * Straightforward implementation of the {@link Queue} interface.
 */
public class SimpleQueue<E>
	implements Queue<E>, Cloneable, Serializable
{
	private LinkedList<E> elements;

	private static final long serialVersionUID = 1L;


	// ********** constructors **********

	/**
	 * Construct an empty queue.
	 */
	public SimpleQueue() {
		super();
		this.elements = new LinkedList<E>();
	}

	/**
	 * Construct a 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 #dequeue()}).
	 */
	public SimpleQueue(Collection<? extends E> c) {
		super();
		this.elements = new LinkedList<E>(c);
	}


	// ********** Queue implementation **********

	public void enqueue(E o) {
		this.elements.addLast(o);
	}

	public E dequeue() {
		return this.elements.removeFirst();
	}

	public E peek() {
		return this.elements.getFirst();
	}

	public boolean isEmpty() {
		return this.elements.isEmpty();
	}


	// ********** Cloneable implementation **********

	@Override
	public SimpleQueue<E> clone() {
		try {
			@SuppressWarnings("unchecked")
			SimpleQueue<E> clone = (SimpleQueue<E>) super.clone();
			@SuppressWarnings("unchecked")
			LinkedList<E> ll = (LinkedList<E>) this.elements.clone();
			clone.elements = ll;
			return clone;
		} catch (CloneNotSupportedException ex) {
			throw new InternalError();
		}
	}

	@Override
	public String toString() {
		return StringTools.buildToStringFor(this, this.peek());
	}

}

Back to the top