Revision 20749

View differences:

trunk/extensions/extRasterTools-SE/src/org/gvsig/raster/util/Queue.java
1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.util;
20

  
21
/*
22
 * Created on 10-mar-2006
23
 *
24
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
25
 *
26
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
41
 *
42
 * For more information, contact:
43
 */
44

  
45
import java.util.Vector;
46

  
47
/**
48
 * A simple FIFO queue class which causes the calling thread to wait if the
49
 * queue is empty and notifies threads that are waiting when it is not
50
 * empty.
51
 * 
52
 * @author Anil V (akv@eng.sun.com)
53
 */
54
public class Queue {
55
	private Vector vector = new Vector();
56

  
57
	/**
58
	 * Put the object into the queue.
59
	 * 
60
	 * @param object
61
	 *            the object to be appended to the queue.
62
	 */
63
	public synchronized void put(Object object) {
64
		vector.addElement(object);
65
		notify();
66
	}
67

  
68
	/**
69
	 * Pull the first object out of the queue. Wait if the queue is empty.
70
	 */
71
	public synchronized Object pull() {
72
		while (isEmpty())
73
			try {
74
				wait();
75
			} catch (InterruptedException ex) {
76
			}
77
			return get();
78
	}
79

  
80
	/**
81
	 * Get the first object out of the queue. Return null if the queue is
82
	 * empty.
83
	 */
84
	public synchronized Object get() {
85
		Object object = peek();
86
		if (object != null)
87
			vector.removeElementAt(0);
88
		return object;
89
	}
90

  
91
	/**
92
	 * Peek to see if something is available.
93
	 */
94
	public Object peek() {
95
		if (isEmpty())
96
			return null;
97
		return vector.elementAt(0);
98
	}
99

  
100
	/**
101
	 * Is the queue empty?
102
	 */
103
	public boolean isEmpty() {
104
		return vector.isEmpty();
105
	}
106

  
107
	/**
108
	 * How many elements are there in this queue?
109
	 */
110
	public int size() {
111
		return vector.size();
112
	}
113
}

Also available in: Unified diff