Package com.google.protobuf
Class MapField<K,V>
- java.lang.Object
-
- com.google.protobuf.MapField<K,V>
-
- All Implemented Interfaces:
MutabilityOracle
public class MapField<K,V> extends java.lang.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 aList
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static interface
MapField.Converter<K,V>
private static class
MapField.ImmutableMessageConverter<K,V>
private static class
MapField.MutatabilityAwareMap<K,V>
An internal map that checks for mutability before delegating.private static class
MapField.StorageMode
Indicates where the data of this map field is currently stored.
-
Field Summary
Fields Modifier and Type Field Description private MapField.Converter<K,V>
converter
private boolean
isMutable
private java.util.List<Message>
listData
private MapField.MutatabilityAwareMap<K,V>
mapData
private MapField.StorageMode
mode
-
Fields inherited from interface com.google.protobuf.MutabilityOracle
IMMUTABLE
-
-
Constructor Summary
Constructors Modifier Constructor Description private
MapField(MapEntry<K,V> defaultEntry, MapField.StorageMode mode, java.util.Map<K,V> mapData)
private
MapField(MapField.Converter<K,V> converter, MapField.StorageMode mode, java.util.Map<K,V> mapData)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
private Message
convertKeyAndValueToMessage(K key, V value)
private MapField.MutatabilityAwareMap<K,V>
convertListToMap(java.util.List<Message> listData)
private java.util.List<Message>
convertMapToList(MapField.MutatabilityAwareMap<K,V> mapData)
private void
convertMessageToKeyAndValue(Message message, java.util.Map<K,V> map)
MapField<K,V>
copy()
Returns a deep copy of this MapField.static <K,V>
MapField<K,V>emptyMapField(MapEntry<K,V> defaultEntry)
Returns an immutable empty MapField.void
ensureMutable()
Throws anUnsupportedOperationException
if not mutable.boolean
equals(java.lang.Object object)
(package private) java.util.List<Message>
getList()
Gets the content of this MapField as a read-only List.java.util.Map<K,V>
getMap()
Returns the content of this MapField as a read-only Map.(package private) Message
getMapEntryMessageDefaultInstance()
Gets the default instance of the message stored in the list view of this map field.(package private) java.util.List<Message>
getMutableList()
Gets a mutable List view of this MapField.java.util.Map<K,V>
getMutableMap()
Gets a mutable Map view of this MapField.int
hashCode()
boolean
isMutable()
Returns whether this field can be modified.void
makeImmutable()
Makes this list immutable.void
mergeFrom(MapField<K,V> other)
static <K,V>
MapField<K,V>newMapField(MapEntry<K,V> defaultEntry)
Creates a new mutable empty MapField.
-
-
-
Field Detail
-
isMutable
private volatile boolean isMutable
-
mode
private volatile MapField.StorageMode mode
-
mapData
private MapField.MutatabilityAwareMap<K,V> mapData
-
listData
private java.util.List<Message> listData
-
converter
private final MapField.Converter<K,V> converter
-
-
Constructor Detail
-
MapField
private MapField(MapField.Converter<K,V> converter, MapField.StorageMode mode, java.util.Map<K,V> mapData)
-
-
Method Detail
-
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.
-
convertMessageToKeyAndValue
private void convertMessageToKeyAndValue(Message message, java.util.Map<K,V> map)
-
convertMapToList
private java.util.List<Message> convertMapToList(MapField.MutatabilityAwareMap<K,V> mapData)
-
convertListToMap
private MapField.MutatabilityAwareMap<K,V> convertListToMap(java.util.List<Message> listData)
-
clear
public void clear()
-
equals
public boolean equals(java.lang.Object object)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
getList
java.util.List<Message> getList()
Gets the content of this MapField as a read-only List.
-
getMutableList
java.util.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 anUnsupportedOperationException
.
-
isMutable
public boolean isMutable()
Returns whether this field can be modified.
-
ensureMutable
public void ensureMutable()
Description copied from interface:MutabilityOracle
Throws anUnsupportedOperationException
if not mutable.- Specified by:
ensureMutable
in interfaceMutabilityOracle
-
-