src/java.xml/share/classes/com/sun/org/apache/xml/internal/dtm/ref/DTMStringPool.java
author joehw
Wed, 18 Oct 2017 13:25:49 -0700
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
permissions -rw-r--r--
8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked Reviewed-by: lancea, rriggs, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     3
 * @LastModified: Oct 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    11
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *
44797
8b3b3b911b8a 8162572: Update License Header for all JAXP sources
joehw
parents: 25868
diff changeset
    13
 *      http://www.apache.org/licenses/LICENSE-2.0
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
package com.sun.org.apache.xml.internal.dtm.ref;
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
import com.sun.org.apache.xml.internal.utils.IntVector;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    25
import java.util.ArrayList;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    26
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
/** <p>DTMStringPool is an "interning" mechanism for strings. It will
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 * create a stable 1:1 mapping between a set of string values and a set of
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * integer index values, so the integers can be used to reliably and
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 * uniquely identify (and when necessary retrieve) the strings.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * <p>Design Priorities:
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 * <ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * <li>String-to-index lookup speed is critical.</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * <li>Index-to-String lookup speed is slightly less so.</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * <li>Threadsafety is not guaranteed at this level.
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * Enforce that in the application if needed.</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * <li>Storage efficiency is an issue but not a huge one.
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * It is expected that string pools won't exceed about 2000 entries.</li>
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * </ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * </p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * <p>Implementation detail: A standard Hashtable is relatively
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * inefficient when looking up primitive int values, especially when
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * we're already maintaining an int-to-string vector.  So I'm
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * maintaining a simple hash chain within this class.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * <p>NOTE: There is nothing in the code that has a real dependency upon
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * String. It would work with any object type that implements reliable
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * .hashCode() and .equals() operations. The API enforces Strings because
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * it's safer that way, but this could trivially be turned into a general
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * ObjectPool if one was needed.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * <p>Status: Passed basic test in main().</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * */
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
public class DTMStringPool
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
{
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    59
  List<String> m_intToString;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
  static final int HASHPRIME=101;
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  int[] m_hashStart=new int[HASHPRIME];
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
  IntVector m_hashChain;
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  public static final int NULL=-1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   * Create a DTMStringPool using the given chain size
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * @param chainSize The size of the hash chain vector
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
  public DTMStringPool(int chainSize)
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    72
      m_intToString = new ArrayList<>();
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    73
      m_hashChain= new IntVector(chainSize);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
      removeAllElements();
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
      // -sb Add this to force empty strings to be index 0.
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
      stringToIndex("");
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  public DTMStringPool()
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
      this(512);
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  public void removeAllElements()
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    87
      m_intToString.clear();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
      for(int i=0;i<HASHPRIME;++i)
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        m_hashStart[i]=NULL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
      m_hashChain.removeAllElements();
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
  /** @return string whose value is uniquely identified by this integer index.
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    94
   * @throws java.lang.IndexOutOfBoundsException
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
   *  if index doesn't map to a string.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
   * */
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
  public String indexToString(int i)
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    98
    throws java.lang.IndexOutOfBoundsException
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
      if(i==NULL) return null;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   101
      return m_intToString.get(i);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
  /** @return integer index uniquely identifying the value of this string. */
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  public int stringToIndex(String s)
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      if(s==null) return NULL;
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
      int hashslot=s.hashCode()%HASHPRIME;
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
      if(hashslot<0) hashslot=-hashslot;
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
      // Is it one we already know?
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
      int hashlast=m_hashStart[hashslot];
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
      int hashcandidate=hashlast;
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
      while(hashcandidate!=NULL)
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   117
          if(m_intToString.get(hashcandidate).equals(s))
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
            return hashcandidate;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
          hashlast=hashcandidate;
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
          hashcandidate=m_hashChain.elementAt(hashcandidate);
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
      // New value. Add to tables.
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
      int newIndex=m_intToString.size();
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   126
      m_intToString.add(s);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
      m_hashChain.addElement(NULL);     // Initialize to no-following-same-hash
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
      if(hashlast==NULL)  // First for this hash
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
        m_hashStart[hashslot]=newIndex;
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
      else // Link from previous with same hash
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        m_hashChain.setElementAt(newIndex,hashlast);
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
      return newIndex;
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
  /** Command-line unit test driver. This test relies on the fact that
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
   * this version of the pool assigns indices consecutively, starting
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
   * from zero, as new unique strings are encountered.
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
  public static void _main(String[] args)
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    String[] word={
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
      "Zero","One","Two","Three","Four","Five",
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
      "Six","Seven","Eight","Nine","Ten",
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
      "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
      "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
      "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
      "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
      "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
      "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
      "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    DTMStringPool pool=new DTMStringPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
    System.out.println("If no complaints are printed below, we passed initial test.");
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    for(int pass=0;pass<=1;++pass)
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        int i;
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        for(i=0;i<word.length;++i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
          {
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
            int j=pool.stringToIndex(word[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
            if(j!=i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
              System.out.println("\tMismatch populating pool: assigned "+
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
                                 j+" for create "+i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        for(i=0;i<word.length;++i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
          {
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
            int j=pool.stringToIndex(word[i]);
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
            if(j!=i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
              System.out.println("\tMismatch in stringToIndex: returned "+
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
                                 j+" for lookup "+i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        for(i=0;i<word.length;++i)
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
          {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
            String w=pool.indexToString(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
            if(!word[i].equals(w))
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
              System.out.println("\tMismatch in indexToString: returned"+
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
                                 w+" for lookup "+i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
        pool.removeAllElements();
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
        System.out.println("\nPass "+pass+" complete\n");
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
      } // end pass loop
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
}