KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > fjank > jcache > AttributesImpl


1 /* Open Source Java Caching Service
2  * Copyright (C) 2002 Frank Karlstrøm
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * The author can be contacted by email: fjankk@sourceforge.net
18  */

19 package org.fjank.jcache;
20
21 import java.io.Serializable JavaDoc;
22 import javax.util.jcache.Attributes;
23 import javax.util.jcache.CacheEventListener;
24 import javax.util.jcache.CacheLoader;
25 import javax.util.jcache.InvalidArgumentException;
26
27 /**
28  * This class defines the attributes an object in the cache can have.
29  */

30 public final class AttributesImpl implements Cloneable JavaDoc, Serializable JavaDoc, Attributes {
31     private static final int ONE_DAY = 24;
32     private static final int ONE_HOUR = 60;
33     private static final int ONE_MINUTE = 60;
34     /** the time the associated CacheObject was created. */
35     private long createTime = System.currentTimeMillis();
36     /** the default time the associated CacheObject should live in the cache */
37     private long defaultTTL = -1;
38     /**
39      * Is used to specify wich attributes should be set in the attributes
40      * object. The differnet attributes wich is valid is defined as public
41      * static variables in the {@link Attributes}class.
42      */

43     private long flags;
44     /** the time the associated CacheObject should be idle in the cache */
45     private long idleTime;
46     /** the CacheListener for the associated CacheObject */
47     private transient CacheEventListener listener;
48     /** the CacheLoader for the associated CacheObject */
49     private transient CacheLoader loader;
50     /** the size for the associated CacheObject */
51     private int size;
52     /** the time the associated CacheObject should be live the cache */
53     private long timeToLive = -1;
54     /** the version of the associated CacheObject */
55     private long version;
56
57     /**
58      * Creates new Attributes
59      */

60     public AttributesImpl() {
61     }
62
63     public AttributesImpl(Attributes attributes) {
64         this.defaultTTL=attributes.getDefaultTimeToLive();
65         this.idleTime=attributes.getIdleTime();
66         
67         this.listener=attributes.getListener();
68         this.loader=attributes.getLoader();
69         this.size=attributes.getSize();
70         this.timeToLive=attributes.getTimeToLive();
71         this.version=attributes.getVersion();
72         long flags = 0;
73            
74         if(attributes.isSet(Attributes.DISTRIBUTE)) {
75             flags |= Attributes.DISTRIBUTE;
76         }
77         if(attributes.isSet(Attributes.NOFLUSH)) {
78             flags |= Attributes.NOFLUSH;
79         }
80         if(attributes.isSet(Attributes.REPLY)) {
81             flags |= Attributes.REPLY;
82         }
83         if(attributes.isSet(Attributes.SYNCHRONIZE)) {
84             flags |= Attributes.SYNCHRONIZE;
85         }
86         if(attributes.isSet(Attributes.SPOOL)) {
87             flags |= Attributes.SPOOL;
88         }
89         if(attributes.isSet(Attributes.GROUP_TTL_DESTROY)) {
90             flags |= Attributes.GROUP_TTL_DESTROY;
91         }
92         if(attributes.isSet(Attributes.ORIGINAL)) {
93             flags |= Attributes.ORIGINAL;
94         }
95         this.flags=flags;
96     }
97
98     public void applyAttributes(Attributes attributes) {
99         this.idleTime = attributes.getIdleTime();
100         this.timeToLive = attributes.getTimeToLive();
101         this.defaultTTL = attributes.getDefaultTimeToLive();
102         CacheEventListener listener = attributes.getListener();
103         setListener(Attributes.INVALIDATE_EVENT, listener);
104     }
105
106     public boolean equals(Object JavaDoc arg0) {
107         if (arg0 instanceof AttributesImpl) {
108             AttributesImpl obj = (AttributesImpl) arg0;
109             if (defaultTTL != obj.defaultTTL)
110                 return false;
111             if (flags != obj.flags)
112                 return false;
113             if (listener != obj.listener)
114                 return false;
115             if (loader != obj.loader)
116                 return false;
117             if (size != obj.size)
118                 return false;
119             if (timeToLive != obj.timeToLive)
120                 return false;
121             if (version != obj.version)
122                 return false;
123             return true;
124         }
125         return super.equals(arg0);
126     }
127
128     /**
129      * returns the time the object was loaded into the cache. The time is the
130      * number of milliseconds since midnight, January 1, 1970 (UTC).
131      *
132      * @return the time the object was loaded into the cache. The time is the
133      * number of milliseconds since midnight, January 1, 1970 (UTC).
134      */

135     public long getCreateTime() {
136         return createTime;
137     }
138
139     public long getDefaultTimeToLive() {
140         return defaultTTL;
141     }
142
143     /**
144      * returns the current value for the idle time interval.
145      *
146      * @return the current value for the idle time interval.
147      */

148     public long getIdleTime() {
149         return idleTime;
150     }
151
152     /**
153      * Gets the CacheEventListener.
154      * @author Philippe Fajeau
155      * @return The CacheEventListener.
156      */

157     public CacheEventListener getListener() {
158         return listener;
159     }
160
161     /**
162      * returns the {@link CacheLoader}attribute.
163      *
164      * @return the {@link CacheLoader}attribute.
165      */

166     public CacheLoader getLoader() {
167         return loader;
168     }
169
170     /**
171      * returns the size of the object. this size is set by the {@link
172      * #setSize(int)} method, or in the case of StreamAccess objects, the size
173      * is calculated by the cache.
174      * @todo create and implement an online size calculator.
175      * @return the size of the object, or 0 if the size has not been set.
176      */

177     public int getSize() {
178         return size;
179     }
180
181     /**
182      * returns the current value for the time to live interval.
183      *
184      * @return the current value for the time to live interval.
185      */

186     public long getTimeToLive() {
187         if (defaultTTL != -1 && timeToLive == -1) {
188             return defaultTTL;
189         }
190         return timeToLive;
191     }
192
193     /**
194      * returns the current value of version.
195      *
196      * @return the current value of version.
197      */

198     public long getVersion() {
199         return version;
200     }
201
202     /**
203      * Checks wether the flags are set or not.
204      *
205      * @param theFlags the flags to be checked. may be OR-ed together, for wich
206      * this method will return true only if all flags are set.
207      *
208      * @return true if the specified attribute is set, false otherwise.
209      */

210     public boolean isSet(final long theFlags) {
211         return ((this.flags | theFlags) ^ this.flags) == 0;
212     }
213
214     /**
215      * resets the Attributes to its default values. The attributes wich are
216      * reset are expiration time attributes, time to live, default time to
217      * live, idle time and event handlers.
218      *
219      * @todo This method should be package private, thus this class should be
220      * moved to org.fjank.jcache, an interface should be extracted, and
221      * that should be public, and in this package.
222      */

223     public void reset() {
224         AttributesImpl def = new AttributesImpl();
225         this.idleTime = def.idleTime;
226         this.timeToLive = def.timeToLive;
227         this.defaultTTL = def.defaultTTL;
228         this.listener = def.listener;
229     }
230
231     /**
232      * Sets the createTime.
233      *
234      * @param aCreateTime The createTime to set
235      */

236     public void setCreateTime(final long aCreateTime) {
237         if (aCreateTime < 0) {
238             return;
239         }
240         this.createTime = aCreateTime;
241     }
242
243     /**
244      * Will set the maximum time the associated cache object will stay in the
245      * cache before it is invalidated. For regions and groups, this will
246      * establish a default time to live that is applied individually to each
247      * member in the group or region. It will not cause the entire group or
248      * region to time out as a whole. For individual objects, the default time
249      * to live is equivalent with time to live. If both are set the default time
250      * to live is ignored. The time starts when the object is loaded into the
251      * cache(by the {@link CacheLoader}objector put by the {@link
252      * CacheAccess#replace(Object, Object)}) or when the time to live attribute
253      * is set by the {@link CacheLoader#setAttributes(Object,Attributes)}
254      * method.
255      *
256      * @param ttl the time to live in seconds. The {@link #timeToSeconds(int,
257      * int, int, int)} can be used to convert days, hours, minutes to
258      * secounds.
259      *
260      * @throws InvalidArgumentException if a negative value for ttl is supplied.
261      */

262     public void setDefaultTimeToLive(final long ttl) throws InvalidArgumentException {
263         if (ttl < 0) {
264             throw new InvalidArgumentException("Default time to live must be a positive number.");
265         }
266         this.defaultTTL = ttl;
267     }
268
269     /**
270      * Is used to specify wich attributes should be set in the attributes
271      * object. The different attributes wich is valid is defined as public
272      * static variables in the {@link Attributes}class.
273      *
274      * @param theFlags The attributes to set. the attributes may be OR-ed
275      * together. I.e. Attributes.DISTRIBUTE | Attributes.SYNCHRONIZE
276      * Invalid flags are silently ignored. To reset all flags you use
277      * 0 as a parameter. I.e. setFlags(0)
278      */

279     public void setFlags(final long theFlags) {
280         if (theFlags < 0) {
281             return;
282         }
283         this.flags = theFlags;
284     }
285
286     /**
287      * sets the maximum time the associated cache object will remain in the
288      * cache without being referenced before it is invalidated.
289      *
290      * @param idle is in seconds. The {@link #timeToSeconds(int, int, int,int)}
291      * can be used to convert days, hours, minutes to secounds.
292      *
293      * @throws InvalidArgumentException if a negative value for idle is
294      * supplied.
295      */

296     public void setIdleTime(final long idle) throws InvalidArgumentException {
297         if (idle < 0) {
298             throw new InvalidArgumentException("Idle time must be a positive number.");
299         }
300         this.idleTime = idle;
301     }
302
303     /**
304      * Register an event listener object to be executed when the specified event
305      * occurs with relationship to the associated object. Currently the only
306      * invalidate event being monitored is Attributes.INVALIDATE_EVENT. If
307      * invalid parameters are passed such as invalid events, or null as
308      * listener, this method silently returns without doing any changes to this
309      * object.
310      *
311      * @param event The event to listen for.
312      * @param aListener the listener to fire when the event occurs.
313      *
314      *@todo Should these Attributes only have one Listener, or should several be
315      * supported?
316      */

317     public void setListener(final int event, final CacheEventListener aListener) {
318         if ((event == INVALIDATE_EVENT) && (aListener != null)) {
319             this.listener = aListener;
320         }
321     }
322
323     /**
324      * Will associate a loader object with this object.
325      *
326      * @param aLoader The loader to set. This parameter can be null.
327      */

328     public void setLoader(final CacheLoader aLoader) {
329         this.loader = aLoader;
330     }
331
332     /**
333      * Is used to specify the size in bytes of the object being cached. This is
334      * used to determine when the cache capacity is reached. If the cache is not
335      * using object size to determine the capacity (It can also use object
336      * counts) this value is ignored.
337      *
338      * @param aSize the size to be set. if this parameter is smaller than zero,
339      * this method silently returns.
340      */

341     public void setSize(final int aSize) {
342         if (aSize < 0) {
343             return;
344         }
345         this.size = aSize;
346     }
347
348     /**
349      * Will set the maximum time the associated cache object will stay in the
350      * cache before it is invalidated. The time starts when the object is loaded
351      * into the cache(by the {@link CacheLoader}object or put by the
352      * {@link CacheAccess#replace(Object, Object)}) or when the time to live
353      * attribute is set by the {@link CacheLoader#setAttributes(Object,
354      * Attributes)} method.
355      *
356      * @param ttl the time to live in seconds. The {@link #timeToSeconds(int,
357      * int, int, int)} can be used to convert days, hours, minutes to
358      * seconds.
359      *
360      * @throws InvalidArgumentException if a negative value for ttl is supplied.
361      */

362     public void setTimeToLive(final long ttl) throws InvalidArgumentException {
363         if (ttl < 0) {
364             throw new InvalidArgumentException("Time to live must be a positive number.");
365         }
366         this.timeToLive = ttl;
367     }
368
369     /**
370      * Sets the version attribute. Is only maintained for user convenience. It
371      * is not used internally by the cache.
372      *
373      * @param aVersion the version number to set.
374      */

375     public void setVersion(final long aVersion) {
376         this.version = aVersion;
377     }
378
379     /**
380      * Will convert the time specified into seconds.
381      *
382      * @param days number of days.
383      * @param hours number of hours.
384      * @param minutes number of minutes.
385      * @param seconds number of seconds.
386      *
387      * @return the converted time in seconds.
388      *
389      * @throws InvalidArgumentException if any of the parameters are negative
390      * values.
391      */

392     public long timeToSeconds(final int days, final int hours, final int minutes, final int seconds) throws InvalidArgumentException {
393         if (days < 0) {
394             throw new InvalidArgumentException("Days must be larger than zero.");
395         }
396         if (hours < 0) {
397             throw new InvalidArgumentException("Hours must be larger than zero.");
398         }
399         if (minutes < 0) {
400             throw new InvalidArgumentException("Minutes must be larger than zero.");
401         }
402         if (seconds < 0) {
403             throw new InvalidArgumentException("Seconds must be larger than zero.");
404         }
405         return seconds + (ONE_MINUTE * minutes) + (ONE_MINUTE * ONE_HOUR * hours) + (ONE_MINUTE * ONE_HOUR * ONE_DAY * days);
406     }
407
408     /**
409      * returns these Attributes as a String.
410      *
411      * @return these Attributes as a String.
412      */

413     public String JavaDoc toString() {
414         return "Attributes {ttl:" + timeToLive + '}'; //$NON-NLS-1$
415
}
416 }
Popular Tags