PageRenderTime 23ms CodeModel.GetById 9ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

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

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 159 lines | 119 code | 18 blank | 22 comment | 12 complexity | ee39424ed03d2fc529a4acd14f922b5c 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.concurrent.BlockingQueue;
 23import java.util.concurrent.TimeUnit;
 24import java.util.concurrent.locks.Condition;
 25import java.util.concurrent.locks.Lock;
 26import java.util.concurrent.locks.ReentrantLock;
 27
 28public class ResponseQueueFactory {
 29    public static BlockingQueue newResponseQueue() {
 30        return new LockBasedResponseQueue();
 31    }
 32
 33    private final static class LockBasedResponseQueue extends AbstractQueue implements BlockingQueue {
 34        private Object response = null;
 35        private final Lock lock = new ReentrantLock();
 36        private final Condition noValue = lock.newCondition();
 37        private final static Object NULL = new Object();
 38
 39        public Object take() throws InterruptedException {
 40            lock.lock();
 41            try {
 42                //noinspection WhileLoopSpinsOnField
 43                while (response == null) {
 44                    noValue.await();
 45                }
 46                return getAndRemoveResponse();
 47            } finally {
 48                lock.unlock();
 49            }
 50        }
 51
 52        public boolean offer(Object o, long timeout, TimeUnit unit) throws InterruptedException {
 53            return offer(o);
 54        }
 55
 56        public Object poll(long timeout, TimeUnit unit) throws InterruptedException {
 57            if (timeout < 0) throw new IllegalArgumentException();
 58            long remaining = unit.toMillis(timeout);
 59            lock.lock();
 60            try {
 61                while (response == null && remaining > 0) {
 62                    long start = Clock.currentTimeMillis();
 63                    noValue.await(remaining, TimeUnit.MILLISECONDS);
 64                    remaining -= (Clock.currentTimeMillis() - start);
 65                }
 66                return getAndRemoveResponse();
 67            } finally {
 68                lock.unlock();
 69            }
 70        }
 71
 72        public void put(Object o) throws InterruptedException {
 73            offer(o);
 74        }
 75
 76        public boolean offer(Object obj) {
 77            if (obj == null) {
 78                obj = NULL;
 79            }
 80            lock.lock();
 81            try {
 82                if (response != null) {
 83                    return false;
 84                }
 85                response = obj;
 86                //noinspection CallToSignalInsteadOfSignalAll
 87                noValue.signal();
 88                return true;
 89            } finally {
 90                lock.unlock();
 91            }
 92        }
 93
 94        public Object poll() {
 95            lock.lock();
 96            try {
 97                return getAndRemoveResponse();
 98            } finally {
 99                lock.unlock();
100            }
101        }
102
103        /**
104         * Internal method, should be called under lock.
105         *
106         * @return response
107         */
108        private Object getAndRemoveResponse() {
109            final Object value = response;
110            response = null;
111            return (value == NULL) ? null : value;
112        }
113
114        public int remainingCapacity() {
115            throw new UnsupportedOperationException();
116        }
117
118        public int drainTo(Collection c) {
119            throw new UnsupportedOperationException();
120        }
121
122        public int drainTo(Collection c, int maxElements) {
123            throw new UnsupportedOperationException();
124        }
125
126        public void clear() {
127            lock.lock();
128            try {
129                response = null;
130            } finally {
131                lock.unlock();
132            }
133        }
134
135        @Override
136        public Iterator iterator() {
137            throw new UnsupportedOperationException();
138        }
139
140        @Override
141        public int size() {
142            lock.lock();
143            try {
144                return (response == null) ? 0 : 1;
145            } finally {
146                lock.unlock();
147            }
148        }
149
150        public Object peek() {
151            lock.lock();
152            try {
153                return response;
154            } finally {
155                lock.unlock();
156            }
157        }
158    }
159}