com.fitbur.google.common.collect.RegularImmutableMap Maven / Gradle / Ivy
/*
* Copyright (C) 2008 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in com.fitburpliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.com.fitbur/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.fitbur.google.com.fitburmon.collect;
import static com.fitbur.google.com.fitburmon.collect.CollectPreconditions.checkEntryNotNull;
import com.fitbur.google.com.fitburmon.annotations.GwtCompatible;
import com.fitbur.google.com.fitburmon.collect.ImmutableMapEntry.TerminalEntry;
import javax.annotation.Nullable;
/**
* Implementation of {@link ImmutableMap} with two or more entries.
*
* @author Jesse Wilson
* @author Kevin Bourrillion
* @author Gregory Kick
*/
@GwtCompatible(serializable = true, emulated = true)
final class RegularImmutableMap extends ImmutableMap {
// entries in insertion order
private final transient ImmutableMapEntry[] entries;
// array of linked lists of entries
private final transient ImmutableMapEntry[] table;
// 'and' with an int to get a table index
private final transient int mask;
RegularImmutableMap(TerminalEntry, ?>... theEntries) {
this(theEntries.length, theEntries);
}
/**
* Constructor for RegularImmutableMap that takes as input an array of {@code TerminalEntry}
* entries. Assumes that these entries have already been checked for null.
*
* This allows reuse of the entry objects from the array in the actual implementation.
*/
RegularImmutableMap(int size, TerminalEntry, ?>[] theEntries) {
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")
TerminalEntry entry = (TerminalEntry) theEntries[entryIndex];
K key = entry.getKey();
int tableIndex = Hashing.smear(key.hashCode()) & mask;
@Nullable ImmutableMapEntry existing = table[tableIndex];
// prepend, not append, so the entries can be immutable
ImmutableMapEntry newEntry = (existing == null)
? entry
: new NonTerminalMapEntry(entry, existing);
table[tableIndex] = newEntry;
entries[entryIndex] = newEntry;
checkNoConflictInBucket(key, newEntry, existing);
}
}
/**
* Constructor for RegularImmutableMap that makes no assumptions about the input entries.
*/
RegularImmutableMap(Entry, ?>[] theEntries) {
int 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 Entrys
Entry entry = (Entry) theEntries[entryIndex];
K key = entry.getKey();
V value = entry.getValue();
checkEntryNotNull(key, value);
int tableIndex = Hashing.smear(key.hashCode()) & mask;
@Nullable ImmutableMapEntry existing = table[tableIndex];
// prepend, not append, so the entries can be immutable
ImmutableMapEntry newEntry = (existing == null)
? new TerminalEntry(key, value)
: new NonTerminalMapEntry(key, value, existing);
table[tableIndex] = newEntry;
entries[entryIndex] = newEntry;
checkNoConflictInBucket(key, newEntry, existing);
}
}
private void checkNoConflictInBucket(
K key, ImmutableMapEntry entry, ImmutableMapEntry bucketHead) {
for (; bucketHead != null; bucketHead = bucketHead.getNextInKeyBucket()) {
checkNoConflict(!key.equals(bucketHead.getKey()), "key", entry, bucketHead);
}
}
private static final class NonTerminalMapEntry extends ImmutableMapEntry {
private final ImmutableMapEntry nextInKeyBucket;
NonTerminalMapEntry(K key, V value, ImmutableMapEntry nextInKeyBucket) {
super(key, value);
this.nextInKeyBucket = nextInKeyBucket;
}
NonTerminalMapEntry(ImmutableMapEntry contents, ImmutableMapEntry nextInKeyBucket) {
super(contents);
this.nextInKeyBucket = nextInKeyBucket;
}
@Override
ImmutableMapEntry getNextInKeyBucket() {
return nextInKeyBucket;
}
@Override
@Nullable
ImmutableMapEntry getNextInValueBucket() {
return null;
}
}
/**
* Closed addressing tends to perform well even with high load factors.
* Being conservative here ensures that the table is still likely to be
* relatively sparse (hence it misses fast) while saving space.
*/
private static final double MAX_LOAD_FACTOR = 1.2;
/**
* Creates an {@code ImmutableMapEntry} array to hold parameterized entries. The
* result must never be upcast back to ImmutableMapEntry[] (or Object[], etc.), or
* allowed to escape the class.
*/
@SuppressWarnings("unchecked") // Safe as long as the javadocs are followed
private ImmutableMapEntry[] createEntryArray(int size) {
return new ImmutableMapEntry[size];
}
@Override public V get(@Nullable Object key) {
if (key == null) {
return null;
}
int index = Hashing.smear(key.hashCode()) & mask;
for (ImmutableMapEntry entry = table[index];
entry != null;
entry = entry.getNextInKeyBucket()) {
K candidateKey = entry.getKey();
/*
* Assume that equals uses the == optimization when appropriate, and that
* it would check hash codes as an optimization when appropriate. If we
* did these things, it would just make things worse for the most
* performance-conscious users.
*/
if (key.equals(candidateKey)) {
return entry.getValue();
}
}
return null;
}
@Override
public int size() {
return entries.length;
}
@Override boolean isPartialView() {
return false;
}
@Override
ImmutableSet> createEntrySet() {
return new EntrySet();
}
@SuppressWarnings("serial") // uses writeReplace(), not com.fitburfault serialization
private class EntrySet extends ImmutableMapEntrySet {
@Override ImmutableMap map() {
return RegularImmutableMap.this;
}
@Override
public UnmodifiableIterator> iterator() {
return asList().iterator();
}
@Override
ImmutableList> createAsList() {
return new RegularImmutableAsList>(this, entries);
}
}
// This class is never actually serialized directly, but we have to make the
// warning go away (and suppressing would suppress for all nested classes too)
private static final long serialVersionUID = 0;
}