/** * Utility method that creates an {@link Int2ObjectHashMap} with its initialCapacity calculated * to minimize rehash operations */ public static <V> Int2ObjectHashMap<V> createInt2ObjectHashMap(int expectedMapSize) { final int initialCapacity = (int) (expectedMapSize / Int2ObjectHashMap.DEFAULT_LOAD_FACTOR) + 1; return new Int2ObjectHashMap<V>(initialCapacity, Int2ObjectHashMap.DEFAULT_LOAD_FACTOR); }
@Override public void destroy() { factories.clear(); }
@Override public V get(final Object key) { return get(((Integer) key).intValue()); }
/** * Compact the {@link Map} backing arrays by rehashing with a capacity just larger than current size * and giving consideration to the load factor. */ public void compact() { final int idealCapacity = (int) Math.round(size() * (1.0d / loadFactor)); rehash(QuickMath.nextPowerOfTwo(idealCapacity)); }
private void register(int factoryId, DataSerializableFactory factory) { final DataSerializableFactory current = factories.get(factoryId); if (current != null) { if (current.equals(factory)) { Logger.getLogger(getClass()).warning("DataSerializableFactory[" + factoryId + "] is already registered! Skipping " + factory); } else { throw new IllegalArgumentException("DataSerializableFactory[" + factoryId + "] is already registered! " + current + " -> " + factory); } } else { factories.put(factoryId, factory); } }
Object mergingValue, int eventType) { if (eventDataPerEventType.containsKey(eventType)) { return eventDataPerEventType.get(eventType); } else { Data dataOldValue = oldValue == null ? null : mapServiceContext.toData(oldValue); eventDataPerEventType.put(eventType, entryEventData); if (empty) { empty = false;
@Override public V put(final Integer key, final V value) { return put(key.intValue(), value); }
@Override public boolean containsKey(final Object key) { checkNotNull(key, "Null keys are not permitted"); return containsKey(((Integer) key).intValue()); }
private void increaseCapacity() { final int newCapacity = capacity << 1; if (newCapacity < 0) { throw new IllegalStateException("Max capacity reached at size=" + size); } rehash(newCapacity); }
@Override public V remove(final Object key) { return remove(((Integer) key).intValue()); }
@Override public String toString() { final StringBuilder sb = new StringBuilder(); sb.append('{'); for (final Entry<Integer, V> entry : entrySet()) { sb.append(entry.getKey().intValue()); sb.append('='); sb.append(entry.getValue()); sb.append(", "); } if (sb.length() > 1) { sb.setLength(sb.length() - 2); } sb.append('}'); return sb.toString(); }
/** * Overloaded version of {@link Map#remove(Object)} that takes a primitive int key. * * @param key for indexing the {@link Map} * @return the value if found otherwise null */ @SuppressWarnings("unchecked") public V remove(final int key) { int index = intHash(key, mask); Object value; while (null != (value = values[index])) { if (key == keys[index]) { values[index] = null; --size; compactChain(index); return (V) value; } index = ++index & mask; } return null; }
/** * Overloaded version of {@link Map#put(Object, Object)} that takes a primitive int key. * * @param key for indexing the {@link Map} * @param value to be inserted in the {@link Map} * @return the previous value if found otherwise null */ @SuppressWarnings("unchecked") public V put(final int key, final V value) { checkNotNull(value, "Value cannot be null"); V oldValue = null; int index = intHash(key, mask); while (null != values[index]) { if (key == keys[index]) { oldValue = (V) values[index]; break; } index = ++index & mask; } if (null == oldValue) { ++size; keys[index] = key; } values[index] = value; if (size > resizeThreshold) { increaseCapacity(); } return oldValue; }
private void register(int factoryId, DataSerializableFactory factory) { final DataSerializableFactory current = factories.get(factoryId); if (current != null) { if (current.equals(factory)) { Logger.getLogger(getClass()).warning("DataSerializableFactory[" + factoryId + "] is already registered! Skipping " + factory); } else { throw new IllegalArgumentException("DataSerializableFactory[" + factoryId + "] is already registered! " + current + " -> " + factory); } } else { factories.put(factoryId, factory); } }
Object mergingValue, int eventType) { if (eventDataPerEventType.containsKey(eventType)) { return eventDataPerEventType.get(eventType); } else { Data dataOldValue = oldValue == null ? null : mapServiceContext.toData(oldValue); eventDataPerEventType.put(eventType, entryEventData); if (empty) { empty = false;
/** * Compact the {@link Map} backing arrays by rehashing with a capacity just larger than current size * and giving consideration to the load factor. */ public void compact() { final int idealCapacity = (int) Math.round(size() * (1.0d / loadFactor)); rehash(QuickMath.nextPowerOfTwo(idealCapacity)); }
@Override public V put(final Integer key, final V value) { return put(key.intValue(), value); }
@Override public boolean containsKey(final Object key) { checkNotNull(key, "Null keys are not permitted"); return containsKey(((Integer) key).intValue()); }
private void increaseCapacity() { final int newCapacity = capacity << 1; if (newCapacity < 0) { throw new IllegalStateException("Max capacity reached at size=" + size); } rehash(newCapacity); }
@Override public V remove(final Object key) { return remove(((Integer) key).intValue()); }