KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > StringCache


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.lang.ref.WeakReference JavaDoc;
8
9 import org.h2.engine.Constants;
10
11 public class StringCache {
12     private static final boolean ENABLED = true;
13     private static WeakReference JavaDoc weakCache = new WeakReference JavaDoc(null);
14
15     // testing: cacheHit / miss are public!
16
// public static int cacheHit = 0, cacheMiss = 0;
17

18     // 4703
19
// public static String get(String s) {
20
// if (s == null) {
21
// return s;
22
// } else if (s.length() == 0) {
23
// return "";
24
// }
25
// if (!Constants.USE_OBJECT_CACHE || !ENABLED || s.length() > MAX_CACHE_SIZE / 10) {
26
// return s;
27
// }
28
// int hash = s.hashCode();
29
// int index = hash & (Constants.OBJECT_CACHE_SIZE - 1);
30
// String cached = cache[index];
31
// if (cached != null) {
32
// if (s.equals(cached)) {
33
// // cacheHit++;
34
// return cached;
35
// }
36
// }
37
// // cacheMiss++;
38
// replace(index, s);
39
// return s;
40
// }
41

42     // 3500
43
// public static String get(String s) {
44
// return s;
45
// }
46

47     // 3906
48
public static String JavaDoc get(String JavaDoc s) {
49         if (!Constants.OBJECT_CACHE || !ENABLED) {
50             return s;
51         }
52         if (s == null) {
53             return s;
54         } else if (s.length() == 0) {
55             return "";
56         }
57         String JavaDoc[] cache = (String JavaDoc[]) weakCache.get();
58         int hash = s.hashCode();
59         if (cache == null) {
60             cache = new String JavaDoc[Constants.OBJECT_CACHE_SIZE];
61             weakCache = new WeakReference JavaDoc(cache);
62         }
63         int index = hash & (Constants.OBJECT_CACHE_SIZE - 1);
64         String JavaDoc cached = cache[index];
65         if (cached != null) {
66             if (s.equals(cached)) {
67                 // cacheHit++;
68
return cached;
69             }
70         }
71         cache[index] = s;
72         return s;
73     }
74     
75     public static String JavaDoc getNew(String JavaDoc s) {
76         if (!Constants.OBJECT_CACHE || !ENABLED) {
77             return s;
78         }
79         if (s == null) {
80             return s;
81         } else if (s.length() == 0) {
82             return "";
83         }
84         String JavaDoc[] cache = (String JavaDoc[]) weakCache.get();
85         int hash = s.hashCode();
86         if (cache == null) {
87             cache = new String JavaDoc[Constants.OBJECT_CACHE_SIZE];
88             weakCache = new WeakReference JavaDoc(cache);
89         }
90         int index = hash & (Constants.OBJECT_CACHE_SIZE - 1);
91         String JavaDoc cached = cache[index];
92         if (cached != null) {
93             if (s.equals(cached)) {
94                 // cacheHit++;
95
return cached;
96             }
97         }
98         // create a new object that is not shared (to avoid out of memory if it is a substring of a big String)
99
s = new String JavaDoc(s);
100         cache[index] = s;
101         return s;
102     }
103     
104     public static void clearCache() {
105         weakCache = new WeakReference JavaDoc(null);
106     }
107
108 }
109
Popular Tags