KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > dtm > ref > DTMStringPool


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: DTMStringPool.java,v 1.8 2004/02/16 23:06:11 minchau Exp $
18  */

19
20 package org.apache.xml.dtm.ref;
21
22 import java.util.Vector JavaDoc;
23
24 import org.apache.xml.utils.IntVector;
25
26 /** <p>DTMStringPool is an "interning" mechanism for strings. It will
27  * create a stable 1:1 mapping between a set of string values and a set of
28  * integer index values, so the integers can be used to reliably and
29  * uniquely identify (and when necessary retrieve) the strings.</p>
30  *
31  * <p>Design Priorities:
32  * <ul>
33  * <li>String-to-index lookup speed is critical.</li>
34  * <li>Index-to-String lookup speed is slightly less so.</li>
35  * <li>Threadsafety is not guaranteed at this level.
36  * Enforce that in the application if needed.</li>
37  * <li>Storage efficiency is an issue but not a huge one.
38  * It is expected that string pools won't exceed about 2000 entries.</li>
39  * </ul>
40  * </p>
41  *
42  * <p>Implementation detail: A standard Hashtable is relatively
43  * inefficient when looking up primitive int values, especially when
44  * we're already maintaining an int-to-string vector. So I'm
45  * maintaining a simple hash chain within this class.</p>
46  *
47  * <p>NOTE: There is nothing in the code that has a real dependency upon
48  * String. It would work with any object type that implements reliable
49  * .hashCode() and .equals() operations. The API enforces Strings because
50  * it's safer that way, but this could trivially be turned into a general
51  * ObjectPool if one was needed.</p>
52  *
53  * <p>Status: Passed basic test in main().</p>
54  * */

55 public class DTMStringPool
56 {
57   Vector JavaDoc m_intToString;
58   static final int HASHPRIME=101;
59   int[] m_hashStart=new int[HASHPRIME];
60   IntVector m_hashChain;
61   public static final int NULL=-1;
62
63   /**
64    * Create a DTMStringPool using the given chain size
65    *
66    * @param chainSize The size of the hash chain vector
67    */

68   public DTMStringPool(int chainSize)
69     {
70       m_intToString=new Vector JavaDoc();
71       m_hashChain=new IntVector(chainSize);
72       removeAllElements();
73       
74       // -sb Add this to force empty strings to be index 0.
75
stringToIndex("");
76     }
77   
78   public DTMStringPool()
79     {
80       this(512);
81     }
82     
83   public void removeAllElements()
84     {
85       m_intToString.removeAllElements();
86       for(int i=0;i<HASHPRIME;++i)
87         m_hashStart[i]=NULL;
88       m_hashChain.removeAllElements();
89     }
90
91   /** @return string whose value is uniquely identified by this integer index.
92    * @throws java.lang.ArrayIndexOutOfBoundsException
93    * if index doesn't map to a string.
94    * */

95   public String JavaDoc indexToString(int i)
96     throws java.lang.ArrayIndexOutOfBoundsException JavaDoc
97     {
98       if(i==NULL) return null;
99       return (String JavaDoc) m_intToString.elementAt(i);
100     }
101
102   /** @return integer index uniquely identifying the value of this string. */
103   public int stringToIndex(String JavaDoc s)
104     {
105       if(s==null) return NULL;
106       
107       int hashslot=s.hashCode()%HASHPRIME;
108       if(hashslot<0) hashslot=-hashslot;
109
110       // Is it one we already know?
111
int hashlast=m_hashStart[hashslot];
112       int hashcandidate=hashlast;
113       while(hashcandidate!=NULL)
114         {
115           if(m_intToString.elementAt(hashcandidate).equals(s))
116             return hashcandidate;
117
118           hashlast=hashcandidate;
119           hashcandidate=m_hashChain.elementAt(hashcandidate);
120         }
121       
122       // New value. Add to tables.
123
int newIndex=m_intToString.size();
124       m_intToString.addElement(s);
125
126       m_hashChain.addElement(NULL); // Initialize to no-following-same-hash
127
if(hashlast==NULL) // First for this hash
128
m_hashStart[hashslot]=newIndex;
129       else // Link from previous with same hash
130
m_hashChain.setElementAt(newIndex,hashlast);
131
132       return newIndex;
133     }
134
135   /** Command-line unit test driver. This test relies on the fact that
136    * this version of the pool assigns indices consecutively, starting
137    * from zero, as new unique strings are encountered.
138    */

139   public static void main(String JavaDoc[] args)
140   {
141     String JavaDoc[] word={
142       "Zero","One","Two","Three","Four","Five",
143       "Six","Seven","Eight","Nine","Ten",
144       "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
145       "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
146       "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
147       "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
148       "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
149       "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
150       "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
151
152     DTMStringPool pool=new DTMStringPool();
153
154     System.out.println("If no complaints are printed below, we passed initial test.");
155
156     for(int pass=0;pass<=1;++pass)
157       {
158         int i;
159
160         for(i=0;i<word.length;++i)
161           {
162             int j=pool.stringToIndex(word[i]);
163             if(j!=i)
164               System.out.println("\tMismatch populating pool: assigned "+
165                                  j+" for create "+i);
166           }
167
168         for(i=0;i<word.length;++i)
169           {
170             int j=pool.stringToIndex(word[i]);
171             if(j!=i)
172               System.out.println("\tMismatch in stringToIndex: returned "+
173                                  j+" for lookup "+i);
174           }
175
176         for(i=0;i<word.length;++i)
177           {
178             String JavaDoc w=pool.indexToString(i);
179             if(!word[i].equals(w))
180               System.out.println("\tMismatch in indexToString: returned"+
181                                  w+" for lookup "+i);
182           }
183         
184         pool.removeAllElements();
185         
186         System.out.println("\nPass "+pass+" complete\n");
187       } // end pass loop
188
}
189 }
190
Popular Tags