KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > StringCache


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor.ext;
21
22 import org.netbeans.editor.StringMap;
23
24 /**
25 * Cache holding the most commonly used strings.
26 * The unused strings are discarded when they reach the end of chain.
27 *
28 * @author Miloslav Metelka
29 * @version 1.00
30 */

31
32 public class StringCache {
33
34     private static final int DEFAULT_MAX_SIZE = 300;
35
36     private static final int DEFAULT_INITIAL_CAPACITY = 701;
37
38     int maxSize;
39
40     int size;
41
42     StringMap strMap;
43
44     /** First chain member */
45     private Entry chain;
46
47     /** Last chain member */
48     private Entry endChain;
49
50     /** Last entry that was made free */
51     private Entry freeEntry;
52
53     public int statQueries; // count of queries
54
public int statHits; // count of cache hits
55

56     public StringCache() {
57         this(DEFAULT_MAX_SIZE, DEFAULT_INITIAL_CAPACITY);
58     }
59
60     public StringCache(int maxSize) {
61         this(maxSize, 2 * maxSize);
62     }
63
64     public StringCache(int maxSize, int initialMapCapacity) {
65         this.maxSize = maxSize;
66         strMap = new StringMap(initialMapCapacity);
67     }
68
69     private void toStart(Entry e) {
70         if (e != chain) {
71             // chain removal
72
Entry ep = e.prev; // ep surely not null
73
Entry en = e.next;
74             if (en != null) {
75                 en.prev = ep;
76             } else { // last chain member
77
endChain = ep;
78             }
79             ep.next = en;
80
81             // insert to chain start
82
if (chain != null) {
83                 e.next = chain;
84                 chain.prev = e;
85             }
86             chain = e;
87         }
88     }
89
90     public String JavaDoc getString(char[] chars, int offset, int len) {
91         statQueries++;
92         Object JavaDoc o = strMap.get(chars, offset, len);
93         String JavaDoc ret;
94         if (o instanceof Entry) {
95             Entry e = (Entry)o;
96             toStart(e);
97             statHits++;
98             ret = e.str;
99         } else if (o instanceof String JavaDoc) {
100             statHits++;
101             ret = (String JavaDoc)o;
102         } else { // string not found in cache
103
ret = new String JavaDoc(chars, offset, len);
104             storeString(ret);
105         }
106         return ret;
107     }
108
109     /** Remove string that can be in the cache */
110     private void removeString(String JavaDoc s) {
111         Object JavaDoc o = strMap.remove(s);
112         if (o instanceof Entry) {
113             Entry e = (Entry)o;
114             Entry ep = e.prev;
115             Entry en = e.next;
116
117             if (e == chain) {
118                 chain = en;
119                 if (e == endChain) {
120                     endChain = null;
121                 }
122             } else { // not begining of chain
123
if (en != null) {
124                     en.prev = ep;
125                 } else {
126                     endChain = ep;
127                 }
128             }
129
130             freeEntry = e; // free - can be reused for addition
131
size--;
132         }
133         /* In other cases the removed object was either
134         * the string which should be fine here
135         * or it was null.
136         */

137     }
138
139     /** Store string that's not yet in the cache */
140     private void storeString(String JavaDoc s) {
141         Entry e;
142         if (size >= maxSize) {
143             // take last one and move to begining and replace value
144
e = endChain;
145             toStart(e);
146             strMap.remove(e.str);
147             e.str = s;
148         } else { // count of entries less than max
149
if (freeEntry != null) {
150                 e = freeEntry;
151                 freeEntry = null;
152                 e.str = s;
153                 e.next = chain;
154             } else {
155                 e = new Entry(s, chain);
156             }
157
158             if (chain != null) {
159                 chain.prev = e;
160             } else { // nothing inserted yet
161
endChain = e;
162             }
163             chain = e;
164             size++;
165         }
166         strMap.put(s, e);
167     }
168
169     /** Put a string into cache that will survive there
170     * so that it will be never removed.
171     */

172     public void putSurviveString(String JavaDoc s) {
173         removeString(s);
174         strMap.put(s, s);
175     }
176
177     static class Entry {
178
179         Entry(String JavaDoc str, Entry next) { // prev always null
180
this.str = str;
181             this.next = next;
182         }
183
184         String JavaDoc str;
185
186         Entry next;
187
188         Entry prev;
189
190     }
191
192     public String JavaDoc toString() {
193         String JavaDoc ret = "size=" + size + ", maxSize=" + maxSize // NOI18N
194
+ ", statHits=" + statHits + ", statQueries=" + statQueries; // NOI18N
195
if (statQueries > 0) {
196             ret += ", hit ratio=" + (statHits * 100 / statQueries) + "%"; // NOI18N
197
}
198         return ret;
199     }
200
201 }
202
Popular Tags