AbstractHashedMap.java

Index Score
com.espertech.esper.collection.apachecommons
Esper

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
DECL_COMMENTSComments in declarations
DOC_COMMENTNumber of javadoc comment lines
COMMENTSComment lines
RETURNSNumber of return points from functions
SIZESize of the file in bytes
INTERFACE_COMPLEXITYInterface complexity
FUNCTIONSNumber of function declarations
LOOPSNumber of loops
CYCLOMATICCyclomatic complexity
LINESNumber of lines in the source file
JAVA0242JAVA0242 Transient field in non-Serializable class
BLOCKSNumber of blocks
PARAMSNumber of formal parameter declarations
JAVA0081JAVA0081 Boolean literal in comparison
LOGICAL_LINESNumber of statements
COMPARISONSNumber of comparison operators
LOCLines of code
OPERATORSNumber of operators
ELOCEffective lines of code
PROGRAM_LENGTHHalstead program length
OPERANDSNumber of operands
UNIQUE_OPERANDSNumber of unique operands
PROGRAM_VOCABHalstead program vocabulary
JAVA0277JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0058JAVA0058 Constructor calls super()
JAVA0113JAVA0113 Incorrect javadoc: no @author tag
JAVA0034JAVA0034 Missing braces in if statement
JAVA0179JAVA0179 Local variable hides visible field
EXITSProcedure exits
UNIQUE_OPERATORSNumber of unique operators
JAVA0114JAVA0114 Incorrect javadoc: no @version tag
JAVA0123JAVA0123 Use all three components of for loop
JAVA0254JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0067JAVA0067 Array descriptor on identifier name
JAVA0259JAVA0259 Return of collection/array field
JAVA0117JAVA0117 Missing javadoc: method 'method'
LINE_COMMENTNumber of line comments
JAVA0076JAVA0076 Use of magic number
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
WHITESPACENumber of whitespace lines
EXEC_COMMENTSComments in executable code
JAVA0126JAVA0126 Method declares unchecked exception in throws
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0068JAVA0068 Modifiers not declared in recommended order
NEST_DEPTHMaximum nesting depth
JAVA0145JAVA0145 Tab character used in source file
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.espertech.esper.collection.apachecommons; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.AbstractCollection; import java.util.AbstractMap; import java.util.AbstractSet; import java.util.Collection; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; /** * An abstract implementation of a hash-based map which provides numerous points for * subclasses to override. * <p> * This class implements all the features necessary for a subclass hash-based map. * Key-value entries are stored in instances of the <code>HashEntry</code> class, * which can be overridden and replaced. The iterators can similarly be replaced, * without the need to replace the KeySet, EntrySet and Values view classes. * <p> * Overridable methods are provided to change the default hashing behaviour, and * to change how entries are added to and removed from the map. Hopefully, all you * need for unusual subclasses is here. * <p> * NOTE: From Commons Collections 3.1 this class extends AbstractMap. * This is to provide backwards compatibility for ReferenceMap between v3.0 and v3.1. * This extends clause will be removed in v4.0. * * @since Commons Collections 3.0 * @version $Revision$ $Date$ * * @author java util HashMap * @author Stephen Colebourne * @author Christian Siefkes */ public class AbstractHashedMap extends AbstractMap implements IterableMap { /** * Message. */ protected static final String NO_NEXT_ENTRY = "No next() entry in the iteration"; /** * Message. */ protected static final String NO_PREVIOUS_ENTRY = "No previous() entry in the iteration"; /** * Message. */ protected static final String REMOVE_INVALID = "remove() can only be called once after next()"; /** * Message. */ protected static final String GETKEY_INVALID = "getKey() can only be called after next() and before remove()"; /** * Message. */ protected static final String GETVALUE_INVALID = "getValue() can only be called after next() and before remove()"; /** * Message. */ protected static final String SETVALUE_INVALID = "setValue() can only be called after next() and before remove()"; /** The default capacity to use */ protected static final int DEFAULT_CAPACITY = 16; /** The default threshold to use */ protected static final int DEFAULT_THRESHOLD = 12; /** The default load factor to use */ protected static final float DEFAULT_LOAD_FACTOR = 0.75f; /** The maximum capacity allowed */ protected static final int MAXIMUM_CAPACITY = 1 << 30; /** An object for masking null */ protected static final Object NULL = new Object(); /** Load factor, normally 0.75 */ protected transient float loadFactor; /** The size of the map */ protected transient int size; /** Map entries */ protected transient HashEntry[] data; /** Size at which to rehash */ protected transient int threshold; /** Modification count for iterators */ protected transient int modCount; /** Entry set */ protected transient EntrySet entrySet; /** Key set */ protected transient KeySet keySet; /** Values */ protected transient Values values; /** * Constructor only used in deserialization, do not use otherwise. */ protected AbstractHashedMap() { super(); } /** * Constructor which performs no validation on the passed in parameters. * * @param initialCapacity the initial capacity, must be a power of two * @param loadFactor the load factor, must be &gt; 0.0f and generally &lt; 1.0f * @param threshold the threshold, must be sensible */ protected AbstractHashedMap(int initialCapacity, float loadFactor, int threshold) { super(); this.loadFactor = loadFactor; this.data = new HashEntry[initialCapacity]; this.threshold = threshold; init(); } /** * Constructs a new, empty map with the specified initial capacity and * default load factor. * * @param initialCapacity the initial capacity * @throws IllegalArgumentException if the initial capacity is less than one */ protected AbstractHashedMap(int initialCapacity) { this(initialCapacity, DEFAULT_LOAD_FACTOR); } /** * Constructs a new, empty map with the specified initial capacity and * load factor. * * @param initialCapacity the initial capacity * @param loadFactor the load factor * @throws IllegalArgumentException if the initial capacity is less than one * or if the load factor is less than or equal to zero */ protected AbstractHashedMap(int initialCapacity, float loadFactor) { super(); if (initialCapacity < 1) { throw new IllegalArgumentException("Initial capacity must be greater than 0"); } if (loadFactor <= 0.0f || Float.isNaN(loadFactor)) { throw new IllegalArgumentException("Load factor must be greater than 0"); } this.loadFactor = loadFactor; initialCapacity = calculateNewCapacity(initialCapacity); this.threshold = calculateThreshold(initialCapacity, loadFactor); this.data = new HashEntry[initialCapacity]; init(); } /** * Constructor copying elements from another map. * * @param map the map to copy * @throws NullPointerException if the map is null */ protected AbstractHashedMap(Map map) { this(Math.max(2 * map.size(), DEFAULT_CAPACITY), DEFAULT_LOAD_FACTOR); putAll(map); } /** * Initialise subclasses during construction, cloning or deserialization. */ protected void init() { } //----------------------------------------------------------------------- /** * Gets the value mapped to the key specified. * * @param key the key * @return the mapped value, null if no match */ public Object get(Object key) { key = convertKey(key); int hashCode = hash(key); HashEntry entry = data[hashIndex(hashCode, data.length)]; // no local for hash index while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(key, entry.key)) { return entry.getValue(); } entry = entry.next; } return null; } /** * Gets the size of the map. * * @return the size */ public int size() { return size; } /** * Checks whether the map is currently empty. * * @return true if the map is currently size zero */ public boolean isEmpty() { return (size == 0); } //----------------------------------------------------------------------- /** * Checks whether the map contains the specified key. * * @param key the key to search for * @return true if the map contains the key */ public boolean containsKey(Object key) { key = convertKey(key); int hashCode = hash(key); HashEntry entry = data[hashIndex(hashCode, data.length)]; // no local for hash index while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(key, entry.key)) { return true; } entry = entry.next; } return false; } /** * Checks whether the map contains the specified value. * * @param value the value to search for * @return true if the map contains the value */ public boolean containsValue(Object value) { if (value == null) { for (int i = 0, isize = data.length; i < isize; i++) { HashEntry entry = data[i]; while (entry != null) { if (entry.getValue() == null) { return true; } entry = entry.next; } } } else { for (int i = 0, isize = data.length; i < isize; i++) { HashEntry entry = data[i]; while (entry != null) { if (isEqualValue(value, entry.getValue())) { return true; } entry = entry.next; } } } return false; } //----------------------------------------------------------------------- /** * Puts a key-value mapping into this map. * * @param key the key to add * @param value the value to add * @return the value previously mapped to this key, null if none */ public Object put(Object key, Object value) { key = convertKey(key); int hashCode = hash(key); int index = hashIndex(hashCode, data.length); HashEntry entry = data[index]; while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(key, entry.key)) { Object oldValue = entry.getValue(); updateEntry(entry, value); return oldValue; } entry = entry.next; } addMapping(index, hashCode, key, value); return null; } /** * Puts all the values from the specified map into this map. * <p> * This implementation iterates around the specified map and * uses {@link #put(Object, Object)}. * * @param map the map to add * @throws NullPointerException if the map is null */ public void putAll(Map map) { int mapSize = map.size(); if (mapSize == 0) { return; } int newSize = (int) ((size + mapSize) / loadFactor + 1); ensureCapacity(calculateNewCapacity(newSize)); for (Iterator it = map.entrySet().iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); put(entry.getKey(), entry.getValue()); } } /** * Removes the specified mapping from this map. * * @param key the mapping to remove * @return the value mapped to the removed key, null if key not in map */ public Object remove(Object key) { key = convertKey(key); int hashCode = hash(key); int index = hashIndex(hashCode, data.length); HashEntry entry = data[index]; HashEntry previous = null; while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(key, entry.key)) { Object oldValue = entry.getValue(); removeMapping(entry, index, previous); return oldValue; } previous = entry; entry = entry.next; } return null; } /** * Clears the map, resetting the size to zero and nullifying references * to avoid garbage collection issues. */ public void clear() { modCount++; HashEntry[] data = this.data; for (int i = data.length - 1; i >= 0; i--) { data[i] = null; } size = 0; } //----------------------------------------------------------------------- /** * Converts input keys to another object for storage in the map. * This implementation masks nulls. * Subclasses can override this to perform alternate key conversions. * <p> * The reverse conversion can be changed, if required, by overriding the * getKey() method in the hash entry. * * @param key the key convert * @return the converted key */ protected Object convertKey(Object key) { return (key == null ? NULL : key); } /** * Gets the hash code for the key specified. * This implementation uses the additional hashing routine from JDK1.4. * Subclasses can override this to return alternate hash codes. * * @param key the key to get a hash code for * @return the hash code */ protected int hash(Object key) { // same as JDK 1.4 int h = key.hashCode(); h += ~(h << 9); h ^= (h >>> 14); h += (h << 4); h ^= (h >>> 10); return h; } /** * Compares two keys, in internal converted form, to see if they are equal. * This implementation uses the equals method and assumes neither key is null. * Subclasses can override this to match differently. * * @param key1 the first key to compare passed in from outside * @param key2 the second key extracted from the entry via <code>entry.key</code> * @return true if equal */ protected boolean isEqualKey(Object key1, Object key2) { return (key1 == key2 || key1.equals(key2)); } /** * Compares two values, in external form, to see if they are equal. * This implementation uses the equals method and assumes neither value is null. * Subclasses can override this to match differently. * * @param value1 the first value to compare passed in from outside * @param value2 the second value extracted from the entry via <code>getValue()</code> * @return true if equal */ protected boolean isEqualValue(Object value1, Object value2) { return (value1 == value2 || value1.equals(value2)); } /** * Gets the index into the data storage for the hashCode specified. * This implementation uses the least significant bits of the hashCode. * Subclasses can override this to return alternate bucketing. * * @param hashCode the hash code to use * @param dataSize the size of the data to pick a bucket from * @return the bucket index */ protected int hashIndex(int hashCode, int dataSize) { return hashCode & (dataSize - 1); } //----------------------------------------------------------------------- /** * Gets the entry mapped to the key specified. * <p> * This method exists for subclasses that may need to perform a multi-step * process accessing the entry. The public methods in this class don't use this * method to gain a small performance boost. * * @param key the key * @return the entry, null if no match */ protected HashEntry getEntry(Object key) { key = convertKey(key); int hashCode = hash(key); HashEntry entry = data[hashIndex(hashCode, data.length)]; // no local for hash index while (entry != null) { if (entry.hashCode == hashCode && isEqualKey(key, entry.key)) { return entry; } entry = entry.next; } return null; } //----------------------------------------------------------------------- /** * Updates an existing key-value mapping to change the value. * <p> * This implementation calls <code>setValue()</code> on the entry. * Subclasses could override to handle changes to the map. * * @param entry the entry to update * @param newValue the new value to store */ protected void updateEntry(HashEntry entry, Object newValue) { entry.setValue(newValue); } /** * Reuses an existing key-value mapping, storing completely new data. * <p> * This implementation sets all the data fields on the entry. * Subclasses could populate additional entry fields. * * @param entry the entry to update, not null * @param hashIndex the index in the data array * @param hashCode the hash code of the key to add * @param key the key to add * @param value the value to add */ protected void reuseEntry(HashEntry entry, int hashIndex, int hashCode, Object key, Object value) { entry.next = data[hashIndex]; entry.hashCode = hashCode; entry.key = key; entry.value = value; } //----------------------------------------------------------------------- /** * Adds a new key-value mapping into this map. * <p> * This implementation calls <code>createEntry()</code>, <code>addEntry()</code> * and <code>checkCapacity()</code>. * It also handles changes to <code>modCount</code> and <code>size</code>. * Subclasses could override to fully control adds to the map. * * @param hashIndex the index into the data array to store at * @param hashCode the hash code of the key to add * @param key the key to add * @param value the value to add */ protected void addMapping(int hashIndex, int hashCode, Object key, Object value) { modCount++; HashEntry entry = createEntry(data[hashIndex], hashCode, key, value); addEntry(entry, hashIndex); size++; checkCapacity(); } /** * Creates an entry to store the key-value data. * <p> * This implementation creates a new HashEntry instance. * Subclasses can override this to return a different storage class, * or implement caching. * * @param next the next entry in sequence * @param hashCode the hash code to use * @param key the key to store * @param value the value to store * @return the newly created entry */ protected HashEntry createEntry(HashEntry next, int hashCode, Object key, Object value) { return new HashEntry(next, hashCode, key, value); } /** * Adds an entry into this map. * <p> * This implementation adds the entry to the data storage table. * Subclasses could override to handle changes to the map. * * @param entry the entry to add * @param hashIndex the index into the data array to store at */ protected void addEntry(HashEntry entry, int hashIndex) { data[hashIndex] = entry; } //----------------------------------------------------------------------- /** * Removes a mapping from the map. * <p> * This implementation calls <code>removeEntry()</code> and <code>destroyEntry()</code>. * It also handles changes to <code>modCount</code> and <code>size</code>. * Subclasses could override to fully control removals from the map. * * @param entry the entry to remove * @param hashIndex the index into the data structure * @param previous the previous entry in the chain */ protected void removeMapping(HashEntry entry, int hashIndex, HashEntry previous) { modCount++; removeEntry(entry, hashIndex, previous); size--; destroyEntry(entry); } /** * Removes an entry from the chain stored in a particular index. * <p> * This implementation removes the entry from the data storage table. * The size is not updated. * Subclasses could override to handle changes to the map. * * @param entry the entry to remove * @param hashIndex the index into the data structure * @param previous the previous entry in the chain */ protected void removeEntry(HashEntry entry, int hashIndex, HashEntry previous) { if (previous == null) { data[hashIndex] = entry.next; } else { previous.next = entry.next; } } /** * Kills an entry ready for the garbage collector. * <p> * This implementation prepares the HashEntry for garbage collection. * Subclasses can override this to implement caching (override clear as well). * * @param entry the entry to destroy */ protected void destroyEntry(HashEntry entry) { entry.next = null; entry.key = null; entry.value = null; } //----------------------------------------------------------------------- /** * Checks the capacity of the map and enlarges it if necessary. * <p> * This implementation uses the threshold to check if the map needs enlarging */ protected void checkCapacity() { if (size >= threshold) { int newCapacity = data.length * 2; if (newCapacity <= MAXIMUM_CAPACITY) { ensureCapacity(newCapacity); } } } /** * Changes the size of the data structure to the capacity proposed. * * @param newCapacity the new capacity of the array (a power of two, less or equal to max) */ protected void ensureCapacity(int newCapacity) { int oldCapacity = data.length; if (newCapacity <= oldCapacity) { return; } if (size == 0) { threshold = calculateThreshold(newCapacity, loadFactor); data = new HashEntry[newCapacity]; } else { HashEntry oldEntries[] = data; HashEntry newEntries[] = new HashEntry[newCapacity]; modCount++; for (int i = oldCapacity - 1; i >= 0; i--) { HashEntry entry = oldEntries[i]; if (entry != null) { oldEntries[i] = null; // gc do { HashEntry next = entry.next; int index = hashIndex(entry.hashCode, newCapacity); entry.next = newEntries[index]; newEntries[index] = entry; entry = next; } while (entry != null); } } threshold = calculateThreshold(newCapacity, loadFactor); 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(int proposedCapacity) { int newCapacity = 1; if (proposedCapacity > MAXIMUM_CAPACITY) { newCapacity = MAXIMUM_CAPACITY; } else { while (newCapacity < proposedCapacity) { newCapacity <<= 1; // multiply by two } if (newCapacity > MAXIMUM_CAPACITY) { newCapacity = 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(int newCapacity, 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(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(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(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(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 */ public MapIterator mapIterator() { if (size == 0) { return EmptyMapIterator.INSTANCE; } return new HashMapIterator(this); } /** * MapIterator implementation. */ protected static class HashMapIterator extends HashIterator implements MapIterator { /** * Ctor. * @param parent the parent */ protected HashMapIterator(AbstractHashedMap parent) { super(parent); } public Object next() { return super.nextEntry().getKey(); } public Object getKey() { HashEntry current = currentEntry(); if (current == null) { throw new IllegalStateException(AbstractHashedMap.GETKEY_INVALID); } return current.getKey(); } public Object getValue() { HashEntry current = currentEntry(); if (current == null) { throw new IllegalStateException(AbstractHashedMap.GETVALUE_INVALID); } return current.getValue(); } public Object setValue(Object value) { 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 (entrySet == null) { entrySet = new EntrySet(this); } return 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 EmptyIterator.INSTANCE; } return new EntrySetIterator(this); } /** * EntrySet implementation. */ protected static class EntrySet extends AbstractSet { /** The parent map */ protected final AbstractHashedMap parent; /** * Ctor. * @param parent the parent */ protected EntrySet(AbstractHashedMap parent) { super(); this.parent = parent; } public int size() { return parent.size(); } public void clear() { parent.clear(); } public boolean contains(Object entry) { if (entry instanceof Map.Entry) { Map.Entry e = (Map.Entry) entry; Entry match = parent.getEntry(e.getKey()); return (match != null && match.equals(e)); } return false; } public boolean remove(Object obj) { if (obj instanceof Map.Entry == false) { return false; } if (contains(obj) == false) { return false; } Map.Entry entry = (Map.Entry) obj; Object key = entry.getKey(); parent.remove(key); return true; } public Iterator iterator() { return parent.createEntrySetIterator(); } } /** * EntrySet iterator. */ protected static class EntrySetIterator extends HashIterator { /** * Ctor. * @param parent the parent */ protected EntrySetIterator(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 (keySet == null) { keySet = new KeySet(this); } return 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 EmptyIterator.INSTANCE; } return new KeySetIterator(this); } /** * KeySet implementation. */ protected static class KeySet extends AbstractSet { /** The parent map */ protected final AbstractHashedMap parent; /** * Ctor. * @param parent the parent */ protected KeySet(AbstractHashedMap parent) { super(); this.parent = parent; } public int size() { return parent.size(); } public void clear() { parent.clear(); } public boolean contains(Object key) { return parent.containsKey(key); } public boolean remove(Object key) { boolean result = parent.containsKey(key); parent.remove(key); return result; } public Iterator iterator() { return parent.createKeySetIterator(); } } /** * KeySet iterator. */ protected static class KeySetIterator extends EntrySetIterator { /** * Ctor. * @param parent the parent */ protected KeySetIterator(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 (values == null) { values = new Values(this); } return 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 EmptyIterator.INSTANCE; } return new ValuesIterator(this); } /** * Values implementation. */ protected static class Values extends AbstractCollection { /** The parent map */ protected final AbstractHashedMap parent; /** * Ctor. * @param parent the parent */ protected Values(AbstractHashedMap parent) { super(); this.parent = parent; } public int size() { return parent.size(); } public void clear() { parent.clear(); } public boolean contains(Object value) { return parent.containsValue(value); } public Iterator iterator() { return parent.createValuesIterator(); } } /** * Values iterator. */ protected static class ValuesIterator extends HashIterator { /** * Ctor. * @param parent the parent */ protected ValuesIterator(AbstractHashedMap parent) { super(parent); } public Object next() { return super.nextEntry().getValue(); } } //----------------------------------------------------------------------- /** * HashEntry used to store the data. * <p> * If you subclass <code>AbstractHashedMap</code> but not <code>HashEntry</code> * then you will not be able to access the protected fields. * The <code>entryXxx()</code> methods on <code>AbstractHashedMap</code> exist * to provide the necessary access. */ protected static class HashEntry implements Map.Entry, KeyValue { /** The next entry in the hash chain */ protected HashEntry next; /** The hash code of the key */ protected int hashCode; /** The key */ protected Object key; /** The value */ protected Object value; /** * Ctor. * @param next the next * @param hashCode the hash code * @param key the key * @param value the value */ protected HashEntry(HashEntry next, int hashCode, Object key, Object value) { super(); this.next = next; this.hashCode = hashCode; this.key = key; this.value = value; } public Object getKey() { return (key == NULL ? null : key); } public Object getValue() { return value; } public Object setValue(Object value) { Object old = this.value; this.value = value; return old; } public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Map.Entry == false) { return false; } Map.Entry other = (Map.Entry) obj; return (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) && (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue())); } public int hashCode() { return (getKey() == null ? 0 : getKey().hashCode()) ^ (getValue() == null ? 0 : getValue().hashCode()); } public String toString() { return new StringBuffer().append(getKey()).append('=').append(getValue()).toString(); } } /** * Base Iterator */ protected static abstract class HashIterator implements Iterator { /** The parent map */ protected final AbstractHashedMap parent; /** The current index into the array of buckets */ protected int hashIndex; /** The last returned entry */ protected HashEntry last; /** The next entry */ protected HashEntry next; /** The modification count expected */ protected int expectedModCount; /** * Ctor. * @param parent parent map */ protected HashIterator(AbstractHashedMap parent) { super(); this.parent = parent; HashEntry[] data = parent.data; int i = data.length; HashEntry next = null; while (i > 0 && next == null) { next = data[--i]; } this.next = next; this.hashIndex = i; this.expectedModCount = parent.modCount; } public boolean hasNext() { return (next != null); } /** * Next entry. * @return next entry. */ protected HashEntry nextEntry() { if (parent.modCount != expectedModCount) { throw new ConcurrentModificationException(); } HashEntry newCurrent = next; if (newCurrent == null) { throw new NoSuchElementException(AbstractHashedMap.NO_NEXT_ENTRY); } HashEntry[] data = parent.data; int i = hashIndex; HashEntry n = newCurrent.next; while (n == null && i > 0) { n = data[--i]; } next = n; hashIndex = i; last = newCurrent; return newCurrent; } /** * Current entry. * @return current entry */ protected HashEntry currentEntry() { return last; } public void remove() { if (last == null) { throw new IllegalStateException(AbstractHashedMap.REMOVE_INVALID); } if (parent.modCount != expectedModCount) { throw new ConcurrentModificationException(); } parent.remove(last.getKey()); last = null; expectedModCount = parent.modCount; } public String toString() { if (last != null) { return "Iterator[" + last.getKey() + "=" + last.getValue() + "]"; } else { return "Iterator[]"; } } } //----------------------------------------------------------------------- /** * Writes the map data to the stream. This method must be overridden if a * subclass must be setup before <code>put()</code> is used. * <p> * Serialization is not one of the JDK's nicest topics. Normal serialization will * initialise the superclass before the subclass. Sometimes however, this isn't * what you want, as in this case the <code>put()</code> method on read can be * affected by subclass state. * <p> * The solution adopted here is to serialize the state data of this class in * this protected method. This method must be called by the * <code>writeObject()</code> of the first serializable subclass. * <p> * Subclasses may override if they have a specific field that must be present * on read before this implementation will work. Generally, the read determines * what must be serialized here, if anything. * * @param out the output stream * @throws IOException write error */ protected void doWriteObject(ObjectOutputStream out) throws IOException { out.writeFloat(loadFactor); out.writeInt(data.length); out.writeInt(size); for (MapIterator it = mapIterator(); it.hasNext();) { out.writeObject(it.next()); out.writeObject(it.getValue()); } } /** * Reads the map data from the stream. This method must be overridden if a * subclass must be setup before <code>put()</code> is used. * <p> * Serialization is not one of the JDK's nicest topics. Normal serialization will * initialise the superclass before the subclass. Sometimes however, this isn't * what you want, as in this case the <code>put()</code> method on read can be * affected by subclass state. * <p> * The solution adopted here is to deserialize the state data of this class in * this protected method. This method must be called by the * <code>readObject()</code> of the first serializable subclass. * <p> * Subclasses may override if the subclass has a specific field that must be present * before <code>put()</code> or <code>calculateThreshold()</code> will work correctly. * * @param in the input stream * @throws IOException io error * @throws ClassNotFoundException class not found */ protected void doReadObject(ObjectInputStream in) throws IOException, ClassNotFoundException { loadFactor = in.readFloat(); int capacity = in.readInt(); int size = in.readInt(); init(); threshold = calculateThreshold(capacity, loadFactor); data = new HashEntry[capacity]; for (int i = 0; i < size; i++) { Object key = in.readObject(); Object value = in.readObject(); put(key, value); } } //----------------------------------------------------------------------- /** * Clones the map without cloning the keys or values. * <p> * To implement <code>clone()</code>, a subclass must implement the * <code>Cloneable</code> interface and make this method public. * * @return a shallow clone */ protected Object clone() throws CloneNotSupportedException { AbstractHashedMap cloned = (AbstractHashedMap) super.clone(); cloned.data = new HashEntry[data.length]; cloned.entrySet = null; cloned.keySet = null; cloned.values = null; cloned.modCount = 0; cloned.size = 0; cloned.init(); cloned.putAll(this); return cloned; } /** * Compares this map with another. * * @param obj the object to compare to * @return true if equal */ public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Map == false) { return false; } Map map = (Map) obj; if (map.size() != size()) { return false; } MapIterator it = mapIterator(); try { while (it.hasNext()) { Object key = it.next(); Object value = it.getValue(); if (value == null) { if (map.get(key) != null || map.containsKey(key) == false) { return false; } } else { if (value.equals(map.get(key)) == false) { return false; } } } } catch (ClassCastException ignored) { return false; } catch (NullPointerException ignored) { return false; } return true; } /** * Gets the standard Map hashCode. * * @return the hash code defined in the Map interface */ public int hashCode() { int total = 0; Iterator it = createEntrySetIterator(); while (it.hasNext()) { total += it.next().hashCode(); } return total; } /** * Gets the map as a String. * * @return a string version of the map */ public String toString() { if (size() == 0) { return "{}"; } StringBuffer buf = new StringBuffer(32 * size()); buf.append('{'); MapIterator it = mapIterator(); boolean hasNext = it.hasNext(); while (hasNext) { Object key = it.next(); Object value = it.getValue(); buf.append(key == this ? "(this Map)" : key) .append('=') .append(value == this ? "(this Map)" : value); hasNext = it.hasNext(); if (hasNext) { buf.append(',').append(' '); } } buf.append('}'); return buf.toString(); } }

The table below shows all metrics for AbstractHashedMap.java.

MetricValueDescription
BLOCKS168.00Number of blocks
BLOCK_COMMENT16.00Number of block comment lines
COMMENTS586.00Comment lines
COMMENT_DENSITY 1.12Comment density
COMPARISONS100.00Number of comparison operators
CYCLOMATIC181.00Cyclomatic complexity
DECL_COMMENTS120.00Comments in declarations
DOC_COMMENT552.00Number of javadoc comment lines
ELOC521.00Effective lines of code
EXEC_COMMENTS 1.00Comments in executable code
EXITS60.00Procedure exits
FUNCTIONS91.00Number of function declarations
HALSTEAD_DIFFICULTY114.47Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY217.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 0.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 0.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 0.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 1.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA0034 0.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 0.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 8.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 2.00JAVA0067 Array descriptor on identifier name
JAVA0068 1.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA0076 4.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 6.00JAVA0081 Boolean literal in comparison
JAVA0082 0.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 3.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 0.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 0.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 0.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 9.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 9.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 1.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA0117 0.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 2.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 0.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 0.00JAVA0144 Line exceeds maximum M characters
JAVA0145 0.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 0.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 0.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 0.00JAVA0177 Variable declaration missing initializer
JAVA0179 3.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 8.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 2.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 0.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 1.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 3.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 0.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 4.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 1.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES1391.00Number of lines in the source file
LINE_COMMENT18.00Number of line comments
LOC689.00Lines of code
LOGICAL_LINES351.00Number of statements
LOOPS21.00Number of loops
NEST_DEPTH 5.00Maximum nesting depth
OPERANDS1339.00Number of operands
OPERATORS2800.00Number of operators
PARAMS78.00Number of formal parameter declarations
PROGRAM_LENGTH4139.00Halstead program length
PROGRAM_VOCAB452.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS139.00Number of return points from functions
SIZE46187.00Size of the file in bytes
UNIQUE_OPERANDS386.00Number of unique operands
UNIQUE_OPERATORS66.00Number of unique operators
WHITESPACE116.00Number of whitespace lines