KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > utils > StringPool


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.utils;
12
13 import java.util.HashMap JavaDoc;
14
15 /**
16  * A string pool is used for sharing strings in a way that eliminates duplicate
17  * equal strings. A string pool instance can be maintained over a long period
18  * of time, or used as a temporary structure during a string sharing pass over
19  * a data structure.
20  * <p>
21  * This class is not intended to be subclassed by clients.
22  * </p>
23  *
24  * @see IStringPoolParticipant
25  * @since 3.1
26  */

27 public final class StringPool {
28     private int savings;
29     private final HashMap JavaDoc map = new HashMap JavaDoc();
30
31     /**
32      * Creates a new string pool.
33      */

34     public StringPool() {
35         super();
36     }
37
38     /**
39      * Adds a <code>String</code> to the pool. Returns a <code>String</code>
40      * that is equal to the argument but that is unique within this pool.
41      * @param string The string to add to the pool
42      * @return A string that is equal to the argument.
43      */

44     public String JavaDoc add(String JavaDoc string) {
45         if (string == null)
46             return string;
47         Object JavaDoc result = map.get(string);
48         if (result != null) {
49             if (result != string)
50                 savings += 44 + 2 * string.length();
51             return (String JavaDoc) result;
52         }
53         map.put(string, string);
54         return string;
55     }
56
57     /**
58      * Returns an estimate of the size in bytes that was saved by sharing strings in
59      * the pool. In particular, this returns the size of all strings that were added to the
60      * pool after an equal string had already been added. This value can be used
61      * to estimate the effectiveness of a string sharing operation, in order to
62      * determine if or when it should be performed again.
63      *
64      * In some cases this does not precisely represent the number of bytes that
65      * were saved. For example, say the pool already contains string S1. Now
66      * string S2, which is equal to S1 but not identical, is added to the pool five
67      * times. This method will return the size of string S2 multiplied by the
68      * number of times it was added, even though the actual savings in this case
69      * is only the size of a single copy of S2.
70      */

71     public int getSavedStringCount() {
72         return savings;
73     }
74 }
75
Popular Tags