Class LazyFieldLite
- java.lang.Object
-
- com.google.protobuf.LazyFieldLite
-
- Direct Known Subclasses:
LazyField
public class LazyFieldLite extends java.lang.ObjectLazyFieldLite encapsulates the logic of lazily parsing message fields. It stores the message in a ByteString initially and then parses it on-demand.LazyFieldLite is thread-compatible: concurrent reads are safe once the proto that this LazyFieldLite is a part of is no longer being mutated by its Builder. However, explicit synchronization is needed under read/write situations.
When a LazyFieldLite is used in the context of a MessageLite object, its behavior is considered to be immutable and none of the setter methods in its API are expected to be invoked. All of the getters are expected to be thread-safe. When used in the context of a MessageLite.Builder, setters can be invoked, but there is no guarantee of thread safety.
TODO: Consider splitting this class's functionality and put the mutable methods into a separate builder class to allow us to give stronger compile-time guarantees.
This class is internal implementation detail of the protobuf library, so you don't need to use it directly.
-
-
Field Summary
Fields Modifier and Type Field Description private ByteStringdelayedBytesA delayed-parsed version of the contents of this field.private ExtensionRegistryLiteextensionRegistryAnExtensionRegistryLitefor parsing bytes.private ByteStringmemoizedBytesThe memoized bytes forvalue.protected MessageLitevalueThe parsed value.
-
Constructor Summary
Constructors Constructor Description LazyFieldLite()Constructs a LazyFieldLite with no contents, and no ability to parse extensions.LazyFieldLite(ExtensionRegistryLite extensionRegistry, ByteString bytes)Constructs a LazyFieldLite with bytes that will be parsed lazily.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static voidcheckArguments(ExtensionRegistryLite extensionRegistry, ByteString bytes)voidclear()Clears the value state of this instance.booleancontainsDefaultInstance()Determines whether this LazyFieldLite instance represents the default instance of this type.protected voidensureInitialized(MessageLite defaultInstance)Might lazily parse the bytes that were previously passed in.booleanequals(java.lang.Object o)static LazyFieldLitefromValue(MessageLite value)Constructs a LazyFieldLite instance with a value.intgetSerializedSize()Due to the optional field can be duplicated at the end of serialized bytes, which will make the serialized size changed after LazyField parsed.MessageLitegetValue(MessageLite defaultInstance)Returns message instance.inthashCode()voidmerge(LazyFieldLite other)Merges another instance's contents.voidmergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)Merges another instance's contents from a stream.private static MessageLitemergeValueAndBytes(MessageLite value, ByteString otherBytes, ExtensionRegistryLite extensionRegistry)voidset(LazyFieldLite other)Overrides the contents of this LazyField.voidsetByteString(ByteString bytes, ExtensionRegistryLite extensionRegistry)Sets this field with bytes to delay-parse.MessageLitesetValue(MessageLite value)Sets the value of the instance and returns the old value without delay parsing anything.ByteStringtoByteString()Returns a BytesString for this field in a thread-safe way.(package private) voidwriteTo(Writer writer, int fieldNumber)Writes this lazy field into aWriter.
-
-
-
Field Detail
-
delayedBytes
private ByteString delayedBytes
A delayed-parsed version of the contents of this field. When this field is non-null, then the "value" field is allowed to be null until the time that the value needs to be read.When delayedBytes is non-null then
extensionRegistryis required to also be non-null.valueandmemoizedByteswill be initialized lazily.
-
extensionRegistry
private ExtensionRegistryLite extensionRegistry
AnExtensionRegistryLitefor parsing bytes. It is non-null on a best-effort basis. It is only guaranteed to be non-null if this message was initialized using bytes and anExtensionRegistry. If it directly had a value set then it will be null, unless it has been merged with anotherLazyFieldLitethat had anExtensionRegistry.
-
value
protected volatile MessageLite value
The parsed value. When this is null and a caller needs access to the MessageLite value, thendelayedByteswill be parsed lazily at that time.
-
memoizedBytes
private volatile ByteString memoizedBytes
The memoized bytes forvalue. This is an optimization for the toByteString() method to not have to recompute its return-value on each invocation. TODO: Figure out whether this optimization is actually necessary.
-
-
Constructor Detail
-
LazyFieldLite
public LazyFieldLite(ExtensionRegistryLite extensionRegistry, ByteString bytes)
Constructs a LazyFieldLite with bytes that will be parsed lazily.
-
LazyFieldLite
public LazyFieldLite()
Constructs a LazyFieldLite with no contents, and no ability to parse extensions.
-
-
Method Detail
-
fromValue
public static LazyFieldLite fromValue(MessageLite value)
Constructs a LazyFieldLite instance with a value. The LazyFieldLite may not be able to parse the extensions in the value as it has no ExtensionRegistry.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
containsDefaultInstance
public boolean containsDefaultInstance()
Determines whether this LazyFieldLite instance represents the default instance of this type.
-
clear
public void clear()
Clears the value state of this instance.LazyField is not thread-safe for write access. Synchronizations are needed under read/write situations.
-
set
public void set(LazyFieldLite other)
Overrides the contents of this LazyField.LazyField is not thread-safe for write access. Synchronizations are needed under read/write situations.
-
getValue
public MessageLite getValue(MessageLite defaultInstance)
Returns message instance. It may do some thread-safe delayed parsing of bytes.- Parameters:
defaultInstance- its message's default instance. It's also used to get parser for the message type.
-
setValue
public MessageLite setValue(MessageLite value)
Sets the value of the instance and returns the old value without delay parsing anything.LazyField is not thread-safe for write access. Synchronizations are needed under read/write situations.
-
merge
public void merge(LazyFieldLite other)
Merges another instance's contents. In some cases may drop some extensions if both fields contain data. If the other field has anExtensionRegistrybut this does not, then this field will copy over thatExtensionRegistry.LazyField is not thread-safe for write access. Synchronizations are needed under read/write situations.
-
mergeFrom
public void mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry) throws java.io.IOException
Merges another instance's contents from a stream.LazyField is not thread-safe for write access. Synchronizations are needed under read/write situations.
- Throws:
java.io.IOException
-
mergeValueAndBytes
private static MessageLite mergeValueAndBytes(MessageLite value, ByteString otherBytes, ExtensionRegistryLite extensionRegistry)
-
setByteString
public void setByteString(ByteString bytes, ExtensionRegistryLite extensionRegistry)
Sets this field with bytes to delay-parse.
-
getSerializedSize
public int getSerializedSize()
Due to the optional field can be duplicated at the end of serialized bytes, which will make the serialized size changed after LazyField parsed. Be careful when using this method.
-
toByteString
public ByteString toByteString()
Returns a BytesString for this field in a thread-safe way.
-
writeTo
void writeTo(Writer writer, int fieldNumber) throws java.io.IOException
Writes this lazy field into aWriter.- Throws:
java.io.IOException
-
ensureInitialized
protected void ensureInitialized(MessageLite defaultInstance)
Might lazily parse the bytes that were previously passed in. Is thread-safe.
-
checkArguments
private static void checkArguments(ExtensionRegistryLite extensionRegistry, ByteString bytes)
-
-