View Javadoc
1   package eu.simuline.util;
2   
3   import java.util.Vector;
4   import java.util.Collection;
5   import java.util.NoSuchElementException;
6   import java.util.Queue;
7   
8   /**
9    * The FIFOList class represents a first-in-first-out (FIFO) stack of objects. 
10   * <!--It extends class Vector with five operations 
11   * that allow a vector to be treated as a stack. --> 
12   * The usual push and pop operations are provided, 
13   * as well as a method to peek at the top item on the stack, 
14   * a method to test for whether the stack is empty, 
15   * and a method to search the stack for an item 
16   * and discover how far it is from the top.
17   * <p>
18   * When a stack is first created, it contains no items.
19   *
20   * @param <E>
21   *    the class of the elements of this list. 
22   *
23   * @author <a href="mailto:ernst.reissner@simuline.eu">Ernst Reissner</a>
24   * @version 1.0
25   */
26  public final class FIFOList<E> extends Vector<E> implements Queue<E> {
27  
28      /* -------------------------------------------------------------------- *
29       * constants.                                                           *
30       * -------------------------------------------------------------------- */
31  
32      private static final long serialVersionUID = -2479143000061671589L;
33  
34      /* -------------------------------------------------------------------- *
35       * constructors.                                                        *
36       * -------------------------------------------------------------------- */
37  
38      /**
39       * Creates an empty FIFOList.
40       */
41      public FIFOList() {
42  	// is empty. 
43      }
44  
45      /**
46       * Constructs a vector 
47       * containing the elements of the specified collection, 
48       * in the order they are returned by the collection's iterator. 
49       *
50       * @param coll
51       *    the collection whose elements are to be placed into this FIFOList. 
52       * @throws NullPointerException 
53       *    if the specified collection is null.     
54       */
55      protected FIFOList(Collection<? extends E> coll) {
56  	super(coll);
57      } // FIFOList constructor
58  
59  
60      public static <E> FIFOList<E> create() {
61  	return new FIFOList<E>();
62      }
63  
64      public static <E> FIFOList<E> create(Collection<? extends E> coll) {
65  	return new FIFOList<E>(coll);
66      }
67  
68      /* -------------------------------------------------------------------- *
69       * methods.                                                             *
70       * -------------------------------------------------------------------- */
71  
72      /**
73       * Constructs a vector. 
74       *
75       * @param item
76       *    the element to insert. 
77       * @return 
78       *    true since nothing can prevent the item from being inserted. 
79       */
80      public boolean offer(E item) {
81  	add(item);
82  	return true;
83      }
84  
85      // api-docs provided by javadoc. 
86      public E poll() {
87  	return isEmpty() ? null : remove(0);
88      }
89  
90      // api-docs provided by javadoc. 
91      public E remove() {
92  	if (isEmpty()) {
93  	    throw new NoSuchElementException();
94  	}
95  	return remove(0);
96      }
97  
98      // api-docs provided by javadoc. 
99      public E peek() {
100 	return isEmpty() ? null : get(0);
101     }
102 
103     // api-docs provided by javadoc. 
104     public E element() {
105 	if (isEmpty()) {
106 	    throw new NoSuchElementException();
107 	}
108 	return get(0);
109     }
110 } // FIFOList