source: josm/trunk/src/org/openstreetmap/josm/io/CacheCustomContent.java@ 9997

Last change on this file since 9997 was 8926, checked in by Don-vip, 8 years ago

javadoc fixes

  • Property svn:eol-style set to native
File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import java.io.BufferedInputStream;
5import java.io.BufferedOutputStream;
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FileOutputStream;
9import java.io.IOException;
10import java.nio.charset.StandardCharsets;
11
12import org.openstreetmap.josm.Main;
13
14/**
15 * Use this class if you want to cache and store a single file that gets updated regularly.
16 * Unless you flush() it will be kept in memory. If you want to cache a lot of data and/or files, use CacheFiles.
17 * @author xeen
18 * @param <T> a {@link Throwable} that may be thrown during {@link #updateData()},
19 * use {@link RuntimeException} if no exception must be handled.
20 * @since 1450
21 */
22public abstract class CacheCustomContent<T extends Throwable> {
23
24 /** Update interval meaning an update is always needed */
25 public static final int INTERVAL_ALWAYS = -1;
26 /** Update interval meaning an update is needed each hour */
27 public static final int INTERVAL_HOURLY = 60*60;
28 /** Update interval meaning an update is needed each day */
29 public static final int INTERVAL_DAILY = INTERVAL_HOURLY * 24;
30 /** Update interval meaning an update is needed each week */
31 public static final int INTERVAL_WEEKLY = INTERVAL_DAILY * 7;
32 /** Update interval meaning an update is needed each month */
33 public static final int INTERVAL_MONTHLY = INTERVAL_WEEKLY * 4;
34 /** Update interval meaning an update is never needed */
35 public static final int INTERVAL_NEVER = Integer.MAX_VALUE;
36
37 /**
38 * Where the data will be stored
39 */
40 private byte[] data;
41
42 /**
43 * The ident that identifies the stored file. Includes file-ending.
44 */
45 private final String ident;
46
47 /**
48 * The (file-)path where the data will be stored
49 */
50 private final File path;
51
52 /**
53 * How often to update the cached version
54 */
55 private final int updateInterval;
56
57 /**
58 * This function will be executed when an update is required. It has to be implemented by the
59 * inheriting class and should use a worker if it has a long wall time as the function is
60 * executed in the current thread.
61 * @return the data to cache
62 * @throws T a {@link Throwable}
63 */
64 protected abstract byte[] updateData() throws T;
65
66 /**
67 * Initializes the class. Note that all read data will be stored in memory until it is flushed
68 * by flushData().
69 * @param ident ident that identifies the stored file. Includes file-ending.
70 * @param updateInterval update interval in seconds. -1 means always
71 */
72 public CacheCustomContent(String ident, int updateInterval) {
73 this.ident = ident;
74 this.updateInterval = updateInterval;
75 this.path = new File(Main.pref.getCacheDirectory(), ident);
76 }
77
78 /**
79 * This function serves as a comfort hook to perform additional checks if the cache is valid
80 * @return True if the cached copy is still valid
81 */
82 protected boolean isCacheValid() {
83 return true;
84 }
85
86 private boolean needsUpdate() {
87 if (isOffline()) {
88 return false;
89 }
90 return Main.pref.getInteger("cache." + ident, 0) + updateInterval < System.currentTimeMillis()/1000
91 || !isCacheValid();
92 }
93
94 private boolean isOffline() {
95 try {
96 checkOfflineAccess();
97 return false;
98 } catch (OfflineAccessException e) {
99 return true;
100 }
101 }
102
103 protected void checkOfflineAccess() {
104 // To be overriden by subclasses
105 }
106
107 /**
108 * Updates data if required
109 * @return Returns the data
110 * @throws T if an error occurs
111 */
112 public byte[] updateIfRequired() throws T {
113 if (needsUpdate())
114 return updateForce();
115 return getData();
116 }
117
118 /**
119 * Updates data if required
120 * @return Returns the data as string
121 * @throws T if an error occurs
122 */
123 public String updateIfRequiredString() throws T {
124 if (needsUpdate())
125 return updateForceString();
126 return getDataString();
127 }
128
129 /**
130 * Executes an update regardless of updateInterval
131 * @return Returns the data
132 * @throws T if an error occurs
133 */
134 public byte[] updateForce() throws T {
135 this.data = updateData();
136 saveToDisk();
137 Main.pref.putInteger("cache." + ident, (int) (System.currentTimeMillis()/1000));
138 return data;
139 }
140
141 /**
142 * Executes an update regardless of updateInterval
143 * @return Returns the data as String
144 * @throws T if an error occurs
145 */
146 public String updateForceString() throws T {
147 updateForce();
148 return new String(data, StandardCharsets.UTF_8);
149 }
150
151 /**
152 * Returns the data without performing any updates
153 * @return the data
154 * @throws T if an error occurs
155 */
156 public byte[] getData() throws T {
157 if (data == null) {
158 loadFromDisk();
159 }
160 return data;
161 }
162
163 /**
164 * Returns the data without performing any updates
165 * @return the data as String
166 * @throws T if an error occurs
167 */
168 public String getDataString() throws T {
169 byte[] array = getData();
170 if (array == null) {
171 return null;
172 }
173 return new String(array, StandardCharsets.UTF_8);
174 }
175
176 /**
177 * Tries to load the data using the given ident from disk. If this fails, data will be updated, unless run in offline mode
178 * @throws T a {@link Throwable}
179 */
180 private void loadFromDisk() throws T {
181 try (BufferedInputStream input = new BufferedInputStream(new FileInputStream(path))) {
182 this.data = new byte[input.available()];
183 if (input.read(this.data) < this.data.length) {
184 Main.error("Failed to read expected contents from "+path);
185 }
186 } catch (IOException e) {
187 if (!isOffline()) {
188 this.data = updateForce();
189 }
190 }
191 }
192
193 /**
194 * Stores the data to disk
195 */
196 private void saveToDisk() {
197 try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(path))) {
198 output.write(this.data);
199 output.flush();
200 } catch (IOException e) {
201 Main.error(e);
202 }
203 }
204
205 /**
206 * Flushes the data from memory. Class automatically reloads it from disk or updateData() if required
207 */
208 public void flushData() {
209 data = null;
210 }
211}
Note: See TracBrowser for help on using the repository browser.