| 1 | /* |
| 2 | * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. |
| 3 | * |
| 4 | * WSO2 Inc. licenses this file to you under the Apache License, |
| 5 | * Version 2.0 (the "License"); you may not use this file except |
| 6 | * in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, |
| 12 | * software distributed under the License is distributed on an |
| 13 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | * KIND, either express or implied. See the License for the |
| 15 | * specific language governing permissions and limitations |
| 16 | * under the License. |
| 17 | */ |
| 18 | package org.wso2.siddhi.core.util; |
| 19 | |
| 20 | import java.util.Iterator; |
| 21 | import java.util.concurrent.LinkedBlockingQueue; |
| 22 | |
| 23 | public class SchedulerQueue<T> { |
| 24 | private volatile boolean isScheduledForDispatching = false; |
| 25 | private LinkedBlockingQueue<T> linkedBlockingQueue = new LinkedBlockingQueue<T>(); |
| 26 | |
| 27 | public boolean put(T t) throws InterruptedException { |
| 28 | linkedBlockingQueue.put(t); |
| 29 | if (!isScheduledForDispatching) { |
| 30 | isScheduledForDispatching = true; |
| 31 | return false; |
| 32 | } |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | public T poll() { |
| 38 | T t = linkedBlockingQueue.poll(); |
| 39 | if (t == null) { |
| 40 | isScheduledForDispatching = false; |
| 41 | } |
| 42 | return t; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | public T take() throws InterruptedException { |
| 47 | return linkedBlockingQueue.take(); |
| 48 | } |
| 49 | |
| 50 | public T peek() { |
| 51 | T t = linkedBlockingQueue.peek(); |
| 52 | if (t == null) { |
| 53 | isScheduledForDispatching = false; |
| 54 | } |
| 55 | return t; |
| 56 | } |
| 57 | |
| 58 | public Iterator<T> iterator() { |
| 59 | return linkedBlockingQueue.iterator(); |
| 60 | } |
| 61 | |
| 62 | } |