Class LazyMap

java.lang.Object
java.util.AbstractMap<String,Object>
org.apache.groovy.json.internal.LazyMap
All Implemented Interfaces:
Map<String,Object>

public class LazyMap extends AbstractMap<String,Object>
A map that defers creating its backing map, so the small objects that dominate JSON documents do not incur that overhead. Entries are held in parallel key/value arrays until the first lookup, or until the entry count passes the linear-scan threshold, whichever comes first.
  • Nested Class Summary

    Nested classes/interfaces inherited from class java.util.AbstractMap

    AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>

    Nested classes/interfaces inherited from interface java.util.Map

    Map.Entry<K,V>
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a lazy map with the default key and value buffer size.
    LazyMap(int initialSize)
    Creates a lazy map with a caller-supplied key and value buffer size.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the compact buffers or hydrated map, depending on the current state.
    Copies the compact entries into a new LazyMap and clears this instance.
    protected Object
    Clones the hydrated backing map when one already exists.
    boolean
    Hydrates the backing map if needed and tests whether the key is present.
    boolean
    Hydrates the backing map if needed and tests whether the value is present.
    Returns the hydrated entry set, building the backing map if necessary.
    boolean
    Hydrates the backing map if needed and compares it with another object.
    get(Object key)
    Hydrates the backing map if needed and returns the value for the supplied key.
    static <V> V[]
    grow(V[] array)
    Doubles the capacity of the supplied array while preserving its component type.
    int
    Hydrates the backing map if needed and returns its hash code.
    boolean
    Returns whether the map currently contains any entries.
    Hydrates the backing map if needed and returns its key set.
    put(String key, Object value)
    Stores a mapping while keeping the compact array-backed representation until hydration is needed.
    void
    Hydrates the backing map if needed and copies all supplied entries into it.
    Hydrates the backing map if needed and removes the supplied key.
    int
    Returns the current size without hydrating when the compact representation is still active.
    Hydrates the backing map if needed and returns its string form.
    Hydrates the backing map if needed and returns its values collection.

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • LazyMap

      public LazyMap()
      Creates a lazy map with the default key and value buffer size.
    • LazyMap

      public LazyMap(int initialSize)
      Creates a lazy map with a caller-supplied key and value buffer size.
      Parameters:
      initialSize - initial buffer size
  • Method Details

    • put

      public Object put(String key, Object value)
      Stores a mapping while keeping the compact array-backed representation until hydration is needed. Once the entry count passes the linear-scan threshold, the backing map is built and delegated to, keeping insertion amortized constant-time for wide objects.
      Specified by:
      put in interface Map<String,Object>
      Overrides:
      put in class AbstractMap<String,Object>
      Parameters:
      key - entry key
      value - entry value
      Returns:
      previous value for key, or null
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Returns the hydrated entry set, building the backing map if necessary.
      Specified by:
      entrySet in interface Map<String,Object>
      Specified by:
      entrySet in class AbstractMap<String,Object>
      Returns:
      hydrated entry set
    • size

      public int size()
      Returns the current size without hydrating when the compact representation is still active.
      Specified by:
      size in interface Map<String,Object>
      Overrides:
      size in class AbstractMap<String,Object>
      Returns:
      map size
    • isEmpty

      public boolean isEmpty()
      Returns whether the map currently contains any entries.
      Specified by:
      isEmpty in interface Map<String,Object>
      Overrides:
      isEmpty in class AbstractMap<String,Object>
      Returns:
      true when no entries are stored
    • containsValue

      public boolean containsValue(Object value)
      Hydrates the backing map if needed and tests whether the value is present.
      Specified by:
      containsValue in interface Map<String,Object>
      Overrides:
      containsValue in class AbstractMap<String,Object>
      Parameters:
      value - value to test
      Returns:
      true when the value is present
    • containsKey

      public boolean containsKey(Object key)
      Hydrates the backing map if needed and tests whether the key is present.
      Specified by:
      containsKey in interface Map<String,Object>
      Overrides:
      containsKey in class AbstractMap<String,Object>
      Parameters:
      key - key to test
      Returns:
      true when the key is present
    • get

      public Object get(Object key)
      Hydrates the backing map if needed and returns the value for the supplied key.
      Specified by:
      get in interface Map<String,Object>
      Overrides:
      get in class AbstractMap<String,Object>
      Parameters:
      key - key to look up
      Returns:
      mapped value, or null
    • remove

      public Object remove(Object key)
      Hydrates the backing map if needed and removes the supplied key.
      Specified by:
      remove in interface Map<String,Object>
      Overrides:
      remove in class AbstractMap<String,Object>
      Parameters:
      key - key to remove
      Returns:
      removed value, or null
    • putAll

      public void putAll(Map m)
      Hydrates the backing map if needed and copies all supplied entries into it.
      Specified by:
      putAll in interface Map<String,Object>
      Overrides:
      putAll in class AbstractMap<String,Object>
      Parameters:
      m - entries to copy
    • clear

      public void clear()
      Clears the compact buffers or hydrated map, depending on the current state.
      Specified by:
      clear in interface Map<String,Object>
      Overrides:
      clear in class AbstractMap<String,Object>
    • keySet

      public Set<String> keySet()
      Hydrates the backing map if needed and returns its key set.
      Specified by:
      keySet in interface Map<String,Object>
      Overrides:
      keySet in class AbstractMap<String,Object>
      Returns:
      hydrated key set
    • values

      public Collection<Object> values()
      Hydrates the backing map if needed and returns its values collection.
      Specified by:
      values in interface Map<String,Object>
      Overrides:
      values in class AbstractMap<String,Object>
      Returns:
      hydrated values collection
    • equals

      public boolean equals(Object o)
      Hydrates the backing map if needed and compares it with another object.
      Specified by:
      equals in interface Map<String,Object>
      Overrides:
      equals in class AbstractMap<String,Object>
      Parameters:
      o - other object
      Returns:
      true when equal
    • hashCode

      public int hashCode()
      Hydrates the backing map if needed and returns its hash code.
      Specified by:
      hashCode in interface Map<String,Object>
      Overrides:
      hashCode in class AbstractMap<String,Object>
      Returns:
      hydrated hash code
    • toString

      public String toString()
      Hydrates the backing map if needed and returns its string form.
      Overrides:
      toString in class AbstractMap<String,Object>
      Returns:
      hydrated map text
    • clone

      protected Object clone() throws CloneNotSupportedException
      Clones the hydrated backing map when one already exists.
      Overrides:
      clone in class AbstractMap<String,Object>
      Returns:
      a clone of the hydrated backing map, or null when still compact
      Throws:
      CloneNotSupportedException - never thrown by the current implementation
    • clearAndCopy

      public LazyMap clearAndCopy()
      Copies the compact entries into a new LazyMap and clears this instance.
      Returns:
      a copy containing the current compact entries
    • grow

      public static <V> V[] grow(V[] array)
      Doubles the capacity of the supplied array while preserving its component type.
      Type Parameters:
      V - component type
      Parameters:
      array - array to grow
      Returns:
      the expanded array