KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > cache > CacheLine


1 /*
2  * $Id: CacheLine.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.util.cache;
26
27 import java.io.Serializable JavaDoc;
28
29 import org.ofbiz.base.util.UtilObject;
30
31 /**
32  *
33  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
34  * @version $Rev: 5462 $
35  * @since 3.2
36  */

37 public class CacheLine implements Serializable JavaDoc {
38
39     public static final String JavaDoc module = CacheLine.class.getName();
40
41     public Object JavaDoc valueRef = null;
42     public long loadTime = 0;
43     public long expireTime = 0;
44     public boolean useSoftReference = false;
45
46     public CacheLine(Object JavaDoc value, boolean useSoftReference, long expireTime) {
47         if (useSoftReference) {
48             this.valueRef = new CacheSoftReference(value);
49         } else {
50             this.valueRef = value;
51         }
52         this.useSoftReference = useSoftReference;
53         this.expireTime = expireTime;
54     }
55
56     public CacheLine(Object JavaDoc value, boolean useSoftReference, long loadTime, long expireTime) {
57         this(value, useSoftReference, expireTime);
58         this.loadTime = loadTime;
59     }
60
61     public Object JavaDoc getValue() {
62         if (valueRef == null) return null;
63         if (useSoftReference) {
64             return ((CacheSoftReference) valueRef).get();
65         } else {
66             return valueRef;
67         }
68     }
69     
70     public boolean softReferenceCleared() {
71         if (!this.useSoftReference || valueRef == null) {
72             return false;
73         } else {
74             if (((CacheSoftReference) valueRef).get() == null) {
75                 return true;
76             } else {
77                 return false;
78             }
79         }
80     }
81
82     public void setUseSoftReference(boolean useSoftReference) {
83         if (this.useSoftReference != useSoftReference) {
84             synchronized (this) {
85                 this.useSoftReference = useSoftReference;
86                 if (useSoftReference) {
87                     this.valueRef = new CacheSoftReference(this.valueRef);
88                 } else {
89                     this.valueRef = ((CacheSoftReference) this.valueRef).get();
90                 }
91             }
92         }
93     }
94
95     public long getExpireTime() {
96         return this.expireTime;
97     }
98
99     public long getSizeInBytes() {
100         return UtilObject.getByteCount(this);
101     }
102 }
103
104
Popular Tags