| 1 | |
package org.webslinger.util; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
|
| 5 | 2637 | public abstract class TTLCachedObject<T> extends TTLObject<T> { |
| 6 | |
public static final long NOT_EXISTANT_TIMESTAMP = Long.MIN_VALUE; |
| 7 | |
public static final long FORCE_REGEN = Long.MIN_VALUE + 1; |
| 8 | |
|
| 9 | 2638 | protected long lastModifiedTime = -1; |
| 10 | |
|
| 11 | |
public long getTimestamp() throws IOException { |
| 12 | 0 | getObject(); |
| 13 | 0 | return lastModifiedTime; |
| 14 | |
} |
| 15 | |
|
| 16 | |
protected T load(T old, int serial) throws IOException { |
| 17 | 2880 | long timestamp = getTimestamp(old); |
| 18 | 2880 | if (lastModifiedTime != timestamp) { |
| 19 | 1653 | if (timestamp != NOT_EXISTANT_TIMESTAMP) { |
| 20 | 1570 | GeneratedResult<T> result = generate(old, serial); |
| 21 | 1568 | lastModifiedTime = result.lastModifiedTime; |
| 22 | 1568 | return result.object; |
| 23 | |
} else { |
| 24 | 83 | lastModifiedTime = timestamp; |
| 25 | |
try { |
| 26 | 83 | return getInitial(); |
| 27 | 0 | } catch (IOException e) { |
| 28 | 0 | throw e; |
| 29 | 0 | } catch (RuntimeException e) { |
| 30 | 0 | throw e; |
| 31 | 0 | } catch (Exception e) { |
| 32 | 0 | throw (IOException) new IOException(e.getMessage()).initCause(e); |
| 33 | |
} |
| 34 | |
} |
| 35 | |
} |
| 36 | 1227 | return old; |
| 37 | |
} |
| 38 | |
|
| 39 | |
protected GeneratedResult<T> generate(T old) throws IOException { |
| 40 | 0 | throw new AbstractMethodError(); |
| 41 | |
} |
| 42 | |
|
| 43 | |
protected GeneratedResult<T> generate(T old, int serial) throws IOException { |
| 44 | 1570 | return generate(old); |
| 45 | |
} |
| 46 | |
|
| 47 | |
protected abstract long getTimestamp(T old) throws IOException; |
| 48 | |
} |