View Javadoc

1   /***
2    * FIFOCache.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.LinkedList;
29  
30  /***
31   * The FIFO cache is based on a First In First Out algorithm to select the
32   * objects that are removed from the cache to make room. This implementation
33   * uses a LinkedList.
34   * 
35   * @author $Author: dbregeon $
36   * @version $Revision: 1.2 $
37   */
38  public class FIFOCache extends HashCache {
39  	/***
40  	 * The container that holds the objects.
41  	 */
42  	private final LinkedList keysList = new LinkedList();
43  
44  	/***
45  	 * Creates new LIFOCache
46  	 */
47  	public FIFOCache() {
48  		// Set FIFO policy.
49  		super(CachePolicy.FIFO);
50  	}
51  
52  	/***
53  	 * Creates new LIFOCache
54  	 * 
55  	 * @param minSize
56  	 *            the initial size of the Cache.
57  	 * @param maxSize
58  	 *            the maximum number of objects contained by the cache.
59  	 */
60  	public FIFOCache(int minSize, int maxSize) {
61  		// set FIFO policy.
62  		super(minSize, maxSize, CachePolicy.FIFO);
63  	}
64  
65  	/***
66  	 * This method adds an object in the cache.
67  	 * 
68  	 * @param key
69  	 *            the key that will be used to retrieve the object.
70  	 * @param value
71  	 *            the object to store in the cache.
72  	 * @throws CacheFullException
73  	 *             if the object could not be stored in the cache.
74  	 */
75  	public synchronized void registerObject(final Object key, final Object value)
76  			throws CacheFullException {
77  		if (getSize() >= getMaxSize()) {
78  			removeOneElement();
79  		}
80  		super.registerObject(key, value);
81  		this.keysList.add(key);
82  	}
83  
84  	/***
85  	 * This method removes an object from the cache.
86  	 * 
87  	 * @param key
88  	 *            the key that was used to store the object in the cache.
89  	 */
90  	public synchronized void unregisterObject(final Object key) {
91  		this.keysList.remove(key);
92  		super.unregisterObject(key);
93  	}
94  
95  	/***
96  	 * Removes all the objects from the Cache, leaving it empty.
97  	 */
98  	public synchronized void clear() {
99  		super.clear();
100 		this.keysList.clear();
101 		fireElementRemoved(buildEvent(null));
102 	}
103 
104 	/***
105 	 * This method is called when room is needed to add a new object in the
106 	 * cache.
107 	 */
108 	protected synchronized void removeOneElement() {
109 		final Object key = this.keysList.getFirst();
110 		unregisterObject(key);
111 	}
112 }