View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 1999-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: DTMStringPool.java,v 1.2.4.1 2005/09/15 08:15:05 suresh_emailid Exp $
22   */
23  
24  package com.sun.org.apache.xml.internal.dtm.ref;
25  
26  import java.util.Vector;
27  
28  import com.sun.org.apache.xml.internal.utils.IntVector;
29  
30  /** <p>DTMStringPool is an "interning" mechanism for strings. It will
31   * create a stable 1:1 mapping between a set of string values and a set of
32   * integer index values, so the integers can be used to reliably and
33   * uniquely identify (and when necessary retrieve) the strings.</p>
34   *
35   * <p>Design Priorities:
36   * <ul>
37   * <li>String-to-index lookup speed is critical.</li>
38   * <li>Index-to-String lookup speed is slightly less so.</li>
39   * <li>Threadsafety is not guaranteed at this level.
40   * Enforce that in the application if needed.</li>
41   * <li>Storage efficiency is an issue but not a huge one.
42   * It is expected that string pools won't exceed about 2000 entries.</li>
43   * </ul>
44   * </p>
45   *
46   * <p>Implementation detail: A standard Hashtable is relatively
47   * inefficient when looking up primitive int values, especially when
48   * we're already maintaining an int-to-string vector.  So I'm
49   * maintaining a simple hash chain within this class.</p>
50   *
51   * <p>NOTE: There is nothing in the code that has a real dependency upon
52   * String. It would work with any object type that implements reliable
53   * .hashCode() and .equals() operations. The API enforces Strings because
54   * it's safer that way, but this could trivially be turned into a general
55   * ObjectPool if one was needed.</p>
56   *
57   * <p>Status: Passed basic test in main().</p>
58   * */
59  public class DTMStringPool
60  {
61    Vector m_intToString;
62    static final int HASHPRIME=101;
63    int[] m_hashStart=new int[HASHPRIME];
64    IntVector m_hashChain;
65    public static final int NULL=-1;
66  
67    /**
68     * Create a DTMStringPool using the given chain size
69     *
70     * @param chainSize The size of the hash chain vector
71     */
72    public DTMStringPool(int chainSize)
73      {
74        m_intToString=new Vector();
75        m_hashChain=new IntVector(chainSize);
76        removeAllElements();
77  
78        // -sb Add this to force empty strings to be index 0.
79        stringToIndex("");
80      }
81  
82    public DTMStringPool()
83      {
84        this(512);
85      }
86  
87    public void removeAllElements()
88      {
89        m_intToString.removeAllElements();
90        for(int i=0;i<HASHPRIME;++i)
91          m_hashStart[i]=NULL;
92        m_hashChain.removeAllElements();
93      }
94  
95    /** @return string whose value is uniquely identified by this integer index.
96     * @throws java.lang.ArrayIndexOutOfBoundsException
97     *  if index doesn't map to a string.
98     * */
99    public String indexToString(int i)
100     throws java.lang.ArrayIndexOutOfBoundsException
101     {
102       if(i==NULL) return null;
103       return (String) m_intToString.elementAt(i);
104     }
105 
106   /** @return integer index uniquely identifying the value of this string. */
107   public int stringToIndex(String s)
108     {
109       if(s==null) return NULL;
110 
111       int hashslot=s.hashCode()%HASHPRIME;
112       if(hashslot<0) hashslot=-hashslot;
113 
114       // Is it one we already know?
115       int hashlast=m_hashStart[hashslot];
116       int hashcandidate=hashlast;
117       while(hashcandidate!=NULL)
118         {
119           if(m_intToString.elementAt(hashcandidate).equals(s))
120             return hashcandidate;
121 
122           hashlast=hashcandidate;
123           hashcandidate=m_hashChain.elementAt(hashcandidate);
124         }
125 
126       // New value. Add to tables.
127       int newIndex=m_intToString.size();
128       m_intToString.addElement(s);
129 
130       m_hashChain.addElement(NULL);     // Initialize to no-following-same-hash
131       if(hashlast==NULL)  // First for this hash
132         m_hashStart[hashslot]=newIndex;
133       else // Link from previous with same hash
134         m_hashChain.setElementAt(newIndex,hashlast);
135 
136       return newIndex;
137     }
138 
139   /** Command-line unit test driver. This test relies on the fact that
140    * this version of the pool assigns indices consecutively, starting
141    * from zero, as new unique strings are encountered.
142    */
143   public static void _main(String[] args)
144   {
145     String[] word={
146       "Zero","One","Two","Three","Four","Five",
147       "Six","Seven","Eight","Nine","Ten",
148       "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
149       "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
150       "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
151       "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
152       "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
153       "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
154       "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
155 
156     DTMStringPool pool=new DTMStringPool();
157 
158     System.out.println("If no complaints are printed below, we passed initial test.");
159 
160     for(int pass=0;pass<=1;++pass)
161       {
162         int i;
163 
164         for(i=0;i<word.length;++i)
165           {
166             int j=pool.stringToIndex(word[i]);
167             if(j!=i)
168               System.out.println("\tMismatch populating pool: assigned "+
169                                  j+" for create "+i);
170           }
171 
172         for(i=0;i<word.length;++i)
173           {
174             int j=pool.stringToIndex(word[i]);
175             if(j!=i)
176               System.out.println("\tMismatch in stringToIndex: returned "+
177                                  j+" for lookup "+i);
178           }
179 
180         for(i=0;i<word.length;++i)
181           {
182             String w=pool.indexToString(i);
183             if(!word[i].equals(w))
184               System.out.println("\tMismatch in indexToString: returned"+
185                                  w+" for lookup "+i);
186           }
187 
188         pool.removeAllElements();
189 
190         System.out.println("\nPass "+pass+" complete\n");
191       } // end pass loop
192   }
193 }