Package com.google.protobuf
Class FieldSet<T extends FieldSet.FieldDescriptorLite<T>>
java.lang.Object
com.google.protobuf.FieldSet<T>
A class which represents an arbitrary set of fields of some message type. This is used to
implement
DynamicMessage
, and also to represent extensions in GeneratedMessage
.
This class is package-private, since outside users should probably be using DynamicMessage
.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final class
FieldSet.Builder<T extends FieldSet.FieldDescriptorLite<T>>
A FieldSet Builder that accept aMessageLite.Builder
as a field value.static interface
Interface for a FieldDescriptor or lite extension descriptor. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final FieldSet
<?> private final SmallSortedMap
<T, Object> private boolean
private boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addRepeatedField
(T descriptor, Object value) Useful for implementingMessage.Builder.addRepeatedField(Descriptors.FieldDescriptor,Object)
.void
clear()
void
clearField
(T descriptor) Useful for implementingMessage.Builder.clearField(Descriptors.FieldDescriptor)
.clone()
Clones the FieldSet.private static <T extends FieldSet.FieldDescriptorLite<T>>
SmallSortedMap<T, Object> cloneAllFieldsMap
(SmallSortedMap<T, Object> fields, boolean copyList, boolean resolveLazyFields) private static <T extends FieldSet.FieldDescriptorLite<T>>
voidcloneFieldEntry
(Map<T, Object> map, Map.Entry<T, Object> entry, boolean copyList, boolean resolveLazyFields) private static Object
cloneIfMutable
(Object value) (package private) static int
computeElementSize
(WireFormat.FieldType type, int number, Object value) Compute the number of bytes that would be needed to encode a single tag/value pair of arbitrary type.(package private) static int
computeElementSizeNoTag
(WireFormat.FieldType type, Object value) Compute the number of bytes that would be needed to encode a particular value of arbitrary type, excluding tag.static int
computeFieldSize
(FieldSet.FieldDescriptorLite<?> descriptor, Object value) Compute the number of bytes needed to encode a particular field.Get an iterator over the fields in the map in descending (i.e.static <T extends FieldSet.FieldDescriptorLite<T>>
FieldSet<T> emptySet()
Get an immutable empty FieldSet.boolean
Get a simple map containing all the fields.Useful for implementingMessageOrBuilder.getField(Descriptors.FieldDescriptor)
.int
LikegetSerializedSize()
but uses MessageSet wire format.private int
getMessageSetSerializedSize
(Map.Entry<T, Object> entry) getRepeatedField
(T descriptor, int index) Useful for implementingMessageOrBuilder.getRepeatedField(Descriptors.FieldDescriptor,int)
.int
getRepeatedFieldCount
(T descriptor) Useful for implementingMessageOrBuilder.getRepeatedFieldCount(Descriptors.FieldDescriptor)
.int
(package private) static int
getWireFormatForFieldType
(WireFormat.FieldType type, boolean isPacked) Given a field type, return the wire type.boolean
Useful for implementingMessageOrBuilder.hasField(Descriptors.FieldDescriptor)
.int
hashCode()
(package private) boolean
isEmpty()
Returnstrue
if empty,false
otherwise.boolean
Returns whether the FieldSet is immutable.boolean
private static <T extends FieldSet.FieldDescriptorLite<T>>
booleanisInitialized
(Map.Entry<T, Object> entry) private static boolean
private static boolean
isValidType
(WireFormat.FieldType type, Object value) iterator()
Get an iterator to the field map.void
Make this FieldSet immutable from this point forward.void
LikeMessage.Builder.mergeFrom(Message)
, but merges from anotherFieldSet
.private void
mergeFromField
(Map.Entry<T, Object> entry) static <T extends FieldSet.FieldDescriptorLite<T>>
FieldSet.Builder<T> Construct a new Builder.static <T extends FieldSet.FieldDescriptorLite<T>>
FieldSet<T> Construct a new FieldSet.static Object
readPrimitiveField
(CodedInputStream input, WireFormat.FieldType type, boolean checkUtf8) Read a field of any primitive type for immutable messages from a CodedInputStream.void
Useful for implementingMessage.Builder.setField(Descriptors.FieldDescriptor,Object)
.void
setRepeatedField
(T descriptor, int index, Object value) Useful for implementingMessage.Builder.setRepeatedField(Descriptors.FieldDescriptor,int,Object)
.private void
verifyType
(T descriptor, Object value) Verifies that the given object is of the correct type to be a valid value for the given field.(package private) static void
writeElement
(CodedOutputStream output, WireFormat.FieldType type, int number, Object value) Write a single tag-value pair to the stream.(package private) static void
writeElementNoTag
(CodedOutputStream output, WireFormat.FieldType type, Object value) Write a field of arbitrary type, without its tag, to the stream.static void
writeField
(FieldSet.FieldDescriptorLite<?> descriptor, Object value, CodedOutputStream output) Write a single field.void
writeMessageSetTo
(CodedOutputStream output) LikewriteTo(com.google.protobuf.CodedOutputStream)
but uses MessageSet wire format.private void
writeMessageSetTo
(Map.Entry<T, Object> entry, CodedOutputStream output) void
writeTo
(CodedOutputStream output)
-
Field Details
-
fields
-
isImmutable
private boolean isImmutable -
hasLazyField
private boolean hasLazyField -
DEFAULT_INSTANCE
-
-
Constructor Details
-
FieldSet
private FieldSet()Construct a new FieldSet. -
FieldSet
private FieldSet(boolean dummy) Construct an empty FieldSet. This is only used to initialize DEFAULT_INSTANCE. -
FieldSet
-
-
Method Details
-
newFieldSet
Construct a new FieldSet. -
emptySet
Get an immutable empty FieldSet. -
newBuilder
Construct a new Builder. -
isEmpty
boolean isEmpty()Returnstrue
if empty,false
otherwise. -
makeImmutable
public void makeImmutable()Make this FieldSet immutable from this point forward. -
isImmutable
public boolean isImmutable()Returns whether the FieldSet is immutable. This is true if it is theemptySet()
or ifmakeImmutable()
were called.- Returns:
- whether the FieldSet is immutable.
-
equals
-
hashCode
public int hashCode() -
clone
Clones the FieldSet. The returned FieldSet will be mutable even if the original FieldSet was immutable. -
clear
public void clear() -
getAllFields
Get a simple map containing all the fields. -
cloneAllFieldsMap
private static <T extends FieldSet.FieldDescriptorLite<T>> SmallSortedMap<T,Object> cloneAllFieldsMap(SmallSortedMap<T, Object> fields, boolean copyList, boolean resolveLazyFields) -
cloneFieldEntry
private static <T extends FieldSet.FieldDescriptorLite<T>> void cloneFieldEntry(Map<T, Object> map, Map.Entry<T, Object> entry, boolean copyList, boolean resolveLazyFields) -
iterator
Get an iterator to the field map. This iterator should not be leaked out of the protobuf library as it is not protected from mutation when fields is not immutable. -
descendingIterator
Get an iterator over the fields in the map in descending (i.e. reverse) order. This iterator should not be leaked out of the protobuf library as it is not protected from mutation when fields is not immutable. -
hasField
Useful for implementingMessageOrBuilder.hasField(Descriptors.FieldDescriptor)
. -
getField
Useful for implementingMessageOrBuilder.getField(Descriptors.FieldDescriptor)
. This method returnsnull
if the field is not set; in this case it is up to the caller to fetch the field's default value. -
setField
Useful for implementingMessage.Builder.setField(Descriptors.FieldDescriptor,Object)
. -
clearField
Useful for implementingMessage.Builder.clearField(Descriptors.FieldDescriptor)
. -
getRepeatedFieldCount
Useful for implementingMessageOrBuilder.getRepeatedFieldCount(Descriptors.FieldDescriptor)
. -
getRepeatedField
Useful for implementingMessageOrBuilder.getRepeatedField(Descriptors.FieldDescriptor,int)
. -
setRepeatedField
Useful for implementingMessage.Builder.setRepeatedField(Descriptors.FieldDescriptor,int,Object)
. -
addRepeatedField
Useful for implementingMessage.Builder.addRepeatedField(Descriptors.FieldDescriptor,Object)
. -
verifyType
Verifies that the given object is of the correct type to be a valid value for the given field. (For repeated fields, this checks if the object is the right type to be one element of the field.)- Throws:
IllegalArgumentException
- the value is not of the right type
-
isValidType
-
isInitialized
public boolean isInitialized()SeeMessageLiteOrBuilder.isInitialized()
. Note: SinceFieldSet
itself does not have any way of knowing about required fields that aren't actually present in the set, it is up to the caller to check that all required fields are present. -
isInitialized
private static <T extends FieldSet.FieldDescriptorLite<T>> boolean isInitialized(Map.Entry<T, Object> entry) -
isMessageFieldValueInitialized
-
getWireFormatForFieldType
Given a field type, return the wire type.- Returns:
- One of the
WIRETYPE_
constants defined inWireFormat
.
-
mergeFrom
LikeMessage.Builder.mergeFrom(Message)
, but merges from anotherFieldSet
. -
cloneIfMutable
-
mergeFromField
-
readPrimitiveField
public static Object readPrimitiveField(CodedInputStream input, WireFormat.FieldType type, boolean checkUtf8) throws IOException Read a field of any primitive type for immutable messages from a CodedInputStream. Enums, groups, and embedded messages are not handled by this method.- Parameters:
input
- the stream from which to readtype
- declared type of the fieldcheckUtf8
- When true, check that the input is valid UTF-8- Returns:
- an object representing the field's value, of the exact type which would be returned by
MessageOrBuilder.getField(Descriptors.FieldDescriptor)
for this field - Throws:
IOException
-
writeTo
- Throws:
IOException
-
writeMessageSetTo
LikewriteTo(com.google.protobuf.CodedOutputStream)
but uses MessageSet wire format.- Throws:
IOException
-
writeMessageSetTo
private void writeMessageSetTo(Map.Entry<T, Object> entry, CodedOutputStream output) throws IOException- Throws:
IOException
-
writeElement
static void writeElement(CodedOutputStream output, WireFormat.FieldType type, int number, Object value) throws IOException Write a single tag-value pair to the stream.- Parameters:
output
- The output stream.type
- The field's type.number
- The field's number.value
- Object representing the field's value. Must be of the exact type which would be returned byMessageOrBuilder.getField(Descriptors.FieldDescriptor)
for this field.- Throws:
IOException
-
writeElementNoTag
static void writeElementNoTag(CodedOutputStream output, WireFormat.FieldType type, Object value) throws IOException Write a field of arbitrary type, without its tag, to the stream.- Parameters:
output
- The output stream.type
- The field's type.value
- Object representing the field's value. Must be of the exact type which would be returned byMessageOrBuilder.getField(Descriptors.FieldDescriptor)
for this field.- Throws:
IOException
-
writeField
public static void writeField(FieldSet.FieldDescriptorLite<?> descriptor, Object value, CodedOutputStream output) throws IOException Write a single field.- Throws:
IOException
-
getSerializedSize
public int getSerializedSize()SeeMessageLite.getSerializedSize()
. It's up to the caller to cache the resulting size if desired. -
getMessageSetSerializedSize
public int getMessageSetSerializedSize()LikegetSerializedSize()
but uses MessageSet wire format. -
getMessageSetSerializedSize
-
computeElementSize
Compute the number of bytes that would be needed to encode a single tag/value pair of arbitrary type.- Parameters:
type
- The field's type.number
- The field's number.value
- Object representing the field's value. Must be of the exact type which would be returned byMessageOrBuilder.getField(Descriptors.FieldDescriptor)
for this field.
-
computeElementSizeNoTag
Compute the number of bytes that would be needed to encode a particular value of arbitrary type, excluding tag.- Parameters:
type
- The field's type.value
- Object representing the field's value. Must be of the exact type which would be returned byMessageOrBuilder.getField(Descriptors.FieldDescriptor)
for this field.
-
computeFieldSize
Compute the number of bytes needed to encode a particular field.
-