HashOjbect之HashObjectMap(3)

/**
     * Returns index for hash code h.
     */
    static int indexFor(int h, int length) {
        return h & (length-1);
    }

    /**
     * Returns the number of key-value mappings in this map.
     *
     * @return the number of key-value mappings in this map
     */
    public int size() {
        return size;
    }

    /**
     * Returns <tt>true</tt> if this map contains no key-value mappings.
     *
     * @return <tt>true</tt> if this map contains no key-value mappings
     */
    public boolean isEmpty() {
        return size == 0;
    }
   
    public T put(BaseHashObject object) {
        if (object == null)
            return null;
        int hash = object.keyHashCode();
        int i = indexFor(hash, table.length);
        for (BaseHashObject e = table[i]; e != null; e = e.getNext()) {
            if (e.keyHashCode() == hash && (e.keyEquals(object))) {
            e.copyObject(object);
                return (T)e;
            }
        }
        modCount++;
        addObject(hash, object, i);
        return null;
    }
   
    void addObject(int hash, BaseHashObject object, int bucketIndex) {
    BaseHashObject e = table[bucketIndex];
    object.setNext(e);
        table[bucketIndex] = object;
            if (size++ >= threshold)
                resize(2 * table.length);
    }
    void resize(int newCapacity) {
    HashObject[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {
            threshold = Integer.MAX_VALUE;
            if(logger.isDebugEnabled())
            {
            logger.debug("resize(),threshold:"+threshold+"size:"+size);
            }
            return;
        }

        BaseHashObject[] newTable = new BaseHashObject[newCapacity];
        transfer(newTable);
        table = newTable;
        threshold = (int)(newCapacity * loadFactor);
        if(logger.isDebugEnabled())
        {
        logger.debug("resize(),threshold:"+threshold+"size:"+size);
        }
    }
   
    void transfer(BaseHashObject[] newTable) {
    BaseHashObject[] src = table;
        int newCapacity = newTable.length;
        for (int j = 0; j < src.length; j++) {
        BaseHashObject e = src[j];
            if (e != null) {
                src[j] = null;
                do {
                BaseHashObject next = e.getNext();
                    int i = indexFor(e.keyHashCode(), newCapacity);
                    e.setNext(newTable[i]);
                    newTable[i] = e;
                    e = next;
                } while (e != null);
            }
        }
    }

你可能感兴趣的:(J#)