⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstracthashedmap.java

📁 jboss规则引擎
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                                                     newCapacity );
                        entry.next = newEntries[index];
                        newEntries[index] = entry;
                        entry = next;
                    } while ( entry != null );
                }
            }
            this.threshold = calculateThreshold( newCapacity,
                                                 this.loadFactor );
            this.data = newEntries;
        }
    }

    /**
     * Calculates the new capacity of the map. This implementation normalizes
     * the capacity to a power of two.
     * 
     * @param proposedCapacity
     *            the proposed capacity
     * @return the normalized new capacity
     */
    protected int calculateNewCapacity(final int proposedCapacity) {
        int newCapacity = 1;
        if ( proposedCapacity > AbstractHashedMap.MAXIMUM_CAPACITY ) {
            newCapacity = AbstractHashedMap.MAXIMUM_CAPACITY;
        } else {
            while ( newCapacity < proposedCapacity ) {
                newCapacity <<= 1; // multiply by two
            }
            if ( newCapacity > AbstractHashedMap.MAXIMUM_CAPACITY ) {
                newCapacity = AbstractHashedMap.MAXIMUM_CAPACITY;
            }
        }
        return newCapacity;
    }

    /**
     * Calculates the new threshold of the map, where it will be resized. This
     * implementation uses the load factor.
     * 
     * @param newCapacity
     *            the new capacity
     * @param factor
     *            the load factor
     * @return the new resize threshold
     */
    protected int calculateThreshold(final int newCapacity,
                                     final float factor) {
        return (int) (newCapacity * factor);
    }

    // -----------------------------------------------------------------------
    /**
     * Gets the <code>next</code> field from a <code>HashEntry</code>. Used
     * in subclasses that have no visibility of the field.
     * 
     * @param entry
     *            the entry to query, must not be null
     * @return the <code>next</code> field of the entry
     * @throws NullPointerException
     *             if the entry is null
     * @since Commons Collections 3.1
     */
    protected HashEntry entryNext(final HashEntry entry) {
        return entry.next;
    }

    /**
     * Gets the <code>hashCode</code> field from a <code>HashEntry</code>.
     * Used in subclasses that have no visibility of the field.
     * 
     * @param entry
     *            the entry to query, must not be null
     * @return the <code>hashCode</code> field of the entry
     * @throws NullPointerException
     *             if the entry is null
     * @since Commons Collections 3.1
     */
    protected int entryHashCode(final HashEntry entry) {
        return entry.hashCode;
    }

    /**
     * Gets the <code>key</code> field from a <code>HashEntry</code>. Used
     * in subclasses that have no visibility of the field.
     * 
     * @param entry
     *            the entry to query, must not be null
     * @return the <code>key</code> field of the entry
     * @throws NullPointerException
     *             if the entry is null
     * @since Commons Collections 3.1
     */
    protected Object entryKey(final HashEntry entry) {
        return entry.key;
    }

    /**
     * Gets the <code>value</code> field from a <code>HashEntry</code>.
     * Used in subclasses that have no visibility of the field.
     * 
     * @param entry
     *            the entry to query, must not be null
     * @return the <code>value</code> field of the entry
     * @throws NullPointerException
     *             if the entry is null
     * @since Commons Collections 3.1
     */
    protected Object entryValue(final HashEntry entry) {
        return entry.value;
    }

    // -----------------------------------------------------------------------
    /**
     * Gets an iterator over the map. Changes made to the iterator affect this
     * map.
     * <p>
     * A MapIterator returns the keys in the map. It also provides convenient
     * methods to get the key and value, and set the value. It avoids the need
     * to create an entrySet/keySet/values object. It also avoids creating the
     * Map.Entry object.
     * 
     * @return the map iterator
     */
    private MapIterator mapIterator() {
        if ( this.size == 0 ) {
            return EmptyMapIterator.INSTANCE;
        }
        return new HashMapIterator( this );
    }

    /**
     * MapIterator implementation.
     */
    protected static class HashMapIterator extends HashIterator
        implements
        MapIterator {

        protected HashMapIterator(final AbstractHashedMap parent) {
            super( parent );
        }

        public Object next() {
            return super.nextEntry().getKey();
        }

        public Object getKey() {
            final HashEntry current = currentEntry();
            if ( current == null ) {
                throw new IllegalStateException( AbstractHashedMap.GETKEY_INVALID );
            }
            return current.getKey();
        }

        public Object getValue() {
            final HashEntry current = currentEntry();
            if ( current == null ) {
                throw new IllegalStateException( AbstractHashedMap.GETVALUE_INVALID );
            }
            return current.getValue();
        }

        public Object setValue(final Object value) {
            final HashEntry current = currentEntry();
            if ( current == null ) {
                throw new IllegalStateException( AbstractHashedMap.SETVALUE_INVALID );
            }
            return current.setValue( value );
        }
    }

    // -----------------------------------------------------------------------
    /**
     * Gets the entrySet view of the map. Changes made to the view affect this
     * map. To simply iterate through the entries, use {@link #mapIterator()}.
     * 
     * @return the entrySet view
     */
    public Set entrySet() {
        if ( this.entrySet == null ) {
            this.entrySet = new EntrySet( this );
        }
        return this.entrySet;
    }

    /**
     * Creates an entry set iterator. Subclasses can override this to return
     * iterators with different properties.
     * 
     * @return the entrySet iterator
     */
    protected Iterator createEntrySetIterator() {
        if ( size() == 0 ) {
            return Collections.EMPTY_MAP.keySet().iterator();
        }
        return new EntrySetIterator( this );
    }

    /**
     * EntrySet implementation.
     */
    protected static class EntrySet extends AbstractSet {
        /** The parent map */
        protected final AbstractHashedMap parent;

        protected EntrySet(final AbstractHashedMap parent) {
            super();
            this.parent = parent;
        }

        public int size() {
            return this.parent.size();
        }

        public void clear() {
            this.parent.clear();
        }

        public boolean contains(final Object entry) {
            if ( entry instanceof Map.Entry ) {
                final Map.Entry e = (Map.Entry) entry;
                final Entry match = this.parent.getEntry( e.getKey() );
                return (match != null && match.equals( e ));
            }
            return false;
        }

        public boolean remove(final Object obj) {
            if ( obj instanceof Map.Entry == false ) {
                return false;
            }
            if ( contains( obj ) == false ) {
                return false;
            }
            final Map.Entry entry = (Map.Entry) obj;
            final Object key = entry.getKey();
            this.parent.remove( key );
            return true;
        }

        public Iterator iterator() {
            return this.parent.createEntrySetIterator();
        }
    }

    /**
     * EntrySet iterator.
     */
    protected static class EntrySetIterator extends HashIterator {

        protected EntrySetIterator(final AbstractHashedMap parent) {
            super( parent );
        }

        public Object next() {
            return super.nextEntry();
        }
    }

    // -----------------------------------------------------------------------
    /**
     * Gets the keySet view of the map. Changes made to the view affect this
     * map. To simply iterate through the keys, use {@link #mapIterator()}.
     * 
     * @return the keySet view
     */
    public Set keySet() {
        if ( this.keySet == null ) {
            this.keySet = new KeySet( this );
        }
        return this.keySet;
    }

    /**
     * Creates a key set iterator. Subclasses can override this to return
     * iterators with different properties.
     * 
     * @return the keySet iterator
     */
    protected Iterator createKeySetIterator() {
        if ( size() == 0 ) {
            return Collections.EMPTY_MAP.keySet().iterator();
        }
        return new KeySetIterator( this );
    }

    /**
     * KeySet implementation.
     */
    protected static class KeySet extends AbstractSet {
        /** The parent map */
        protected final AbstractHashedMap parent;

        protected KeySet(final AbstractHashedMap parent) {
            super();
            this.parent = parent;
        }

        public int size() {
            return this.parent.size();
        }

        public void clear() {
            this.parent.clear();
        }

        public boolean contains(final Object key) {
            return this.parent.containsKey( key );
        }

        public boolean remove(final Object key) {
            final boolean result = this.parent.containsKey( key );
            this.parent.remove( key );
            return result;
        }

        public Iterator iterator() {
            return this.parent.createKeySetIterator();
        }
    }

    /**
     * KeySet iterator.
     */
    protected static class KeySetIterator extends EntrySetIterator {

        protected KeySetIterator(final AbstractHashedMap parent) {
            super( parent );
        }

        public Object next() {
            return super.nextEntry().getKey();
        }
    }

    // -----------------------------------------------------------------------
    /**
     * Gets the values view of the map. Changes made to the view affect this
     * map. To simply iterate through the values, use {@link #mapIterator()}.
     * 
     * @return the values view
     */
    public Collection values() {
        if ( this.values == null ) {
            this.values = new Values( this );
        }
        return this.values;
    }

    /**
     * Creates a values iterator. Subclasses can override this to return
     * iterators with different properties.
     * 
     * @return the values iterator
     */
    protected Iterator createValuesIterator() {
        if ( size() == 0 ) {
            return Collections.EMPTY_MAP.keySet().iterator();
        }
        return new ValuesIterator( this );
    }

    /**
     * Values implementation.
     */
    protected static class Values extends AbstractCollection {
        /** The parent map */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -