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