1 16 package org.apache.commons.collections.buffer; 17 18 import java.util.Collection ; 19 20 import org.apache.commons.collections.Buffer; 21 import org.apache.commons.collections.BufferUnderflowException; 22 23 46 public class BlockingBuffer extends SynchronizedBuffer { 47 48 49 private static final long serialVersionUID = 1719328905017860541L; 50 51 58 public static Buffer decorate(Buffer buffer) { 59 return new BlockingBuffer(buffer); 60 } 61 62 69 protected BlockingBuffer(Buffer buffer) { 70 super(buffer); 71 } 72 73 public boolean add(Object o) { 75 synchronized (lock) { 76 boolean result = collection.add(o); 77 notifyAll(); 78 return result; 79 } 80 } 81 82 public boolean addAll(Collection c) { 83 synchronized (lock) { 84 boolean result = collection.addAll(c); 85 notifyAll(); 86 return result; 87 } 88 } 89 90 public Object get() { 91 synchronized (lock) { 92 while (collection.isEmpty()) { 93 try { 94 wait(); 95 } catch (InterruptedException e) { 96 throw new BufferUnderflowException(); 97 } 98 } 99 return getBuffer().get(); 100 } 101 } 102 103 public Object remove() { 104 synchronized (lock) { 105 while (collection.isEmpty()) { 106 try { 107 wait(); 108 } catch (InterruptedException e) { 109 throw new BufferUnderflowException(); 110 } 111 } 112 return getBuffer().remove(); 113 } 114 } 115 116 } 117 | Popular Tags |