Merge "Adding feature odl-openflowplugin-nxm-extensions and configs."
[openflowplugin.git] / openflowplugin-api / src / main / java / org / opendaylight / openflowplugin / api / openflow / md / util / PollableQueuesPriorityZipper.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  * 
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.api.openflow.md.util;
9
10 import java.util.Queue;
11
12 /**
13  * Zipper groups together a list of queues and exposes one poll method. Polling
14  * iterates through all groups and returns first not-null result of poll method
15  * on each queue. If after polling each grouped queue for one time there is
16  * still null result, poll will return null. <br/>
17  * Iterating keeps last position so this polling is supposed to be fairly
18  * distributed.
19  * 
20  * @param <T>
21  *            common item type of zipped queues
22  */
23 public class PollableQueuesPriorityZipper<T> {
24
25     private Queue<T> prioritizedSource;
26     private PollableQueuesZipper<T> zipper;
27
28     /**
29      * default ctor
30      */
31     public PollableQueuesPriorityZipper() {
32         zipper = new PollableQueuesZipper<>();
33     }
34
35     /**
36      * Add all member queues before first invocation of
37      * {@link PollableQueuesPriorityZipper#poll()}
38      * 
39      * @param queue
40      *            to be added to group
41      */
42     public void addSource(Queue<T> queue) {
43         zipper.addSource(queue);
44     }
45
46     /**
47      * @return next common product of polling member groups
48      */
49     public T poll() {
50         T item = null;
51
52         item = prioritizedSource.poll();
53         if (item == null) {
54             item = zipper.poll();
55         }
56
57         return item;
58     }
59
60     public void setPrioritizedSource(Queue<T> prioritizedSource) {
61         this.prioritizedSource = prioritizedSource;
62     }
63 }