PageRenderTime 42ms CodeModel.GetById 7ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/util/SimpleBlockingQueue.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 162 lines | 123 code | 23 blank | 16 comment | 21 complexity | 7a487d7e41ac9dc5284735a7f969f29d MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package com.hazelcast.util;
 18
 19import java.util.AbstractQueue;
 20import java.util.Collection;
 21import java.util.Iterator;
 22import java.util.LinkedList;
 23import java.util.concurrent.BlockingQueue;
 24import java.util.concurrent.TimeUnit;
 25
 26public class SimpleBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E> {
 27
 28    private final Object lock = new Object();
 29    private final LinkedList<E> items = new LinkedList<E>();
 30    private final LinkedList<E> prioritizedItems;
 31
 32    public SimpleBlockingQueue() {
 33        this(false);
 34    }
 35
 36    public SimpleBlockingQueue(boolean priorityAware) {
 37        prioritizedItems = (priorityAware) ? new LinkedList<E>() : null;
 38    }
 39
 40    public boolean offer(E e) {
 41        put(e);
 42        return true;
 43    }
 44
 45    public void put(E e) {
 46        synchronized (lock) {
 47            if (prioritizedItems != null && e instanceof Prioritized) {
 48                prioritizedItems.add(e);
 49            } else {
 50                items.add(e);
 51            }
 52            //noinspection CallToNotifyInsteadOfNotifyAll
 53            lock.notify();
 54        }
 55    }
 56
 57    public boolean remove(Object obj) {
 58        synchronized (lock) {
 59            boolean removed = items.remove(obj);
 60            if (!removed && prioritizedItems != null) {
 61                removed = prioritizedItems.remove(obj);
 62            }
 63            return removed;
 64        }
 65    }
 66
 67    public E take() throws InterruptedException {
 68        return poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
 69    }
 70
 71    public E poll() {
 72        synchronized (lock) {
 73            return removeFirst();
 74        }
 75    }
 76
 77    private E removeFirst() {
 78        E e = null;
 79        if (prioritizedItems != null && prioritizedItems.size() > 0) {
 80            e = prioritizedItems.removeFirst();
 81        } else if (items.size() > 0) {
 82            e = items.removeFirst();
 83        }
 84        return e;
 85    }
 86
 87    private int totalSize() {
 88        return items.size() + ((prioritizedItems == null) ? 0 : prioritizedItems.size());
 89    }
 90
 91    @SuppressWarnings("CallToNativeMethodWhileLocked")
 92    public E poll(long timeout, TimeUnit unit) throws InterruptedException {
 93        long timeLeft = unit.toMillis(timeout);
 94        long start = Clock.currentTimeMillis();
 95        synchronized (lock) {
 96            E e = removeFirst();
 97            while (e == null && timeLeft > 0) {
 98                lock.wait(timeLeft);
 99                e = removeFirst();
100                long now = Clock.currentTimeMillis();
101                timeLeft -= (now - start);
102                start = now;
103            }
104            return e;
105        }
106    }
107
108    public void clear() {
109        synchronized (lock) {
110            items.clear();
111            if (prioritizedItems != null) {
112                prioritizedItems.clear();
113            }
114        }
115    }
116
117    public boolean add(E e) {
118        put(e);
119        return true;
120    }
121
122    public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
123        put(e);
124        return true;
125    }
126
127    public int remainingCapacity() {
128        return Integer.MAX_VALUE;
129    }
130
131    public int drainTo(Collection<? super E> c) {
132        return drainTo(c, Integer.MAX_VALUE);
133    }
134
135    public int drainTo(Collection<? super E> c, int maxElements) {
136        synchronized (lock) {
137            int count = 0;
138            E removed = removeFirst();
139            while (removed != null && count > maxElements) {
140                c.add(removed);
141                removed = removeFirst();
142            }
143            return count;
144        }
145    }
146
147    public E peek() {
148        throw new UnsupportedOperationException();
149    }
150
151    @Override
152    public Iterator<E> iterator() {
153        throw new UnsupportedOperationException();
154    }
155
156    @Override
157    public int size() {
158        synchronized (lock) {
159            return totalSize();
160        }
161    }
162}