001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 /* 019 * $Id: DTMSafeStringPool.java 468653 2006-10-28 07:07:05Z minchau $ 020 */ 021 022 package org.apache.xml.dtm.ref; 023 024 025 /** <p>Like DTMStringPool, but threadsafe. It's been proposed that DTMs 026 * share their string pool(s); that raises threadsafety issues which 027 * this addresses. Of course performance is inferior to that of the 028 * bare-bones version.</p> 029 * 030 * <p>Status: Passed basic test in main().</p> 031 * */ 032 public class DTMSafeStringPool 033 extends DTMStringPool 034 { 035 public synchronized void removeAllElements() 036 { 037 super.removeAllElements(); 038 } 039 040 /** @return string whose value is uniquely identified by this integer index. 041 * @throws java.lang.ArrayIndexOutOfBoundsException 042 * if index doesn't map to a string. 043 * */ 044 public synchronized String indexToString(int i) 045 throws java.lang.ArrayIndexOutOfBoundsException 046 { 047 return super.indexToString(i); 048 } 049 050 /** @return integer index uniquely identifying the value of this string. */ 051 public synchronized int stringToIndex(String s) 052 { 053 return super.stringToIndex(s); 054 } 055 056 /** Command-line unit test driver. This test relies on the fact that 057 * this version of the pool assigns indices consecutively, starting 058 * from zero, as new unique strings are encountered. 059 */ 060 public static void main(String[] args) 061 { 062 String[] word={ 063 "Zero","One","Two","Three","Four","Five", 064 "Six","Seven","Eight","Nine","Ten", 065 "Eleven","Twelve","Thirteen","Fourteen","Fifteen", 066 "Sixteen","Seventeen","Eighteen","Nineteen","Twenty", 067 "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four", 068 "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight", 069 "Twenty-Nine","Thirty","Thirty-One","Thirty-Two", 070 "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six", 071 "Thirty-Seven","Thirty-Eight","Thirty-Nine"}; 072 073 DTMStringPool pool=new DTMSafeStringPool(); 074 075 System.out.println("If no complaints are printed below, we passed initial test."); 076 077 for(int pass=0;pass<=1;++pass) 078 { 079 int i; 080 081 for(i=0;i<word.length;++i) 082 { 083 int j=pool.stringToIndex(word[i]); 084 if(j!=i) 085 System.out.println("\tMismatch populating pool: assigned "+ 086 j+" for create "+i); 087 } 088 089 for(i=0;i<word.length;++i) 090 { 091 int j=pool.stringToIndex(word[i]); 092 if(j!=i) 093 System.out.println("\tMismatch in stringToIndex: returned "+ 094 j+" for lookup "+i); 095 } 096 097 for(i=0;i<word.length;++i) 098 { 099 String w=pool.indexToString(i); 100 if(!word[i].equals(w)) 101 System.out.println("\tMismatch in indexToString: returned"+ 102 w+" for lookup "+i); 103 } 104 105 pool.removeAllElements(); 106 107 System.out.println("\nPass "+pass+" complete\n"); 108 } // end pass loop 109 } 110 } // DTMSafeStringPool