Revision 2100 org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/util/CachedValue.java
CachedValue.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* gvSIG. Desktop Geographic Information System. |
3 | 3 |
* |
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
|
5 | 5 |
* |
6 | 6 |
* This program is free software; you can redistribute it and/or |
7 | 7 |
* modify it under the terms of the GNU General Public License |
... | ... | |
23 | 23 |
*/ |
24 | 24 |
package org.gvsig.tools.util; |
25 | 25 |
|
26 |
import java.io.Closeable; |
|
27 |
import java.io.IOException; |
|
28 |
import org.gvsig.tools.dispose.Disposable; |
|
29 |
import org.gvsig.tools.dispose.DisposeUtils; |
|
30 |
|
|
26 | 31 |
/** |
27 | 32 |
* |
28 | 33 |
* @author jjdelcerro |
29 | 34 |
*/ |
30 | 35 |
public abstract class CachedValue<T> { |
31 | 36 |
|
32 |
T value = null; |
|
33 |
long lastAccess = 0; |
|
34 |
long expireTimeInMillis; |
|
37 |
private T value = null;
|
|
38 |
private long lastAccess = 0;
|
|
39 |
private long expireTimeInMillis;
|
|
35 | 40 |
|
36 | 41 |
public CachedValue() { |
37 | 42 |
this(3000); |
... | ... | |
41 | 46 |
this.expireTimeInMillis = expireTimeInMillis; |
42 | 47 |
} |
43 | 48 |
|
44 |
protected abstract void reload(); |
|
49 |
protected void reload() { |
|
50 |
// Overwrite this method. |
|
51 |
} |
|
45 | 52 |
|
46 |
public boolean isExpired() { |
|
53 |
protected final T getValue() { |
|
54 |
return this.value; |
|
55 |
} |
|
56 |
|
|
57 |
protected void setValue(T value) { |
|
58 |
this.value = value; |
|
59 |
} |
|
60 |
|
|
61 |
public final boolean isExpired() { |
|
47 | 62 |
long now = System.currentTimeMillis(); |
48 | 63 |
return (now - lastAccess) > expireTimeInMillis; |
49 | 64 |
} |
50 | 65 |
|
51 |
public T get() { |
|
66 |
public final void set(T value) { |
|
67 |
this.value = value; |
|
68 |
lastAccess = System.currentTimeMillis(); |
|
69 |
} |
|
70 |
|
|
71 |
public final void setExpireTime(long expireTimeInMillis) { |
|
72 |
this.expireTimeInMillis = expireTimeInMillis; |
|
73 |
} |
|
74 |
|
|
75 |
public final long getExpireTime() { |
|
76 |
return expireTimeInMillis; |
|
77 |
} |
|
78 |
|
|
79 |
public final void resetAccess() { |
|
80 |
this.lastAccess = System.currentTimeMillis(); |
|
81 |
} |
|
82 |
|
|
83 |
public final void expired() { |
|
84 |
this.lastAccess = 0; |
|
85 |
} |
|
86 |
|
|
87 |
public final T get() { |
|
52 | 88 |
if (isExpired()) { |
89 |
if( value instanceof Disposable ) { |
|
90 |
DisposeUtils.disposeQuietly((Disposable) value); |
|
91 |
} else if( value instanceof Closeable ) { |
|
92 |
try { |
|
93 |
((Closeable)value).close(); |
|
94 |
} catch (IOException ex) { |
|
95 |
// Ignore |
|
96 |
} |
|
97 |
} |
|
98 |
value = null; |
|
53 | 99 |
reload(); |
54 | 100 |
} |
55 | 101 |
lastAccess = System.currentTimeMillis(); |
Also available in: Unified diff