3f1512dfdaa2eb10531cfac6bf7b915917fd1d9d
[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 {
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         static final int        min_cache_size = 9;
35         static final int        max_cache_size = 24;
36
37         private Object          fetch_lock = new Object();
38         private Object          cache_lock = new Object();
39
40         int                     cache_size = min_cache_size;
41
42         AltosUIMapImage[]       images = new AltosUIMapImage[cache_size];
43
44         long                    used;
45
46         public void set_cache_size(int new_size) {
47                 if (new_size < min_cache_size)
48                         new_size = min_cache_size;
49                 if (new_size > max_cache_size)
50                         new_size = max_cache_size;
51                 if (new_size == cache_size)
52                         return;
53
54                 synchronized(cache_lock) {
55                         AltosUIMapImage[]       new_images = new AltosUIMapImage[new_size];
56
57                         for (int i = 0; i < cache_size; i++) {
58                                 if (i < new_size)
59                                         new_images[i] = images[i];
60                                 else if (images[i] != null)
61                                         images[i].flush();
62                         }
63                         images = new_images;
64                         cache_size = new_size;
65                 }
66         }
67
68         public Image get(AltosUIMapTile tile, AltosUIMapStore store, int width, int height) {
69                 int             oldest = -1;
70                 long            age = used;
71
72                 synchronized(cache_lock) {
73                         AltosUIMapImage image = null;
74                         for (int i = 0; i < cache_size; i++) {
75                                 image = images[i];
76
77                                 if (image == null) {
78                                         oldest = i;
79                                         break;
80                                 }
81                                 if (store.equals(image.store)) {
82                                         image.used = used++;
83                                         return image.image;
84                                 }
85                                 if (image.used < age) {
86                                         oldest = i;
87                                         age = image.used;
88                                 }
89                         }
90
91                         try {
92                                 image = new AltosUIMapImage(tile, store);
93                                 image.used = used++;
94                                 if (images[oldest] != null)
95                                         images[oldest].flush();
96
97                                 images[oldest] = image;
98
99                                 if (image.image == null)
100                                         tile.set_status(loading);
101                                 else
102                                         tile.set_status(success);
103
104                                 return image.image;
105                         } catch (IOException e) {
106                                 tile.set_status(failed);
107                                 return null;
108                         }
109                 }
110         }
111
112         public AltosUIMapCache() {
113         }
114 }