Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / Queue.java @ 40561

History | View | Annotate | Download (2.63 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id: Queue.java 29631 2009-06-29 16:56:19Z jpiera $
27
* $Log$
28
* Revision 1.1  2006-03-14 19:23:07  azabala
29
* *** empty log message ***
30
*
31
*
32
*/
33
package org.gvsig.utils;
34
import java.util.Vector;
35

    
36
        /**
37
         * A simple FIFO queue class which causes the calling thread to wait if the
38
         * queue is empty and notifies threads that are waiting when it is not
39
         * empty.
40
         * 
41
         * @author Anil V (akv@eng.sun.com)
42
         */
43
        public class Queue {
44
            private Vector vector = new Vector();
45

    
46
            /**
47
                 * Put the object into the queue.
48
                 * 
49
                 * @param object
50
                 *            the object to be appended to the queue.
51
                 */
52
            public synchronized void put(Object object) {
53
                vector.addElement(object);
54
                notify();
55
            }
56

    
57
            /**
58
                 * Pull the first object out of the queue. Wait if the queue is empty.
59
                 */
60
            public synchronized Object pull() {
61
                while (isEmpty())
62
                    try {
63
                        wait();
64
                    } catch (InterruptedException ex) {
65
                    }
66
                return get();
67
            }
68

    
69
            /**
70
                 * Get the first object out of the queue. Return null if the queue is
71
                 * empty.
72
                 */
73
            public synchronized Object get() {
74
                Object object = peek();
75
                if (object != null)
76
                    vector.removeElementAt(0);
77
                return object;
78
            }
79

    
80
            /**
81
                 * Peek to see if something is available.
82
                 */
83
            public Object peek() {
84
                if (isEmpty())
85
                    return null;
86
                return vector.elementAt(0);
87
            }
88

    
89
            /**
90
                 * Is the queue empty?
91
                 */
92
            public boolean isEmpty() {
93
                return vector.isEmpty();
94
            }
95

    
96
            /**
97
                 * How many elements are there in this queue?
98
                 */
99
            public int size() {
100
                return vector.size();
101
            }
102
        }
103

    
104