2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.api.openflow.md.util;
10 import java.util.ArrayList;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Queue;
15 import com.google.common.collect.Iterators;
18 * Zipper groups together a list of queues and exposes one poll method. Polling iterates through
19 * all groups and returns first not-null result of poll method on each queue. If after polling each
20 * grouped queue for one time there is still null result, poll will return null.
22 * Iterating keeps last position so this polling is supposed to be fairly distributed.
24 * @param <T> common item type of zipped queues
26 public class PollableQueuesZipper<T> {
28 private List<Queue<T>> sources;
29 private Iterator<Queue<T>> cursor;
34 public PollableQueuesZipper() {
35 sources = new ArrayList<>();
39 * Add all member queues before first invocation of {@link PollableQueuesZipper#poll()}
40 * @param queue to be added to group
42 public void addSource(Queue<T> queue) {
47 * @return next common product of polling member groups
52 cursor = Iterators.cycle(sources);
56 for (int i = 0; i < sources.size(); i++) {
57 queue = cursor.next();