Class MapField<K,V>

java.lang.Object
com.google.protobuf.MapField<K,V>
All Implemented Interfaces:
MutabilityOracle

public class MapField<K,V> extends Object implements MutabilityOracle
Internal representation of map fields in generated messages.

This class supports accessing the map field as a Map to be used in generated API and also supports accessing the field as a List to be used in reflection API. It keeps track of where the data is currently stored and do necessary conversions between map and list.

This class is a protobuf implementation detail. Users shouldn't use this class directly.

THREAD-SAFETY NOTE: Read-only access is thread-safe. Users can call getMap() and getList() concurrently in multiple threads. If write-access is needed, all access must be synchronized.

  • Field Details

  • Constructor Details

  • Method Details

    • emptyMapField

      public static <K, V> MapField<K,V> emptyMapField(MapEntry<K,V> defaultEntry)
      Returns an immutable empty MapField.
    • newMapField

      public static <K, V> MapField<K,V> newMapField(MapEntry<K,V> defaultEntry)
      Creates a new mutable empty MapField.
    • convertKeyAndValueToMessage

      private Message convertKeyAndValueToMessage(K key, V value)
    • convertMessageToKeyAndValue

      private void convertMessageToKeyAndValue(Message message, Map<K,V> map)
    • convertMapToList

      private List<Message> convertMapToList(MapField.MutabilityAwareMap<K,V> mapData)
    • convertListToMap

      private MapField.MutabilityAwareMap<K,V> convertListToMap(List<Message> listData)
    • getMap

      public Map<K,V> getMap()
      Returns the content of this MapField as a read-only Map.
    • getMutableMap

      public Map<K,V> getMutableMap()
      Gets a mutable Map view of this MapField.
    • mergeFrom

      public void mergeFrom(MapField<K,V> other)
    • clear

      public void clear()
    • equals

      public boolean equals(Object object)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • copy

      public MapField<K,V> copy()
      Returns a deep copy of this MapField.
    • getList

      List<Message> getList()
      Gets the content of this MapField as a read-only List.
    • getMutableList

      List<Message> getMutableList()
      Gets a mutable List view of this MapField.
    • getMapEntryMessageDefaultInstance

      Message getMapEntryMessageDefaultInstance()
      Gets the default instance of the message stored in the list view of this map field.
    • makeImmutable

      public void makeImmutable()
      Makes this list immutable. All subsequent modifications will throw an UnsupportedOperationException.
    • isMutable

      public boolean isMutable()
      Returns whether this field can be modified.
    • ensureMutable

      public void ensureMutable()
      Description copied from interface: MutabilityOracle
      Throws an UnsupportedOperationException if not mutable.
      Specified by:
      ensureMutable in interface MutabilityOracle