summaryrefslogtreecommitdiff
path: root/altosuilib/AltosUIMapCache.java
blob: b9064cf435860fc4b812c6e93c566845f8ed14bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright © 2010 Anthony Towns <aj@erisian.com.au>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.altosuilib_3;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
import java.net.*;

public class AltosUIMapCache implements AltosUIMapCacheListener {
	static final int	success = 0;
	static final int	loading = 1;
	static final int	failed = 2;
	static final int	bad_request = 3;
	static final int	forbidden = 4;

	int			min_cache_size;		/* configured minimum cache size */
	int			cache_size;		/* current cache size */
	int			requested_cache_size;	/* cache size computed by application */

	private Object 		fetch_lock = new Object();
	private Object 		cache_lock = new Object();

	AltosUIMapImage[]	images = new AltosUIMapImage[cache_size];

	long			used;

	public void set_cache_size(int new_size) {

		requested_cache_size = new_size;

		if (new_size < min_cache_size)
			new_size = min_cache_size;

		if (new_size == cache_size)
			return;

		synchronized(cache_lock) {
			AltosUIMapImage[]	new_images = new AltosUIMapImage[new_size];

			for (int i = 0; i < cache_size; i++) {
				if (i < new_size)
					new_images[i] = images[i];
				else if (images[i] != null)
					images[i].flush();
			}
			images = new_images;
			cache_size = new_size;
		}
	}

	public Image get(AltosUIMapTile tile, AltosUIMapStore store, int width, int height) {
		int		oldest = -1;
		long		age = used;

		synchronized(cache_lock) {
			AltosUIMapImage	image = null;
			for (int i = 0; i < cache_size; i++) {
				image = images[i];

				if (image == null) {
					oldest = i;
					break;
				}
				if (store.equals(image.store)) {
					image.used = used++;
					return image.image;
				}
				if (image.used < age) {
					oldest = i;
					age = image.used;
				}
			}

			try {
				image = new AltosUIMapImage(tile, store);
				image.used = used++;
				if (images[oldest] != null)
					images[oldest].flush();

				images[oldest] = image;

				if (image.image == null)
					tile.set_status(loading);
				else
					tile.set_status(success);

				return image.image;
			} catch (IOException e) {
				tile.set_status(failed);
				return null;
			}
		}
	}

	public void map_cache_changed(int map_cache) {
		min_cache_size = map_cache;

		set_cache_size(requested_cache_size);
	}

	public void dispose() {
		AltosUIPreferences.unregister_map_cache_listener(this);

		for (int i = 0; i < cache_size; i++) {
			AltosUIMapImage image = images[i];

			if (image != null)
			    image.flush();
		}
	}

	public AltosUIMapCache() {
		min_cache_size = AltosUIPreferences.map_cache();

		set_cache_size(0);

		AltosUIPreferences.register_map_cache_listener(this);
	}
}