1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  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 org.apache.commons.collections.buffer;
  17. import java.util.Collection;
  18. import org.apache.commons.collections.Buffer;
  19. import org.apache.commons.collections.BufferUnderflowException;
  20. /**
  21. * Decorates another <code>Buffer</code> to make {@link #get()} and
  22. * {@link #remove()} block when the <code>Buffer</code> is empty.
  23. * <p>
  24. * If either <code>get</code> or <code>remove</code> is called on an empty
  25. * <code>Buffer</code>, the calling thread waits for notification that
  26. * an <code>add</code> or <code>addAll</code> operation has completed.
  27. * <p>
  28. * When one or more entries are added to an empty <code>Buffer</code>,
  29. * all threads blocked in <code>get</code> or <code>remove</code> are notified.
  30. * There is no guarantee that concurrent blocked <code>get</code> or
  31. * <code>remove</code> requests will be "unblocked" and receive data in the
  32. * order that they arrive.
  33. * <p>
  34. * This class is Serializable from Commons Collections 3.1.
  35. *
  36. * @since Commons Collections 3.0
  37. * @version $Revision: 1.7 $ $Date: 2004/06/03 22:02:13 $
  38. *
  39. * @author Stephen Colebourne
  40. * @author Janek Bogucki
  41. * @author Phil Steitz
  42. */
  43. public class BlockingBuffer extends SynchronizedBuffer {
  44. /** Serialization version */
  45. private static final long serialVersionUID = 1719328905017860541L;
  46. /**
  47. * Factory method to create a blocking buffer.
  48. *
  49. * @param buffer the buffer to decorate, must not be null
  50. * @return a new blocking Buffer
  51. * @throws IllegalArgumentException if buffer is null
  52. */
  53. public static Buffer decorate(Buffer buffer) {
  54. return new BlockingBuffer(buffer);
  55. }
  56. //-----------------------------------------------------------------------
  57. /**
  58. * Constructor that wraps (not copies).
  59. *
  60. * @param buffer the buffer to decorate, must not be null
  61. * @throws IllegalArgumentException if the buffer is null
  62. */
  63. protected BlockingBuffer(Buffer buffer) {
  64. super(buffer);
  65. }
  66. //-----------------------------------------------------------------------
  67. public boolean add(Object o) {
  68. synchronized (lock) {
  69. boolean result = collection.add(o);
  70. notifyAll();
  71. return result;
  72. }
  73. }
  74. public boolean addAll(Collection c) {
  75. synchronized (lock) {
  76. boolean result = collection.addAll(c);
  77. notifyAll();
  78. return result;
  79. }
  80. }
  81. public Object get() {
  82. synchronized (lock) {
  83. while (collection.isEmpty()) {
  84. try {
  85. wait();
  86. } catch (InterruptedException e) {
  87. throw new BufferUnderflowException();
  88. }
  89. }
  90. return getBuffer().get();
  91. }
  92. }
  93. public Object remove() {
  94. synchronized (lock) {
  95. while (collection.isEmpty()) {
  96. try {
  97. wait();
  98. } catch (InterruptedException e) {
  99. throw new BufferUnderflowException();
  100. }
  101. }
  102. return getBuffer().remove();
  103. }
  104. }
  105. }