KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > cache > CacheEntry


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Developer of the Shared Modifications is Jahia Solution Sarl.
30  * Portions created by the Initial Developer are Copyright (C) 2002 by the
31  * Initial Developer. All Rights Reserved.
32  *
33  * Contributor(s):
34  * 29-JUL-2003, Jahia Solutions Sarl, Fulco Houkes : Initial version
35  *
36  * ----- END LICENSE BLOCK -----
37  */

38
39
40 package org.jahia.services.cache;
41
42 import java.util.Map JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.io.Serializable JavaDoc;
46
47
48 /** <p>This class represents an entry in a class.</p>
49  * <p>Each entry holds an object and some statistics on that object,
50  * like its expiration date, last accessed date and the amount of times it has
51  * been request from the cache.</p>
52  *
53  * @author Fulco Houkes, Copyright (c) 2003 by Jahia Ltd.
54  * @version 1.0
55  * @since Jahia 4.0
56  * @see org.jahia.services.cache.Cache Cache
57  */

58 public class CacheEntry implements Serializable JavaDoc {
59
60     /** the normal operation mode constant. */
61     public static final String JavaDoc MODE_NORMAL = "normal";
62
63     /** the debugging operation mode constant. */
64     public static final String JavaDoc MODE_DEBUG = "debug";
65
66     /** the edition operation mode constant. */
67     public static final String JavaDoc MODE_EDIT = "edit";
68
69
70     /** the entry object. */
71     private Object JavaDoc object;
72
73
74     /** number of time the entry was requested. */
75     protected int hits = 0;
76
77     /** the operation mode. */
78     protected String JavaDoc operationMode = ""; // this can take the values
79
// "normal", "edit" or "debug" (see ParamBean defined modes)
80

81     /** the properties table. */
82     protected Map JavaDoc properties = new HashMap JavaDoc ();
83
84     /** the entry's expiration date. */
85     protected Date JavaDoc expirationDate;
86
87     /** last accessed date. */
88     protected long lastAccessedTimeMillis;
89
90     /** creation date. */
91     protected long creationTimeMillis;
92
93
94     /** <p>Default constructor, creates a new <code>CacheEntry</code> instance.</p>
95      */

96     public CacheEntry () {
97       creationTimeMillis = System.currentTimeMillis();
98     }
99
100
101     /** <p>Creates a new <code>CacheEntry</code> object instance.</p>
102      *
103      * @param entryObj the object instance to associate with the cache entry
104      */

105     public CacheEntry (final Object JavaDoc entryObj) {
106         object = entryObj;
107         creationTimeMillis = System.currentTimeMillis();
108     }
109
110
111     /** <p>Retrieves the object associated with this cache entry.</p>
112      *
113      * @return the stored object
114      */

115     public Object JavaDoc getObject () {
116         return object;
117     }
118
119
120     /** <p>Sets/Updates the object associated to this cache entry.</p>
121      *
122      * @param object the new object to store in this cache entry.
123      */

124     public void setObject (final Object JavaDoc object) {
125         this.object = object;
126     }
127
128
129     /** <p>Set the property of name <code>key</code> with the specified value
130      * <code>value</code>.</p>
131      *
132      * <p>Properties can have a <code>null</code> value, but key names do not. When a property
133      * already exists in the cache entry, the property's value is updated. When the
134      * <code>name</code> is </code>null</code>, the set operation is canceled.</p>
135      *
136      * @param key the property's key
137      * @param value the property's value
138      */

139     public void setProperty (final String JavaDoc key, final Object JavaDoc value) {
140         if (key == null)
141             return;
142         properties.put (key, value);
143     }
144
145
146     /** <p>Retrieve the property value associated with the key <code>key</code>.</p>
147      *
148      * <p>Propertiescan have a <code>null</code> value, but key names must be not
149      * <code>null</code>. When value of <code>null</code> is returned, it does mean
150      * the key is not found in the entry or the associated property has a
151      * <code>null</code> value. The two cases can be distinguished with the
152      * <code>containsKey()</code> method.</p>
153      *
154      * @param key the property key
155      *
156      * @return return the property value.
157      */

158     public Object JavaDoc getProperty (final String JavaDoc key) {
159         return properties.get (key);
160     }
161
162
163     /** <p>Returns <code>true</code> if this entry contains the mapping for the specified
164      * property name <code>key</code>.</p>
165      *
166      * @param key the property name
167      *
168      * @return <code>true</code> if this entry contains the mapping for the specified
169      * property name <code>key</code>.
170      */

171     public boolean containsKey (final String JavaDoc key) {
172         if (key == null)
173             return false;
174         return properties.containsKey (key);
175     }
176
177
178     /** <p>Retrieves the properties <code>Map</code> instance.</p>
179      *
180      * @return the properties <code>Map</code> instance
181      */

182     public Map JavaDoc getExtendedProperties () {
183         return properties;
184     }
185
186
187     /** <p>Sets the new properties <code>Map</code> instance. The operation is
188      * ignored if the <code>newProperties</code> is <code>null</code>.</p>
189      *
190      * @param newProperties the new properties
191      */

192     public void setExtendedProperties (final Map JavaDoc newProperties) {
193         if (newProperties == null)
194             return;
195         properties = newProperties;
196     }
197
198
199     /** <p>Retrieves the number of hits of the entry (the number of times the
200      * entry was requested).</p>
201      *
202      * @return the entry's hits
203      */

204     final public int getHits () {
205         return hits;
206     }
207
208
209     /** <p>Resets the number of times the entry was requested.</p>
210      */

211     final public void resetHits () {
212         hits = 0;
213     }
214
215
216     /** <p>Increments the number of hits by one.</p>
217      * */

218     final public void incrementHits () {
219         hits++;
220     }
221
222
223     /** <p>Retrieve the operation mode.</p>
224      *
225      * <p>Returned values are <code>MODE_NORMAL</code>, <code>MODE_EDIT</code>
226      * or <code>MODE_DEBUG</code>, corresponding respectively to the normal
227      * operation mode, the edition mode and debugginf mode.</p>
228      *
229      * @return the operation mode.
230      */

231     final public String JavaDoc getOperationMode () {
232         return this.operationMode;
233     }
234
235
236     /** <p>Sets the operation mode.</p>
237      *
238      * <p>Accepted values are <code>MODE_NORMAL</code>, <code>MODE_EDIT</code>
239      * or <code>MODE_DEBUG</code>, corresponding respectively to the normal
240      * operation mode, the edition mode and debugginf mode.</p>
241      *
242      * @param opMode the operation mode
243      */

244     final public void setOperationMode (final String JavaDoc opMode) {
245         this.operationMode = opMode;
246     }
247
248
249     /** <p>Retrieves the entry's expiration date.</p>
250      *
251      * @return the expiration date
252      */

253     final public Date JavaDoc getExpirationDate () {
254         return expirationDate;
255     }
256
257
258     /** <p>Sets the entry's expiration date.</p>
259      *
260      * @param expirationDate the expiration date
261      */

262     final public void setExpirationDate (Date JavaDoc expirationDate) {
263         this.expirationDate = expirationDate;
264     }
265
266
267     /** <p>Set the last accessed date to the current time.</p>
268      */

269     final public void setLastAccessedTimeNow () {
270         lastAccessedTimeMillis = System.currentTimeMillis();
271         // lastAccessedDate = new Date ();
272
}
273     public long getLastAccessedTimeMillis() {
274         return lastAccessedTimeMillis;
275     }
276
277
278     /**
279      * Returns entrie's creation time.
280      * @return entrie's creation time
281      */

282     public long getCreationTimeMillis()
283     {
284       return creationTimeMillis;
285     }
286
287 }
288
Popular Tags