01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.base.util.cache;
19:
20: import java.io.Serializable;
21:
22: import org.ofbiz.base.util.UtilObject;
23:
24: public class CacheLine implements Serializable {
25:
26: public static final String module = CacheLine.class.getName();
27:
28: public Object valueRef = null;
29: public long loadTime = 0;
30: public long expireTime = 0;
31: public boolean useSoftReference = false;
32:
33: public CacheLine(Object value, boolean useSoftReference,
34: long expireTime) {
35: if (useSoftReference) {
36: this .valueRef = new CacheSoftReference(value);
37: } else {
38: this .valueRef = value;
39: }
40: this .useSoftReference = useSoftReference;
41: this .expireTime = expireTime;
42: }
43:
44: public CacheLine(Object value, boolean useSoftReference,
45: long loadTime, long expireTime) {
46: this (value, useSoftReference, expireTime);
47: this .loadTime = loadTime;
48: }
49:
50: public Object getValue() {
51: if (valueRef == null)
52: return null;
53: if (useSoftReference) {
54: return ((CacheSoftReference) valueRef).get();
55: } else {
56: return valueRef;
57: }
58: }
59:
60: public boolean softReferenceCleared() {
61: if (!this .useSoftReference || valueRef == null) {
62: return false;
63: } else {
64: if (((CacheSoftReference) valueRef).get() == null) {
65: return true;
66: } else {
67: return false;
68: }
69: }
70: }
71:
72: public void setUseSoftReference(boolean useSoftReference) {
73: if (this .useSoftReference != useSoftReference) {
74: synchronized (this ) {
75: this .useSoftReference = useSoftReference;
76: if (useSoftReference) {
77: this .valueRef = new CacheSoftReference(
78: this .valueRef);
79: } else {
80: this .valueRef = ((CacheSoftReference) this .valueRef)
81: .get();
82: }
83: }
84: }
85: }
86:
87: public long getExpireTime() {
88: return this .expireTime;
89: }
90:
91: public long getSizeInBytes() {
92: return UtilObject.getByteCount(this);
93: }
94: }
|