1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2001, 2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 2001, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.util;
  58. /**
  59. * This class is an unsynchronized hash table primary used for String
  60. * to Object mapping.
  61. * <p>
  62. * The hash code uses the same algorithm as SymbolTable class.
  63. *
  64. * @author Elena Litani
  65. * @version $Id: SymbolHash.java,v 1.7 2003/05/08 20:12:00 elena Exp $
  66. */
  67. public class SymbolHash {
  68. //
  69. // Constants
  70. //
  71. /** Default table size. */
  72. protected int fTableSize = 101;
  73. //
  74. // Data
  75. //
  76. /** Buckets. */
  77. protected Entry[] fBuckets;
  78. /** Number of elements. */
  79. protected int fNum = 0;
  80. //
  81. // Constructors
  82. //
  83. /** Constructs a key table with the default size. */
  84. public SymbolHash() {
  85. fBuckets = new Entry[fTableSize];
  86. }
  87. /**
  88. * Constructs a key table with a given size.
  89. *
  90. * @param size the size of the key table.
  91. */
  92. public SymbolHash(int size) {
  93. fTableSize = size;
  94. fBuckets = new Entry[fTableSize];
  95. }
  96. //
  97. // Public methods
  98. //
  99. /**
  100. * Adds the key/value mapping to the key table. If the key already exists,
  101. * the previous value associated with this key is overwritten by the new
  102. * value.
  103. *
  104. * @param key
  105. * @param value
  106. */
  107. public void put(Object key, Object value) {
  108. int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize;
  109. Entry entry = search(key, bucket);
  110. // replace old value
  111. if (entry != null) {
  112. entry.value = value;
  113. }
  114. // create new entry
  115. else {
  116. entry = new Entry(key, value, fBuckets[bucket]);
  117. fBuckets[bucket] = entry;
  118. fNum++;
  119. }
  120. }
  121. /**
  122. * Get the value associated with the given key.
  123. *
  124. * @param key
  125. * @return the value associated with the given key.
  126. */
  127. public Object get(Object key) {
  128. int bucket = (key.hashCode() & 0x7FFFFFFF) % fTableSize;
  129. Entry entry = search(key, bucket);
  130. if (entry != null) {
  131. return entry.value;
  132. }
  133. return null;
  134. }
  135. /**
  136. * Get the number of key/value pairs stored in this table.
  137. *
  138. * @return the number of key/value pairs stored in this table.
  139. */
  140. public int getLength() {
  141. return fNum;
  142. }
  143. /**
  144. * Add all values to the given array. The array must have enough entry.
  145. *
  146. * @param elements the array to store the elements
  147. * @param from where to start store element in the array
  148. * @return number of elements copied to the array
  149. */
  150. public int getValues(Object[] elements, int from) {
  151. for (int i=0, j=0; i<fTableSize && j<fNum; i++) {
  152. for (Entry entry = fBuckets[i]; entry != null; entry = entry.next) {
  153. elements[from+j] = entry.value;
  154. j++;
  155. }
  156. }
  157. return fNum;
  158. }
  159. /**
  160. * Make a clone of this object.
  161. */
  162. public SymbolHash makeClone() {
  163. SymbolHash newTable = new SymbolHash(fTableSize);
  164. newTable.fNum = fNum;
  165. for (int i = 0; i < fTableSize; i++) {
  166. if (fBuckets[i] != null)
  167. newTable.fBuckets[i] = fBuckets[i].makeClone();
  168. }
  169. return newTable;
  170. }
  171. /**
  172. * Remove all key/value assocaition. This tries to save a bit of GC'ing
  173. * by at least keeping the fBuckets array around.
  174. */
  175. public void clear() {
  176. for (int i=0; i<fTableSize; i++) {
  177. fBuckets[i] = null;
  178. }
  179. fNum = 0;
  180. } // clear(): void
  181. protected Entry search(Object key, int bucket) {
  182. // search for identical key
  183. for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
  184. if (key.equals(entry.key))
  185. return entry;
  186. }
  187. return null;
  188. }
  189. //
  190. // Classes
  191. //
  192. /**
  193. * This class is a key table entry. Each entry acts as a node
  194. * in a linked list.
  195. */
  196. protected static final class Entry {
  197. // key/value
  198. public Object key;
  199. public Object value;
  200. /** The next entry. */
  201. public Entry next;
  202. public Entry() {
  203. key = null;
  204. value = null;
  205. next = null;
  206. }
  207. public Entry(Object key, Object value, Entry next) {
  208. this.key = key;
  209. this.value = value;
  210. this.next = next;
  211. }
  212. public Entry makeClone() {
  213. Entry entry = new Entry();
  214. entry.key = key;
  215. entry.value = value;
  216. if (next != null)
  217. entry.next = next.makeClone();
  218. return entry;
  219. }
  220. } // entry
  221. } // class SymbolHash