f844c728e9b69d18c176ed3afc95133d223fe243
[utils] / support / general / src / main / java / org / wamblee / cache / CachedObject.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */
16
17 package org.wamblee.cache;
18
19 import java.io.Serializable;
20
21 import org.apache.log4j.Logger;
22
23 /**
24  * Represents a cached object. The object is either retrieved from the cache if
25  * the cache has it, or a call back is invoked to get the object (and put it in
26  * the cache).
27  *
28  * @author Erik Brakkee
29  */
30 public class CachedObject<KeyType extends Serializable, ValueType extends Serializable> {
31
32     private static final Logger LOGGER = Logger.getLogger(CachedObject.class);
33
34     /**
35      * Callback invoked to compute an object if it was not found in the cache.
36      * 
37      * @param <T>
38      *            Type of the object
39      */
40     public static interface Computation<Key extends Serializable, Value extends Serializable> {
41         /**
42          * Gets the object. Called when the object is not in the cache.
43          * 
44          * @param aObjectKey
45          *            Id of the object in the cache.
46          * @return Object, must be non-null.
47          */
48         Value getObject(Key aObjectKey);
49     }
50
51     /**
52      * Cache to use.
53      */
54     private Cache<KeyType, ValueType> cache;
55
56     /**
57      * Key of the object in the cache.
58      */
59     private KeyType objectKey;
60
61     /**
62      * Computation used to obtain the object if it is not found in the cache.
63      */
64     private Computation<KeyType, ValueType> computation;
65
66     /**
67      * Constructs the cached object.
68      * 
69      * @param aCache
70      *            Cache to use.
71      * @param aObjectKey
72      *            Key of the object in the cache.
73      * @param aComputation
74      *            Computation to get the object in case the object is not in the
75      *            cache.
76      */
77     public CachedObject(Cache<KeyType, ValueType> aCache, KeyType aObjectKey,
78             Computation<KeyType, ValueType> aComputation) {
79         cache = aCache;
80         objectKey = aObjectKey;
81         computation = aComputation;
82     }
83
84     /**
85      * Gets the object. Since the object is cached, different calls to this
86      * method may return different objects.
87      * 
88      * @return Object.
89      */
90     public ValueType get() {
91         ValueType object = (ValueType) cache.get(objectKey); // the used
92                                                                 // cache is
93                                                                 // thread safe.
94         if (object == null) {
95             // synchronize the computation to make sure that the object is only
96             // computed
97             // once when multiple concurrent threads detect that the entry must
98             // be
99             // recomputed.
100             synchronized (this) {
101                 object = (ValueType) cache.get(objectKey);
102                 if (object == null) {
103                     // No other thread did a recomputation so we must do this
104                     // now.
105                     LOGGER.debug("Refreshing cache for '" + objectKey + "'");
106                     object = computation.getObject(objectKey);
107                     cache.put(objectKey, object);
108                 }
109             }
110         }
111         return object;
112     }
113
114     /**
115      * Invalidates the cache for the object so that it is recomputed the next
116      * time it is requested.
117      * 
118      */
119     public void invalidate() {
120         cache.remove(objectKey);
121     }
122
123     /**
124      * Gets the cache.
125      * 
126      * @return Cache.
127      */
128     public Cache getCache() {
129         return cache;
130     }
131 }