01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.foundation;
22:
23: /**
24: * @exclude
25: */
26: public class BlockingQueue implements Queue4 {
27:
28: protected NonblockingQueue _queue = new NonblockingQueue();
29:
30: protected Lock4 _lock = new Lock4();
31:
32: protected boolean _stopped;
33:
34: public void add(final Object obj) {
35: _lock.run(new SafeClosure4() {
36: public Object run() {
37: _queue.add(obj);
38: _lock.awake();
39: return null;
40: }
41: });
42: }
43:
44: public boolean hasNext() {
45: Boolean hasNext = (Boolean) _lock.run(new SafeClosure4() {
46: public Object run() {
47: return new Boolean(_queue.hasNext());
48: }
49: });
50: return hasNext.booleanValue();
51: }
52:
53: public Iterator4 iterator() {
54: return (Iterator4) _lock.run(new SafeClosure4() {
55: public Object run() {
56: return _queue.iterator();
57: }
58: });
59: }
60:
61: public Object next() throws BlockingQueueStoppedException {
62: return _lock.run(new SafeClosure4() {
63: public Object run() {
64: if (_queue.hasNext()) {
65: return _queue.next();
66: }
67: if (_stopped) {
68: throw new BlockingQueueStoppedException();
69: }
70: _lock.snooze(Integer.MAX_VALUE);
71: Object obj = _queue.next();
72: if (obj == null) {
73: throw new BlockingQueueStoppedException();
74: }
75: return obj;
76: }
77: });
78: }
79:
80: public void stop() {
81: _lock.run(new SafeClosure4() {
82: public Object run() {
83: _stopped = true;
84: _lock.awake();
85: return null;
86: }
87: });
88: }
89: }
|