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