gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / CachedValue.java @ 2335
History | View | Annotate | Download (2.6 KB)
1 | 2096 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | 2100 | jjdelcerro | * Copyright (C) 2007-2020 gvSIG Association.
|
5 | 2096 | jjdelcerro | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | package org.gvsig.tools.util; |
||
25 | |||
26 | 2100 | jjdelcerro | import java.io.Closeable; |
27 | import java.io.IOException; |
||
28 | import org.gvsig.tools.dispose.Disposable; |
||
29 | import org.gvsig.tools.dispose.DisposeUtils; |
||
30 | |||
31 | 2096 | jjdelcerro | /**
|
32 | *
|
||
33 | * @author jjdelcerro
|
||
34 | */
|
||
35 | public abstract class CachedValue<T> { |
||
36 | |||
37 | 2100 | jjdelcerro | private T value = null; |
38 | private long lastAccess = 0; |
||
39 | private long expireTimeInMillis; |
||
40 | 2096 | jjdelcerro | |
41 | public CachedValue() {
|
||
42 | this(3000); |
||
43 | } |
||
44 | |||
45 | public CachedValue(long expireTimeInMillis) { |
||
46 | this.expireTimeInMillis = expireTimeInMillis;
|
||
47 | } |
||
48 | |||
49 | 2100 | jjdelcerro | protected void reload() { |
50 | // Overwrite this method.
|
||
51 | } |
||
52 | 2096 | jjdelcerro | |
53 | 2100 | jjdelcerro | 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() { |
||
62 | 2096 | jjdelcerro | long now = System.currentTimeMillis(); |
63 | return (now - lastAccess) > expireTimeInMillis;
|
||
64 | } |
||
65 | |||
66 | 2100 | jjdelcerro | 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() { |
||
88 | 2096 | jjdelcerro | if (isExpired()) {
|
89 | 2100 | jjdelcerro | 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;
|
||
99 | 2096 | jjdelcerro | reload(); |
100 | } |
||
101 | lastAccess = System.currentTimeMillis();
|
||
102 | return value;
|
||
103 | } |
||
104 | } |