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

Last change on this file since 2990 was 2986, checked in by jttt, 14 years ago

Fix some of FindBugs warnings

File size: 4.9 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.util.Date;
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,
17 * use CacheFiles
18 * @author xeen
19 *
20 */
21public abstract class CacheCustomContent {
22 /**
23 * Common intervals
24 */
25 final static public int INTERVAL_ALWAYS = -1;
26 final static public int INTERVAL_HOURLY = 60*60;
27 final static public int INTERVAL_DAILY = INTERVAL_HOURLY * 24;
28 final static public int INTERVAL_WEEKLY = INTERVAL_DAILY * 7;
29 final static public int INTERVAL_MONTHLY = INTERVAL_WEEKLY * 4;
30 final static public int INTERVAL_NEVER = Integer.MAX_VALUE;
31
32 /**
33 * Where the data will be stored
34 */
35 private byte[] data = null;
36
37 /**
38 * The ident that identifies the stored file. Includes file-ending.
39 */
40 final private String ident;
41
42 /**
43 * The (file-)path where the data will be stored
44 */
45 final private File path;
46
47 /**
48 * How often to update the cached version
49 */
50 final private int updateInterval;
51
52 /**
53 * This function will be executed when an update is required. It has to be implemented by the
54 * inheriting class and should use a worker if it has a long wall time as the function is
55 * executed in the current thread.
56 * @return the data to cache
57 */
58 protected abstract byte[] updateData();
59
60 /**
61 * This function serves as a comfort hook to perform additional checks if the cache is valid
62 * @return True if the cached copy is still valid
63 */
64 protected boolean isCacheValid() {
65 return true;
66 }
67
68 /**
69 * Initializes the class. Note that all read data will be stored in memory until it is flushed
70 * by flushData().
71 * @param ident
72 * @param updateInterval
73 */
74 public CacheCustomContent(String ident, int updateInterval) {
75 this.ident = ident;
76 this.updateInterval = updateInterval;
77 this.path = new File(Main.pref.getPreferencesDir(), ident);
78 }
79
80 /**
81 * Updates data if required
82 * @return Returns the data
83 */
84 public byte[] updateIfRequired() {
85 if(Main.pref.getInteger("cache." + ident, 0) + updateInterval < new Date().getTime()/1000
86 || !isCacheValid())
87 return updateForce();
88 return getData();
89 }
90
91 /**
92 * Updates data if required
93 * @return Returns the data as string
94 */
95 public String updateIfRequiredString() {
96 if(Main.pref.getInteger("cache." + ident, 0) + updateInterval < new Date().getTime()/1000
97 || !isCacheValid())
98 return updateForceString();
99 return getDataString();
100 }
101
102 /**
103 * Executes an update regardless of updateInterval
104 * @return Returns the data
105 */
106 public byte[] updateForce() {
107 this.data = updateData();
108 saveToDisk();
109 Main.pref.putInteger("cache." + ident, (int)(new Date().getTime()/1000));
110 return data;
111 }
112
113 /**
114 * Executes an update regardless of updateInterval
115 * @return Returns the data as String
116 */
117 public String updateForceString() {
118 updateForce();
119 return new String(data);
120 }
121
122 /**
123 * Returns the data without performing any updates
124 * @return the data
125 */
126 public byte[] getData() {
127 if(data == null) {
128 loadFromDisk();
129 }
130 return data;
131 }
132
133 /**
134 * Returns the data without performing any updates
135 * @return the data as String
136 */
137 public String getDataString() {
138 return new String(getData());
139 }
140
141 /**
142 * Tries to load the data using the given ident from disk. If this fails, data will be updated
143 */
144 private void loadFromDisk() {
145 try {
146 BufferedInputStream input = new BufferedInputStream(new FileInputStream(path));
147 this.data = new byte[input.available()];
148 input.read(this.data);
149 input.close();
150 } catch(IOException e) {
151 this.data = updateForce();
152 }
153 }
154
155 /**
156 * Stores the data to disk
157 */
158 private void saveToDisk() {
159 try {
160 BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(path));
161 output.write(this.data);
162 output.flush();
163 output.close();
164 } catch(Exception e) {
165 e.printStackTrace();
166 }
167 }
168
169 /**
170 * Flushes the data from memory. Class automatically reloads it from disk or updateData() if
171 * required
172 */
173 public void flushData() {
174 data = null;
175 }
176}
Note: See TracBrowser for help on using the repository browser.