Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / CachedValue.java @ 2738

History | View | Annotate | Download (3.31 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2021 gvSIG Association.
5
 *
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
import java.io.Closeable;
27
import java.io.IOException;
28
import org.gvsig.tools.dispose.Disposable;
29
import org.gvsig.tools.dispose.DisposeUtils;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32

    
33
/**
34
 *
35
 * @author gvSIG Team
36
 * @param <T>
37
 */
38
public class CachedValue<T> {
39
    
40
    public static final Logger LOGGER = LoggerFactory.getLogger(CachedValue.class);
41

    
42
  private T value = null;
43
  private long lastAccess = 0;
44
  private long expireTimeInMillis;
45
  private java.util.concurrent.Callable<T> reload;
46

    
47
  public CachedValue() {
48
    this(3000, null);
49
  }
50

    
51
  public CachedValue(long expireTimeInMillis) {
52
    this(expireTimeInMillis, null);
53
  }
54

    
55
  public CachedValue(java.util.concurrent.Callable<T> reload) {
56
      this(3000, reload);
57
  }
58
  
59
  public CachedValue(long expireTimeInMillis, java.util.concurrent.Callable<T> reload) {
60
    this.expireTimeInMillis = expireTimeInMillis;
61
    this.reload = reload;
62
  }
63

    
64
  protected void reload() {
65
    if( this.reload!=null ) {
66
        try {
67
            this.value = this.reload.call();
68
        } catch (Exception ex) {
69
            // FIXME: calling Callable.call exception ??
70
        }
71
    }
72
  }
73

    
74
  protected final T getValue() {
75
    return this.value;
76
  }
77

    
78
  protected void setValue(T value) {
79
    this.value = value;
80
  }
81
  
82
  public final boolean isExpired() {
83
    long now = System.currentTimeMillis();
84
    return (now - lastAccess) > expireTimeInMillis;
85
  }
86

    
87
  public final void set(T value) {
88
    this.value = value;
89
    lastAccess = System.currentTimeMillis();
90
  }
91

    
92
  public final void setExpireTime(long expireTimeInMillis) {
93
    this.expireTimeInMillis = expireTimeInMillis;
94
  }
95

    
96
  public final long getExpireTime() {
97
    return expireTimeInMillis;
98
  }
99

    
100
  public final void resetAccess() {
101
    this.lastAccess = System.currentTimeMillis();
102
  }
103
  
104
  public final void expired() {
105
//      LOGGER.info("Force expired "+this.getClass().getSimpleName()+" ***"+this.toString()+"***");
106
    this.lastAccess = 0;
107
  }
108
  
109
  public final T get() {
110
    if (isExpired()) {
111
      if( value instanceof Disposable ) {
112
        DisposeUtils.disposeQuietly((Disposable) value);
113
      } else if( value instanceof Closeable ) {
114
        try {
115
          ((Closeable)value).close();
116
        } catch (IOException ex) {
117
          // Ignore
118
        }
119
      }
120
      value = null;
121
      reload();
122
    }
123
    lastAccess = System.currentTimeMillis();
124
    return value;
125
  }
126
}