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