KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > cache > hash > DODSLinkedHashCache


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  */

20 package org.enhydra.dods.cache.hash;
21
22 import org.enhydra.dods.cache.base.DODSCache;
23
24 /**
25  * DODSLinkedHashCache class implements LRU cache (for storing data objects (or
26  * DataStruct objects), or simple queries, or complex queries), and provides
27  * query statistics about the cache (query number, cache hits number, their
28  * get/set/increment methods, percents of used cache, cache hits,...).
29  *
30  * @author Tanja Jovanovic
31  * @author Nenad Vico
32  * @author Zorica Suvajdzin
33  * @version 2.0 15.06.2003.
34  */

35 public class DODSLinkedHashCache extends LinkedHashCache implements DODSCache {
36
37     /**
38      * Total number of times the cache was accessed.
39      */

40     protected int cacheAccessNum = 0;
41
42     /**
43      * Number of queries performed on the cache successfully.
44      */

45     protected int cacheHitsNum = 0;
46
47     /**
48      * Constructor (int).
49      *
50      * @param maxSize Maximal number of objects in DODSLinkedHashCache.
51      */

52     DODSLinkedHashCache(int maxSize) {
53         super(maxSize);
54         clearStatistics();
55     }
56
57     /**
58      * Returns total number of times the cache was accessed.
59      *
60      * @return total number of times the cache was accessed.
61      */

62     public int getCacheAccessNum() {
63         return cacheAccessNum;
64     }
65
66     /**
67      * Sets total number of times the cache was accessed.
68      *
69      * @param num Total number of times the cache was accessed.
70      */

71     public void setCacheAccessNum(int num) {
72         this.cacheAccessNum = num;
73     }
74
75     /**
76      * Increases total number of times the cache was accessed.
77      */

78     public void incrementCacheAccessNum(int num) {
79         cacheAccessNum += num;
80     }
81
82     /**
83      * Returns number of queries performed on the cache successfully.
84      *
85      * @return Number of queries performed on the cache successfully.
86      */

87     public int getCacheHitsNum() {
88         return cacheHitsNum;
89     }
90
91     /**
92      * Sets number of queries performed on the cache successfully.
93      *
94      * @param cacheHitsNum Number of queries performed on the cache successfully.
95      */

96     public void setCacheHitsNum(int cacheHitsNum) {
97         this.cacheHitsNum = cacheHitsNum;
98     }
99
100     /**
101      * Increases number of queries performed on the cache successfully for one.
102      */

103     public void incrementCacheHitsNum(int num) {
104         cacheHitsNum += num;
105     }
106
107     /**
108      * Returns how much cache is currently used. This value is given in percents.
109      * If cache is unbounded, method returns 100%.
110      *
111      * @return Percents - how much cache is currently used.
112      */

113     public double getUsedPercents() {
114         int maxCacheSize = this.maxEntries;
115
116         if (maxCacheSize == 0) {
117             return 0;
118         }
119         if (maxCacheSize < 0) {
120             return 100;
121         }
122         int temp = size() * 10000;
123         double res = temp / maxCacheSize;
124
125         return res / 100;
126     }
127
128     /**
129      * Returns how much queries performed on the cache were successful.
130      * This value is given in percents.
131      *
132      * @return Percents - how much queries performed on the cache were
133      * successful.
134      */

135     public double getCacheHitsPercents() {
136         if (cacheAccessNum == 0) {
137             return 0;
138         }
139         int temp = cacheHitsNum * 10000;
140         double res = temp / cacheAccessNum;
141
142         return res / 100;
143     }
144
145     /**
146      * Clears statistics.
147      */

148     public void clearStatistics() {
149         this.cacheAccessNum = 0;
150         this.cacheHitsNum = 0;
151     }
152     
153     public boolean isNeedToSynchronize() {
154         return false;
155     }
156     
157 }
158
Popular Tags