首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

HashObject之LinkedHashObjectMap(二)

2012-12-25 
HashObject之LinkedHashObjectMap(2)private abstract class LinkedBaseHashObjectIterator implements It

HashObject之LinkedHashObjectMap(2)
private abstract class LinkedBaseHashObjectIterator implements Iterator<T> {
LinkedBaseHashObject nextObject    = header.after;
LinkedBaseHashObject lastReturned = null;

/**
* The modCount value that the iterator believes that the backing
* List should have.  If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;

public boolean hasNext() {
            return nextObject != header;
}

public void remove() {
    if (lastReturned == null)
     throw new IllegalStateException();
    if (modCount != expectedModCount)
     throw new ConcurrentModificationException();

    LinkedHashObjectMap.this.remove(lastReturned);
        lastReturned = null;
        expectedModCount = modCount;
}

T nextObject() {
    if (modCount != expectedModCount)
       throw new ConcurrentModificationException();
        if (nextObject == header)
                throw new NoSuchElementException();

        LinkedBaseHashObject e = lastReturned = nextObject;
        nextObject = e.after;
        return (T)e;
}
}

private class ObjectIterator extends LinkedBaseHashObjectIterator {
public T next() { return (T)nextObject(); }
}

    // These Overrides alter the behavior of superclass view iterator() methods
Iterator<T> newObjectIterator() { return new ObjectIterator(); }

/**
* 复写了父类的addObject方法,
* 支持双向链表功能,新添加的数据,会放在链表顶部。
* 支持老化数据的功能。
*/
void addObject(int hash, BaseHashObject object, int bucketIndex) {
LinkedBaseHashObject old = (LinkedBaseHashObject)table[bucketIndex];
LinkedBaseHashObject e = (LinkedBaseHashObject)object;
e.next = old;
    table[bucketIndex] = e;
    e.addBefore(header);
    size++;
        // Remove eldest entry if instructed, else grow capacity if appropriate
        LinkedBaseHashObject eldest = header.after;
        if (removeEldestEntry(eldest)) {
            removeObjectForKey(eldest);
        } else {
            if (size >= threshold)
                resize(2 * table.length);
        }
    }
    /**
     * 判断是否要老化数据
     * @param eldest
     * @return
     */
protected boolean removeEldestEntry(LinkedBaseHashObject eldest) {
if(log.isDebugEnabled())
{
log.debug("size():"+size()+";maxUserCount:"+maxUserCount);
}
if(size()> maxUserCount)
            return true;
return false;
    }

public int getMaxUserCount() {
return maxUserCount;
}
    /**
     * 设值缓存最大值
     * @param maxUserCount
     */
public void setMaxUserCount(int maxUserCount) {
this.maxUserCount = maxUserCount;
if(log.isDebugEnabled())
{
log.debug("set maxUserCount:"+maxUserCount);
}
}

}

热点排行