Add method to create a new ProtobufList for the given element type. Add method to make a mutable copy with a capacity hint. PiperOrigin-RevId: 540737857
diff --git a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java index fa5a070..91f7225 100644 --- a/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java +++ b/java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
@@ -485,11 +485,27 @@ return new BooleanArrayList(); } - @SuppressWarnings("unchecked") // Guaranteed by proto runtime. protected static <ListT extends ProtobufList<?>> ListT makeMutableCopy(ListT list) { + return makeMutableCopy(list, 0); + } + + @SuppressWarnings("unchecked") // Guaranteed by proto runtime. + protected static <ListT extends ProtobufList<?>> ListT makeMutableCopy( + ListT list, int minCapacity) { int size = list.size(); - return (ListT) - list.mutableCopyWithCapacity(size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2); + if (minCapacity <= size) { + minCapacity = size * 2; + } + if (minCapacity <= 0) { + minCapacity = AbstractProtobufList.DEFAULT_CAPACITY; + } + + return (ListT) list.mutableCopyWithCapacity(minCapacity); + } + + @SuppressWarnings("unchecked") // The empty list can be safely cast + protected static <T> ProtobufList<T> emptyList(Class<T> elementType) { + return (ProtobufList<T>) ProtobufArrayList.emptyList(); } @Override