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;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  /***
22   * CacheEntry represents a single entry in the object cache. A cache entry
23   * contains the cached object and properties associated with the entry. These
24   * properties may be used by the CachingStrategy to detirmine if an entry
25   * passes validation.
26   *
27   * @author <a href="mailto:bangroot@users.sf.net">Dave Brown</a>
28   */
29  
30  public class CacheEntry
31  {
32  	private Object cachedObject;
33  
34  	private Object key;
35  	private Map properties;
36  	private ObjectCache myCache;
37  
38  	/***
39  	 * Empty constructor for subclasses only
40  	 */
41  	protected CacheEntry()
42  	{
43  	}
44  
45  	/***
46  	 * Creates a default CacheEntry
47  	 *
48  	 * @param entryObject - the object this entry represents
49  	 */
50  	public CacheEntry(Object entryKey, Object entryObject)
51  	{
52  		this(entryKey, entryObject, new HashMap(), null);
53  	}
54  
55  	/***
56  	 * Creates a CacheEntry with properties pre-specified
57  	 *
58  	 * @param entryObject     - the object this entry represents
59  	 * @param entryProperties - the properties of this entry
60  	 */
61  	public CacheEntry(Object entryKey, Object entryObject, Map entryProperties)
62  	{
63  		this(entryKey, entryObject, entryProperties, null);
64  	}
65  
66  	public CacheEntry(Object entryKey, Object entryObject, Map entryProperties, ObjectCache theCache)
67  	{
68  		key = entryKey;
69  		cachedObject = entryObject;
70  		properties = entryProperties;
71  		myCache = theCache;
72  	}
73  
74  	public Object getEntryKey()
75  	{
76  		return key;
77  	}
78  
79  	public void setEntryKey(Object key)
80  	{
81  		this.key = key;
82  	}
83  
84  	/***
85  	 * Returns the object this entry represents.
86  	 *
87  	 * @return the entries object
88  	 */
89  	public Object getEntryObject()
90  	{
91  		return cachedObject;
92  	}
93  
94  	/***
95  	 * Sets the object this entry represents
96  	 *
97  	 * @param entryObject - the new object to represent
98  	 */
99  	public void setEntryObject(Object entryObject)
100 	{
101 		cachedObject = entryObject;
102 	}
103 
104 	/***
105 	 * Adds a property to this entry.
106 	 *
107 	 * @param key   - the key to this property
108 	 * @param value - the value to hold in the property
109 	 */
110 	public void addProperty(Object key, Object value)
111 	{
112 		synchronized (properties)
113 		{
114 			properties.put(key, value);
115 		}
116 	}
117 
118 	/***
119 	 * Looks up a property of this entry
120 	 *
121 	 * @param key - the key of the property to lookup
122 	 * @return the value of the property or null if not set
123 	 */
124 	public Object getProperty(Object key)
125 	{
126 		synchronized (properties)
127 		{
128 			return properties.get(key);
129 		}
130 	}
131 
132 	/***
133 	 * Adds a group of properties to this entry, en masse
134 	 *
135 	 * @param propertiesToAdd - map of properties to add
136 	 */
137 	public void addProperties(Map propertiesToAdd)
138 	{
139 		synchronized (properties)
140 		{
141 			properties.putAll(propertiesToAdd);
142 		}
143 	}
144 
145 	/***
146 	 * Returns the entire Map of this entry's properties
147 	 *
148 	 * @return the properties
149 	 */
150 	public Map getProperties()
151 	{
152 		return properties;
153 	}
154 
155 	public ObjectCache getCache()
156 	{
157 		return myCache;
158 	}
159 
160 	public void setCache(ObjectCache myCache)
161 	{
162 		this.myCache = myCache;
163 	}
164 }