File Source: ExpiringCacheEntry.java
/*
P/P * Method: org.apache.roller.weblogger.util.cache.ExpiringCacheEntry__static_init
*/
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */
18
19 package org.apache.roller.weblogger.util.cache;
20
21 import java.io.Serializable;
22
23
24 /**
25 * A cache entry that expires.
26 *
27 * We use this class to wrap objects being cached and associate a timestamp
28 * and timeout period with them so we can know when they expire.
29 */
30 public class ExpiringCacheEntry implements Serializable {
31
32 private Object value;
33 private long timeCached = -1;
34 private long timeout = 0;
35
36
/*
P/P * Method: void org.apache.roller.weblogger.util.cache.ExpiringCacheEntry(Object, long)
*
* Postconditions:
* init'ed(this.timeCached)
* this.timeout == One-of{0, timeout}
* this.timeout >= 0
* this.value == value
* init'ed(this.value)
*
* Test Vectors:
* timeout: {-263..0}, {1..264-1}
*/
37 public ExpiringCacheEntry(Object value, long timeout) {
38 this.value = value;
39
40 // make sure that we don't support negative values
41 if(timeout > 0) {
42 this.timeout = timeout;
43 }
44
45 this.timeCached = System.currentTimeMillis();
46 }
47
48
49 public long getTimeCached() {
/*
P/P * Method: long getTimeCached()
*
* Preconditions:
* init'ed(this.timeCached)
*
* Postconditions:
* return_value == this.timeCached
* init'ed(return_value)
*/
50 return this.timeCached;
51 }
52
53
54 public long getTimeout() {
/*
P/P * Method: long getTimeout()
*
* Preconditions:
* init'ed(this.timeout)
*
* Postconditions:
* return_value == this.timeout
* init'ed(return_value)
*/
55 return this.timeout;
56 }
57
58
59 /**
60 * Retrieve the value of this cache entry.
61 *
62 * If the value has expired then we return null.
63 */
64 public Object getValue() {
/*
P/P * Method: Object getValue()
*
* Preconditions:
* init'ed(this.timeCached)
* init'ed(this.timeout)
* (soft) init'ed(this.value)
*
* Postconditions:
* return_value == One-of{null, this.value}
* (soft) init'ed(return_value)
*/
65 if(this.hasExpired()) {
66 return null;
67 } else {
68 return this.value;
69 }
70 }
71
72
73 /**
74 * Determine if this cache entry has expired.
75 */
76 public boolean hasExpired() {
77
/*
P/P * Method: bool hasExpired()
*
* Preconditions:
* init'ed(this.timeCached)
* init'ed(this.timeout)
*
* Postconditions:
* init'ed(return_value)
*/
78 long now = System.currentTimeMillis();
79
80 return ((this.timeCached + this.timeout) < now);
81 }
82
83 }
SofCheck Inspector Build Version : 2.18479
| ExpiringCacheEntry.java |
2009-Jan-02 14:24:58 |
| ExpiringCacheEntry.class |
2009-Sep-04 03:12:32 |