| 1 |
/* |
| 2 |
* Copyright 1999-2010 University of Chicago |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 |
* express or implied. |
| 12 |
* |
| 13 |
* See the License for the specific language governing permissions and limitations under the License. |
| 14 |
*/ |
| 15 |
package org.globus.util; |
| 16 |
|
| 17 |
public class CircularBuffer { |
| 18 |
|
| 19 |
protected Object[] buf; |
| 20 |
protected int in = 0; |
| 21 |
protected int out= 0; |
| 22 |
protected int count= 0; |
| 23 |
protected int size; |
| 24 |
|
| 25 |
protected boolean interruptPut = false; |
| 26 |
protected boolean interruptGet = false; |
| 27 |
protected boolean closePut = false; |
| 28 |
|
| 29 |
public CircularBuffer(int size) { |
| 30 |
this.size = size; |
| 31 |
buf = new Object[size]; |
| 32 |
} |
| 33 |
|
| 34 |
public synchronized boolean isEmpty() { |
| 35 |
return (this.count == 0); |
| 36 |
} |
| 37 |
|
| 38 |
public synchronized boolean put(Object o) |
| 39 |
throws InterruptedException { |
| 40 |
if (this.interruptPut) { |
| 41 |
return false; |
| 42 |
} |
| 43 |
while (count==size) { |
| 44 |
wait(); |
| 45 |
if (this.interruptPut) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
} |
| 49 |
buf[in] = o; |
| 50 |
++count; |
| 51 |
in=(in+1) % size; |
| 52 |
notify(); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
public synchronized Object get() |
| 57 |
throws InterruptedException { |
| 58 |
if (this.interruptGet) { |
| 59 |
return null; |
| 60 |
} |
| 61 |
while (count==0) { |
| 62 |
if (this.closePut) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
wait(); |
| 66 |
if (this.interruptGet) { |
| 67 |
return null; |
| 68 |
} |
| 69 |
} |
| 70 |
Object o =buf[out]; |
| 71 |
buf[out]=null; |
| 72 |
--count; |
| 73 |
out=(out+1) % size; |
| 74 |
notify(); |
| 75 |
return (o); |
| 76 |
} |
| 77 |
|
| 78 |
public synchronized void closePut() { |
| 79 |
this.closePut = true; |
| 80 |
notifyAll(); |
| 81 |
} |
| 82 |
|
| 83 |
public synchronized boolean isPutClosed() { |
| 84 |
return this.closePut; |
| 85 |
} |
| 86 |
|
| 87 |
public synchronized void interruptBoth() { |
| 88 |
this.interruptGet = true; |
| 89 |
this.interruptPut = true; |
| 90 |
notifyAll(); |
| 91 |
} |
| 92 |
|
| 93 |
public synchronized void interruptGet() { |
| 94 |
this.interruptGet = true; |
| 95 |
notifyAll(); |
| 96 |
} |
| 97 |
|
| 98 |
public synchronized void interruptPut() { |
| 99 |
this.interruptPut = true; |
| 100 |
notifyAll(); |
| 101 |
} |
| 102 |
|
| 103 |
public synchronized boolean isGetInterrupted() { |
| 104 |
return this.interruptGet; |
| 105 |
} |
| 106 |
|
| 107 |
public synchronized boolean isPutInterrupted() { |
| 108 |
return this.interruptPut; |
| 109 |
} |
| 110 |
|
| 111 |
} |