in progress...
[jalview.git] / forester / java / src / org / forester / datastructures / Queue.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.datastructures;
27
28 import java.util.LinkedList;
29 import java.util.NoSuchElementException;
30
31 /*
32  * A simple Queue data structure. Created: 10/23/2005 by Christian M. Zmasek.
33  * Last modified: 10/23/2005 by Christian M. Zmasek.
34  *
35  * @author Christian M. Zmasek
36  *
37  * @version 1.000
38  */
39 public class Queue {
40
41     // Instance variables
42     // ------------------
43     private final LinkedList<Object> _data;
44
45     // Constructor
46     // -----------
47     /**
48      * This created a new, empty Queue object.
49      */
50     public Queue() {
51         _data = new LinkedList<Object>();
52     }
53
54     /**
55      * Removes all elements from this queue.
56      */
57     public void clear() {
58         getData().clear();
59     }
60
61     /**
62      * Dequeues one element from this queue.
63      *
64      * @return the dequeued object
65      * @throws NoSuchElementException
66      *             if this queue is empty
67      */
68     public Object dequeue() throws NoSuchElementException {
69         if ( isEmpty() ) {
70             throw new NoSuchElementException( "Attempt to dequeue from an empty Queue." );
71         }
72         return getData().removeFirst();
73     }
74
75     // Public methods
76     // --------------
77     /**
78      * Adds Object element to thisqueue.
79      *
80      * @param element
81      *            the Object to be enqueued
82      */
83     public void enqueue( final Object element ) {
84         getData().add( element );
85     }
86
87     // Private methods
88     // ---------------
89     /**
90      * Returns the LinkedList upon which this queue is based.
91      *
92      * @return the LinkedList upon which this queue is based
93      */
94     private LinkedList<Object> getData() {
95         return _data;
96     }
97
98     /**
99      * Returns whether or not this queue is empty.
100      *
101      * @return true if this queue is empty, false otherwise
102      */
103     public boolean isEmpty() {
104         return getData().isEmpty();
105     }
106 } // end of class Queue.