Package com.google.protobuf
Class FieldSet<T extends FieldSet.FieldDescriptorLite<T>>
- java.lang.Object
-
- com.google.protobuf.FieldSet<T>
-
final class FieldSet<T extends FieldSet.FieldDescriptorLite<T>> extends java.lang.Object
A class which represents an arbitrary set of fields of some message type. This is used to implementDynamicMessage
, and also to represent extensions inGeneratedMessage
. This class is package-private, since outside users should probably be usingDynamicMessage
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
FieldSet.Builder<T extends FieldSet.FieldDescriptorLite<T>>
A FieldSet Builder that accept aMessageLite.Builder
as a field value.static interface
FieldSet.FieldDescriptorLite<T extends FieldSet.FieldDescriptorLite<T>>
Interface for a FieldDescriptor or lite extension descriptor.
-
Field Summary
Fields Modifier and Type Field Description private static FieldSet<?>
DEFAULT_INSTANCE
private SmallSortedMap<T,java.lang.Object>
fields
private boolean
hasLazyField
private boolean
isImmutable
-
Constructor Summary
Constructors Modifier Constructor Description private
FieldSet()
Construct a new FieldSet.private
FieldSet(boolean dummy)
Construct an empty FieldSet.private
FieldSet(SmallSortedMap<T,java.lang.Object> fields)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addRepeatedField(T descriptor, java.lang.Object value)
Useful for implementingMessage.Builder.addRepeatedField(Descriptors.FieldDescriptor,Object)
.void
clear()
void
clearField(T descriptor)
Useful for implementingMessage.Builder.clearField(Descriptors.FieldDescriptor)
.FieldSet<T>
clone()
Clones the FieldSet.private static <T extends FieldSet.FieldDescriptorLite<T>>
SmallSortedMap<T,java.lang.Object>cloneAllFieldsMap(SmallSortedMap<T,java.lang.Object> fields, boolean copyList, boolean resolveLazyFields)
private static <T extends FieldSet.FieldDescriptorLite<T>>
voidcloneFieldEntry(java.util.Map<T,java.lang.Object> map, java.util.Map.Entry<T,java.lang.Object> entry, boolean copyList, boolean resolveLazyFields)
private static java.lang.Object
cloneIfMutable(java.lang.Object value)
(package private) static int
computeElementSize(WireFormat.FieldType type, int number, java.lang.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, java.lang.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, java.lang.Object value)
Compute the number of bytes needed to encode a particular field.(package private) java.util.Iterator<java.util.Map.Entry<T,java.lang.Object>>
descendingIterator()
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
equals(java.lang.Object o)
java.util.Map<T,java.lang.Object>
getAllFields()
Get a simple map containing all the fields.java.lang.Object
getField(T descriptor)
Useful for implementingMessageOrBuilder.getField(Descriptors.FieldDescriptor)
.int
getMessageSetSerializedSize()
LikegetSerializedSize()
but uses MessageSet wire format.private int
getMessageSetSerializedSize(java.util.Map.Entry<T,java.lang.Object> entry)
java.lang.Object
getRepeatedField(T descriptor, int index)
Useful for implementingMessageOrBuilder.getRepeatedField(Descriptors.FieldDescriptor,int)
.int
getRepeatedFieldCount(T descriptor)
Useful for implementingMessageOrBuilder.getRepeatedFieldCount(Descriptors.FieldDescriptor)
.int
getSerializedSize()
(package private) static int
getWireFormatForFieldType(WireFormat.FieldType type, boolean isPacked)
Given a field type, return the wire type.boolean
hasField(T descriptor)
Useful for implementingMessageOrBuilder.hasField(Descriptors.FieldDescriptor)
.int
hashCode()
(package private) boolean
isEmpty()
Returnstrue
if empty,false
otherwise.boolean
isImmutable()
Returns whether the FieldSet is immutable.boolean
isInitialized()
private static <T extends FieldSet.FieldDescriptorLite<T>>
booleanisInitialized(java.util.Map.Entry<T,java.lang.Object> entry)
private static boolean
isMessageFieldValueInitialized(java.lang.Object value)
private static boolean
isValidType(WireFormat.FieldType type, java.lang.Object value)
java.util.Iterator<java.util.Map.Entry<T,java.lang.Object>>
iterator()
Get an iterator to the field map.void
makeImmutable()
Make this FieldSet immutable from this point forward.void
mergeFrom(FieldSet<T> other)
LikeMessage.Builder.mergeFrom(Message)
, but merges from anotherFieldSet
.private void
mergeFromField(java.util.Map.Entry<T,java.lang.Object> entry)
static <T extends FieldSet.FieldDescriptorLite<T>>
FieldSet.Builder<T>newBuilder()
Construct a new Builder.static <T extends FieldSet.FieldDescriptorLite<T>>
FieldSet<T>newFieldSet()
Construct a new FieldSet.static java.lang.Object
readPrimitiveField(CodedInputStream input, WireFormat.FieldType type, boolean checkUtf8)
Read a field of any primitive type for immutable messages from a CodedInputStream.void
setField(T descriptor, java.lang.Object value)
Useful for implementingMessage.Builder.setField(Descriptors.FieldDescriptor,Object)
.void
setRepeatedField(T descriptor, int index, java.lang.Object value)
Useful for implementingMessage.Builder.setRepeatedField(Descriptors.FieldDescriptor,int,Object)
.private void
verifyType(T descriptor, java.lang.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, java.lang.Object value)
Write a single tag-value pair to the stream.(package private) static void
writeElementNoTag(CodedOutputStream output, WireFormat.FieldType type, java.lang.Object value)
Write a field of arbitrary type, without its tag, to the stream.static void
writeField(FieldSet.FieldDescriptorLite<?> descriptor, java.lang.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(java.util.Map.Entry<T,java.lang.Object> entry, CodedOutputStream output)
void
writeTo(CodedOutputStream output)
-
-
-
Field Detail
-
fields
private final SmallSortedMap<T extends FieldSet.FieldDescriptorLite<T>,java.lang.Object> fields
-
isImmutable
private boolean isImmutable
-
hasLazyField
private boolean hasLazyField
-
DEFAULT_INSTANCE
private static final FieldSet<?> DEFAULT_INSTANCE
-
-
Constructor Detail
-
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
private FieldSet(SmallSortedMap<T,java.lang.Object> fields)
-
-
Method Detail
-
newFieldSet
public static <T extends FieldSet.FieldDescriptorLite<T>> FieldSet<T> newFieldSet()
Construct a new FieldSet.
-
emptySet
public static <T extends FieldSet.FieldDescriptorLite<T>> FieldSet<T> emptySet()
Get an immutable empty FieldSet.
-
newBuilder
public static <T extends FieldSet.FieldDescriptorLite<T>> FieldSet.Builder<T> 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
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
clone
public FieldSet<T> clone()
Clones the FieldSet. The returned FieldSet will be mutable even if the original FieldSet was immutable.- Overrides:
clone
in classjava.lang.Object
- Returns:
- the newly cloned FieldSet
-
clear
public void clear()
-
getAllFields
public java.util.Map<T,java.lang.Object> getAllFields()
Get a simple map containing all the fields.
-
cloneAllFieldsMap
private static <T extends FieldSet.FieldDescriptorLite<T>> SmallSortedMap<T,java.lang.Object> cloneAllFieldsMap(SmallSortedMap<T,java.lang.Object> fields, boolean copyList, boolean resolveLazyFields)
-
cloneFieldEntry
private static <T extends FieldSet.FieldDescriptorLite<T>> void cloneFieldEntry(java.util.Map<T,java.lang.Object> map, java.util.Map.Entry<T,java.lang.Object> entry, boolean copyList, boolean resolveLazyFields)
-
iterator
public java.util.Iterator<java.util.Map.Entry<T,java.lang.Object>> 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
java.util.Iterator<java.util.Map.Entry<T,java.lang.Object>> 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
public boolean hasField(T descriptor)
Useful for implementingMessageOrBuilder.hasField(Descriptors.FieldDescriptor)
.
-
getField
public java.lang.Object getField(T descriptor)
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
public void setField(T descriptor, java.lang.Object value)
Useful for implementingMessage.Builder.setField(Descriptors.FieldDescriptor,Object)
.
-
clearField
public void clearField(T descriptor)
Useful for implementingMessage.Builder.clearField(Descriptors.FieldDescriptor)
.
-
getRepeatedFieldCount
public int getRepeatedFieldCount(T descriptor)
Useful for implementingMessageOrBuilder.getRepeatedFieldCount(Descriptors.FieldDescriptor)
.
-
getRepeatedField
public java.lang.Object getRepeatedField(T descriptor, int index)
Useful for implementingMessageOrBuilder.getRepeatedField(Descriptors.FieldDescriptor,int)
.
-
setRepeatedField
public void setRepeatedField(T descriptor, int index, java.lang.Object value)
Useful for implementingMessage.Builder.setRepeatedField(Descriptors.FieldDescriptor,int,Object)
.
-
addRepeatedField
public void addRepeatedField(T descriptor, java.lang.Object value)
Useful for implementingMessage.Builder.addRepeatedField(Descriptors.FieldDescriptor,Object)
.
-
verifyType
private void verifyType(T descriptor, java.lang.Object value)
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:
java.lang.IllegalArgumentException
- the value is not of the right type
-
isValidType
private static boolean isValidType(WireFormat.FieldType type, java.lang.Object value)
-
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(java.util.Map.Entry<T,java.lang.Object> entry)
-
isMessageFieldValueInitialized
private static boolean isMessageFieldValueInitialized(java.lang.Object value)
-
getWireFormatForFieldType
static int getWireFormatForFieldType(WireFormat.FieldType type, boolean isPacked)
Given a field type, return the wire type.- Returns:
- One of the
WIRETYPE_
constants defined inWireFormat
.
-
mergeFrom
public void mergeFrom(FieldSet<T> other)
LikeMessage.Builder.mergeFrom(Message)
, but merges from anotherFieldSet
.
-
cloneIfMutable
private static java.lang.Object cloneIfMutable(java.lang.Object value)
-
mergeFromField
private void mergeFromField(java.util.Map.Entry<T,java.lang.Object> entry)
-
readPrimitiveField
public static java.lang.Object readPrimitiveField(CodedInputStream input, WireFormat.FieldType type, boolean checkUtf8) throws java.io.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:
java.io.IOException
-
writeTo
public void writeTo(CodedOutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeMessageSetTo
public void writeMessageSetTo(CodedOutputStream output) throws java.io.IOException
LikewriteTo(com.google.protobuf.CodedOutputStream)
but uses MessageSet wire format.- Throws:
java.io.IOException
-
writeMessageSetTo
private void writeMessageSetTo(java.util.Map.Entry<T,java.lang.Object> entry, CodedOutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeElement
static void writeElement(CodedOutputStream output, WireFormat.FieldType type, int number, java.lang.Object value) throws java.io.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:
java.io.IOException
-
writeElementNoTag
static void writeElementNoTag(CodedOutputStream output, WireFormat.FieldType type, java.lang.Object value) throws java.io.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:
java.io.IOException
-
writeField
public static void writeField(FieldSet.FieldDescriptorLite<?> descriptor, java.lang.Object value, CodedOutputStream output) throws java.io.IOException
Write a single field.- Throws:
java.io.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
private int getMessageSetSerializedSize(java.util.Map.Entry<T,java.lang.Object> entry)
-
computeElementSize
static int computeElementSize(WireFormat.FieldType type, int number, java.lang.Object value)
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
static int computeElementSizeNoTag(WireFormat.FieldType type, java.lang.Object value)
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
public static int computeFieldSize(FieldSet.FieldDescriptorLite<?> descriptor, java.lang.Object value)
Compute the number of bytes needed to encode a particular field.
-
-