Class RegularImmutableMap<K,V>

java.lang.Object
com.google.common.collect.ImmutableMap<K,V>
com.google.common.collect.RegularImmutableMap<K,V>
All Implemented Interfaces:
Serializable, Map<K,V>

final class RegularImmutableMap<K,V> extends ImmutableMap<K,V>
Implementation of ImmutableMap used for 0 entries and for 2+ entries. Additional implementations exist for particular cases, like ImmutableTable views and hash flooding. (This doc discusses ImmutableMap subclasses only for the JRE flavor; the Android flavor differs.)
  • Field Details

    • EMPTY

      static final ImmutableMap<Object,Object> EMPTY
    • MAX_LOAD_FACTOR

      static final double MAX_LOAD_FACTOR
      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.
      See Also:
    • HASH_FLOODING_FPP

      static final double HASH_FLOODING_FPP
      Maximum allowed false positive probability of detecting a hash flooding attack given random input.
      See Also:
    • MAX_HASH_BUCKET_LENGTH

      static final int MAX_HASH_BUCKET_LENGTH
      Maximum allowed length of a hash table bucket before falling back to a j.u.HashMap based implementation. Experimentally determined.
      See Also:
    • entries

      final transient Map.Entry<K,V>[] entries
    • table

      @CheckForNull private final transient ImmutableMapEntry<K,V>[] table
    • mask

      private final transient int mask
    • serialVersionUID

      private static final long serialVersionUID
      See Also:
  • Constructor Details

  • Method Details

    • fromEntries

      static <K, V> ImmutableMap<K,V> fromEntries(Map.Entry<K,V>... entries)
    • fromEntryArray

      static <K, V> ImmutableMap<K,V> fromEntryArray(int n, Map.Entry<K,V>[] entryArray, boolean throwIfDuplicateKeys)
      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.
    • fromEntryArrayCheckingBucketOverflow

      private static <K, V> ImmutableMap<K,V> fromEntryArrayCheckingBucketOverflow(int n, Map.Entry<K,V>[] entryArray, boolean throwIfDuplicateKeys) throws RegularImmutableMap.BucketOverflowException
      Throws:
      RegularImmutableMap.BucketOverflowException
    • removeDuplicates

      static <K, V> Map.Entry<K,V>[] removeDuplicates(Map.Entry<K,V>[] entries, int n, int newN, IdentityHashMap<Map.Entry<K,V>,Boolean> duplicates)
      Constructs a new entry array where each duplicated key from the original appears only once, at its first position but with its final value. The duplicates map is modified.
      Parameters:
      entries - the original array of entries including duplicates
      n - the number of valid entries in entries
      newN - the expected number of entries once duplicates are removed
      duplicates - a map of canonical Map.Entry objects for each duplicate key. This map will be updated by the method, setting each value to false as soon as the Map.Entry has been included in the new entry array.
      Returns:
      an array of newN entries where no key appears more than once.
    • makeImmutable

      static <K, V> ImmutableMapEntry<K,V> makeImmutable(Map.Entry<K,V> entry, K key, V value)
      Makes an entry usable internally by a new ImmutableMap without rereading its contents.
    • makeImmutable

      static <K, V> ImmutableMapEntry<K,V> makeImmutable(Map.Entry<K,V> entry)
      Makes an entry usable internally by a new ImmutableMap.
    • checkNoConflictInKeyBucket

      @CheckForNull static <K, V> ImmutableMapEntry<K,V> checkNoConflictInKeyBucket(Object key, Object newValue, @CheckForNull ImmutableMapEntry<K,V> keyBucketHead, boolean throwIfDuplicateKeys) throws RegularImmutableMap.BucketOverflowException
      Checks if the given key already appears in the hash chain starting at keyBucketHead. If it does not, then null is returned. If it does, then if throwIfDuplicateKeys is true an IllegalArgumentException is thrown, and otherwise the existing Map.Entry is returned.
      Throws:
      IllegalArgumentException - if another entry in the bucket has the same key and throwIfDuplicateKeys is true
      RegularImmutableMap.BucketOverflowException - if this bucket has too many entries, which may indicate a hash flooding attack
    • get

      @CheckForNull public V get(@CheckForNull Object key)
      Specified by:
      get in interface Map<K,V>
      Specified by:
      get in class ImmutableMap<K,V>
    • get

      @CheckForNull static <V> V get(@CheckForNull Object key, @CheckForNull ImmutableMapEntry<?,V>[] keyTable, int mask)
    • forEach

      public void forEach(BiConsumer<? super K,? super V> action)
    • size

      public int size()
    • isPartialView

      boolean isPartialView()
      Specified by:
      isPartialView in class ImmutableMap<K,V>
    • createEntrySet

      ImmutableSet<Map.Entry<K,V>> createEntrySet()
      Specified by:
      createEntrySet in class ImmutableMap<K,V>
    • createKeySet

      ImmutableSet<K> createKeySet()
      Specified by:
      createKeySet in class ImmutableMap<K,V>
    • createValues

      ImmutableCollection<V> createValues()
      Specified by:
      createValues in class ImmutableMap<K,V>
    • writeReplace

      Object writeReplace()
      Description copied from class: ImmutableMap
      Returns a serializable form of this object. Non-public subclasses should not override this method. Publicly-accessible subclasses must override this method and should return a subclass of SerializedForm whose readResolve() method returns objects of the subclass type.
      Overrides:
      writeReplace in class ImmutableMap<K,V>