KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > cache > CachePolicy


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.cache;
11
12 import java.io.Serializable JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 /**
17  * A CachePolicy object determines for a given object whether it should be cached or not, and how.
18  * Code that makes use of a cache should use a CachePolicy object, when available, to determine if the
19  * object should be cached or not.
20  *
21  * @since MMBase 1.8
22  * @author Pierre van Rooden
23  * @version $Id: CachePolicy.java,v 1.3 2005/09/23 13:59:26 pierre Exp $
24  */

25 abstract public class CachePolicy implements Serializable JavaDoc {
26
27     // map with all known policies
28
static private Map JavaDoc policies = new HashMap JavaDoc();
29
30     /**
31      * Standard cache policy that advises to never cache a passed object.
32      * Accessible with the key "never".
33      */

34     static public CachePolicy NEVER = new CachePolicy("never") {
35         public boolean checkPolicy(Object JavaDoc o) {
36             return false;
37         }
38
39         public String JavaDoc getDescription() {
40             return "CACHE NEVER";
41         }
42     };
43
44     /**
45      * Standard cache policy that advises to always cache a passed object.
46      * Accessible with the key "always".
47      */

48     static public CachePolicy ALWAYS = new CachePolicy("always") {
49         public boolean checkPolicy(Object JavaDoc o) {
50             return true;
51         }
52
53         public String JavaDoc getDescription() {
54             return "CACHE ALWAYS";
55         }
56     };
57
58     /**
59      * Obtains a cache policy given a policy key.
60      * @param policyKey the key of the cache policy
61      * @return the policy key
62      * @throws IllegalArgumentException if the policy does not exist
63      */

64     static public CachePolicy getPolicy(Object JavaDoc policyKey) {
65         CachePolicy policy = (CachePolicy) policies.get(policyKey);
66         if (policy == null) {
67             throw new IllegalArgumentException JavaDoc("There is no cache policy known with key '"+policyKey+"'");
68         }
69         return policy;
70     }
71
72
73     static public void putPolicy(Object JavaDoc policyKey, CachePolicy policy) {
74         policies.put(policyKey, policy);
75     }
76
77     /**
78      * Instantiates a new cache policy, and registers it with the given policy key.
79      */

80     protected CachePolicy(Object JavaDoc policyKey) {
81         CachePolicy.putPolicy(policyKey, this);
82     }
83
84     /**
85      * Instantiates a new cache policy without registering it
86      */

87     protected CachePolicy() {
88     }
89
90     /**
91      * Checks whether the policy advises to cache the passed object.
92      * @param o the object to check the cache for
93      * @return <code>true</code> if the policy advises to cache this object, <code>false</code> otherwise.
94      */

95     abstract public boolean checkPolicy(Object JavaDoc o);
96
97     /**
98      * Returns a description of the policy.
99      */

100     public String JavaDoc getDescription() {
101         return getClass().getName();
102     }
103
104 }
105
Popular Tags