KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > dods > cache > DODSHashMap


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;
21
22 import java.util.HashMap JavaDoc;
23 import org.enhydra.dods.statistics.CacheStatistics;
24
25 /**
26  * DODSHashMap class implements Hash map (for storing data objects), and
27  * provides 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  * @version 1.0 15.09.2003.
32  */

33 public class DODSHashMap extends HashMap JavaDoc implements CacheStatistics {
34
35     /**
36      * Total number of times the cache was accessed.
37      */

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

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

50     DODSHashMap() {
51         super();
52         clearStatistics();
53     }
54
55     /**
56      * Returns total number of times the cache was accessed.
57      *
58      * @return total number of times the cache was accessed.
59      */

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

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

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

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

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

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

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

130     public double getCacheHitsPercents() {
131         if (cacheAccessNum == 0) {
132             return 0;
133         }
134         int temp = cacheHitsNum * 10000;
135         double res = temp / cacheAccessNum;
136
137         return res / 100;
138     }
139
140     /**
141      * Clears statistics.
142      */

143     public void clearStatistics() {
144         this.cacheAccessNum = 0;
145         this.cacheHitsNum = 0;
146     }
147 }
148
Popular Tags