KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > whirlycott > cache > Item


1 /*
2  Copyright 2004 Philip Jacob <phil@whirlycott.com>
3  Seth Fitzsimmons <seth@note.amherst.edu>
4  
5  Licensed under the Apache License, Version 2.0 (the "License");
6  you may not use this file except in compliance with the License.
7  You may obtain a copy of the License at
8  
9  http://www.apache.org/licenses/LICENSE-2.0
10  
11  Unless required by applicable law or agreed to in writing, software
12  distributed under the License is distributed on an "AS IS" BASIS,
13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  See the License for the specific language governing permissions and
15  limitations under the License.
16  */

17
18 package com.whirlycott.cache;
19
20 /**
21  * Wraps an item in the cache and records some information about when it was
22  * last used, added, etc.
23  *
24  * @author Philip Jacob
25  */

26 public class Item {
27     
28     protected Object JavaDoc item;
29     
30     /** Relative time that the Item was added to the cache. */
31     protected long added;
32     
33     /** Relative time that the Item was last used. */
34     protected long used;
35     
36     /** Expire this Item after this much time. */
37     protected long expiresAfter = -1;
38     
39     /** Number of times that the Item has been accessed. */
40     protected volatile long count;
41     
42     /**
43      * Lock for the counter.
44      */

45     protected final Object JavaDoc countLock = new Object JavaDoc();
46     
47     public Item(final Object JavaDoc _item, final long _added, final long _expiresTime) {
48         item = _item;
49         added = _added;
50         expiresAfter = _expiresTime;
51     }
52     
53     /**
54      * @return Returns the item.
55      */

56     public Object JavaDoc getItem() {
57         return item;
58     }
59     
60     void setUsed(final long _used) {
61         used = _used;
62     }
63     
64     void incrementCount() {
65         synchronized(countLock) {
66             count++;
67         }
68     }
69
70     public synchronized long getAdded() {
71         return added;
72     }
73     
74     public synchronized long getCount() {
75         return count;
76     }
77     
78     public synchronized long getUsed() {
79         return used;
80     }
81     
82     public synchronized void setCount(final long count) {
83         this.count = count;
84     }
85     
86     public long getExpiresAfter() {
87         return expiresAfter;
88     }
89 }
90     
91
Popular Tags