/******************************************************************************* * 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.utility.internal; import java.io.Serializable; import java.util.Collection; import java.util.LinkedList; /** * Straightforward implementation of the {@link Queue} interface. */ public class SimpleQueue implements Queue, Cloneable, Serializable { private LinkedList elements; private static final long serialVersionUID = 1L; // ********** constructors ********** /** * Construct an empty queue. */ public SimpleQueue() { super(); this.elements = new LinkedList(); } /** * 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 c) { super(); this.elements = new LinkedList(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 clone() { try { @SuppressWarnings("unchecked") SimpleQueue clone = (SimpleQueue) super.clone(); @SuppressWarnings("unchecked") LinkedList ll = (LinkedList) this.elements.clone(); clone.elements = ll; return clone; } catch (CloneNotSupportedException ex) { throw new InternalError(); } } @Override public String toString() { return StringTools.buildToStringFor(this, this.peek()); } }