Skip to content

Commit

Permalink
Create a helper function that can make a mutable copy of any Protobuf…
Browse files Browse the repository at this point in the history
…List

instance to replace the specialized versions.

PiperOrigin-RevId: 500198486
  • Loading branch information
protobuf-github-bot authored and Copybara-Service committed Jan 6, 2023
1 parent de5fae0 commit 5669606
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
Expand Up @@ -43,6 +43,7 @@
import com.google.protobuf.Internal.FloatList;
import com.google.protobuf.Internal.IntList;
import com.google.protobuf.Internal.LongList;
import com.google.protobuf.Internal.ProtobufList;
// In opensource protobuf, we have versioned this GeneratedMessageV3 class to GeneratedMessageV3 and
// in the future may have GeneratedMessageV4 etc. This allows us to change some aspects of this
// class without breaking binary compatibility with old generated code that still subclasses
Expand Down Expand Up @@ -417,10 +418,9 @@ protected static IntList newIntList() {
return new IntArrayList();
}

// TODO(b/258340024): Redundant with makeMutableCopy(). Remove.
protected static IntList mutableCopy(IntList list) {
int size = list.size();
return list.mutableCopyWithCapacity(
size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2);
return makeMutableCopy(list);
}

protected static LongList emptyLongList() {
Expand All @@ -432,10 +432,9 @@ protected static LongList newLongList() {
return new LongArrayList();
}

// TODO(b/258340024): Redundant with makeMutableCopy(). Remove.
protected static LongList mutableCopy(LongList list) {
int size = list.size();
return list.mutableCopyWithCapacity(
size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2);
return makeMutableCopy(list);
}

protected static FloatList emptyFloatList() {
Expand All @@ -447,10 +446,9 @@ protected static FloatList newFloatList() {
return new FloatArrayList();
}

// TODO(b/258340024): Redundant with makeMutableCopy(). Remove.
protected static FloatList mutableCopy(FloatList list) {
int size = list.size();
return list.mutableCopyWithCapacity(
size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2);
return makeMutableCopy(list);
}

protected static DoubleList emptyDoubleList() {
Expand All @@ -462,10 +460,9 @@ protected static DoubleList newDoubleList() {
return new DoubleArrayList();
}

// TODO(b/258340024): Redundant with makeMutableCopy(). Remove.
protected static DoubleList mutableCopy(DoubleList list) {
int size = list.size();
return list.mutableCopyWithCapacity(
size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2);
return makeMutableCopy(list);
}

protected static BooleanList emptyBooleanList() {
Expand All @@ -477,10 +474,16 @@ protected static BooleanList newBooleanList() {
return new BooleanArrayList();
}

// TODO(b/258340024): Redundant with makeMutableCopy(). Remove.
protected static BooleanList mutableCopy(BooleanList list) {
return makeMutableCopy(list);
}

@SuppressWarnings("unchecked") // Guaranteed by proto runtime.
protected static <ListT extends ProtobufList<?>> ListT makeMutableCopy(ListT list) {
int size = list.size();
return list.mutableCopyWithCapacity(
size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2);
return (ListT)
list.mutableCopyWithCapacity(size == 0 ? AbstractProtobufList.DEFAULT_CAPACITY : size * 2);
}

@Override
Expand Down

0 comments on commit 5669606

Please sign in to comment.