1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.

   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  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: CustomStringPool.java,v 1.2.4.1 2005/09/15 08:14:59 suresh_emailid Exp $
  22  */
  23 package com.sun.org.apache.xml.internal.dtm.ref;
  24 
  25 import java.util.HashMap;
  26 import java.util.Map;
  27 
  28 /**
  29  * CustomStringPool is an example of an application-provided data structure for a
  30  * DTM implementation to hold symbol references, e.g. element names. It will
  31  * follow the DTMStringPool interface and use two simple methods
  32  * indexToString(int i) and stringToIndex(String s) to map between a set of
  33  * string values and a set of integer index values. Therefore, an application
  34  * may improve DTM processing speed by substituting the DTM symbol resolution
  35  * tables with application specific quick symbol resolution tables.
  36  * <p>
  37  * %REVIEW% The only difference between this an DTMStringPool seems to be that
  38  * it uses a java.lang.Hashtable full of Integers rather than implementing its
  39  * own hashing. Joe deliberately avoided that approach when writing
  40  * DTMStringPool, since it is both much more memory-hungry and probably slower
  41  * -- especially in JDK 1.1.x, where Hashtable is synchronized. We need to
  42  * either justify this implementation or discard it.
  43  *
  44  * <p>
  45  * Status: In progress, under discussion.
  46  *
  47  * @LastModified: Oct 2017
  48  */
  49 public class CustomStringPool extends DTMStringPool {
  50 
  51     final Map<String, Integer> m_stringToInt = new HashMap<>();
  52     public static final int NULL = -1;
  53 
  54     public CustomStringPool() {
  55         super();
  56     }
  57 
  58     public void removeAllElements() {
  59         m_intToString.clear();
  60         if (m_stringToInt != null) {
  61             m_stringToInt.clear();
  62         }
  63     }
  64 
  65     /**
  66      * @return string whose value is uniquely identified by this integer index.
  67      * @throws java.lang.IndexOutOfBoundsException if index doesn't map to
  68      * a string.
  69      */
  70     @Override
  71     public String indexToString(int i)
  72             throws IndexOutOfBoundsException {
  73         return m_intToString.get(i);
  74     }
  75 
  76     /**
  77      * @return integer index uniquely identifying the value of this string.
  78      */
  79     @Override
  80     public int stringToIndex(String s) {
  81         if (s == null) {
  82             return NULL;
  83         }
  84         Integer iobj = m_stringToInt.get(s);
  85         if (iobj == null) {
  86             m_intToString.add(s);
  87             iobj = m_intToString.size();
  88             m_stringToInt.put(s, iobj);
  89         }
  90         return iobj;
  91     }
  92 }
--- EOF ---