altosuilib: Make map cache size configurable
[fw/altos] / altosuilib / AltosUIMapCache.java
1 /*
2  * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_3;
19
20 import javax.swing.*;
21 import javax.imageio.ImageIO;
22 import java.awt.image.*;
23 import java.awt.*;
24 import java.io.*;
25 import java.net.*;
26
27 public class AltosUIMapCache implements AltosUIMapCacheListener {
28         static final int        success = 0;
29         static final int        loading = 1;
30         static final int        failed = 2;
31         static final int        bad_request = 3;
32         static final int        forbidden = 4;
33
34         int                     min_cache_size;         /* configured minimum cache size */
35         int                     cache_size;             /* current cache size */
36         int                     requested_cache_size;   /* cache size computed by application */
37
38         private Object          fetch_lock = new Object();
39         private Object          cache_lock = new Object();
40
41         AltosUIMapImage[]       images = new AltosUIMapImage[cache_size];
42
43         long                    used;
44
45         public void set_cache_size(int new_size) {
46
47                 requested_cache_size = new_size;
48
49                 if (new_size < min_cache_size)
50                         new_size = min_cache_size;
51
52                 if (new_size == cache_size)
53                         return;
54
55                 System.out.printf("cache size now %d\n", new_size);
56
57                 synchronized(cache_lock) {
58                         AltosUIMapImage[]       new_images = new AltosUIMapImage[new_size];
59
60                         for (int i = 0; i < cache_size; i++) {
61                                 if (i < new_size)
62                                         new_images[i] = images[i];
63                                 else if (images[i] != null)
64                                         images[i].flush();
65                         }
66                         images = new_images;
67                         cache_size = new_size;
68                 }
69         }
70
71         public Image get(AltosUIMapTile tile, AltosUIMapStore store, int width, int height) {
72                 int             oldest = -1;
73                 long            age = used;
74
75                 synchronized(cache_lock) {
76                         AltosUIMapImage image = null;
77                         for (int i = 0; i < cache_size; i++) {
78                                 image = images[i];
79
80                                 if (image == null) {
81                                         oldest = i;
82                                         break;
83                                 }
84                                 if (store.equals(image.store)) {
85                                         image.used = used++;
86                                         return image.image;
87                                 }
88                                 if (image.used < age) {
89                                         oldest = i;
90                                         age = image.used;
91                                 }
92                         }
93
94                         try {
95                                 image = new AltosUIMapImage(tile, store);
96                                 image.used = used++;
97                                 if (images[oldest] != null) {
98                                         System.out.printf("drop %s\n", images[oldest].store.file.toString());
99                                         images[oldest].flush();
100                                 }
101
102                                 System.out.printf("load %s\n", store.file.toString());
103
104                                 images[oldest] = image;
105
106                                 if (image.image == null)
107                                         tile.set_status(loading);
108                                 else
109                                         tile.set_status(success);
110
111                                 return image.image;
112                         } catch (IOException e) {
113                                 tile.set_status(failed);
114                                 return null;
115                         }
116                 }
117         }
118
119         public void map_cache_changed(int map_cache) {
120                 min_cache_size = map_cache;
121
122                 set_cache_size(requested_cache_size);
123         }
124
125         public void dispose() {
126                 AltosUIPreferences.unregister_map_cache_listener(this);
127
128                 for (int i = 0; i < cache_size; i++) {
129                         AltosUIMapImage image = images[i];
130
131                         if (image != null)
132                             image.flush();
133                 }
134         }
135
136         public AltosUIMapCache() {
137                 min_cache_size = AltosUIPreferences.map_cache();
138
139                 set_cache_size(0);
140
141                 AltosUIPreferences.register_map_cache_listener(this);
142         }
143 }