Statistics
| Revision:

gvsig-raster / org.gvsig.raster.cache / trunk / org.gvsig.raster.cache / org.gvsig.raster.cache.lib.impl / src / main / java / org / gvsig / raster / cache / tile / impl / pool / TilePipeImpl.java @ 990

History | View | Annotate | Download (2.6 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.raster.cache.tile.impl.pool;
23

    
24
import java.util.LinkedList;
25
import java.util.Queue;
26

    
27
import org.gvsig.raster.cache.tile.Tile;
28
import org.gvsig.raster.cache.tile.pool.AtomicTask;
29
import org.gvsig.raster.cache.tile.pool.TilePipe;
30

    
31
/**
32
 * This class manages transactions between the producer and the consumer. The
33
 * producer download tiles from a server and the consumer is a tile provider
34
 * which will return downloaded tiles.
35
 *
36
 * @author Nacho Brodin (nachobrodin@gmail.com)
37
 */
38
public class TilePipeImpl implements TilePipe {
39
        private boolean              isEmpty        = true;
40
        private Queue<AtomicTask>    buffer         = new LinkedList<AtomicTask>();
41
        private boolean              debug          = false;
42
        
43
        public TilePipeImpl() {
44
                this.debug = false;
45
        }
46
        
47
        public TilePipeImpl(boolean debug) {
48
                this.debug = debug;
49
        }
50
        
51
        /**
52
         * Sets a tile in the pipe
53
         * @param tile
54
         */
55
        public synchronized void setAtomicTask(AtomicTask task) {
56
                if(task != null)
57
                        buffer.add(task);
58
                isEmpty = false;
59
                
60
                if(debug)
61
                        System.err.println("Setting tile " + ((Tile)task).getCol() + " " + ((Tile)task).getRow());
62
                
63
                notify();
64
        }
65

    
66
        /**
67
         * Gets a tile from the pipe
68
         * @return
69
         */
70
        public synchronized AtomicTask getAtomicTask() {
71
                if(debug)
72
                        System.out.println("0-Getting tile ");
73
                while( isEmpty == true ) {
74
                        try {
75
                                wait();
76
                        } catch( InterruptedException e ) {
77
                        }
78
                }
79

    
80
                AtomicTask task = buffer.poll() ;
81
                
82
                if(debug)
83
                        System.out.println("1-Getting tile " + ((Tile)task).getCol() + " " + ((Tile)task).getRow());
84
                
85
                if( getSize() == 0 )
86
                        isEmpty = true;
87

    
88
                return task;
89
        }
90
        
91
        /**
92
         * Returns the number of elements in the queue
93
         * @return
94
         */
95
        public synchronized int getSize() {
96
                return buffer.size();
97
        }
98
}