@Override public <K, V> Map<K, V> createMap(int initialCapacity) { return new THashMap<K, V>(initialCapacity); }
/** * checks for the present of <tt>key</tt> in the keys of the map. * * @param key an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean containsKey(Object key) { return contains(key); }
/** * Inserts a key/value pair into the map. * * @param key an <code>Object</code> value * @param value an <code>Object</code> value * @return the previous value associated with <tt>key</tt>, or {@code null} if none was found. */ public V put(K key, V value) { int index = insertionIndex(key); return doPut(key, value, index); }
public boolean remove(Object o) { return removeElement((E) o); }
public String toString() { final StringBuilder buf = new StringBuilder("{"); forEachEntry(new TObjectObjectProcedure<K, V>() { private boolean first = true; public boolean execute(K key, V value) { if (first) first = false; else buf.append(","); buf.append(key); buf.append("="); buf.append(value); return true; } }); buf.append("}"); return buf.toString(); }
/** * Compares this map with another map for equality of their stored entries. * * @param other an <code>Object</code> value * @return a <code>boolean</code> value */ public boolean equals(Object other) { if (!(other instanceof Map)) { return false; } Map<K, V> that = (Map<K, V>) other; if (that.size() != this.size()) { return false; } return forEachEntry(new EqProcedure<K, V>(that)); }
/** * rehashes the map to the new capacity. * * @param newCapacity an <code>int</code> value */ protected void rehash(int newCapacity) { int oldCapacity = _set.length; Object oldKeys[] = _set; V oldVals[] = _values; _set = new Object[newCapacity]; Arrays.fill(_set, FREE); _values = (V[]) new Object[newCapacity]; for (int i = oldCapacity; i-- > 0; ) { if (oldKeys[i] != FREE && oldKeys[i] != REMOVED) { Object o = oldKeys[i]; int index = insertionIndex((K) o); if (index < 0) { throwObjectContractViolation(_set[(-index - 1)], o); } _set[index] = o; _values[index] = oldVals[i]; } } }
/** * copies the key/value mappings in <tt>map</tt> into this map. * * @param map a <code>Map</code> value */ public void putAll(Map<? extends K, ? extends V> map) { ensureCapacity(map.size()); // could optimize this for cases when map instanceof THashMap for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = map.entrySet().iterator(); i.hasNext(); ) { Map.Entry<? extends K, ? extends V> e = i.next(); put(e.getKey(), e.getValue()); } }
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { // VERSION byte version = in.readByte(); // NOTE: super was not written in version 0 if (version != 0) super.readExternal(in); // NUMBER OF ENTRIES int size = in.readInt(); setUp(size); // ENTRIES while (size-- > 0) { //noinspection unchecked K key = (K) in.readObject(); //noinspection unchecked V val = (V) in.readObject(); put(key, val); } } } // THashMap
/** * Executes <tt>procedure</tt> for each key in the map. * * @param procedure a <code>TObjectProcedure</code> value * @return false if the loop over the keys terminated because the procedure returned false for * some key. */ public boolean forEachKey(TObjectProcedure<K> procedure) { return forEach(procedure); }
/** * retrieves the value for <tt>key</tt> * * @param key an <code>Object</code> value * @return the value of <tt>key</tt> or null if no such mapping exists. */ public V get(Object key) { int index = index((K) key); return index < 0 ? null : _values[index]; }
private V doPut(K key, V value, int index) { V previous = null; Object oldKey; boolean isNewMapping = true; if (index < 0) { index = -index - 1; previous = _values[index]; isNewMapping = false; } oldKey = _set[index]; _set[index] = key; _values[index] = value; if (isNewMapping) { postInsertHook(oldKey == FREE); } return previous; }
@Override public <K, V> boolean forEachEntry(Map<K, V> map, final MapProcedure<K, V> procedure) { if (map instanceof THashMap) { return ((THashMap<K, V>) map).forEachEntry(new TObjectObjectProcedure<K, V>() { @Override public boolean execute(K key, V value) { return procedure.execute(key, value); } }); } return super.forEachEntry(map, procedure); }
public void writeExternal(ObjectOutput out) throws IOException { // VERSION out.writeByte(1); // NOTE: Super was not written in version 0 super.writeExternal(out); // NUMBER OF ENTRIES out.writeInt(_size); // ENTRIES SerializationProcedure writeProcedure = new SerializationProcedure(out); if (!forEachEntry(writeProcedure)) { throw writeProcedure.exception; } }
/** * Inserts a key/value pair into the map if the specified key is not already associated with a * value. * * @param key an <code>Object</code> value * @param value an <code>Object</code> value * @return the previous value associated with <tt>key</tt>, or {@code null} if none was found. */ public V putIfAbsent(K key, V value) { int index = insertionIndex(key); if (index < 0) return _values[-index - 1]; return doPut(key, value, index); }
@Override public <K, V> Map<K, V> createMap() { return new THashMap<K, V>(); }
public int hashCode() { HashProcedure p = new HashProcedure(); forEachEntry(p); return p.getHashCode(); }
@Override public <K, V> Map<K, V> createMap(Map<K, V> map) { return new THashMap<K, V>(map); }
@Override public <K, V> Map<K, V> createMap(int initialCapacity, float loadFactor) { return new THashMap<K, V>(initialCapacity, loadFactor); }