KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > cache > RandCache


1 /*
2  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  *
4  * All rights reserved.
5  *
6  * See end of file.
7  */

8
9 package com.hp.hpl.jena.util.cache;
10
11 import java.util.Collection JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 /**
19  *
20  * @author bwm
21  */

22 public class RandCache implements Cache, CacheControl {
23     int size;
24     int threshhold;
25     boolean enabled = true;
26                                                // so we can identify caches
27
String JavaDoc name; // e.g. when logging
28

29     HashMap JavaDoc map;
30     Collection JavaDoc collection;
31
32     protected static Log logger = LogFactory.getLog(RandCache.class);
33     
34     long gets = 0;
35     long puts = 0;
36     long hits = 0;
37
38     /** Creates new RandCache */
39     RandCache(String JavaDoc name, int size) {
40         this.size = size;
41         try {
42             map = new HashMap JavaDoc(size * 100 / 75); // based on .75 loadfactor
43
} catch (IllegalArgumentException JavaDoc e) {
44             if ("Illegal load factor: NaN".equals(e.getMessage())) {
45                 // This strange construction needs explanation.
46
// When we implemented XSDbase64Binary/XSDhexBinary support involving use
47
// of byte[] we started seeing this error here. Since the default loadfactor
48
// is a static final constant in HashMap this should never be possible.
49
// It only happens under JDK 1.4.1 not under 1.3.1 nor 1.4.2.
50
// The retry, however does seem to work and hence gives us a work around
51
// which is completely mysterious but at least enables the unit tests to pass.
52
// - der 4/5/04
53
logger.warn("Detected a NaN anomaly believed to be due to use of JDK 1.4.1");
54                 map = new HashMap JavaDoc(size*100/75, 0.75f);
55             } else {
56                 throw e;
57             }
58         }
59         threshhold = size;
60         if (threshhold < 2) {
61             throw new Error JavaDoc("Cache size too small: " + size);
62         }
63         collection = map.values();
64     }
65
66     public synchronized Object JavaDoc get(Object JavaDoc key) {
67         if (enabled) {
68             if (gets == Long.MAX_VALUE) {
69                 forgetStats();
70             }
71             gets++;
72             Object JavaDoc result = map.get(key);
73             if (result != null) {
74                 hits++;
75             }
76             return result;
77         } else {
78             return null;
79         }
80     }
81
82     public synchronized void put(Object JavaDoc key, Object JavaDoc value) {
83
84         // don't allow null values
85
if (value == null) {
86             throw new NullPointerException JavaDoc();
87         }
88
89         if (enabled) {
90             if (puts == Long.MAX_VALUE) {
91                 forgetStats();
92             }
93             puts++;
94             if (map.size() >= threshhold) {
95                 makeSpace();
96             }
97             map.put(key, value);
98         }
99     }
100
101     protected void makeSpace() {
102         Iterator JavaDoc iter = collection.iterator();
103
104         // we are going to remove every 3rd member of the cache
105
int size = map.size();
106         int i = 3;
107         while (i < size ) {
108             iter.next();
109             iter.remove();
110             iter.next();
111             iter.next();
112             i = i + 3;
113         }
114     }
115
116     public synchronized boolean getEnabled() {
117         return enabled;
118     }
119
120     public synchronized boolean setEnabled(boolean enabled) {
121         boolean result = enabled;
122         this.enabled = enabled;
123         return result;
124     }
125
126     public synchronized void clear() {
127         map.clear();
128     }
129
130     public synchronized long getHits() {
131         return hits;
132     }
133
134     public synchronized long getGets() {
135         return gets;
136     }
137
138     public synchronized long getPuts() {
139         return puts;
140     }
141
142     protected void forgetStats() {
143         gets = gets/2;
144         puts = puts/2;
145         hits = hits/2;
146     }
147
148 }
149 /*
150  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
151  *
152  * All rights reserved.
153  *
154  *
155  * Redistribution and use in source and binary forms, with or without
156  * modification, are permitted provided that the following conditions
157  * are met:
158  * 1. Redistributions of source code must retain the above copyright
159  * notice, this list of conditions and the following disclaimer.
160  * 2. Redistributions in binary form must reproduce the above copyright
161  * notice, this list of conditions and the following disclaimer in the
162  * documentation and/or other materials provided with the distribution.
163  * 3. The name of the author may not be used to endorse or promote products
164  * derived from this software without specific prior written permission.
165
166  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
167  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
168  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
169  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
170  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
171  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
172  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
173  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
174  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
175  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
176  *
177  * $Id: RandCache.java,v 1.8 2005/02/21 12:19:12 andy_seaborne Exp $
178  */

179
Popular Tags