1 /*
   2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Oct 2017
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 /*
  22  * $Id: CustomStringPool.java,v 1.2.4.1 2005/09/15 08:14:59 suresh_emailid Exp $
  23  */
  24 package com.sun.org.apache.xml.internal.dtm.ref;
  25 
  26 import java.util.HashMap;
  27 import java.util.Map;
  28 
  29 /**
  30  * CustomStringPool is an example of an application-provided data structure for a
  31  * DTM implementation to hold symbol references, e.g. element names. It will
  32  * follow the DTMStringPool interface and use two simple methods
  33  * indexToString(int i) and stringToIndex(String s) to map between a set of
  34  * string values and a set of integer index values. Therefore, an application
  35  * may improve DTM processing speed by substituting the DTM symbol resolution
  36  * tables with application specific quick symbol resolution tables.
  37  * <p>
  38  * %REVIEW% The only difference between this an DTMStringPool seems to be that
  39  * it uses a java.lang.Hashtable full of Integers rather than implementing its
  40  * own hashing. Joe deliberately avoided that approach when writing
  41  * DTMStringPool, since it is both much more memory-hungry and probably slower
  42  * -- especially in JDK 1.1.x, where Hashtable is synchronized. We need to
  43  * either justify this implementation or discard it.
  44  *
  45  * <p>
  46  * Status: In progress, under discussion.
  47  *

  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 ---