01: /*
02: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
03:
04: This program is free software; you can redistribute it and/or
05: modify it under the terms of the GNU General Public License
06: as published by the Free Software Foundation; either version 2
07: of the License, or (at your option) any later version.
08:
09: This program is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with this program; if not, write to the Free Software
16: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17: */
18:
19: package org.homedns.dade.jcgrid.util;
20:
21: import java.io.*;
22: import java.util.*;
23:
24: public class Queue {
25: protected Vector elements;
26:
27: public Queue() {
28: elements = new Vector();
29: }
30:
31: public Object pop() throws InterruptedException {
32: return this .pop(null);
33: }
34:
35: public Object pop(long timeout) throws InterruptedException {
36: return this .pop(null, timeout);
37: }
38:
39: public Object pop(FilterQueueObject fqo)
40: throws InterruptedException {
41: return this .pop(fqo, 0);
42: }
43:
44: public Object pop(FilterQueueObject fqo, long timeout)
45: throws InterruptedException {
46: Object qo = null;
47: boolean found = false;
48: boolean checkAndExit = false;
49:
50: synchronized (elements) {
51: while (!found) {
52: for (int i = 0; i < elements.size(); i++) {
53: Object o = (Object) elements.elementAt(i);
54:
55: if ((fqo == null) || (fqo.isValid(o))) {
56: elements.removeElementAt(i);
57: qo = o;
58: found = true;
59: break;
60: }
61: }
62:
63: if (checkAndExit)
64: break;
65:
66: if (!found) {
67: elements.wait(timeout);
68:
69: if (timeout > 0)
70: checkAndExit = true;
71: }
72: }
73: }
74:
75: return qo;
76: }
77:
78: public void push(Object qo) {
79: synchronized (elements) {
80: elements.add(qo);
81: elements.notifyAll();
82: }
83: }
84:
85: public int size() {
86: synchronized (elements) {
87: return elements.size();
88: }
89: }
90: }
|