Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License. You can obtain
  * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
  * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
  * Sun designates this particular file as subject to the "Classpath" exception
  * as provided by Sun in the GPL Version 2 section of the License file that
  * accompanied this code.  If applicable, add the following below the License
  * Header, with the fields enclosed by brackets [] replaced by your own
  * identifying information: "Portions Copyrighted [year]
  * [name of copyright owner]"
  *
  * Contributor(s):
  *
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  *
  *
  * This file incorporates work covered by the following copyright and
  * permission notice:
  *
  * Copyright 2004 The Apache Software Foundation
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 
 
 package org.apache.tomcat.util.collections;
 
 import java.util.*;
 
 /* **************************************** Stolen from Crimson ******************** */
 /* From Crimson/Parser - in a perfect world we'll just have a common set of
    utilities, and all apache project will just use those.
 
 */
 
 // can't be replaced using a Java 2 "Collections" API
 // since this package must also run on JDK 1.1
 

This class implements a special purpose hashtable. It works like a normal java.util.Hashtable except that:
  1. Keys to "get" are strings which are known to be interned, so that "==" is used instead of "String.equals". (Interning could be document-relative instead of global.)
  2. It's not synchronized, since it's to be used only by one thread at a time.
  3. The keys () enumerator allocates no memory, with live updates to the data disallowed.
  4. It's got fewer bells and whistles: fixed threshold and load factor, no JDK 1.2 collection support, only keys can be enumerated, things can't be removed, simpler inheritance; more.

The overall result is that it's less expensive to use these in performance-critical locations, in terms both of CPU and memory, than java.util.Hashtable instances. In this package it makes a significant difference when normalizing attributes, which is done for each start-element construct.

 
 public final class SimpleHashtable implements Enumeration
 {
    private static com.sun.org.apache.commons.logging.Log log=
        com.sun.org.apache.commons.logging.LogFactory.getLogSimpleHashtable.class );
    // entries ...
    private Entry		table[];
    // currently enumerated key
    private Entry		current = null;
    private int			currentBucket = 0;
    // number of elements in hashtable
    private int			count;
    private int			threshold;
    private static final float	loadFactor = 0.75f;


    
Constructs a new, empty hashtable with the specified initial capacity.

Parameters:
initialCapacity the initial capacity of the hashtable.
    public SimpleHashtable(int initialCapacity) {
	if (initialCapacity < 0)
	    throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        if (initialCapacity==0)
            initialCapacity = 1;
	 = new Entry[initialCapacity];
	 = (int)(initialCapacity * );
    }

    
Constructs a new, empty hashtable with a default capacity.
    public SimpleHashtable() {
	this(11);
    }

    
    public void clear ()
    {
	 = 0;
	 = null;
	for (int i = 0; i < .i++)
	     [i] = null;
    }

    
Returns the number of keys in this hashtable.

Returns:
the number of keys in this hashtable.
    public int size() {
	return ;
    }

    
Returns an enumeration of the keys in this hashtable.

Returns:
an enumeration of the keys in this hashtable.
See also:
java.util.Enumeration
    public Enumeration keys() {
	 = null;
	return this;
    }

    
Used to view this as an enumeration; returns true if there are more keys to be enumerated.
    public boolean hasMoreElements ()
    {
	if ( != null)
	    return true;
	while ( < .) {
	     =  [++];
	    if ( != null)
		return true;
	}
	return false;
    }

    
Used to view this as an enumeration; returns the next key in the enumeration.
    public Object nextElement ()
    {
	Object retval;
	if ( == null)
	    throw new IllegalStateException ();
	retval = .;
	// Advance to the next position ( we may call next after next,
	// without hasMore )
	return retval;
    }


    
Returns the value to which the specified key is mapped in this hashtable.
    public Object getInterned (String key) {
	Entry tab[] = ;
	int hash = key.hashCode();
	int index = (hash & 0x7FFFFFFF) % tab.length;
	for (Entry e = tab[index] ; e != null ; e = e.next) {
	    if ((e.hash == hash) && (e.key == key))
		return e.value;
	}
	return null;
    }

    
Returns the value to which the specified key is mapped in this hashtable ... the key isn't necessarily interned, though.
    public Object get(String key) {
	Entry tab[] = ;
	int hash = key.hashCode();
	int index = (hash & 0x7FFFFFFF) % tab.length;
	for (Entry e = tab[index] ; e != null ; e = e.next) {
	    if ((e.hash == hash) && e.key.equals(key))
		return e.value;
	}
	return null;
    }

    
Increases the capacity of and internally reorganizes this hashtable, in order to accommodate and access its entries more efficiently. This method is called automatically when the number of keys in the hashtable exceeds this hashtable's capacity and load factor.
    private void rehash() {
	int oldCapacity = .;
	Entry oldMap[] = ;
	int newCapacity = oldCapacity * 2 + 1;
	Entry newMap[] = new Entry[newCapacity];
	 = (int)(newCapacity * );
	 = newMap;
	/*
	System.out.pr intln("rehash old=" + oldCapacity
		+ ", new=" + newCapacity
		+ ", thresh=" + threshold
		+ ", count=" + count);
	*/
	for (int i = oldCapacity ; i-- > 0 ;) {
	    for (Entry old = oldMap[i] ; old != null ; ) {
		Entry e = old;
		old = old.next;
		int index = (e.hash & 0x7FFFFFFF) % newCapacity;
		e.next = newMap[index];
		newMap[index] = e;
	    }
	}
    }

    
Maps the specified key to the specified value in this hashtable. Neither the key nor the value can be null.

The value can be retrieved by calling the get method with a key that is equal to the original key.

    public Object put(Object keyObject value) {
	// Make sure the value is not null
	if (value == null) {
	    throw new NullPointerException();
	}
	// Makes sure the key is not already in the hashtable.
	Entry tab[] = ;
	int hash = key.hashCode();
	int index = (hash & 0x7FFFFFFF) % tab.length;
	for (Entry e = tab[index] ; e != null ; e = e.next) {
	    // if ((e.hash == hash) && e.key.equals(key)) {
	    if ((e.hash == hash) && (e.key == key)) {
		Object old = e.value;
		e.value = value;
		return old;
	    }
	}
	if ( >= ) {
	    // Rehash the table if the threshold is exceeded
	    rehash();
            tab = ;
            index = (hash & 0x7FFFFFFF) % tab.length;
	// Creates the new entry.
	Entry e = new Entry(hashkeyvaluetab[index]);
	tab[index] = e;
	return null;
    }
    public Object remove(Object key) {
	Entry tab[] = ;
	Entry prev=null;
	int hash = key.hashCode();
	int index = (hash & 0x7FFFFFFF) % tab.length;
	if > 0 ) d("Idx " + index +  " " + tab[index] );
	for (Entry e = tab[index] ; e != null ; prev=ee = e.next) {
	    if > 0 ) d("> " + prev + " " + e.next + " " + e + " " + e.key);
	    if ((e.hash == hash) && e.key.equals(key)) {
		ifprev!=null ) {
		    prev.next=e.next;
else {
		    tab[index]=e.next;
		}
		if > 0 ) d("Removing from list " + tab[index] + " " + prev +
			       " " + e.value);
		Object res=e.value;
		e.value=null;
		return res;
	    }
	}
	return null;
    }

    
Hashtable collision list.
    private static class Entry {
	int	hash;
	protected Entry(int hashObject keyObject valueEntry next) {
	    this. = hash;
	    this. = key;
	    this. = value;
	    this. = next;
	}
    }
    private static final int dL=0;
    private void d(String s ) {
        if (.isDebugEnabled())
	    .debug"SimpleHashtable: " + s );
    }
New to GrepCode? Check out our FAQ X