KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.jobs;
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  * Note: This class is copied from org.eclipse.core.resources
25  *
26  * @since 3.1
27  */

28 public final class StringPool {
29     private int savings;
30     private final HashMap JavaDoc map = new HashMap JavaDoc();
31
32     /**
33      * Adds a <code>String</code> to the pool. Returns a <code>String</code>
34      * that is equal to the argument but that is unique within this pool.
35      * @param string The string to add to the pool
36      * @return A string that is equal to the argument.
37      */

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

65     public int getSavedStringCount() {
66         return savings;
67     }
68 }
Popular Tags