(no commit message)
[utils] / support / src / 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 public class CachedObject<KeyType extends Serializable, ValueType extends Serializable> {
29
30     private static final Logger LOGGER = Logger.getLogger(CachedObject.class);
31
32     /**
33      * Callback invoked to compute an object if it was not found in the cache.
34      * 
35      * @param <T>
36      *            Type of the object
37      */
38     public static interface Computation<Key extends Serializable, Value extends Serializable> {
39         /**
40          * Gets the object. Called when the object is not in the cache.
41          * 
42          * @param aObjectKey
43          *            Id of the object in the cache.
44          * @return Object, must be non-null.
45          */
46         Value getObject(Key aObjectKey);
47     }
48
49     /**
50      * Cache to use.
51      */
52     private Cache<KeyType, ValueType> _cache;
53
54     /**
55      * Key of the object in the cache.
56      */
57     private KeyType _objectKey;
58
59     /**
60      * Computation used to obtain the object if it is not found in the cache.
61      */
62     private Computation<KeyType, ValueType> _computation;
63
64     /**
65      * Constructs the cached object.
66      * 
67      * @param aCache
68      *            Cache to use.
69      * @param aObjectKey
70      *            Key of the object in the cache.
71      * @param aComputation
72      *            Computation to get the object in case the object is not in the
73      *            cache.
74      */
75     public CachedObject(Cache<KeyType, ValueType> aCache, KeyType aObjectKey,
76             Computation<KeyType, ValueType> aComputation) {
77         _cache = aCache;
78         _objectKey = aObjectKey;
79         _computation = aComputation;
80     }
81
82     /**
83      * Gets the object. Since the object is cached, different calls to this
84      * method may return different objects.
85      * 
86      * @return Object.
87      */
88     public ValueType get() {
89         ValueType object = (ValueType) _cache.get(_objectKey); // the used
90                                                                 // cache is
91                                                                 // thread safe.
92         if (object == null) {
93             // synchronize the computation to make sure that the object is only
94             // computed
95             // once when multiple concurrent threads detect that the entry must
96             // be
97             // recomputed.
98             synchronized (this) {
99                 object = (ValueType) _cache.get(_objectKey);
100                 if (object == null) {
101                     // No other thread did a recomputation so we must do this
102                     // now.
103                     LOGGER.debug("Refreshing cache for '" + _objectKey + "'");
104                     object = _computation.getObject(_objectKey);
105                     _cache.put(_objectKey, object);
106                 }
107             }
108         }
109         return object;
110     }
111
112     /**
113      * Invalidates the cache for the object so that it is recomputed the next
114      * time it is requested.
115      * 
116      */
117     public void invalidate() {
118         _cache.remove(_objectKey);
119     }
120
121     /**
122      * Gets the cache.
123      * 
124      * @return Cache.
125      */
126     public Cache getCache() {
127         return _cache;
128     }
129 }