`
blueyanghualong
  • 浏览: 227415 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

HashOjbect之HashObjectMap(5)

 
阅读更多
private abstract class HashIterator implements Iterator<T> {
    BaseHashObject next; // next entry to return
        int expectedModCount; // For fast-fail
        int index; // current slot
        BaseHashObject current; // current entry

        HashIterator() {
            expectedModCount = modCount;
            if (size > 0) { // advance to first entry
            BaseHashObject[] t = table;
                while (index < t.length && (next = t[index++]) == null)
                    ;
            }
        }

        public final boolean hasNext() {
            return next != null;
        }

        final T nextObject() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            BaseHashObject e = next;
            if (e == null)
                throw new NoSuchElementException();

            if ((next = e.getNext()) == null) {
            BaseHashObject[] t = table;
                while (index < t.length && (next = t[index++]) == null)
                    ;
            }
        current = e;
            return (T)e;
        }

        public void remove() {
            if (current == null)
                throw new IllegalStateException();
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            BaseHashObject k = current;
            current = null;
            HashObjectMap.this.remove(k);
            expectedModCount = modCount;
        }

    }
   
    public Collection<T> objects() {
        return new Objects();
    }

    private final class Objects extends AbstractCollection<T> {
        public Iterator<T> iterator() {
            return newObjectIterator();
        }
        public int size() {
            return size;
        }
        public boolean contains(BaseHashObject o) {
            return containsObject(o);
        }
        public void clear() {
            HashObjectMap.this.clear();
        }
    }
    public void putAll(Collection<BaseHashObject> objects) {
        int numKeysToBeAdded = objects.size();
        if (numKeysToBeAdded == 0)
            return;

        /*
         * Expand the map if the map if the number of mappings to be added
         * is greater than or equal to threshold.  This is conservative; the
         * obvious condition is (m.size() + size) >= threshold, but this
         * condition could result in a map with twice the appropriate capacity,
         * if the keys to be added overlap with the keys already in this map.
         * By using the conservative calculation, we subject ourself
         * to at most one extra resize.
         */
        if (numKeysToBeAdded > threshold) {
            int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
            if (targetCapacity > MAXIMUM_CAPACITY)
                targetCapacity = MAXIMUM_CAPACITY;
            int newCapacity = table.length;
            while (newCapacity < targetCapacity)
                newCapacity <<= 1;
            if (newCapacity > table.length)
                resize(newCapacity);
        }
       
        for (Iterator<BaseHashObject> i = objects.iterator(); i.hasNext(); ) {
        put(i.next());
        }
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics