View Javadoc

1   /***
2    * CachePolicy.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.caches;
27  
28  import java.util.Hashtable;
29  
30  import org.jcreme.enumerations.Enumeration;
31  
32  /***
33   * This Enumeration subclass describes the possible policies to manage the cache
34   * contents. Each member is a cache factory to be used by AggregateCache
35   * instances.
36   * 
37   * Current implemented values are: NONE (this gives a HashCache): replacement is
38   * undefined. FIFO (this gives a FIFOCache): new elements push older ones. The
39   * oldest is removed. LUFO (this gives a LUFOCache): keep statistics on cache
40   * element usage. The least used element is removed.
41   * 
42   * The policy choice for a cache depends heavily on the use of that cache.
43   * 
44   * @author $Author: dbregeon $
45   * @version $Revision: 1.3 $
46   */
47  public class CachePolicy extends Enumeration {
48  	static final long serialVersionUID = 5687690222374771750L;
49  
50  	/***
51  	 * The CachePolicy instances by their name.
52  	 */
53  	private static final Hashtable fromName = new Hashtable();
54  
55  	/***
56  	 * The name for the 'FIFO' enumeration value.
57  	 */
58  	public static final String S_FIFO = "FIFO";
59  
60  	/***
61  	 * The 'FIFO' enumeration value.
62  	 */
63  	public static final CachePolicy FIFO = new CachePolicy(S_FIFO);
64  
65  	/***
66  	 * The name for the 'LUFO' enumeration value.
67  	 */
68  	public static final String S_LUFO = "LEAST_USED";
69  
70  	/***
71  	 * The 'LUFO' enumeration value.
72  	 */
73  	public static final CachePolicy LUFO = new CachePolicy(S_LUFO);
74  
75  	/***
76  	 * The name for the 'NONE' enumeration value.
77  	 */
78  	public static final String S_NONE = "NONE";
79  
80  	/***
81  	 * The 'NONE' enumeration value.
82  	 */
83  	public static final CachePolicy NONE = new CachePolicy(S_NONE);
84  
85  	/***
86  	 * Creates new CachePolicy
87  	 * 
88  	 * @param name
89  	 *            the name of this enumerated value. It cannot be null.
90  	 * @throws InvalidParameterException
91  	 *             if one of the parameters is null.
92  	 */
93  	protected CachePolicy(String name) {
94  		// Same as super.
95  		super(name);
96  	}
97  
98  	/***
99  	 * This method is for internal use only.
100 	 * 
101 	 * @return the Hashtable that links the enumerated values names with the
102 	 *         actual enumerated value.
103 	 */
104 	protected Hashtable getFromName() {
105 		return fromName;
106 	}
107 
108 	/***
109 	 * This method retrieves a CachePolicy instance from its name.
110 	 * 
111 	 * @param name
112 	 *            the name of the CachePolicy instance.
113 	 * @return the CachePolicy that has this name. Null if no value
114 	 *         corresponding to this name was found.
115 	 */
116 	public static CachePolicy get(final String name) {
117 		CachePolicy result = null;
118 		if (name != null) {
119 			result = (CachePolicy) fromName.get(name);
120 		}
121 		return result;
122 	}
123 
124 	/***
125 	 * This methods gives the instances of this class.
126 	 * 
127 	 * @return all the instances of this class.
128 	 */
129 	public static CachePolicy[] getCachePolicies() {
130 		return (CachePolicy[]) fromName.values().toArray(new CachePolicy[0]);
131 	}
132 
133 	/***
134 	 * This method provides a new cache that implements the policy.
135 	 * 
136 	 * @param minSize
137 	 *            the initial size of the cache.
138 	 * @param maxSize
139 	 *            the maximum size of the created cache.
140 	 * @return a new cache that implements the policy.
141 	 */
142 	public BaseCache getCacheInstance(final int minSize, final int maxSize) {
143 		BaseCache result = null;
144 		if (this == FIFO) {
145 			result = new FIFOCache(minSize, maxSize);
146 		}
147 		if (this == LUFO) {
148 			result = new LUFOCache(minSize, maxSize);
149 		}
150 		if (this == NONE) {
151 			result = new HashCache(minSize, maxSize, this);
152 		}
153 		return result;
154 	}
155 
156 	/***
157 	 * This method provides a new cache that implements the policy.
158 	 * 
159 	 * @return a new cache that implements the policy.
160 	 */
161 	public BaseCache getCacheInstance() {
162 		BaseCache result = null;
163 		if (this == FIFO) {
164 			result = new FIFOCache();
165 		}
166 		if (this == LUFO) {
167 			result = new LUFOCache();
168 		}
169 		if (this == NONE) {
170 			result = new HashCache();
171 		}
172 		return result;
173 	}
174 
175 	/***
176 	 * This method enables to retrieve all the possible values of an Enumeration
177 	 * class.
178 	 * 
179 	 * @return the values for this class.
180 	 */
181 	public static Enumeration[] getValues() {
182 		return getCachePolicies();
183 	}
184 }