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. package com.sun.org.apache.xml.internal.dtm.ref;
  20. /** <p>Like DTMStringPool, but threadsafe. It's been proposed that DTMs
  21. * share their string pool(s); that raises threadsafety issues which
  22. * this addresses. Of course performance is inferior to that of the
  23. * bare-bones version.</p>
  24. *
  25. * <p>Status: Passed basic test in _main().</p>
  26. * */
  27. public class DTMSafeStringPool
  28. extends DTMStringPool
  29. {
  30. public synchronized void removeAllElements()
  31. {
  32. super.removeAllElements();
  33. }
  34. /** @return string whose value is uniquely identified by this integer index.
  35. * @throws java.lang.ArrayIndexOutOfBoundsException
  36. * if index doesn't map to a string.
  37. * */
  38. public synchronized String indexToString(int i)
  39. throws java.lang.ArrayIndexOutOfBoundsException
  40. {
  41. return super.indexToString(i);
  42. }
  43. /** @return integer index uniquely identifying the value of this string. */
  44. public synchronized int stringToIndex(String s)
  45. {
  46. return super.stringToIndex(s);
  47. }
  48. /** Command-line unit test driver. This test relies on the fact that
  49. * this version of the pool assigns indices consecutively, starting
  50. * from zero, as new unique strings are encountered.
  51. */
  52. public static void _main(String[] args)
  53. {
  54. String[] word={
  55. "Zero","One","Two","Three","Four","Five",
  56. "Six","Seven","Eight","Nine","Ten",
  57. "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
  58. "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
  59. "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
  60. "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
  61. "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
  62. "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
  63. "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
  64. DTMStringPool pool=new DTMSafeStringPool();
  65. System.out.println("If no complaints are printed below, we passed initial test.");
  66. for(int pass=0;pass<=1;++pass)
  67. {
  68. int i;
  69. for(i=0;i<word.length;++i)
  70. {
  71. int j=pool.stringToIndex(word[i]);
  72. if(j!=i)
  73. System.out.println("\tMismatch populating pool: assigned "+
  74. j+" for create "+i);
  75. }
  76. for(i=0;i<word.length;++i)
  77. {
  78. int j=pool.stringToIndex(word[i]);
  79. if(j!=i)
  80. System.out.println("\tMismatch in stringToIndex: returned "+
  81. j+" for lookup "+i);
  82. }
  83. for(i=0;i<word.length;++i)
  84. {
  85. String w=pool.indexToString(i);
  86. if(!word[i].equals(w))
  87. System.out.println("\tMismatch in indexToString: returned"+
  88. w+" for lookup "+i);
  89. }
  90. pool.removeAllElements();
  91. System.out.println("\nPass "+pass+" complete\n");
  92. } // end pass loop
  93. }
  94. } // DTMSafeStringPool