Index: third_party/protobuf/java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java |
diff --git a/third_party/protobuf/java/src/main/java/com/google/protobuf/AbstractProtobufList.java b/third_party/protobuf/java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java |
similarity index 76% |
rename from third_party/protobuf/java/src/main/java/com/google/protobuf/AbstractProtobufList.java |
rename to third_party/protobuf/java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java |
index bb6446b2a255bbe1fa4adbb4555b45368281ba23..b17db6e0b31475af77e335251de062330abed5fb 100644 |
--- a/third_party/protobuf/java/src/main/java/com/google/protobuf/AbstractProtobufList.java |
+++ b/third_party/protobuf/java/core/src/main/java/com/google/protobuf/AbstractProtobufList.java |
@@ -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. |
*/ |
@@ -55,6 +61,44 @@ abstract class AbstractProtobufList<E> extends AbstractList<E> implements Protob |
} |
@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(); |
return super.add(e); |