01: /*
02: Copyright 2004 Philip Jacob <phil@whirlycott.com>
03: Seth Fitzsimmons <seth@note.amherst.edu>
04:
05: Licensed under the Apache License, Version 2.0 (the "License");
06: you may not use this file except in compliance with the License.
07: You may obtain a copy of the License at
08:
09: 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 last used, added, etc.
22: *
23: * @author Philip Jacob
24: */
25: public class Item<V> {
26:
27: protected V item;
28:
29: /**
30: * Relative time that the Item was added to the cache.
31: */
32: protected long added;
33:
34: /**
35: * Relative time that the Item was last used.
36: */
37: protected long used;
38:
39: /**
40: * Expire this Item after this much time.
41: */
42: protected long expiresAfter = -1;
43:
44: /**
45: * Number of times that the Item has been accessed.
46: */
47: protected volatile long count;
48:
49: /**
50: * Lock for the counter.
51: */
52: protected final Object countLock = new Object();
53:
54: public Item(final V _item, final long _added,
55: final long _expiresTime) {
56: item = _item;
57: added = _added;
58: expiresAfter = _expiresTime;
59: }
60:
61: /**
62: * @return Returns the item.
63: */
64: public V getItem() {
65: return item;
66: }
67:
68: void setUsed(final long _used) {
69: used = _used;
70: }
71:
72: void incrementCount() {
73: synchronized (countLock) {
74: count++;
75: }
76: }
77:
78: public synchronized long getAdded() {
79: return added;
80: }
81:
82: public synchronized long getCount() {
83: return count;
84: }
85:
86: public synchronized long getUsed() {
87: return used;
88: }
89:
90: public synchronized void setCount(final long count) {
91: this .count = count;
92: }
93:
94: public long getExpiresAfter() {
95: return expiresAfter;
96: }
97: }
|