2 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
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.
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.
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.
18 package org.altusmetrum.altoslib_9;
23 public class AltosMapCache implements AltosMapCacheListener {
25 /* An entry in the MapCache */
26 class MapCacheElement implements AltosMapStoreListener {
28 AltosMapTile tile; /* Notify when image has been loaded */
33 class loader implements Runnable {
36 tile.notify_image(image);
38 image = map_interface.load_image(store.file);
39 } catch (Exception ex) {
42 tile.set_status(AltosMapTile.failed);
44 tile.set_status(AltosMapTile.success);
45 tile.notify_image(image);
50 loader l = new loader();
51 Thread lt = new Thread(l);
62 public boolean has_map() {
63 return store.status() == AltosMapTile.success;
66 public synchronized void notify_store(AltosMapStore store, int status) {
68 case AltosMapTile.loading:
70 case AltosMapTile.success:
74 tile.set_status(status);
75 tile.notify_image(null);
79 public MapCacheElement(AltosMapTile tile, AltosMapStore store) throws IOException {
85 int status = store.status();
87 case AltosMapTile.loading:
88 store.add_listener(this);
90 case AltosMapTile.success:
94 tile.set_status(status);
95 tile.notify_image(null);
101 int min_cache_size; /* configured minimum cache size */
102 int cache_size; /* current cache size */
103 int requested_cache_size; /* cache size computed by application */
105 private Object fetch_lock = new Object();
106 private Object cache_lock = new Object();
108 AltosMapInterface map_interface;
110 MapCacheElement[] elements = new MapCacheElement[cache_size];
114 public void set_cache_size(int new_size) {
116 requested_cache_size = new_size;
118 if (new_size < min_cache_size)
119 new_size = min_cache_size;
121 if (new_size == cache_size)
124 synchronized(cache_lock) {
125 MapCacheElement[] new_elements = new MapCacheElement[new_size];
127 for (int i = 0; i < cache_size; i++) {
129 new_elements[i] = elements[i];
130 else if (elements[i] != null)
133 elements = new_elements;
134 cache_size = new_size;
138 public AltosImage get(AltosMapTile tile, AltosMapStore store, int width, int height) {
142 synchronized(cache_lock) {
143 MapCacheElement element = null;
144 for (int i = 0; i < cache_size; i++) {
145 element = elements[i];
147 if (element == null) {
151 if (store.equals(element.store)) {
152 element.used = used++;
153 return element.image;
155 if (element.used < age) {
162 element = new MapCacheElement(tile, store);
163 element.used = used++;
164 if (elements[oldest] != null)
165 elements[oldest].flush();
167 elements[oldest] = element;
169 if (element.image == null)
170 tile.set_status(AltosMapTile.loading);
172 tile.set_status(AltosMapTile.success);
174 return element.image;
175 } catch (IOException e) {
176 tile.set_status(AltosMapTile.failed);
182 public void map_cache_changed(int map_cache) {
183 min_cache_size = map_cache;
185 set_cache_size(requested_cache_size);
188 public void dispose() {
189 AltosPreferences.unregister_map_cache_listener(this);
191 for (int i = 0; i < cache_size; i++) {
192 MapCacheElement element = elements[i];
199 public AltosMapCache(AltosMapInterface map_interface) {
200 this.map_interface = map_interface;
201 min_cache_size = AltosPreferences.map_cache();
205 AltosPreferences.register_map_cache_listener(this);