<commit_before>/* -----------------------------------------------------------------------------
 * SimpleOrderedHashtable.java
 * Author: <NAME>
 * Copyright (c) 2004-2005 <NAME> <<EMAIL>>
 *
 * SimpleOrderedHashtable.java implements a simple Hashtable that is
 * chronologically ordered.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 *
 * Usage & redistributions of source code must retain the above copyright notice.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should get a copy of the GNU Lesser General Public License from
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 * -----------------------------------------------------------------------------
 */
package org.javarosa.core.util;

import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *  Implements an Ordered Hashtable, with elements in
 *    chronological order (i.e. insertion order)
 */
public class OrderedHashtable {

    private Vector    orderedKeys;
    private Hashtable hashTable;

    /**
     *  Constructor, creates an SimpleOrderedHashtable.
     */
    public OrderedHashtable() {
        orderedKeys = new Vector();
        hashTable = new Hashtable();
    }

    /**
     *  Constructor, creates an SimpleOrderedHashtable.
     *  @param initialCapacity is the initial size for the container.
     */
    public OrderedHashtable(int initialCapacity) {
        orderedKeys = new Vector(initialCapacity);
        hashTable = new Hashtable(initialCapacity);
    }

    /**
     *  Maps the specified key to the specified value in this SimpleOrderedHashtable.
     *  The value can be retrieved by calling the get method with a key that is
     *  equal to the original key.
     *  @param key is the hashtable key.
     *  @param value is the value.
     *  @return the previous value of the specified key in this
     *  SimpleOrderedHashtable, or null if it did not have one.
     */
    synchronized public Object put(Object key, Object value) {
        int i = orderedKeys.indexOf(key);
        if (i == -1)  {
            //  Add new name/value pair.
            orderedKeys.addElement(key); // insert (append) to the end of the list
        } else {
            //  Replace name/value pair.
            orderedKeys.setElementAt(key, i);
        }
        return hashTable.put(key, value);
    }

    /**
     *  Returns the value to which the specified key is mapped in this
     *  hashtable.
     *  @param key is a key in the SimpleOrderedHashtable.
     *  @return the value to which the key is mapped in this hashtable; null if
     *  the key is not mapped to any value in this hashtable.
     */
    synchronized public Object get(Object key) {
        return hashTable.get(key);
    }

    /**
     *  Returns an enumeration of the keys in this SimpleOrderedHashtable.
     *  @return an enumeration of the keys in this SimpleOrderedHashtable.
     */
    synchronized public Enumeration keys() {
        return orderedKeys.elements();
    }

    /**
     *  Returns an enumeration of the elements in this SimpleOrderedHashtable.
     *  @return an enumeration of the elements in this SimpleOrderedHashtable.
     */
    synchronized public Enumeration elements() {
        int s = hashTable.size();
        Vector elements = new Vector(s);
        for (int i=0; i<s; i++) {
            elements.addElement(elementAt(i));
        }
        return elements.elements();
    }

    /**
     *  Returns the component at the specified index.
     *  @param index is an index into this SimpleOrderedHashtable.
     *  @return the <code>Object</code> component at the specified index.
     *  @throws ArrayIndexOutOfBoundsException if index is out of bounds.
     */
    synchronized public Object elementAt(int index)
            throws ArrayIndexOutOfBoundsException {
        Object key = orderedKeys.elementAt(index);
        return hashTable.get(key);
    }

    /**
     *  Returns the key at the specified index.
     *  @param index is an index into this SimpleOrderedHashtable.
     *  @return the <code>Object</code> key at the specified index.
     *  @throws ArrayIndexOutOfBoundsException if index is out of bounds.
     */
    synchronized public Object keyAt(int index)
            throws ArrayIndexOutOfBoundsException {
        return orderedKeys.elementAt(index);
    }

    /**
     *  Returns the index of the specified <code>Object</code>.
     *  @param key is a key in the SimpleOrderedHashtable.
     *  @return the index of the specified <code>Object</code>.
     */
    synchronized public int getIndex(Object key) {
        return orderedKeys.indexOf(key);
    }

    /**
     *  Removes the key (and its corresponding value) from this hashtable. This
     *  method does nothing if the key is not in the hashtable.
     *  @param key is the key that needs to be removed.
     */
    synchronized public void remove(Object key) {
        orderedKeys.removeElement(key);
        hashTable.remove(key);
    }

    /**
     * Removes an element at the specified index.
     * @param i is the index of the element to remove.
     */
    synchronized public void removeElementAt(int i) {
        Object key = orderedKeys.elementAt(i);
        orderedKeys.removeElementAt(i);
        hashTable.remove(key);
    }

    /**
     *  Clears this SimpleOrderedHashtable so that it contains no keys.
     */
    synchronized public void clear() {
        orderedKeys.removeAllElements();
        hashTable.clear();
    }

    /**
     *  Returns the number of components in this SimpleOrderedHashtable.
     *  @return the number of components in this vector.
     */
    synchronized public int size() {
        return orderedKeys.size();
    }

    /**
     * Recomputes the SimpleOrderedHashtable capacity.
     * @param capacity is the capacity to ensure.
     */
    synchronized public void ensureCapacity(int capacity) {
        orderedKeys.ensureCapacity(capacity);
    }
    
    public String toString () {
    	StringBuffer sb = new StringBuffer();
    	sb.append("[");
    	for (Enumeration e = keys(); e.hasMoreElements(); ) {
    		Object key = e.nextElement();
    		sb.append(key.toString());
    		sb.append(" => ");
    		sb.append(get(key).toString());
    		if (e.hasMoreElements())
    			sb.append(", ");
    	}
    	sb.append("]");    	
    	return sb.toString();
    }
}<commit_msg>[r1228] [branches/dev] Make OrderedHashtable a child of Hashtable.<commit_after>/* -----------------------------------------------------------------------------
 * SimpleOrderedHashtable.java
 * Author: <NAME>
 * Copyright (c) 2004-2005 <NAME> <<EMAIL>>
 *
 * SimpleOrderedHashtable.java implements a simple Hashtable that is
 * chronologically ordered.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option) any
 * later version.
 *
 * Usage & redistributions of source code must retain the above copyright notice.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should get a copy of the GNU Lesser General Public License from
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 * -----------------------------------------------------------------------------
 */
package org.javarosa.core.util;

import java.util.Vector;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 *  Implements an Ordered Hashtable, with elements in
 *    chronological order (i.e. insertion order)
 */
public class OrderedHashtable extends Hashtable {
    private Vector orderedKeys;

    /**
     *  Constructor, creates an SimpleOrderedHashtable.
     */
    public OrderedHashtable() {
    	super();
    	orderedKeys = new Vector();
    }

    /**
     *  Constructor, creates an SimpleOrderedHashtable.
     *  @param initialCapacity is the initial size for the container.
     */
    public OrderedHashtable(int initialCapacity) {
    	super(initialCapacity);
    	orderedKeys = new Vector(initialCapacity);
    }

    /**
     *  Clears this SimpleOrderedHashtable so that it contains no keys.
     */
    synchronized public void clear() {
        orderedKeys.removeAllElements();
        super.clear();
    }
    
    /**
     *  Returns the component at the specified index. Not in Hashtable.
     *  @param index is an index into this SimpleOrderedHashtable.
     *  @return the <code>Object</code> component at the specified index.
     *  @throws ArrayIndexOutOfBoundsException if index is out of bounds.
     */
    synchronized public Object elementAt(int index) {
        return get(keyAt(index));
    }
    
    /**
     *  Returns an enumeration of the elements in this SimpleOrderedHashtable.
     *  @return an enumeration of the elements in this SimpleOrderedHashtable.
     */
    synchronized public Enumeration elements() {
        Vector elements = new Vector();
        for (int i = 0; i < size(); i++) {
            elements.addElement(elementAt(i));
        }
        return elements.elements();
    }
    
    /**
     *  Returns the index of the specified <code>Object</code>. Not in Hashtable.
     *  @param key is a key in the SimpleOrderedHashtable.
     *  @return the index of the specified <code>Object</code>.
     */
    synchronized public int indexOfKey (Object key) {
        return orderedKeys.indexOf(key);
    }
    
    /**
     *  Returns the key at the specified index. Not in Hashtable.
     *  @param index is an index into this SimpleOrderedHashtable.
     *  @return the <code>Object</code> key at the specified index.
     *  @throws ArrayIndexOutOfBoundsException if index is out of bounds.
     */
    synchronized public Object keyAt(int index) {
        return orderedKeys.elementAt(index);
    }
    
    /**
     *  Returns an enumeration of the keys in this SimpleOrderedHashtable.
     *  @return an enumeration of the keys in this SimpleOrderedHashtable.
     */
    synchronized public Enumeration keys() {
        return orderedKeys.elements();
    }
    
    /**
     *  Maps the specified key to the specified value in this SimpleOrderedHashtable.
     *  The value can be retrieved by calling the get method with a key that is
     *  equal to the original key.
     *  @param key is the hashtable key.
     *  @param value is the value.
     *  @return the previous value of the specified key in this
     *  SimpleOrderedHashtable, or null if it did not have one.
     */
    synchronized public Object put(Object key, Object value) {
    	if (key == null) {
    		throw new NullPointerException();
    	}
    	
        int i = orderedKeys.indexOf(key);
        if (i == -1)  {
            //  Add new name/value pair.
            orderedKeys.addElement(key); // insert (append) to the end of the list
        } else {
            //  Replace name/value pair.
            orderedKeys.setElementAt(key, i);
        }
        return super.put(key, value);
    }

    /**
     *  Removes the key (and its corresponding value) from this hashtable. This
     *  method does nothing if the key is not in the hashtable.
     *  @param key is the key that needs to be removed.
     */
    synchronized public Object remove(Object key) {
        orderedKeys.removeElement(key);
        return super.remove(key);
    }
    
    /**
     * Removes an element at the specified index. Not in Hashtable.
     * @param i is the index of the element to remove.
     */
    synchronized public void removeAt(int i) {
        remove(keyAt(i));
        orderedKeys.removeElementAt(i);
    }
    
    public String toString () {
    	StringBuffer sb = new StringBuffer();
    	sb.append("[");
    	for (Enumeration e = keys(); e.hasMoreElements(); ) {
    		Object key = e.nextElement();
    		sb.append(key.toString());
    		sb.append(" => ");
    		sb.append(get(key).toString());
    		if (e.hasMoreElements())
    			sb.append(", ");
    	}
    	sb.append("]");    	
    	return sb.toString();
    }
}
{'fraction_non_alphanumeric': 0.10944999606578015, 'fraction_numerical': 0.004248957431741285, 'mean_word_length': 2.2995846313603323, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3750748', 'n_tokens_mistral': 3498, 'n_tokens_neox': 3451, 'n_words': 1251}