Package com.google.protobuf
Class RepeatedFieldBuilderV3<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder>
- java.lang.Object
-
- com.google.protobuf.RepeatedFieldBuilderV3<MType,BType,IType>
-
- Type Parameters:
MType
- the type of message for the fieldBType
- the type of builder for the fieldIType
- the common interface for the message and the builder
- All Implemented Interfaces:
AbstractMessage.BuilderParent
public class RepeatedFieldBuilderV3<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder> extends java.lang.Object implements AbstractMessage.BuilderParent
RepeatedFieldBuilderV3
implements a structure that a protocol message uses to hold a repeated field of other protocol messages. It supports the classical use case of adding immutableMessage
's to the repeated field and is highly optimized around this (no extra memory allocations and sharing of immutable arrays).
It also supports the additional use case of adding aMessage.Builder
to the repeated field and deferring conversion of thatBuilder
to an immutableMessage
. In this way, it's possible to maintain a tree ofBuilder
's that acts as a fully read/write data structure.
Logically, one can think of a tree of builders as converting the entire tree to messages when build is called on the root or when any method is called that desires a Message instead of a Builder. In terms of the implementation, theSingleFieldBuilderV3
andRepeatedFieldBuilderV3
classes cache messages that were created so that messages only need to be created when some change occurred in its builder or a builder for one of its descendants.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
RepeatedFieldBuilderV3.BuilderExternalList<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder>
Provides a live view of the builder as a list of builders.private static class
RepeatedFieldBuilderV3.MessageExternalList<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder>
Provides a live view of the builder as a list of messages.private static class
RepeatedFieldBuilderV3.MessageOrBuilderExternalList<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder>
Provides a live view of the builder as a list of builders.
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<SingleFieldBuilderV3<MType,BType,IType>>
builders
private RepeatedFieldBuilderV3.BuilderExternalList<MType,BType,IType>
externalBuilderList
private RepeatedFieldBuilderV3.MessageExternalList<MType,BType,IType>
externalMessageList
private RepeatedFieldBuilderV3.MessageOrBuilderExternalList<MType,BType,IType>
externalMessageOrBuilderList
private boolean
isClean
private boolean
isMessagesListMutable
private java.util.List<MType>
messages
private AbstractMessage.BuilderParent
parent
-
Constructor Summary
Constructors Constructor Description RepeatedFieldBuilderV3(java.util.List<MType> messages, boolean isMessagesListMutable, AbstractMessage.BuilderParent parent, boolean isClean)
Constructs a new builder with an empty list of messages.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RepeatedFieldBuilderV3<MType,BType,IType>
addAllMessages(java.lang.Iterable<? extends MType> values)
Appends all of the messages in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.BType
addBuilder(int index, MType message)
Inserts a new builder at the specified position in this list.BType
addBuilder(MType message)
Appends a new builder to the end of this list and returns the builder.RepeatedFieldBuilderV3<MType,BType,IType>
addMessage(int index, MType message)
Inserts the specified message at the specified position in this list.RepeatedFieldBuilderV3<MType,BType,IType>
addMessage(MType message)
Appends the specified element to the end of this list.java.util.List<MType>
build()
Builds the list of messages from the builder and returns them.void
clear()
Removes all of the elements from this list.void
dispose()
private void
ensureBuilders()
Ensures that the list of builders is not null.private void
ensureMutableMessageList()
Ensures that the list of messages is mutable so it can be updated.BType
getBuilder(int index)
Gets a builder for the specified index.java.util.List<BType>
getBuilderList()
Gets a view of the builder as a list of builders.int
getCount()
Gets the count of items in the list.MType
getMessage(int index)
Get the message at the specified index.private MType
getMessage(int index, boolean forBuild)
Get the message at the specified index.java.util.List<MType>
getMessageList()
Gets a view of the builder as a list of messages.IType
getMessageOrBuilder(int index)
Gets the base class interface for the specified index.java.util.List<IType>
getMessageOrBuilderList()
Gets a view of the builder as a list of MessageOrBuilders.private void
incrementModCounts()
Increments the mod counts so that an ConcurrentModificationException can be thrown if calling code tries to modify the builder while its iterating the list.boolean
isEmpty()
Gets whether the list is empty.void
markDirty()
A builder becomes dirty whenever a field is modified -- including fields in nested builders -- and becomes clean when build() is called.private void
onChanged()
Called when a the builder or one of its nested children has changed and any parent should be notified of its invalidation.void
remove(int index)
Removes the element at the specified position in this list.RepeatedFieldBuilderV3<MType,BType,IType>
setMessage(int index, MType message)
Sets a message at the specified index replacing the existing item at that index.
-
-
-
Field Detail
-
parent
private AbstractMessage.BuilderParent parent
-
messages
private java.util.List<MType extends AbstractMessage> messages
-
isMessagesListMutable
private boolean isMessagesListMutable
-
builders
private java.util.List<SingleFieldBuilderV3<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder>> builders
-
isClean
private boolean isClean
-
externalMessageList
private RepeatedFieldBuilderV3.MessageExternalList<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder> externalMessageList
-
externalBuilderList
private RepeatedFieldBuilderV3.BuilderExternalList<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder> externalBuilderList
-
externalMessageOrBuilderList
private RepeatedFieldBuilderV3.MessageOrBuilderExternalList<MType extends AbstractMessage,BType extends AbstractMessage.Builder,IType extends MessageOrBuilder> externalMessageOrBuilderList
-
-
Constructor Detail
-
RepeatedFieldBuilderV3
public RepeatedFieldBuilderV3(java.util.List<MType> messages, boolean isMessagesListMutable, AbstractMessage.BuilderParent parent, boolean isClean)
Constructs a new builder with an empty list of messages.- Parameters:
messages
- the current list of messagesisMessagesListMutable
- Whether the messages list is mutableparent
- a listener to notify of changesisClean
- whether the builder is initially marked clean
-
-
Method Detail
-
dispose
public void dispose()
-
ensureMutableMessageList
private void ensureMutableMessageList()
Ensures that the list of messages is mutable so it can be updated. If it's immutable, a copy is made.
-
ensureBuilders
private void ensureBuilders()
Ensures that the list of builders is not null. If it's null, the list is created and initialized to be the same size as the messages list with null entries.
-
getCount
public int getCount()
Gets the count of items in the list.- Returns:
- the count of items in the list.
-
isEmpty
public boolean isEmpty()
Gets whether the list is empty.- Returns:
- whether the list is empty
-
getMessage
public MType getMessage(int index)
Get the message at the specified index. If the message is currently stored as aBuilder
, it is converted to aMessage
by callingMessage.Builder.buildPartial()
on it.- Parameters:
index
- the index of the message to get- Returns:
- the message for the specified index
-
getMessage
private MType getMessage(int index, boolean forBuild)
Get the message at the specified index. If the message is currently stored as aBuilder
, it is converted to aMessage
by callingMessage.Builder.buildPartial()
on it.- Parameters:
index
- the index of the message to getforBuild
- this is being called for build so we want to make sure we SingleFieldBuilderV3.build to send dirty invalidations- Returns:
- the message for the specified index
-
getBuilder
public BType getBuilder(int index)
Gets a builder for the specified index. If no builder has been created for that index, a builder is created on demand by callingMessage.toBuilder()
.- Parameters:
index
- the index of the message to get- Returns:
- The builder for that index
-
getMessageOrBuilder
public IType getMessageOrBuilder(int index)
Gets the base class interface for the specified index. This may either be a builder or a message. It will return whatever is more efficient.- Parameters:
index
- the index of the message to get- Returns:
- the message or builder for the index as the base class interface
-
setMessage
public RepeatedFieldBuilderV3<MType,BType,IType> setMessage(int index, MType message)
Sets a message at the specified index replacing the existing item at that index.- Parameters:
index
- the index to set.message
- the message to set- Returns:
- the builder
-
addMessage
public RepeatedFieldBuilderV3<MType,BType,IType> addMessage(MType message)
Appends the specified element to the end of this list.- Parameters:
message
- the message to add- Returns:
- the builder
-
addMessage
public RepeatedFieldBuilderV3<MType,BType,IType> addMessage(int index, MType message)
Inserts the specified message at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).- Parameters:
index
- the index at which to insert the messagemessage
- the message to add- Returns:
- the builder
-
addAllMessages
public RepeatedFieldBuilderV3<MType,BType,IType> addAllMessages(java.lang.Iterable<? extends MType> values)
Appends all of the messages in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.- Parameters:
values
- the messages to add- Returns:
- the builder
-
addBuilder
public BType addBuilder(MType message)
Appends a new builder to the end of this list and returns the builder.- Parameters:
message
- the message to add which is the basis of the builder- Returns:
- the new builder
-
addBuilder
public BType addBuilder(int index, MType message)
Inserts a new builder at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).- Parameters:
index
- the index at which to insert the buildermessage
- the message to add which is the basis of the builder- Returns:
- the builder
-
remove
public void remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).- Parameters:
index
- the index at which to remove the message
-
clear
public void clear()
Removes all of the elements from this list. The list will be empty after this call returns.
-
build
public java.util.List<MType> build()
Builds the list of messages from the builder and returns them.- Returns:
- an immutable list of messages
-
getMessageList
public java.util.List<MType> getMessageList()
Gets a view of the builder as a list of messages. The returned list is live and will reflect any changes to the underlying builder.- Returns:
- the messages in the list
-
getBuilderList
public java.util.List<BType> getBuilderList()
Gets a view of the builder as a list of builders. This returned list is live and will reflect any changes to the underlying builder.- Returns:
- the builders in the list
-
getMessageOrBuilderList
public java.util.List<IType> getMessageOrBuilderList()
Gets a view of the builder as a list of MessageOrBuilders. This returned list is live and will reflect any changes to the underlying builder.- Returns:
- the builders in the list
-
onChanged
private void onChanged()
Called when a the builder or one of its nested children has changed and any parent should be notified of its invalidation.
-
markDirty
public void markDirty()
Description copied from interface:AbstractMessage.BuilderParent
A builder becomes dirty whenever a field is modified -- including fields in nested builders -- and becomes clean when build() is called. Thus, when a builder becomes dirty, all its parents become dirty as well, and when it becomes clean, all its children become clean. The dirtiness state is used to invalidate certain cached values.To this end, a builder calls markDirty() on its parent whenever it transitions from clean to dirty. The parent must propagate this call to its own parent, unless it was already dirty, in which case the grandparent must necessarily already be dirty as well. The parent can only transition back to "clean" after calling build() on all children.
- Specified by:
markDirty
in interfaceAbstractMessage.BuilderParent
-
incrementModCounts
private void incrementModCounts()
Increments the mod counts so that an ConcurrentModificationException can be thrown if calling code tries to modify the builder while its iterating the list.
-
-