KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > dtm > ref > DTMSafeStringPool


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: DTMSafeStringPool.java,v 1.6 2004/02/16 23:06:11 minchau Exp $
18  */

19
20 package com.sun.org.apache.xml.internal.dtm.ref;
21
22
23 /** <p>Like DTMStringPool, but threadsafe. It's been proposed that DTMs
24  * share their string pool(s); that raises threadsafety issues which
25  * this addresses. Of course performance is inferior to that of the
26  * bare-bones version.</p>
27  *
28  * <p>Status: Passed basic test in _main().</p>
29  * */

30 public class DTMSafeStringPool
31 extends DTMStringPool
32 {
33   public synchronized void removeAllElements()
34     {
35       super.removeAllElements();
36     }
37
38   /** @return string whose value is uniquely identified by this integer index.
39    * @throws java.lang.ArrayIndexOutOfBoundsException
40    * if index doesn't map to a string.
41    * */

42   public synchronized String JavaDoc indexToString(int i)
43     throws java.lang.ArrayIndexOutOfBoundsException JavaDoc
44     {
45       return super.indexToString(i);
46     }
47
48   /** @return integer index uniquely identifying the value of this string. */
49   public synchronized int stringToIndex(String JavaDoc s)
50     {
51       return super.stringToIndex(s);
52     }
53
54   /** Command-line unit test driver. This test relies on the fact that
55    * this version of the pool assigns indices consecutively, starting
56    * from zero, as new unique strings are encountered.
57    */

58   public static void _main(String JavaDoc[] args)
59   {
60     String JavaDoc[] word={
61       "Zero","One","Two","Three","Four","Five",
62       "Six","Seven","Eight","Nine","Ten",
63       "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
64       "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
65       "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
66       "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
67       "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
68       "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
69       "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
70
71     DTMStringPool pool=new DTMSafeStringPool();
72
73     System.out.println("If no complaints are printed below, we passed initial test.");
74
75     for(int pass=0;pass<=1;++pass)
76       {
77         int i;
78
79         for(i=0;i<word.length;++i)
80           {
81             int j=pool.stringToIndex(word[i]);
82             if(j!=i)
83               System.out.println("\tMismatch populating pool: assigned "+
84                                  j+" for create "+i);
85           }
86
87         for(i=0;i<word.length;++i)
88           {
89             int j=pool.stringToIndex(word[i]);
90             if(j!=i)
91               System.out.println("\tMismatch in stringToIndex: returned "+
92                                  j+" for lookup "+i);
93           }
94
95         for(i=0;i<word.length;++i)
96           {
97             String JavaDoc w=pool.indexToString(i);
98             if(!word[i].equals(w))
99               System.out.println("\tMismatch in indexToString: returned"+
100                                  w+" for lookup "+i);
101           }
102         
103         pool.removeAllElements();
104         
105         System.out.println("\nPass "+pass+" complete\n");
106       } // end pass loop
107
}
108 } // DTMSafeStringPool
109
Popular Tags