/** * Creates a RegularImmutableMap from the first n entries in entryArray. This implementation * may replace the entries in entryArray with its own entry objects (though they will have the * same key/value contents), and may take ownership of entryArray. */ static <K, V> RegularImmutableMap<K, V> fromEntryArray(int n, Entry<K, V>[] entryArray) { checkPositionIndex(n, entryArray.length); if (n == 0) { return (RegularImmutableMap<K, V>) EMPTY; } Entry<K, V>[] entries; if (n == entryArray.length) { entries = entryArray; } else { entries = createEntryArray(n); } int tableSize = Hashing.closedTableSize(n, MAX_LOAD_FACTOR); ImmutableMapEntry<K, V>[] table = createEntryArray(tableSize); int mask = tableSize - 1; for (int entryIndex = 0; entryIndex < n; entryIndex++) { Entry<K, V> entry = entryArray[entryIndex]; K key = entry.getKey(); V value = entry.getValue(); checkEntryNotNull(key, value); int tableIndex = Hashing.smear(key.hashCode()) & mask; @Nullable ImmutableMapEntry<K, V> existing = table[tableIndex]; // prepend, not append, so the entries can be immutable ImmutableMapEntry<K, V> newEntry; if (existing == null) { boolean reusable = entry instanceof ImmutableMapEntry && ((ImmutableMapEntry<K, V>) entry).isReusable(); newEntry = reusable ? (ImmutableMapEntry<K, V>) entry : new ImmutableMapEntry<K, V>(key, value); } else { newEntry = new NonTerminalImmutableMapEntry<K, V>(key, value, existing); } table[tableIndex] = newEntry; entries[entryIndex] = newEntry; checkNoConflictInKeyBucket(key, newEntry, existing); } return new RegularImmutableMap<K, V>(entries, table, mask); }
/** * Creates a RegularImmutableMap from the first n entries in entryArray. This implementation * may replace the entries in entryArray with its own entry objects (though they will have the * same key/value contents), and may take ownership of entryArray. */ static <K, V> RegularImmutableMap<K, V> fromEntryArray(int n, Entry<K, V>[] entryArray) { checkPositionIndex(n, entryArray.length); Entry<K, V>[] entries; if (n == entryArray.length) { entries = entryArray; } else { entries = createEntryArray(n); } int tableSize = Hashing.closedTableSize(n, MAX_LOAD_FACTOR); ImmutableMapEntry<K, V>[] table = createEntryArray(tableSize); int mask = tableSize - 1; for (int entryIndex = 0; entryIndex < n; entryIndex++) { Entry<K, V> entry = entryArray[entryIndex]; K key = entry.getKey(); V value = entry.getValue(); checkEntryNotNull(key, value); int tableIndex = Hashing.smear(key.hashCode()) & mask; @Nullable ImmutableMapEntry<K, V> existing = table[tableIndex]; // prepend, not append, so the entries can be immutable ImmutableMapEntry<K, V> newEntry; if (existing == null) { boolean reusable = entry instanceof ImmutableMapEntry && ((ImmutableMapEntry<K, V>) entry).isReusable(); newEntry = reusable ? (ImmutableMapEntry<K, V>) entry : new ImmutableMapEntry<K, V>(key, value); } else { newEntry = new NonTerminalImmutableMapEntry<K, V>(key, value, existing); } table[tableIndex] = newEntry; entries[entryIndex] = newEntry; checkNoConflictInKeyBucket(key, newEntry, existing); } return new RegularImmutableMap<K, V>(entries, table, mask); }
/** * Constructor for RegularImmutableMap that makes no assumptions about the input entries. */ RegularImmutableMap(int size, Entry<?, ?>[] theEntries) { checkPositionIndex(size, theEntries.length); entries = createEntryArray(size); int tableSize = Hashing.closedTableSize(size, MAX_LOAD_FACTOR); table = createEntryArray(tableSize); mask = tableSize - 1; for (int entryIndex = 0; entryIndex < size; entryIndex++) { @SuppressWarnings("unchecked") // all our callers carefully put in only Entry<K, V>s Entry<K, V> entry = (Entry<K, V>) theEntries[entryIndex]; K key = entry.getKey(); V value = entry.getValue(); checkEntryNotNull(key, value); int tableIndex = Hashing.smear(key.hashCode()) & mask; @Nullable ImmutableMapEntry<K, V> existing = table[tableIndex]; // prepend, not append, so the entries can be immutable ImmutableMapEntry<K, V> newEntry; if (existing == null) { boolean reusable = entry instanceof ImmutableMapEntry && ((ImmutableMapEntry<K, V>) entry).isReusable(); newEntry = reusable ? (ImmutableMapEntry<K, V>) entry : new ImmutableMapEntry<K, V>(key, value); } else { newEntry = new NonTerminalImmutableMapEntry<K, V>(key, value, existing); } table[tableIndex] = newEntry; entries[entryIndex] = newEntry; checkNoConflictInKeyBucket(key, newEntry, existing); } }
/** * Creates an ImmutableMap from the first n entries in entryArray. This implementation may replace * the entries in entryArray with its own entry objects (though they will have the same key/value * contents), and may take ownership of entryArray. */ static <K, V> ImmutableMap<K, V> fromEntryArray(int n, Entry<K, V>[] entryArray) { checkPositionIndex(n, entryArray.length); if (n == 0) { return (RegularImmutableMap<K, V>) EMPTY; } Entry<K, V>[] entries; if (n == entryArray.length) { entries = entryArray; } else { entries = createEntryArray(n); } int tableSize = Hashing.closedTableSize(n, MAX_LOAD_FACTOR); ImmutableMapEntry<K, V>[] table = createEntryArray(tableSize); int mask = tableSize - 1; for (int entryIndex = 0; entryIndex < n; entryIndex++) { Entry<K, V> entry = entryArray[entryIndex]; K key = entry.getKey(); V value = entry.getValue(); checkEntryNotNull(key, value); int tableIndex = Hashing.smear(key.hashCode()) & mask; @NullableDecl ImmutableMapEntry<K, V> existing = table[tableIndex]; // prepend, not append, so the entries can be immutable ImmutableMapEntry<K, V> newEntry = (existing == null) ? makeImmutable(entry, key, value) : new NonTerminalImmutableMapEntry<K, V>(key, value, existing); table[tableIndex] = newEntry; entries[entryIndex] = newEntry; int bucketSize = checkNoConflictInKeyBucket(key, newEntry, existing); if (bucketSize > MAX_HASH_BUCKET_LENGTH) { // probable hash flooding attack, fall back to j.u.HM based implementation and use its // implementation of hash flooding protection return JdkBackedImmutableMap.create(n, entryArray); } } return new RegularImmutableMap<>(entries, table, mask); }