Commit cf14183b authored by Jisi Liu's avatar Jisi Liu

Down integrate from Google internal.

parent f00300d7
......@@ -50,17 +50,23 @@ import java.util.Map;
*
* @author kenton@google.com Kenton Varda
*/
public abstract class AbstractMessage extends AbstractMessageLite
implements Message {
public abstract class AbstractMessage
// TODO(dweis): Update GeneratedMessage to parameterize with MessageType and BuilderType.
extends AbstractMessageLite
implements Message {
@Override
public boolean isInitialized() {
return MessageReflection.isInitialized(this);
}
@Override
public List<String> findInitializationErrors() {
return MessageReflection.findMissingFields(this);
}
@Override
public String getInitializationErrorString() {
return MessageReflection.delimitWithCommas(findInitializationErrors());
}
......@@ -83,12 +89,14 @@ public abstract class AbstractMessage extends AbstractMessageLite
return TextFormat.printToString(this);
}
@Override
public void writeTo(final CodedOutputStream output) throws IOException {
MessageReflection.writeMessageTo(this, getAllFields(), output, false);
}
protected int memoizedSize = -1;
@Override
public int getSerializedSize() {
int size = memoizedSize;
if (size != -1) {
......@@ -288,8 +296,8 @@ public abstract class AbstractMessage extends AbstractMessageLite
* other methods.
*/
@SuppressWarnings("unchecked")
public static abstract class Builder<BuilderType extends Builder>
extends AbstractMessageLite.Builder<BuilderType>
public static abstract class Builder<BuilderType extends Builder<BuilderType>>
extends AbstractMessageLite.Builder
implements Message.Builder {
// The compiler produces an error if this is not declared explicitly.
@Override
......@@ -314,6 +322,7 @@ public abstract class AbstractMessage extends AbstractMessageLite
throw new UnsupportedOperationException("clearOneof() is not implemented.");
}
@Override
public BuilderType clear() {
for (final Map.Entry<FieldDescriptor, Object> entry :
getAllFields().entrySet()) {
......@@ -322,14 +331,22 @@ public abstract class AbstractMessage extends AbstractMessageLite
return (BuilderType) this;
}
@Override
public List<String> findInitializationErrors() {
return MessageReflection.findMissingFields(this);
}
@Override
public String getInitializationErrorString() {
return MessageReflection.delimitWithCommas(findInitializationErrors());
}
@Override
protected BuilderType internalMergeFrom(AbstractMessageLite other) {
return mergeFrom((Message) other);
}
@Override
public BuilderType mergeFrom(final Message other) {
if (other.getDescriptorForType() != getDescriptorForType()) {
throw new IllegalArgumentException(
......@@ -407,6 +424,7 @@ public abstract class AbstractMessage extends AbstractMessageLite
return (BuilderType) this;
}
@Override
public BuilderType mergeUnknownFields(final UnknownFieldSet unknownFields) {
setUnknownFields(
UnknownFieldSet.newBuilder(getUnknownFields())
......@@ -415,17 +433,19 @@ public abstract class AbstractMessage extends AbstractMessageLite
return (BuilderType) this;
}
@Override
public Message.Builder getFieldBuilder(final FieldDescriptor field) {
throw new UnsupportedOperationException(
"getFieldBuilder() called on an unsupported message type.");
}
public Message.Builder getRepeatedFieldBuilder(final FieldDescriptor field,
int index) {
@Override
public Message.Builder getRepeatedFieldBuilder(final FieldDescriptor field, int index) {
throw new UnsupportedOperationException(
"getRepeatedFieldBuilder() called on an unsupported message type.");
}
@Override
public String toString() {
return TextFormat.printToString(this);
}
......@@ -462,7 +482,7 @@ public abstract class AbstractMessage extends AbstractMessageLite
@Override
public BuilderType mergeFrom(final ByteString data)
throws InvalidProtocolBufferException {
return super.mergeFrom(data);
return (BuilderType) super.mergeFrom(data);
}
@Override
......@@ -470,20 +490,20 @@ public abstract class AbstractMessage extends AbstractMessageLite
final ByteString data,
final ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return super.mergeFrom(data, extensionRegistry);
return (BuilderType) super.mergeFrom(data, extensionRegistry);
}
@Override
public BuilderType mergeFrom(final byte[] data)
throws InvalidProtocolBufferException {
return super.mergeFrom(data);
return (BuilderType) super.mergeFrom(data);
}
@Override
public BuilderType mergeFrom(
final byte[] data, final int off, final int len)
throws InvalidProtocolBufferException {
return super.mergeFrom(data, off, len);
return (BuilderType) super.mergeFrom(data, off, len);
}
@Override
......@@ -491,7 +511,7 @@ public abstract class AbstractMessage extends AbstractMessageLite
final byte[] data,
final ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return super.mergeFrom(data, extensionRegistry);
return (BuilderType) super.mergeFrom(data, extensionRegistry);
}
@Override
......@@ -499,13 +519,13 @@ public abstract class AbstractMessage extends AbstractMessageLite
final byte[] data, final int off, final int len,
final ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return super.mergeFrom(data, off, len, extensionRegistry);
return (BuilderType) super.mergeFrom(data, off, len, extensionRegistry);
}
@Override
public BuilderType mergeFrom(final InputStream input)
throws IOException {
return super.mergeFrom(input);
return (BuilderType) super.mergeFrom(input);
}
@Override
......@@ -513,7 +533,7 @@ public abstract class AbstractMessage extends AbstractMessageLite
final InputStream input,
final ExtensionRegistryLite extensionRegistry)
throws IOException {
return super.mergeFrom(input, extensionRegistry);
return (BuilderType) super.mergeFrom(input, extensionRegistry);
}
@Override
......
......@@ -43,9 +43,13 @@ import java.util.Collection;
*
* @author kenton@google.com Kenton Varda
*/
public abstract class AbstractMessageLite implements MessageLite {
public abstract class AbstractMessageLite<
MessageType extends AbstractMessageLite<MessageType, BuilderType>,
BuilderType extends AbstractMessageLite.Builder<MessageType, BuilderType>>
implements MessageLite {
protected int memoizedHashCode = 0;
@Override
public ByteString toByteString() {
try {
final ByteString.CodedBuilder out =
......@@ -59,6 +63,7 @@ public abstract class AbstractMessageLite implements MessageLite {
}
}
@Override
public byte[] toByteArray() {
try {
final byte[] result = new byte[getSerializedSize()];
......@@ -73,6 +78,7 @@ public abstract class AbstractMessageLite implements MessageLite {
}
}
@Override
public void writeTo(final OutputStream output) throws IOException {
final int bufferSize =
CodedOutputStream.computePreferredBufferSize(getSerializedSize());
......@@ -82,6 +88,7 @@ public abstract class AbstractMessageLite implements MessageLite {
codedOutput.flush();
}
@Override
public void writeDelimitedTo(final OutputStream output) throws IOException {
final int serialized = getSerializedSize();
final int bufferSize = CodedOutputStream.computePreferredBufferSize(
......@@ -120,25 +127,27 @@ public abstract class AbstractMessageLite implements MessageLite {
* other methods.
*/
@SuppressWarnings("unchecked")
public static abstract class Builder<BuilderType extends Builder>
public abstract static class Builder<
MessageType extends AbstractMessageLite<MessageType, BuilderType>,
BuilderType extends Builder<MessageType, BuilderType>>
implements MessageLite.Builder {
// The compiler produces an error if this is not declared explicitly.
@Override
public abstract BuilderType clone();
public BuilderType mergeFrom(final CodedInputStream input)
throws IOException {
@Override
public BuilderType mergeFrom(final CodedInputStream input) throws IOException {
return mergeFrom(input, ExtensionRegistryLite.getEmptyRegistry());
}
// Re-defined here for return type covariance.
@Override
public abstract BuilderType mergeFrom(
final CodedInputStream input,
final ExtensionRegistryLite extensionRegistry)
final CodedInputStream input, final ExtensionRegistryLite extensionRegistry)
throws IOException;
public BuilderType mergeFrom(final ByteString data)
throws InvalidProtocolBufferException {
@Override
public BuilderType mergeFrom(final ByteString data) throws InvalidProtocolBufferException {
try {
final CodedInputStream input = data.newCodedInput();
mergeFrom(input);
......@@ -153,9 +162,9 @@ public abstract class AbstractMessageLite implements MessageLite {
}
}
@Override
public BuilderType mergeFrom(
final ByteString data,
final ExtensionRegistryLite extensionRegistry)
final ByteString data, final ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
try {
final CodedInputStream input = data.newCodedInput();
......@@ -171,14 +180,14 @@ public abstract class AbstractMessageLite implements MessageLite {
}
}
public BuilderType mergeFrom(final byte[] data)
throws InvalidProtocolBufferException {
@Override
public BuilderType mergeFrom(final byte[] data) throws InvalidProtocolBufferException {
return mergeFrom(data, 0, data.length);
}
public BuilderType mergeFrom(final byte[] data, final int off,
final int len)
throws InvalidProtocolBufferException {
@Override
public BuilderType mergeFrom(final byte[] data, final int off, final int len)
throws InvalidProtocolBufferException {
try {
final CodedInputStream input =
CodedInputStream.newInstance(data, off, len);
......@@ -194,15 +203,17 @@ public abstract class AbstractMessageLite implements MessageLite {
}
}
public BuilderType mergeFrom(
final byte[] data,
final ExtensionRegistryLite extensionRegistry)
@Override
public BuilderType mergeFrom(final byte[] data, final ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return mergeFrom(data, 0, data.length, extensionRegistry);
}
@Override
public BuilderType mergeFrom(
final byte[] data, final int off, final int len,
final byte[] data,
final int off,
final int len,
final ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
try {
......@@ -220,6 +231,7 @@ public abstract class AbstractMessageLite implements MessageLite {
}
}
@Override
public BuilderType mergeFrom(final InputStream input) throws IOException {
final CodedInputStream codedInput = CodedInputStream.newInstance(input);
mergeFrom(codedInput);
......@@ -227,10 +239,9 @@ public abstract class AbstractMessageLite implements MessageLite {
return (BuilderType) this;
}
@Override
public BuilderType mergeFrom(
final InputStream input,
final ExtensionRegistryLite extensionRegistry)
throws IOException {
final InputStream input, final ExtensionRegistryLite extensionRegistry) throws IOException {
final CodedInputStream codedInput = CodedInputStream.newInstance(input);
mergeFrom(codedInput, extensionRegistry);
codedInput.checkLastTagWas(0);
......@@ -292,10 +303,9 @@ public abstract class AbstractMessageLite implements MessageLite {
}
}
@Override
public boolean mergeDelimitedFrom(
final InputStream input,
final ExtensionRegistryLite extensionRegistry)
throws IOException {
final InputStream input, final ExtensionRegistryLite extensionRegistry) throws IOException {
final int firstByte = input.read();
if (firstByte == -1) {
return false;
......@@ -306,11 +316,24 @@ public abstract class AbstractMessageLite implements MessageLite {
return true;
}
public boolean mergeDelimitedFrom(final InputStream input)
throws IOException {
@Override
public boolean mergeDelimitedFrom(final InputStream input) throws IOException {
return mergeDelimitedFrom(input,
ExtensionRegistryLite.getEmptyRegistry());
}
@Override
@SuppressWarnings("unchecked") // isInstance takes care of this
public BuilderType mergeFrom(final MessageLite other) {
if (!getDefaultInstanceForType().getClass().isInstance(other)) {
throw new IllegalArgumentException(
"mergeFrom(MessageLite) can only merge messages of the same type.");
}
return internalMergeFrom((MessageType) other);
}
protected abstract BuilderType internalMergeFrom(MessageType message);
/**
* Construct an UninitializedMessageException reporting missing fields in
......
......@@ -78,26 +78,27 @@ public abstract class AbstractParser<MessageType extends MessageLite>
private static final ExtensionRegistryLite EMPTY_REGISTRY
= ExtensionRegistryLite.getEmptyRegistry();
@Override
public MessageType parsePartialFrom(CodedInputStream input)
throws InvalidProtocolBufferException {
return parsePartialFrom(input, EMPTY_REGISTRY);
}
public MessageType parseFrom(CodedInputStream input,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parseFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return checkMessageInitialized(
parsePartialFrom(input, extensionRegistry));
}
public MessageType parseFrom(CodedInputStream input)
throws InvalidProtocolBufferException {
@Override
public MessageType parseFrom(CodedInputStream input) throws InvalidProtocolBufferException {
return parseFrom(input, EMPTY_REGISTRY);
}
public MessageType parsePartialFrom(ByteString data,
ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
@Override
public MessageType parsePartialFrom(ByteString data, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
MessageType message;
try {
CodedInputStream input = data.newCodedInput();
......@@ -113,24 +114,25 @@ public abstract class AbstractParser<MessageType extends MessageLite>
}
}
public MessageType parsePartialFrom(ByteString data)
throws InvalidProtocolBufferException {
@Override
public MessageType parsePartialFrom(ByteString data) throws InvalidProtocolBufferException {
return parsePartialFrom(data, EMPTY_REGISTRY);
}
public MessageType parseFrom(ByteString data,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parseFrom(ByteString data, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return checkMessageInitialized(parsePartialFrom(data, extensionRegistry));
}
public MessageType parseFrom(ByteString data)
throws InvalidProtocolBufferException {
@Override
public MessageType parseFrom(ByteString data) throws InvalidProtocolBufferException {
return parseFrom(data, EMPTY_REGISTRY);
}
public MessageType parsePartialFrom(byte[] data, int off, int len,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parsePartialFrom(
byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
try {
CodedInputStream input = CodedInputStream.newInstance(data, off, len);
......@@ -146,47 +148,50 @@ public abstract class AbstractParser<MessageType extends MessageLite>
}
}
@Override
public MessageType parsePartialFrom(byte[] data, int off, int len)
throws InvalidProtocolBufferException {
return parsePartialFrom(data, off, len, EMPTY_REGISTRY);
}
public MessageType parsePartialFrom(byte[] data,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parsePartialFrom(byte[] data, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return parsePartialFrom(data, 0, data.length, extensionRegistry);
}
public MessageType parsePartialFrom(byte[] data)
throws InvalidProtocolBufferException {
@Override
public MessageType parsePartialFrom(byte[] data) throws InvalidProtocolBufferException {
return parsePartialFrom(data, 0, data.length, EMPTY_REGISTRY);
}
public MessageType parseFrom(byte[] data, int off, int len,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parseFrom(
byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return checkMessageInitialized(
parsePartialFrom(data, off, len, extensionRegistry));
}
@Override
public MessageType parseFrom(byte[] data, int off, int len)
throws InvalidProtocolBufferException {
return parseFrom(data, off, len, EMPTY_REGISTRY);
}
public MessageType parseFrom(byte[] data,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parseFrom(byte[] data, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return parseFrom(data, 0, data.length, extensionRegistry);
}
public MessageType parseFrom(byte[] data)
throws InvalidProtocolBufferException {
@Override
public MessageType parseFrom(byte[] data) throws InvalidProtocolBufferException {
return parseFrom(data, EMPTY_REGISTRY);
}
public MessageType parsePartialFrom(InputStream input,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parsePartialFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
CodedInputStream codedInput = CodedInputStream.newInstance(input);
MessageType message = parsePartialFrom(codedInput, extensionRegistry);
......@@ -198,26 +203,26 @@ public abstract class AbstractParser<MessageType extends MessageLite>
return message;
}
public MessageType parsePartialFrom(InputStream input)
throws InvalidProtocolBufferException {
@Override
public MessageType parsePartialFrom(InputStream input) throws InvalidProtocolBufferException {
return parsePartialFrom(input, EMPTY_REGISTRY);
}
public MessageType parseFrom(InputStream input,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parseFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return checkMessageInitialized(
parsePartialFrom(input, extensionRegistry));
}
public MessageType parseFrom(InputStream input)
throws InvalidProtocolBufferException {
@Override
public MessageType parseFrom(InputStream input) throws InvalidProtocolBufferException {
return parseFrom(input, EMPTY_REGISTRY);
}
@Override
public MessageType parsePartialDelimitedFrom(
InputStream input,
ExtensionRegistryLite extensionRegistry)
InputStream input, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
int size;
try {
......@@ -233,21 +238,21 @@ public abstract class AbstractParser<MessageType extends MessageLite>
return parsePartialFrom(limitedInput, extensionRegistry);
}
@Override
public MessageType parsePartialDelimitedFrom(InputStream input)
throws InvalidProtocolBufferException {
return parsePartialDelimitedFrom(input, EMPTY_REGISTRY);
}
public MessageType parseDelimitedFrom(
InputStream input,
ExtensionRegistryLite extensionRegistry)
@Override
public MessageType parseDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
return checkMessageInitialized(
parsePartialDelimitedFrom(input, extensionRegistry));
}
public MessageType parseDelimitedFrom(InputStream input)
throws InvalidProtocolBufferException {
@Override
public MessageType parseDelimitedFrom(InputStream input) throws InvalidProtocolBufferException {
return parseDelimitedFrom(input, EMPTY_REGISTRY);
}
}
......@@ -34,19 +34,25 @@ import com.google.protobuf.Internal.ProtobufList;
import java.util.AbstractList;
import java.util.Collection;
import java.util.List;
import java.util.RandomAccess;
/**
* An abstract implementation of {@link ProtobufList} which manages mutability semantics. All mutate
* methods are check if the list is mutable before proceeding. Subclasses must invoke
* methods must check if the list is mutable before proceeding. Subclasses must invoke
* {@link #ensureIsMutable()} manually when overriding those methods.
* <p>
* This implementation assumes all subclasses are array based, supporting random access.
*/
abstract class AbstractProtobufList<E> extends AbstractList<E> implements ProtobufList<E> {
protected static final int DEFAULT_CAPACITY = 10;
/**
* Whether or not this list is modifiable.
*/
private boolean isMutable;
/**
* Constructs a mutable list by default.
*/
......@@ -54,6 +60,44 @@ abstract class AbstractProtobufList<E> extends AbstractList<E> implements Protob
isMutable = true;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof List)) {
return false;
}
// Handle lists that do not support RandomAccess as efficiently as possible by using an iterator
// based approach in our super class. Otherwise our index based approach will avoid those
// allocations.
if (!(o instanceof RandomAccess)) {
return super.equals(o);
}
List<?> other = (List<?>) o;
final int size = size();
if (size != other.size()) {
return false;
}
for (int i = 0; i < size; i++) {
if (!get(i).equals(other.get(i))) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
final int size = size();
int hashCode = 1;
for (int i = 0; i < size; i++) {
hashCode = (31 * hashCode) + get(i).hashCode();
}
return hashCode;
}
@Override
public boolean add(E e) {
ensureIsMutable();
......
......@@ -34,7 +34,6 @@ import com.google.protobuf.Internal.BooleanList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.RandomAccess;
/**
......@@ -45,8 +44,6 @@ import java.util.RandomAccess;
final class BooleanArrayList
extends AbstractProtobufList<Boolean> implements BooleanList, RandomAccess {
private static final int DEFAULT_CAPACITY = 10;
private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList();
static {
EMPTY_LIST.makeImmutable();
......@@ -60,7 +57,7 @@ final class BooleanArrayList
* The backing store for the list.
*/
private boolean[] array;
/**
* The size of the list distinct from the length of the array. That is, it is the number of
* elements set in the list.
......@@ -71,35 +68,57 @@ final class BooleanArrayList
* Constructs a new mutable {@code BooleanArrayList} with default capacity.
*/
BooleanArrayList() {
this(DEFAULT_CAPACITY);
this(new boolean[DEFAULT_CAPACITY], 0);
}
/**
* Constructs a new mutable {@code BooleanArrayList} with the provided capacity.
* Constructs a new mutable {@code BooleanArrayList}.
*/
BooleanArrayList(int capacity) {
array = new boolean[capacity];
size = 0;
private BooleanArrayList(boolean[] array, int size) {
this.array = array;
this.size = size;
}
/**
* Constructs a new mutable {@code BooleanArrayList} containing the same elements as
* {@code other}.
*/
BooleanArrayList(List<Boolean> other) {
if (other instanceof BooleanArrayList) {
BooleanArrayList list = (BooleanArrayList) other;
array = list.array.clone();
size = list.size;
} else {
size = other.size();
array = new boolean[size];
for (int i = 0; i < size; i++) {
array[i] = other.get(i);
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof BooleanArrayList)) {
return super.equals(o);
}
BooleanArrayList other = (BooleanArrayList) o;
if (size != other.size) {
return false;
}
final boolean[] arr = other.array;
for (int i = 0; i < size; i++) {
if (array[i] != arr[i]) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 1;
for (int i = 0; i < size; i++) {
result = (31 * result) + Internal.hashBoolean(array[i]);
}
return result;
}
@Override
public BooleanList mutableCopyWithCapacity(int capacity) {
if (capacity < size) {
throw new IllegalArgumentException();
}
return new BooleanArrayList(Arrays.copyOf(array, capacity), size);
}
@Override
public Boolean get(int index) {
return getBoolean(index);
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -34,7 +34,6 @@ import com.google.protobuf.Internal.DoubleList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.RandomAccess;
/**
......@@ -45,8 +44,6 @@ import java.util.RandomAccess;
final class DoubleArrayList
extends AbstractProtobufList<Double> implements DoubleList, RandomAccess {
private static final int DEFAULT_CAPACITY = 10;
private static final DoubleArrayList EMPTY_LIST = new DoubleArrayList();
static {
EMPTY_LIST.makeImmutable();
......@@ -71,32 +68,56 @@ final class DoubleArrayList
* Constructs a new mutable {@code DoubleArrayList} with default capacity.
*/
DoubleArrayList() {
this(DEFAULT_CAPACITY);
}
/**
* Constructs a new mutable {@code DoubleArrayList} with the provided capacity.
*/
DoubleArrayList(int capacity) {
array = new double[capacity];
size = 0;
this(new double[DEFAULT_CAPACITY], 0);
}
/**
* Constructs a new mutable {@code DoubleArrayList} containing the same elements as {@code other}.
*/
DoubleArrayList(List<Double> other) {
if (other instanceof DoubleArrayList) {
DoubleArrayList list = (DoubleArrayList) other;
array = list.array.clone();
size = list.size;
} else {
size = other.size();
array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = other.get(i);
private DoubleArrayList(double[] array, int size) {
this.array = array;
this.size = size;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DoubleArrayList)) {
return super.equals(o);
}
DoubleArrayList other = (DoubleArrayList) o;
if (size != other.size) {
return false;
}
final double[] arr = other.array;
for (int i = 0; i < size; i++) {
if (array[i] != arr[i]) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 1;
for (int i = 0; i < size; i++) {
long bits = Double.doubleToLongBits(array[i]);
result = (31 * result) + Internal.hashLong(bits);
}
return result;
}
@Override
public DoubleList mutableCopyWithCapacity(int capacity) {
if (capacity < size) {
throw new IllegalArgumentException();
}
return new DoubleArrayList(Arrays.copyOf(array, capacity), size);
}
@Override
......
......@@ -156,18 +156,22 @@ public final class DynamicMessage extends AbstractMessage {
// -----------------------------------------------------------------
// Implementation of Message interface.
@Override
public Descriptor getDescriptorForType() {
return type;
}
@Override
public DynamicMessage getDefaultInstanceForType() {
return getDefaultInstance(type);
}
@Override
public Map<FieldDescriptor, Object> getAllFields() {
return fields.getAllFields();
}
@Override
public boolean hasOneof(OneofDescriptor oneof) {
verifyOneofContainingType(oneof);
FieldDescriptor field = oneofCases[oneof.getIndex()];
......@@ -177,16 +181,19 @@ public final class DynamicMessage extends AbstractMessage {
return true;
}
@Override
public FieldDescriptor getOneofFieldDescriptor(OneofDescriptor oneof) {
verifyOneofContainingType(oneof);
return oneofCases[oneof.getIndex()];
}
@Override
public boolean hasField(FieldDescriptor field) {
verifyContainingType(field);
return fields.hasField(field);
}
@Override
public Object getField(FieldDescriptor field) {
verifyContainingType(field);
Object result = fields.getField(field);
......@@ -202,16 +209,19 @@ public final class DynamicMessage extends AbstractMessage {
return result;
}
@Override
public int getRepeatedFieldCount(FieldDescriptor field) {
verifyContainingType(field);
return fields.getRepeatedFieldCount(field);
}
@Override
public Object getRepeatedField(FieldDescriptor field, int index) {
verifyContainingType(field);
return fields.getRepeatedField(field, index);
}
@Override
public UnknownFieldSet getUnknownFields() {
return unknownFields;
}
......@@ -264,19 +274,22 @@ public final class DynamicMessage extends AbstractMessage {
return size;
}
@Override
public Builder newBuilderForType() {
return new Builder(type);
}
@Override
public Builder toBuilder() {
return newBuilderForType().mergeFrom(this);
}
@Override
public Parser<DynamicMessage> getParserForType() {
return new AbstractParser<DynamicMessage>() {
@Override
public DynamicMessage parsePartialFrom(
CodedInputStream input,
ExtensionRegistryLite extensionRegistry)
CodedInputStream input, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException {
Builder builder = newBuilder(type);
try {
......@@ -370,6 +383,7 @@ public final class DynamicMessage extends AbstractMessage {
}
}
@Override
public DynamicMessage build() {
if (!isInitialized()) {
throw newUninitializedMessageException(
......@@ -394,6 +408,7 @@ public final class DynamicMessage extends AbstractMessage {
return buildPartial();
}
@Override
public DynamicMessage buildPartial() {
fields.makeImmutable();
DynamicMessage result =
......@@ -411,22 +426,27 @@ public final class DynamicMessage extends AbstractMessage {
return result;
}
@Override
public boolean isInitialized() {
return DynamicMessage.isInitialized(type, fields);
}
@Override
public Descriptor getDescriptorForType() {
return type;
}
@Override
public DynamicMessage getDefaultInstanceForType() {
return getDefaultInstance(type);
}
@Override
public Map<FieldDescriptor, Object> getAllFields() {
return fields.getAllFields();
}
@Override
public Builder newBuilderForField(FieldDescriptor field) {
verifyContainingType(field);
......@@ -438,6 +458,7 @@ public final class DynamicMessage extends AbstractMessage {
return new Builder(field.getMessageType());
}
@Override
public boolean hasOneof(OneofDescriptor oneof) {
verifyOneofContainingType(oneof);
FieldDescriptor field = oneofCases[oneof.getIndex()];
......@@ -447,11 +468,13 @@ public final class DynamicMessage extends AbstractMessage {
return true;
}
@Override
public FieldDescriptor getOneofFieldDescriptor(OneofDescriptor oneof) {
verifyOneofContainingType(oneof);
return oneofCases[oneof.getIndex()];
}
@Override
public Builder clearOneof(OneofDescriptor oneof) {
verifyOneofContainingType(oneof);
FieldDescriptor field = oneofCases[oneof.getIndex()];
......@@ -461,11 +484,13 @@ public final class DynamicMessage extends AbstractMessage {
return this;
}
@Override
public boolean hasField(FieldDescriptor field) {
verifyContainingType(field);
return fields.hasField(field);
}
@Override
public Object getField(FieldDescriptor field) {
verifyContainingType(field);
Object result = fields.getField(field);
......@@ -481,6 +506,7 @@ public final class DynamicMessage extends AbstractMessage {
return result;
}
@Override
public Builder setField(FieldDescriptor field, Object value) {
verifyContainingType(field);
ensureIsMutable();
......@@ -505,6 +531,7 @@ public final class DynamicMessage extends AbstractMessage {
return this;
}
@Override
public Builder clearField(FieldDescriptor field) {
verifyContainingType(field);
ensureIsMutable();
......@@ -519,24 +546,27 @@ public final class DynamicMessage extends AbstractMessage {
return this;
}
@Override
public int getRepeatedFieldCount(FieldDescriptor field) {
verifyContainingType(field);
return fields.getRepeatedFieldCount(field);
}
@Override
public Object getRepeatedField(FieldDescriptor field, int index) {
verifyContainingType(field);
return fields.getRepeatedField(field, index);
}
public Builder setRepeatedField(FieldDescriptor field,
int index, Object value) {
@Override
public Builder setRepeatedField(FieldDescriptor field, int index, Object value) {
verifyContainingType(field);
ensureIsMutable();
fields.setRepeatedField(field, index, value);
return this;
}
@Override
public Builder addRepeatedField(FieldDescriptor field, Object value) {
verifyContainingType(field);
ensureIsMutable();
......@@ -544,10 +574,12 @@ public final class DynamicMessage extends AbstractMessage {
return this;
}
@Override
public UnknownFieldSet getUnknownFields() {
return unknownFields;
}
@Override
public Builder setUnknownFields(UnknownFieldSet unknownFields) {
if (getDescriptorForType().getFile().getSyntax()
== Descriptors.FileDescriptor.Syntax.PROTO3) {
......
......@@ -42,6 +42,7 @@ public abstract class Extension<ContainingType extends MessageLite, Type>
public abstract Descriptors.FieldDescriptor getDescriptor();
/** Returns whether or not this extension is a Lite Extension. */
@Override
final boolean isLite() {
return false;
}
......
......@@ -120,6 +120,25 @@ final class FieldSet<FieldDescriptorType extends
public boolean isImmutable() {
return isImmutable;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof FieldSet)) {
return false;
}
FieldSet<?> other = (FieldSet<?>) o;
return other.fields.equals(other.fields);
}
@Override
public int hashCode() {
return fields.hashCode();
}
/**
* Clones the FieldSet. The returned FieldSet will be mutable even if the
......
......@@ -34,7 +34,6 @@ import com.google.protobuf.Internal.FloatList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.RandomAccess;
/**
......@@ -44,8 +43,6 @@ import java.util.RandomAccess;
*/
final class FloatArrayList extends AbstractProtobufList<Float> implements FloatList, RandomAccess {
private static final int DEFAULT_CAPACITY = 10;
private static final FloatArrayList EMPTY_LIST = new FloatArrayList();
static {
EMPTY_LIST.makeImmutable();
......@@ -70,32 +67,55 @@ final class FloatArrayList extends AbstractProtobufList<Float> implements FloatL
* Constructs a new mutable {@code FloatArrayList} with default capacity.
*/
FloatArrayList() {
this(DEFAULT_CAPACITY);
}
/**
* Constructs a new mutable {@code FloatArrayList} with the provided capacity.
*/
FloatArrayList(int capacity) {
array = new float[capacity];
size = 0;
this(new float[DEFAULT_CAPACITY], 0);
}
/**
* Constructs a new mutable {@code FloatArrayList} containing the same elements as {@code other}.
*/
FloatArrayList(List<Float> other) {
if (other instanceof FloatArrayList) {
FloatArrayList list = (FloatArrayList) other;
array = list.array.clone();
size = list.size;
} else {
size = other.size();
array = new float[size];
for (int i = 0; i < size; i++) {
array[i] = other.get(i);
private FloatArrayList(float[] array, int size) {
this.array = array;
this.size = size;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof FloatArrayList)) {
return super.equals(o);
}
FloatArrayList other = (FloatArrayList) o;
if (size != other.size) {
return false;
}
final float[] arr = other.array;
for (int i = 0; i < size; i++) {
if (array[i] != arr[i]) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 1;
for (int i = 0; i < size; i++) {
result = (31 * result) + Float.floatToIntBits(array[i]);
}
return result;
}
@Override
public FloatList mutableCopyWithCapacity(int capacity) {
if (capacity < size) {
throw new IllegalArgumentException();
}
return new FloatArrayList(Arrays.copyOf(array, capacity), size);
}
@Override
......
......@@ -34,7 +34,6 @@ import com.google.protobuf.Internal.IntList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.RandomAccess;
/**
......@@ -44,8 +43,6 @@ import java.util.RandomAccess;
*/
final class IntArrayList extends AbstractProtobufList<Integer> implements IntList, RandomAccess {
private static final int DEFAULT_CAPACITY = 10;
private static final IntArrayList EMPTY_LIST = new IntArrayList();
static {
EMPTY_LIST.makeImmutable();
......@@ -70,32 +67,55 @@ final class IntArrayList extends AbstractProtobufList<Integer> implements IntLis
* Constructs a new mutable {@code IntArrayList} with default capacity.
*/
IntArrayList() {
this(DEFAULT_CAPACITY);
}
/**
* Constructs a new mutable {@code IntArrayList} with the provided capacity.
*/
IntArrayList(int capacity) {
array = new int[capacity];
size = 0;
this(new int[DEFAULT_CAPACITY], 0);
}
/**
* Constructs a new mutable {@code IntArrayList} containing the same elements as {@code other}.
*/
IntArrayList(List<Integer> other) {
if (other instanceof IntArrayList) {
IntArrayList list = (IntArrayList) other;
array = list.array.clone();
size = list.size;
} else {
size = other.size();
array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = other.get(i);
private IntArrayList(int[] array, int size) {
this.array = array;
this.size = size;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof IntArrayList)) {
return super.equals(o);
}
IntArrayList other = (IntArrayList) o;
if (size != other.size) {
return false;
}
final int[] arr = other.array;
for (int i = 0; i < size; i++) {
if (array[i] != arr[i]) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 1;
for (int i = 0; i < size; i++) {
result = (31 * result) + array[i];
}
return result;
}
@Override
public IntList mutableCopyWithCapacity(int capacity) {
if (capacity < size) {
throw new IllegalArgumentException();
}
return new IntArrayList(Arrays.copyOf(array, capacity), size);
}
@Override
......
......@@ -41,6 +41,7 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.RandomAccess;
import java.util.Set;
/**
......@@ -457,10 +458,13 @@ public final class Internal {
public static <T extends EnumLite> Converter<Integer, T> newEnumConverter(
final EnumLiteMap<T> enumMap, final T unrecognizedValue) {
return new Converter<Integer, T>() {
@Override
public T doForward(Integer value) {
T result = enumMap.findValueByNumber(value);
return result == null ? unrecognizedValue : result;
}
@Override
public Integer doBackward(T value) {
return value.getNumber();
}
......@@ -573,8 +577,10 @@ public final class Internal {
/**
* Extends {@link List} to add the capability to make the list immutable and inspect if it is
* modifiable.
* <p>
* All implementations must support efficient random access.
*/
public static interface ProtobufList<E> extends List<E> {
public static interface ProtobufList<E> extends List<E>, RandomAccess {
/**
* Makes this list immutable. All subsequent modifications will throw an
......@@ -586,6 +592,11 @@ public final class Internal {
* Returns whether this list can be modified via the publicly accessible {@link List} methods.
*/
boolean isModifiable();
/**
* Returns a mutable clone of this list with the specified capacity.
*/
ProtobufList<E> mutableCopyWithCapacity(int capacity);
}
/**
......@@ -600,14 +611,20 @@ public final class Internal {
int getInt(int index);
/**
* Like {@link #add(Object)} but more efficient in that it doesn't box the element.
* Like {@link #add(Integer)} but more efficient in that it doesn't box the element.
*/
void addInt(int element);
/**
* Like {@link #set(int, Object)} but more efficient in that it doesn't box the element.
* Like {@link #set(int, Integer)} but more efficient in that it doesn't box the element.
*/
int setInt(int index, int element);
/**
* Returns a mutable clone of this list with the specified capacity.
*/
@Override
IntList mutableCopyWithCapacity(int capacity);
}
/**
......@@ -622,14 +639,20 @@ public final class Internal {
boolean getBoolean(int index);
/**
* Like {@link #add(Object)} but more efficient in that it doesn't box the element.
* Like {@link #add(Boolean)} but more efficient in that it doesn't box the element.
*/
void addBoolean(boolean element);
/**
* Like {@link #set(int, Object)} but more efficient in that it doesn't box the element.
* Like {@link #set(int, Boolean)} but more efficient in that it doesn't box the element.
*/
boolean setBoolean(int index, boolean element);
/**
* Returns a mutable clone of this list with the specified capacity.
*/
@Override
BooleanList mutableCopyWithCapacity(int capacity);
}
/**
......@@ -644,14 +667,20 @@ public final class Internal {
long getLong(int index);
/**
* Like {@link #add(Object)} but more efficient in that it doesn't box the element.
* Like {@link #add(Long)} but more efficient in that it doesn't box the element.
*/
void addLong(long element);
/**
* Like {@link #set(int, Object)} but more efficient in that it doesn't box the element.
* Like {@link #set(int, Long)} but more efficient in that it doesn't box the element.
*/
long setLong(int index, long element);
/**
* Returns a mutable clone of this list with the specified capacity.
*/
@Override
LongList mutableCopyWithCapacity(int capacity);
}
/**
......@@ -666,14 +695,20 @@ public final class Internal {
double getDouble(int index);
/**
* Like {@link #add(Object)} but more efficient in that it doesn't box the element.
* Like {@link #add(Double)} but more efficient in that it doesn't box the element.
*/
void addDouble(double element);
/**
* Like {@link #set(int, Object)} but more efficient in that it doesn't box the element.
* Like {@link #set(int, Double)} but more efficient in that it doesn't box the element.
*/
double setDouble(int index, double element);
/**
* Returns a mutable clone of this list with the specified capacity.
*/
@Override
DoubleList mutableCopyWithCapacity(int capacity);
}
/**
......@@ -688,13 +723,19 @@ public final class Internal {
float getFloat(int index);
/**
* Like {@link #add(Object)} but more efficient in that it doesn't box the element.
* Like {@link #add(Float)} but more efficient in that it doesn't box the element.
*/
void addFloat(float element);
/**
* Like {@link #set(int, Object)} but more efficient in that it doesn't box the element.
* Like {@link #set(int, Float)} but more efficient in that it doesn't box the element.
*/
float setFloat(int index, float element);
/**
* Returns a mutable clone of this list with the specified capacity.
*/
@Override
FloatList mutableCopyWithCapacity(int capacity);
}
}
......@@ -95,12 +95,12 @@ public class LazyField extends LazyFieldLite {
this.entry = entry;
}
// @Override
@Override
public K getKey() {
return entry.getKey();
}
// @Override
@Override
public Object getValue() {
LazyField field = entry.getValue();
if (field == null) {
......@@ -113,7 +113,7 @@ public class LazyField extends LazyFieldLite {
return entry.getValue();
}
// @Override
@Override
public Object setValue(Object value) {
if (!(value instanceof MessageLite)) {
throw new IllegalArgumentException(
......@@ -131,13 +131,13 @@ public class LazyField extends LazyFieldLite {
this.iterator = iterator;
}
// @Override
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
@SuppressWarnings("unchecked")
// @Override
public Entry<K, Object> next() {
Entry<K, ?> entry = iterator.next();
if (entry.getValue() instanceof LazyField) {
......@@ -146,7 +146,7 @@ public class LazyField extends LazyFieldLite {
return (Entry<K, Object>) entry;
}
// @Override
@Override
public void remove() {
iterator.remove();
}
......
......@@ -135,6 +135,43 @@ public class LazyFieldLite {
return lf;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof LazyFieldLite)) {
return false;
}
LazyFieldLite other = (LazyFieldLite) o;
// Lazy fields do not work well with equals... If both are delayedBytes, we do not have a
// mechanism to deserialize them so we rely on bytes equality. Otherwise we coerce into an
// actual message (if necessary) and call equals on the message itself. This implies that two
// messages can by unequal but then be turned equal simply be invoking a getter on a lazy field.
MessageLite value1 = value;
MessageLite value2 = other.value;
if (value1 == null && value2 == null) {
return toByteString().equals(other.toByteString());
} else if (value1 != null && value2 != null) {
return value1.equals(value2);
} else if (value1 != null) {
return value1.equals(other.getValue(value1.getDefaultInstanceForType()));
} else {
return getValue(value2.getDefaultInstanceForType()).equals(value2);
}
}
@Override
public int hashCode() {
// We can't provide a memoizable hash code for lazy fields. The byte strings may have different
// hash codes but evaluate to equivalent messages. And we have no facility for constructing
// a message here if we were not already holding a value.
return 1;
}
/**
* Determines whether this LazyFieldLite instance represents the default instance of this type.
*/
......@@ -340,6 +377,8 @@ public class LazyFieldLite {
* parsed. Be careful when using this method.
*/
public int getSerializedSize() {
// We *must* return delayed bytes size if it was ever set because the dependent messages may
// have memoized serialized size based off of it.
if (memoizedBytes != null) {
return memoizedBytes.size();
} else if (delayedBytes != null) {
......@@ -358,6 +397,8 @@ public class LazyFieldLite {
if (memoizedBytes != null) {
return memoizedBytes;
}
// We *must* return delayed bytes if it was set because the dependent messages may have
// memoized serialized size based off of it.
if (delayedBytes != null) {
return delayedBytes;
}
......
......@@ -64,7 +64,7 @@ import java.util.RandomAccess;
*/
public class LazyStringArrayList extends AbstractProtobufList<String>
implements LazyStringList, RandomAccess {
private static final LazyStringArrayList EMPTY_LIST = new LazyStringArrayList();
static {
EMPTY_LIST.makeImmutable();
......@@ -80,11 +80,11 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
private final List<Object> list;
public LazyStringArrayList() {
list = new ArrayList<Object>();
this(DEFAULT_CAPACITY);
}
public LazyStringArrayList(int intialCapacity) {
list = new ArrayList<Object>(intialCapacity);
this(new ArrayList<Object>(intialCapacity));
}
public LazyStringArrayList(LazyStringList from) {
......@@ -93,7 +93,21 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
}
public LazyStringArrayList(List<String> from) {
list = new ArrayList<Object>(from);
this(new ArrayList<Object>(from));
}
private LazyStringArrayList(ArrayList<Object> list) {
this.list = list;
}
@Override
public LazyStringArrayList mutableCopyWithCapacity(int capacity) {
if (capacity < size()) {
throw new IllegalArgumentException();
}
ArrayList<Object> newList = new ArrayList<Object>(capacity);
newList.addAll(list);
return new LazyStringArrayList(newList);
}
@Override
......@@ -170,7 +184,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return ret;
}
// @Override
@Override
public boolean addAllByteString(Collection<? extends ByteString> values) {
ensureIsMutable();
boolean ret = list.addAll(values);
......@@ -178,7 +192,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return ret;
}
// @Override
@Override
public boolean addAllByteArray(Collection<byte[]> c) {
ensureIsMutable();
boolean ret = list.addAll(c);
......@@ -201,14 +215,14 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
modCount++;
}
// @Override
@Override
public void add(ByteString element) {
ensureIsMutable();
list.add(element);
modCount++;
}
// @Override
@Override
public void add(byte[] element) {
ensureIsMutable();
list.add(element);
......@@ -220,7 +234,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return list.get(index);
}
// @Override
@Override
public ByteString getByteString(int index) {
Object o = list.get(index);
ByteString b = asByteString(o);
......@@ -230,7 +244,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return b;
}
// @Override
@Override
public byte[] getByteArray(int index) {
Object o = list.get(index);
byte[] b = asByteArray(o);
......@@ -240,7 +254,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return b;
}
// @Override
@Override
public void set(int index, ByteString s) {
setAndReturn(index, s);
}
......@@ -250,7 +264,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
return list.set(index, s);
}
// @Override
@Override
public void set(int index, byte[] s) {
setAndReturn(index, s);
}
......@@ -290,12 +304,12 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
}
}
// @Override
@Override
public List<?> getUnderlyingElements() {
return Collections.unmodifiableList(list);
}
// @Override
@Override
public void mergeFrom(LazyStringList other) {
ensureIsMutable();
for (Object o : other.getUnderlyingElements()) {
......@@ -349,7 +363,7 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
}
}
// @Override
@Override
public List<byte[]> asByteArrayList() {
return new ByteArrayListView(this);
}
......@@ -393,12 +407,12 @@ public class LazyStringArrayList extends AbstractProtobufList<String>
}
}
// @Override
@Override
public List<ByteString> asByteStringList() {
return new ByteStringListView(this);
}
// @Override
@Override
public LazyStringList getUnmodifiableView() {
if (isModifiable()) {
return new UnmodifiableLazyStringList(this);
......
......@@ -34,7 +34,6 @@ import com.google.protobuf.Internal.LongList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.RandomAccess;
/**
......@@ -44,8 +43,6 @@ import java.util.RandomAccess;
*/
final class LongArrayList extends AbstractProtobufList<Long> implements LongList, RandomAccess {
private static final int DEFAULT_CAPACITY = 10;
private static final LongArrayList EMPTY_LIST = new LongArrayList();
static {
EMPTY_LIST.makeImmutable();
......@@ -70,32 +67,55 @@ final class LongArrayList extends AbstractProtobufList<Long> implements LongList
* Constructs a new mutable {@code LongArrayList} with default capacity.
*/
LongArrayList() {
this(DEFAULT_CAPACITY);
}
/**
* Constructs a new mutable {@code LongArrayList} with the provided capacity.
*/
LongArrayList(int capacity) {
array = new long[capacity];
size = 0;
this(new long[DEFAULT_CAPACITY], 0);
}
/**
* Constructs a new mutable {@code LongArrayList} containing the same elements as {@code other}.
*/
LongArrayList(List<Long> other) {
if (other instanceof LongArrayList) {
LongArrayList list = (LongArrayList) other;
array = list.array.clone();
size = list.size;
} else {
size = other.size();
array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = other.get(i);
private LongArrayList(long[] array, int size) {
this.array = array;
this.size = size;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof IntArrayList)) {
return super.equals(o);
}
LongArrayList other = (LongArrayList) o;
if (size != other.size) {
return false;
}
final long[] arr = other.array;
for (int i = 0; i < size; i++) {
if (array[i] != arr[i]) {
return false;
}
}
return true;
}
@Override
public int hashCode() {
int result = 1;
for (int i = 0; i < size; i++) {
result = (31 * result) + Internal.hashLong(array[i]);
}
return result;
}
@Override
public LongList mutableCopyWithCapacity(int capacity) {
if (capacity < size) {
throw new IllegalArgumentException();
}
return new LongArrayList(Arrays.copyOf(array, capacity), size);
}
@Override
......
......@@ -41,7 +41,8 @@ import java.io.IOException;
*
* Protobuf internal. Users shouldn't use.
*/
public class MapEntryLite<K, V> extends AbstractMessageLite {
public class MapEntryLite<K, V>
extends AbstractMessageLite<MapEntryLite<K, V>, MapEntryLite.Builder<K, V>> {
private static class Metadata<K, V> {
public final MapEntryLite<K, V> defaultInstance;
public final WireFormat.FieldType keyType;
......@@ -233,7 +234,7 @@ public class MapEntryLite<K, V> extends AbstractMessageLite {
* Builder used to create {@link MapEntryLite} messages.
*/
public static class Builder<K, V>
extends AbstractMessageLite.Builder<Builder<K, V>> {
extends AbstractMessageLite.Builder<MapEntryLite<K, V>, Builder<K, V>> {
private final Metadata<K, V> metadata;
private K key;
private V value;
......@@ -327,5 +328,10 @@ public class MapEntryLite<K, V> extends AbstractMessageLite {
this.value = entry.value;
return this;
}
@Override
protected Builder<K, V> internalMergeFrom(MapEntryLite<K, V> message) {
throw new UnsupportedOperationException();
}
}
}
......@@ -93,15 +93,18 @@ public class MapField<K, V> implements MutabilityOracle {
this.defaultEntry = defaultEntry;
}
@Override
public Message convertKeyAndValueToMessage(K key, V value) {
return defaultEntry.newBuilderForType().setKey(key).setValue(value).buildPartial();
}
@Override
public void convertMessageToKeyAndValue(Message message, Map<K, V> map) {
MapEntry<K, V> entry = (MapEntry<K, V>) message;
map.put(entry.getKey(), entry.getValue());
}
@Override
public Message getMessageDefaultInstance() {
return defaultEntry;
}
......
......@@ -51,6 +51,7 @@ import java.util.Map;
public interface Message extends MessageLite, MessageOrBuilder {
// (From MessageLite, re-declared here only for return type covariance.)
@Override
Parser<? extends Message> getParserForType();
......@@ -97,7 +98,10 @@ public interface Message extends MessageLite, MessageOrBuilder {
// Builders
// (From MessageLite, re-declared here only for return type covariance.)
@Override
Builder newBuilderForType();
@Override
Builder toBuilder();
/**
......@@ -106,6 +110,7 @@ public interface Message extends MessageLite, MessageOrBuilder {
interface Builder extends MessageLite.Builder, MessageOrBuilder {
// (From MessageLite.Builder, re-declared here only for return type
// covariance.)
@Override
Builder clear();
/**
......@@ -131,18 +136,27 @@ public interface Message extends MessageLite, MessageOrBuilder {
// (From MessageLite.Builder, re-declared here only for return type
// covariance.)
@Override
Message build();
@Override
Message buildPartial();
@Override
Builder clone();
@Override
Builder mergeFrom(CodedInputStream input) throws IOException;
Builder mergeFrom(CodedInputStream input,
ExtensionRegistryLite extensionRegistry)
throws IOException;
@Override
Builder mergeFrom(CodedInputStream input, ExtensionRegistryLite extensionRegistry)
throws IOException;
/**
* Get the message's type's descriptor.
* See {@link Message#getDescriptorForType()}.
*/
@Override
Descriptors.Descriptor getDescriptorForType();
/**
......@@ -240,27 +254,39 @@ public interface Message extends MessageLite, MessageOrBuilder {
// (From MessageLite.Builder, re-declared here only for return type
// covariance.)
@Override
Builder mergeFrom(ByteString data) throws InvalidProtocolBufferException;
Builder mergeFrom(ByteString data,
ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException;
@Override
Builder mergeFrom(ByteString data, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException;
@Override
Builder mergeFrom(byte[] data) throws InvalidProtocolBufferException;
Builder mergeFrom(byte[] data, int off, int len)
throws InvalidProtocolBufferException;
Builder mergeFrom(byte[] data,
ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException;
Builder mergeFrom(byte[] data, int off, int len,
ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException;
@Override
Builder mergeFrom(byte[] data, int off, int len) throws InvalidProtocolBufferException;
@Override
Builder mergeFrom(byte[] data, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException;
@Override
Builder mergeFrom(byte[] data, int off, int len, ExtensionRegistryLite extensionRegistry)
throws InvalidProtocolBufferException;
@Override
Builder mergeFrom(InputStream input) throws IOException;
Builder mergeFrom(InputStream input,
ExtensionRegistryLite extensionRegistry)
throws IOException;
boolean mergeDelimitedFrom(InputStream input)
throws IOException;
boolean mergeDelimitedFrom(InputStream input,
ExtensionRegistryLite extensionRegistry)
throws IOException;
@Override
Builder mergeFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
throws IOException;
@Override
boolean mergeDelimitedFrom(InputStream input) throws IOException;
@Override
boolean mergeDelimitedFrom(InputStream input, ExtensionRegistryLite extensionRegistry)
throws IOException;
}
}
......@@ -295,6 +295,27 @@ public interface MessageLite extends MessageLiteOrBuilder {
Builder mergeFrom(InputStream input,
ExtensionRegistryLite extensionRegistry)
throws IOException;
/**
* Merge {@code other} into the message being built. {@code other} must
* have the exact same type as {@code this} (i.e.
* {@code getClass().equals(getDefaultInstanceForType().getClass())}).
*
* Merging occurs as follows. For each field:<br>
* * For singular primitive fields, if the field is set in {@code other},
* then {@code other}'s value overwrites the value in this message.<br>
* * For singular message fields, if the field is set in {@code other},
* it is merged into the corresponding sub-message of this message
* using the same merging rules.<br>
* * For repeated fields, the elements in {@code other} are concatenated
* with the elements in this message.
* * For oneof groups, if the other message has one of the fields set,
* the group of this message is cleared and replaced by the field
* of the other message, so that the oneof constraint is preserved.
*
* This is equivalent to the {@code Message::MergeFrom} method in C++.
*/
Builder mergeFrom(MessageLite other);
/**
* Like {@link #mergeFrom(InputStream)}, but does not read until EOF.
......
......@@ -30,23 +30,24 @@
package com.google.protobuf;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* Helps generate {@link String} representations of {@link MessageLite} protos.
*/
// TODO(dweis): Fix map fields.
final class MessageLiteToString {
/**
* Suffix for *_FIELD_NUMBER fields. This is used to reflectively detect proto fields that should
* be toString()ed.
*/
private static final String FIELD_NUMBER_NAME_SUFFIX = "_FIELD_NUMBER";
private static final String LIST_SUFFIX = "List";
private static final String BUILDER_LIST_SUFFIX = "OrBuilderList";
private static final String BYTES_SUFFIX = "Bytes";
/**
* Returns a {@link String} representation of the {@link MessageLite} object. The first line of
* the {@code String} representation representation includes a comment string to uniquely identify
......@@ -73,52 +74,70 @@ final class MessageLiteToString {
// Build a map of method name to method. We're looking for methods like getFoo(), hasFoo(), and
// getFooList() which might be useful for building an object's string representation.
Map<String, Method> nameToNoArgMethod = new HashMap<String, Method>();
Map<String, Method> nameToMethod = new HashMap<String, Method>();
Set<String> getters = new TreeSet<String>();
for (Method method : messageLite.getClass().getDeclaredMethods()) {
nameToMethod.put(method.getName(), method);
if (method.getParameterTypes().length == 0) {
nameToNoArgMethod.put(method.getName(), method);
if (method.getName().startsWith("get")) {
getters.add(method.getName());
}
}
}
for (Field field : messageLite.getClass().getDeclaredFields()) {
String fieldName = field.getName();
// Skip all fields that aren't in a format like "FOO_BAR_FIELD_NUMBER"
if (!fieldName.endsWith(FIELD_NUMBER_NAME_SUFFIX)) {
continue;
for (String getter : getters) {
String suffix = getter.replaceFirst("get", "");
if (suffix.endsWith(LIST_SUFFIX) && !suffix.endsWith(BUILDER_LIST_SUFFIX)) {
String camelCase = suffix.substring(0, 1).toLowerCase()
+ suffix.substring(1, suffix.length() - LIST_SUFFIX.length());
// Try to reflectively get the value and toString() the field as if it were repeated. This
// only works if the method names have not be proguarded out or renamed.
Method listMethod = nameToNoArgMethod.get("get" + suffix);
if (listMethod != null) {
printField(
buffer,
indent,
camelCaseToSnakeCase(camelCase),
GeneratedMessageLite.invokeOrDie(listMethod, messageLite));
continue;
}
}
// For "FOO_BAR_FIELD_NUMBER" his would be "FOO_BAR"
String upperUnderscore =
fieldName.substring(0, fieldName.length() - FIELD_NUMBER_NAME_SUFFIX.length());
// For "FOO_BAR_FIELD_NUMBER" his would be "FooBar"
String upperCamelCaseName = upperUnderscoreToUpperCamel(upperUnderscore);
Method setter = nameToMethod.get("set" + suffix);
if (setter == null) {
continue;
}
if (suffix.endsWith(BYTES_SUFFIX)
&& nameToNoArgMethod.containsKey(
"get" + suffix.substring(0, suffix.length() - "Bytes".length()))) {
// Heuristic to skip bytes based accessors for string fields.
continue;
}
String camelCase = suffix.substring(0, 1).toLowerCase() + suffix.substring(1);
// Try to reflectively get the value and toString() the field as if it were optional. This
// only works if the method names have not be proguarded out or renamed.
Method getMethod = nameToNoArgMethod.get("get" + upperCamelCaseName);
Method hasMethod = nameToNoArgMethod.get("has" + upperCamelCaseName);
if (getMethod != null && hasMethod != null) {
if ((Boolean) GeneratedMessageLite.invokeOrDie(hasMethod, messageLite)) {
Method getMethod = nameToNoArgMethod.get("get" + suffix);
Method hasMethod = nameToNoArgMethod.get("has" + suffix);
// TODO(dweis): Fix proto3 semantics.
if (getMethod != null) {
Object value = GeneratedMessageLite.invokeOrDie(getMethod, messageLite);
final boolean hasValue = hasMethod == null
? !isDefaultValue(value)
: (Boolean) GeneratedMessageLite.invokeOrDie(hasMethod, messageLite);
// TODO(dweis): This doesn't stop printing oneof case twice: value and enum style.
if (hasValue) {
printField(
buffer,
indent,
upperUnderscore.toLowerCase(),
GeneratedMessageLite.invokeOrDie(getMethod, messageLite));
camelCaseToSnakeCase(camelCase),
value);
}
continue;
}
// Try to reflectively get the value and toString() the field as if it were repeated. This
// only works if the method names have not be proguarded out or renamed.
Method listMethod = nameToNoArgMethod.get("get" + upperCamelCaseName + "List");
if (listMethod != null) {
printField(
buffer,
indent,
upperUnderscore.toLowerCase(),
GeneratedMessageLite.invokeOrDie(listMethod, messageLite));
continue;
}
}
if (messageLite instanceof GeneratedMessageLite.ExtendableMessage) {
......@@ -130,10 +149,39 @@ final class MessageLiteToString {
}
}
if (((GeneratedMessageLite) messageLite).unknownFields != null) {
((GeneratedMessageLite) messageLite).unknownFields.printWithIndent(buffer, indent);
if (((GeneratedMessageLite<?, ?>) messageLite).unknownFields != null) {
((GeneratedMessageLite<?, ?>) messageLite).unknownFields.printWithIndent(buffer, indent);
}
}
private static boolean isDefaultValue(Object o) {
if (o instanceof Boolean) {
return !((Boolean) o);
}
if (o instanceof Integer) {
return ((Integer) o) == 0;
}
if (o instanceof Float) {
return ((Float) o) == 0f;
}
if (o instanceof Double) {
return ((Double) o) == 0d;
}
if (o instanceof String) {
return o.equals("");
}
if (o instanceof ByteString) {
return o.equals(ByteString.EMPTY);
}
if (o instanceof MessageLite) { // Can happen in oneofs.
return o == ((MessageLite) o).getDefaultInstanceForType();
}
if (o instanceof java.lang.Enum<?>) { // Catches oneof enums.
return ((java.lang.Enum<?>) o).ordinal() == 0;
}
return false;
}
/**
* Formats a text proto field.
......@@ -166,7 +214,7 @@ final class MessageLiteToString {
buffer.append(": \"").append(TextFormatEscaper.escapeBytes((ByteString) object)).append('"');
} else if (object instanceof GeneratedMessageLite) {
buffer.append(" {");
reflectivePrintWithIndent((GeneratedMessageLite) object, buffer, indent + 2);
reflectivePrintWithIndent((GeneratedMessageLite<?, ?>) object, buffer, indent + 2);
buffer.append("\n");
for (int i = 0; i < indent; i++) {
buffer.append(' ');
......@@ -176,25 +224,16 @@ final class MessageLiteToString {
buffer.append(": ").append(object.toString());
}
}
/**
* A Guava-less implementation of:
* {@code CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, upperUnderscore)}
*/
private static String upperUnderscoreToUpperCamel(String upperUnderscore) {
String upperCamelCaseName = "";
boolean nextCharacterShouldBeUpper = true;
for (int i = 0; i < upperUnderscore.length(); i++) {
char ch = upperUnderscore.charAt(i);
if (ch == '_') {
nextCharacterShouldBeUpper = true;
} else if (nextCharacterShouldBeUpper){
upperCamelCaseName += Character.toUpperCase(ch);
nextCharacterShouldBeUpper = false;
} else {
upperCamelCaseName += Character.toLowerCase(ch);
private static final String camelCaseToSnakeCase(String camelCase) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < camelCase.length(); i++) {
char ch = camelCase.charAt(i);
if (Character.isUpperCase(ch)) {
builder.append("_");
}
builder.append(Character.toLowerCase(ch));
}
return upperCamelCaseName;
return builder.toString();
}
}
......@@ -42,7 +42,7 @@ import java.util.Map;
public interface MessageOrBuilder extends MessageLiteOrBuilder {
// (From MessageLite, re-declared here only for return type covariance.)
//@Override (Java 1.6 override semantics, but we must support 1.5)
@Override
Message getDefaultInstanceForType();
/**
......
......@@ -364,12 +364,14 @@ class MessageReflection {
* Finishes the merge and returns the underlying object.
*/
Object finish();
}
static class BuilderAdapter implements MergeTarget {
private final Message.Builder builder;
@Override
public Descriptors.Descriptor getDescriptorForType() {
return builder.getDescriptorForType();
}
......@@ -378,6 +380,7 @@ class MessageReflection {
this.builder = builder;
}
@Override
public Object getField(Descriptors.FieldDescriptor field) {
return builder.getField(field);
}
......@@ -387,25 +390,27 @@ class MessageReflection {
return builder.hasField(field);
}
public MergeTarget setField(Descriptors.FieldDescriptor field,
Object value) {
@Override
public MergeTarget setField(Descriptors.FieldDescriptor field, Object value) {
builder.setField(field, value);
return this;
}
@Override
public MergeTarget clearField(Descriptors.FieldDescriptor field) {
builder.clearField(field);
return this;
}
@Override
public MergeTarget setRepeatedField(
Descriptors.FieldDescriptor field, int index, Object value) {
builder.setRepeatedField(field, index, value);
return this;
}
public MergeTarget addRepeatedField(
Descriptors.FieldDescriptor field, Object value) {
@Override
public MergeTarget addRepeatedField(Descriptors.FieldDescriptor field, Object value) {
builder.addRepeatedField(field, value);
return this;
}
......@@ -426,25 +431,30 @@ class MessageReflection {
return builder.getOneofFieldDescriptor(oneof);
}
@Override
public ContainerType getContainerType() {
return ContainerType.MESSAGE;
}
@Override
public ExtensionRegistry.ExtensionInfo findExtensionByName(
ExtensionRegistry registry, String name) {
return registry.findImmutableExtensionByName(name);
}
@Override
public ExtensionRegistry.ExtensionInfo findExtensionByNumber(
ExtensionRegistry registry, Descriptors.Descriptor containingType,
int fieldNumber) {
ExtensionRegistry registry, Descriptors.Descriptor containingType, int fieldNumber) {
return registry.findImmutableExtensionByNumber(containingType,
fieldNumber);
}
public Object parseGroup(CodedInputStream input,
@Override
public Object parseGroup(
CodedInputStream input,
ExtensionRegistryLite extensionRegistry,
Descriptors.FieldDescriptor field, Message defaultInstance)
Descriptors.FieldDescriptor field,
Message defaultInstance)
throws IOException {
Message.Builder subBuilder;
// When default instance is not null. The field is an extension field.
......@@ -463,9 +473,12 @@ class MessageReflection {
return subBuilder.buildPartial();
}
public Object parseMessage(CodedInputStream input,
@Override
public Object parseMessage(
CodedInputStream input,
ExtensionRegistryLite extensionRegistry,
Descriptors.FieldDescriptor field, Message defaultInstance)
Descriptors.FieldDescriptor field,
Message defaultInstance)
throws IOException {
Message.Builder subBuilder;
// When default instance is not null. The field is an extension field.
......@@ -484,9 +497,12 @@ class MessageReflection {
return subBuilder.buildPartial();
}
public Object parseMessageFromBytes(ByteString bytes,
@Override
public Object parseMessageFromBytes(
ByteString bytes,
ExtensionRegistryLite extensionRegistry,
Descriptors.FieldDescriptor field, Message defaultInstance)
Descriptors.FieldDescriptor field,
Message defaultInstance)
throws IOException {
Message.Builder subBuilder;
// When default instance is not null. The field is an extension field.
......@@ -505,8 +521,9 @@ class MessageReflection {
return subBuilder.buildPartial();
}
public MergeTarget newMergeTargetForField(Descriptors.FieldDescriptor field,
Message defaultInstance) {
@Override
public MergeTarget newMergeTargetForField(
Descriptors.FieldDescriptor field, Message defaultInstance) {
if (defaultInstance != null) {
return new BuilderAdapter(
defaultInstance.newBuilderForType());
......@@ -515,8 +532,8 @@ class MessageReflection {
}
}
public WireFormat.Utf8Validation
getUtf8Validation(Descriptors.FieldDescriptor descriptor) {
@Override
public WireFormat.Utf8Validation getUtf8Validation(Descriptors.FieldDescriptor descriptor) {
if (descriptor.needsUtf8Check()) {
return WireFormat.Utf8Validation.STRICT;
}
......@@ -528,9 +545,11 @@ class MessageReflection {
return WireFormat.Utf8Validation.LOOSE;
}
@Override
public Object finish() {
return builder.buildPartial();
}
}
......@@ -542,38 +561,43 @@ class MessageReflection {
this.extensions = extensions;
}
@Override
public Descriptors.Descriptor getDescriptorForType() {
throw new UnsupportedOperationException(
"getDescriptorForType() called on FieldSet object");
}
@Override
public Object getField(Descriptors.FieldDescriptor field) {
return extensions.getField(field);
}
@Override
public boolean hasField(Descriptors.FieldDescriptor field) {
return extensions.hasField(field);
}
public MergeTarget setField(Descriptors.FieldDescriptor field,
Object value) {
@Override
public MergeTarget setField(Descriptors.FieldDescriptor field, Object value) {
extensions.setField(field, value);
return this;
}
@Override
public MergeTarget clearField(Descriptors.FieldDescriptor field) {
extensions.clearField(field);
return this;
}
@Override
public MergeTarget setRepeatedField(
Descriptors.FieldDescriptor field, int index, Object value) {
extensions.setRepeatedField(field, index, value);
return this;
}
public MergeTarget addRepeatedField(
Descriptors.FieldDescriptor field, Object value) {
@Override
public MergeTarget addRepeatedField(Descriptors.FieldDescriptor field, Object value) {
extensions.addRepeatedField(field, value);
return this;
}
......@@ -594,25 +618,31 @@ class MessageReflection {
return null;
}
@Override
public ContainerType getContainerType() {
return ContainerType.EXTENSION_SET;
}
@Override
public ExtensionRegistry.ExtensionInfo findExtensionByName(
ExtensionRegistry registry, String name) {
return registry.findImmutableExtensionByName(name);
}
@Override
public ExtensionRegistry.ExtensionInfo findExtensionByNumber(
ExtensionRegistry registry, Descriptors.Descriptor containingType,
int fieldNumber) {
ExtensionRegistry registry, Descriptors.Descriptor containingType, int fieldNumber) {
return registry.findImmutableExtensionByNumber(containingType,
fieldNumber);
}
public Object parseGroup(CodedInputStream input,
ExtensionRegistryLite registry, Descriptors.FieldDescriptor field,
Message defaultInstance) throws IOException {
@Override
public Object parseGroup(
CodedInputStream input,
ExtensionRegistryLite registry,
Descriptors.FieldDescriptor field,
Message defaultInstance)
throws IOException {
Message.Builder subBuilder =
defaultInstance.newBuilderForType();
if (!field.isRepeated()) {
......@@ -625,9 +655,13 @@ class MessageReflection {
return subBuilder.buildPartial();
}
public Object parseMessage(CodedInputStream input,
ExtensionRegistryLite registry, Descriptors.FieldDescriptor field,
Message defaultInstance) throws IOException {
@Override
public Object parseMessage(
CodedInputStream input,
ExtensionRegistryLite registry,
Descriptors.FieldDescriptor field,
Message defaultInstance)
throws IOException {
Message.Builder subBuilder =
defaultInstance.newBuilderForType();
if (!field.isRepeated()) {
......@@ -640,9 +674,13 @@ class MessageReflection {
return subBuilder.buildPartial();
}
public Object parseMessageFromBytes(ByteString bytes,
ExtensionRegistryLite registry, Descriptors.FieldDescriptor field,
Message defaultInstance) throws IOException {
@Override
public Object parseMessageFromBytes(
ByteString bytes,
ExtensionRegistryLite registry,
Descriptors.FieldDescriptor field,
Message defaultInstance)
throws IOException {
Message.Builder subBuilder = defaultInstance.newBuilderForType();
if (!field.isRepeated()) {
Message originalMessage = (Message) getField(field);
......@@ -654,14 +692,15 @@ class MessageReflection {
return subBuilder.buildPartial();
}
@Override
public MergeTarget newMergeTargetForField(
Descriptors.FieldDescriptor descriptor, Message defaultInstance) {
throw new UnsupportedOperationException(
"newMergeTargetForField() called on FieldSet object");
}
public WireFormat.Utf8Validation
getUtf8Validation(Descriptors.FieldDescriptor descriptor) {
@Override
public WireFormat.Utf8Validation getUtf8Validation(Descriptors.FieldDescriptor descriptor) {
if (descriptor.needsUtf8Check()) {
return WireFormat.Utf8Validation.STRICT;
}
......@@ -669,10 +708,12 @@ class MessageReflection {
return WireFormat.Utf8Validation.LOOSE;
}
@Override
public Object finish() {
throw new UnsupportedOperationException(
"finish() called on FieldSet object");
}
}
/**
......
......@@ -38,7 +38,7 @@ import java.util.List;
/**
* Implements {@link ProtobufList} for non-primitive and {@link String} types.
*/
class ProtobufArrayList<E> extends AbstractProtobufList<E> {
final class ProtobufArrayList<E> extends AbstractProtobufList<E> {
private static final ProtobufArrayList<Object> EMPTY_LIST = new ProtobufArrayList<Object>();
static {
......@@ -51,17 +51,23 @@ class ProtobufArrayList<E> extends AbstractProtobufList<E> {
}
private final List<E> list;
ProtobufArrayList() {
list = new ArrayList<E>();
this(new ArrayList<E>(DEFAULT_CAPACITY));
}
ProtobufArrayList(List<E> toCopy) {
list = new ArrayList<E>(toCopy);
private ProtobufArrayList(List<E> list) {
this.list = list;
}
ProtobufArrayList(int capacity) {
list = new ArrayList<E>(capacity);
@Override
public ProtobufArrayList<E> mutableCopyWithCapacity(int capacity) {
if (capacity < size()) {
throw new IllegalArgumentException();
}
List<E> newList = new ArrayList<E>(capacity);
newList.addAll(list);
return new ProtobufArrayList<E>(newList);
}
@Override
......
......@@ -42,6 +42,7 @@ public interface ProtocolMessageEnum extends Internal.EnumLite {
/**
* Return the value's numeric value as defined in the .proto file.
*/
@Override
int getNumber();
/**
......
......@@ -234,7 +234,7 @@ public class SingleFieldBuilder
}
}
//@Override (Java 1.6 override semantics, but we must support 1.5)
@Override
public void markDirty() {
onChanged();
}
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
<?php
require_once('test.pb.php');
require_once('test_util.php');
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment