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 / memory / LRUTileCache.java @ 995

History | View | Annotate | Download (4.6 KB)

1
/* gvSIG Mini. A free mobile phone viewer of free maps.
2
 *
3
 * Copyright (C) 2009 Prodevelop.
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
 * For more information, contact:
20
 *
21
 *   Prodevelop, S.L.
22
 *   Pza. Don Juan de Villarrasa, 14 - 5
23
 *   46001 Valencia
24
 *   Spain
25
 *
26
 *   +34 963 510 612
27
 *   +34 963 510 968
28
 *   prode@prodevelop.es
29
 *   http://www.prodevelop.es
30
 *
31
 *   gvSIG Mini has been partially funded by IMPIVA (Instituto de la Peque�a y
32
 *   Mediana Empresa de la Comunidad Valenciana) &
33
 *   European Union FEDER funds.
34
 *   
35
 *   2009.
36
 *   author Rub�n Blanco rblanco@prodevelop.es
37
 *
38
 *
39
 * Original version of the code made by Nicolas Gramlich.
40
 * No header license code found on the original source file.
41
 * 
42
 * Original source code downloaded from http://code.google.com/p/osmdroid/
43
 * package org.andnav.osm.views.util;
44
 * 
45
 * License stated in that website is 
46
 * http://www.gnu.org/licenses/lgpl.html
47
 * As no specific LGPL version was specified, LGPL license version is LGPL v2.1
48
 * is assumed.
49
 *  
50
 * 
51
 */
52
package org.gvsig.raster.cache.tile.impl.memory;
53

    
54
import java.util.HashMap;
55
import java.util.LinkedList;
56
import java.util.logging.Level;
57
import java.util.logging.Logger;
58

    
59
import org.gvsig.raster.cache.buffer.Buffer;
60

    
61
/**
62
 * A Last Recent Used cache of Bitmaps
63
 * @author aromeu 
64
 * @author rblanco
65
 *
66
 */
67
public class LRUTileCache extends HashMap<String, Buffer> {
68
        private static final long        serialVersionUID = 6215141;
69
        private final int                maxCacheSize;
70
        private LinkedList<String>       list             = null;
71
        private final static Logger      log              = Logger.getLogger("LRUTileCache");
72

    
73
        /**
74
         * The Constructor
75
         * @param maxCacheSize The cache size in number of Bitmaps
76
         */
77
        public LRUTileCache(final int maxCacheSize) {
78
                super(maxCacheSize);
79
                this.maxCacheSize = Math.max(0, maxCacheSize);
80
                this.list = new LinkedList<String>();
81
        }
82

    
83
        /**
84
         * Clears the cache
85
         */
86
        public synchronized void clear() {                
87
                try {
88
                        /*for (final Buffer b : this.values()) {
89
                                if (b != null) {
90
                                        b.recycle();
91
                                }
92
                        }*/
93
                        super.clear();
94
                        list.clear();
95
                } catch (Exception e) {
96
                        log.log(Level.SEVERE, "clear cache: ", e);
97
                }
98
        }
99

    
100
        /**
101
         * Adds a Bitmap to the cache. If the cache is full, removes the last
102
         */
103
        public synchronized Buffer put(final String key, final Buffer value) {
104
                try {
105
                        if (maxCacheSize == 0) {
106
                                return null;
107
                        }
108

    
109
                        // if the key isn't in the cache and the cache is full...
110
                        if (!super.containsKey(key) && !list.isEmpty()
111
                                        && list.size() + 1 > maxCacheSize) {
112
                                final Object deadKey = list.removeLast();
113
                                super.remove(deadKey);
114
                                //if (bitmap != null)
115
                                        //bitmap.recycle();
116
                        }
117

    
118
                        updateKey(key);                        
119
                } catch (Exception e) {
120
                        log.log(Level.SEVERE, "put", e);
121
                }
122
                return super.put(key, value);                
123
        }
124

    
125
        /**
126
         * Returns a cached Bitmap given a key
127
         * @param key The key of the Bitmap stored on the HashMap
128
         * @return The Bitmap or null if it is not stored in the cache
129
         */
130
        public synchronized Buffer get(final String key) {
131
                try {
132
                        final Buffer value = super.get(key);
133
                        if (value != null) {
134
                                updateKey(key);
135
                        }
136
                        return value;
137
                } catch (Exception e) {
138
                        log.log(Level.SEVERE, "get",  e);
139
                        return null;
140
                }                
141
        } 
142

    
143
        /**
144
         * Removes a Bitmap from the cache
145
         * @param key The key of the Bitmap to remove
146
         */
147
        public synchronized void remove(final String key) {
148
                try {
149
                        list.remove(key);                        
150
                } catch (Exception e) {
151
                        log.log(Level.SEVERE, "remove", e);                        
152
                }
153
                super.remove(key);
154
                //if (bitmap != null)
155
                        //bitmap.recycle();                
156
        }
157

    
158
        /**
159
         * The key is touched (recent used) and added to the top of the list
160
         * @param key The key to be updated
161
         */
162
        private void updateKey(final String key) {
163
                try {
164
                        list.remove(key);
165
                        list.addFirst(key);
166
                } catch (Exception e) {
167
                        log.log(Level.SEVERE, "updatekey", e);
168
                }                
169
        }
170
}