Fix checkstyle api.openflow.md.util
[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> common item type of zipped queues
21  */
22 public class PollableQueuesPriorityZipper<T> {
23
24     private Queue<T> prioritizedSource;
25     private PollableQueuesZipper<T> zipper;
26
27     public PollableQueuesPriorityZipper() {
28         zipper = new PollableQueuesZipper<>();
29     }
30
31     /**
32      * Add all member queues before first invocation of {@link PollableQueuesPriorityZipper#poll()}.
33      * @param queue to be added to group
34      */
35     public void addSource(Queue<T> queue) {
36         zipper.addSource(queue);
37     }
38
39     /**
40      * Next common product.
41      * @return next common product of polling member groups
42      */
43     public T poll() {
44         T item = null;
45
46         item = prioritizedSource.poll();
47         if (item == null) {
48             item = zipper.poll();
49         }
50
51         return item;
52     }
53
54     public void setPrioritizedSource(Queue<T> prioritizedSource) {
55         this.prioritizedSource = prioritizedSource;
56     }
57 }