ImmutableMap的分析

public static class Builder<K, V> {
Comparator<? super V> valueComparator;
ImmutableMapEntry<K, V>[] entries;
int size;
boolean entriesUsed;

public Builder() {
this(ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY);
}

Builder(int initialCapacity) {
this.entries = new ImmutableMapEntry[initialCapacity];
this.size = 0;
this.entriesUsed = false;
class ImmutableMapEntry<K, V> extends ImmutableEntry<K, V> {

static <K, V> ImmutableMapEntry<K, V>[] createEntryArray(int size) {
return new ImmutableMapEntry[size];
}

ImmutableMapEntry(K key, V value) {
super(key, value);
checkEntryNotNull(key, value);
static void checkEntryNotNull(Object key, Object value) {
if (key == null) {
throw new NullPointerException("null key in entry: null=" + value);
} else if (value == null) {
throw new NullPointerException("null value in entry: " + key + "=null");
}
class ImmutableEntry<K, V> extends AbstractMapEntry<K, V> implements Serializable {
final K key;
final V value;
private void ensureCapacity(int minCapacity) {
if (minCapacity > entries.length) {
entries =
Arrays.copyOf(
entries, ImmutableCollection.Builder.expandedCapacity(entries.length, minCapacity));
entriesUsed = false;
public ImmutableMap<K, V> build() {
switch (size) {
case 0:
return of();
case 1:
return new SingletonImmutableBiMap<K, V>(k1, v1);
default:
return new RegularImmutableMap<K, V>(entries, table, mask);
final class RegularImmutableMap<K, V> extends ImmutableMap<K, V> {
// entries in insertion order
private final transient Entry<K, V>[] entries;
// array of linked lists of entries
private final transient ImmutableMapEntry<K, V>[] table;
// 'and' with an int to get a table index
private final transient int mask;
@CanIgnoreReturnValue
@Deprecated
@Override
public final V put(K k, V v) {
throw new UnsupportedOperationException();

posted @ 2021-09-06 08:55  v17166570219  阅读(47)  评论(0)    收藏  举报