FileDocCategorySizeDatePackage
ProxyCharArrayCache.javaAPI DocAndroid 1.5 API4078Wed May 06 22:41:04 BST 2009org.apache.harmony.luni.internal.reflect

ProxyCharArrayCache

public class ProxyCharArrayCache extends Object

Fields Summary
private char[]
keyTable
private int[]
valueTable
private int
elementSize
private int
threshold
Constructors Summary
ProxyCharArrayCache(int initialCapacity)

        if (initialCapacity < 13) {
            initialCapacity = 13;
        }
        this.elementSize = 0;
        this.threshold = (int) (initialCapacity * 0.66f);
        this.keyTable = new char[initialCapacity][];
        this.valueTable = new int[initialCapacity];
    
Methods Summary
static booleanequals(char[] first, char[] second)

        if (first == second) {
            return true;
        }
        if (first == null || second == null) {
            return false;
        }
        if (first.length != second.length) {
            return false;
        }

        for (int i = first.length; --i >= 0;) {
            if (first[i] != second[i]) {
                return false;
            }
        }
        return true;
    
intget(char[] key)

        int index = hashCodeChar(key);
        while (keyTable[index] != null) {
            if (equals(keyTable[index], key)) {
                return valueTable[index];
            }
            index = (index + 1) % keyTable.length;
        }
        return -1;
    
private inthashCodeChar(char[] val)

        int length = val.length;
        int hash = 0;
        int n = 2; // number of characters skipped
        for (int i = 0; i < length; i += n) {
            hash += val[i];
        }
        return (hash & 0x7FFFFFFF) % keyTable.length;
    
intput(char[] key, int value)

        int index = hashCodeChar(key);
        while (keyTable[index] != null) {
            if (equals(keyTable[index], key)) {
                return valueTable[index] = value;
            }
            index = (index + 1) % keyTable.length;
        }
        keyTable[index] = key;
        valueTable[index] = value;

        // assumes the threshold is never equal to the size of the table
        if (++elementSize > threshold) {
            rehash();
        }
        return value;
    
private voidrehash()

        ProxyCharArrayCache newHashtable = new ProxyCharArrayCache(
                keyTable.length * 2);
        for (int i = keyTable.length; --i >= 0;) {
            if (keyTable[i] != null) {
                newHashtable.put(keyTable[i], valueTable[i]);
            }
        }

        this.keyTable = newHashtable.keyTable;
        this.valueTable = newHashtable.valueTable;
        this.threshold = newHashtable.threshold;
    
intsize()

        return elementSize;
    
public java.lang.StringtoString()

        int max = size();
        StringBuilder buf = new StringBuilder();
        buf.append("{"); //$NON-NLS-1$
        for (int i = 0; i < max; ++i) {
            if (keyTable[i] != null) {
                buf.append(keyTable[i]).append("->").append(valueTable[i]); //$NON-NLS-1$
            }
            if (i < max) {
                buf.append(", "); //$NON-NLS-1$
            }
        }
        buf.append("}"); //$NON-NLS-1$
        return buf.toString();