View Javadoc
1   package locklib;
2   
3   import java.util.Collections;
4   import java.util.HashSet;
5   import java.util.Iterator;
6   import java.util.LinkedList;
7   import java.util.Set;
8   
9   import ensure.Ensure;
10  
11  /**
12   * Wait queue that keeps requests in a FIFO queue.
13   * @param <LockType> the type of locks
14   */
15  public class FifoWaitQueue<LockType> extends WaitQueue<LockType> {
16  	/**
17  	 * Queued requests.
18  	 */
19  	private LinkedList<LockGrantFuture<LockType>> m_queue;
20  	
21  	/**
22  	 * Set with all lock requests in the queue.
23  	 */
24  	private Set<LockGrantFuture<LockType>> m_set;
25  	
26  	/**
27  	 * Creates a new wait queue.
28  	 */
29  	public FifoWaitQueue() {
30  		m_queue = new LinkedList<>();
31  		m_set = new HashSet<>();
32  	}
33  	
34  	@Override
35  	public Iterator<LockGrantFuture<LockType>> iterator() {
36  		return Collections.unmodifiableList(m_queue).iterator();
37  	}
38  
39  	@Override
40  	protected void enqueue(LockGrantFuture<LockType> r) {
41  		Ensure.not_null(r, "r != null");
42  		Ensure.is_false(m_set.contains(r), "m_set.contains(r)");
43  		m_queue.addLast(r);
44  		m_set.add(r);
45  	}
46  
47  	@Override
48  	protected void dequeue(LockGrantFuture<LockType> r) {
49  		Ensure.not_null(r, "r == null");
50  		m_queue.remove(r);
51  		m_set.remove(r);
52  	}
53  }