View Javadoc

1   /*
2    *  Copyright 2004 David C. Brown
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 net.sf.ashkay.strategies;
17  
18  import java.lang.ref.ReferenceQueue;
19  import java.lang.ref.SoftReference;
20  import java.util.Map;
21  
22  import net.sf.ashkay.CacheEntry;
23  import net.sf.ashkay.ObjectCache;
24  
25  /***
26   * SoftReferenceCacheEntry represents a cache entry that is wrapped in which
27   * the entry object is wrapped in a SoftReference in order to allow the
28   * garbage collector to still collect the object if all other hard references
29   * are destroyed.
30   * <br><br>
31   * Often, this strategy will be used in memory sensative caches.
32   *
33   * @author <a href="mailto:bangroot@users.sf.net">Dave Brown</a>
34   * @see java.lang.ref.SoftReference
35   */
36  
37  public class SoftReferenceCacheEntry extends CacheEntry
38  {
39  	private CacheEntry theEntry;
40  
41  	public SoftReferenceCacheEntry(CacheEntry originalEntry, ReferenceQueue queue)
42  	{
43  		super();
44  		theEntry = originalEntry;
45  		theEntry.setEntryObject(new SoftReference(theEntry.getEntryObject(), queue));
46  	}
47  
48  	SoftReference getEntryReference()
49  	{
50  		return (SoftReference) theEntry.getEntryObject();
51  	}
52  
53  	public Object getEntryObject()
54  	{
55  		SoftReference ref = (SoftReference) theEntry.getEntryObject();
56  		return ref.get();
57  	}
58  
59  	public void addProperties(Map propertiesToAdd)
60  	{
61  		theEntry.addProperties(propertiesToAdd);
62  	}
63  
64  	public void addProperty(Object key, Object value)
65  	{
66  		theEntry.addProperty(key, value);
67  	}
68  
69  	public Object getProperty(Object key)
70  	{
71  		return theEntry.getProperty(key);
72  	}
73  
74  	public void setEntryObject(Object entryObject)
75  	{
76  		SoftReference ref = new SoftReference(entryObject);
77  		theEntry.setEntryObject(ref);
78  	}
79  
80  	public Object getEntryKey()
81  	{
82  		return theEntry.getEntryKey();
83  	}
84  
85  	public void setEntryKey(Object key)
86  	{
87  		theEntry.setEntryKey(key);
88  	}
89  
90  	public Map getProperties()
91  	{
92  		return theEntry.getProperties();
93  	}
94  
95  	public ObjectCache getCache()
96  	{
97  		return theEntry.getCache();
98  	}
99  
100 	public void setCache(ObjectCache myCache)
101 	{
102 		theEntry.setCache(myCache);
103 	}
104 }