Standardize on Array copyOf

pull/9162/head
David Mollitor 3 years ago
parent b3b3162219
commit 935d099ad9
  1. 13
      java/core/src/main/java/com/google/protobuf/BooleanArrayList.java
  2. 13
      java/core/src/main/java/com/google/protobuf/DoubleArrayList.java
  3. 13
      java/core/src/main/java/com/google/protobuf/FloatArrayList.java
  4. 13
      java/core/src/main/java/com/google/protobuf/IntArrayList.java
  5. 13
      java/core/src/main/java/com/google/protobuf/LongArrayList.java
  6. 4
      java/core/src/main/java/com/google/protobuf/ProtobufArrayList.java

@ -197,10 +197,7 @@ final class BooleanArrayList extends AbstractProtobufList<Boolean>
if (size == array.length) { if (size == array.length) {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
boolean[] newArray = new boolean[length]; array = Arrays.copyOf(array, length);
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
} }
array[size++] = element; array[size++] = element;
@ -219,14 +216,10 @@ final class BooleanArrayList extends AbstractProtobufList<Boolean>
} else { } else {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
boolean[] newArray = new boolean[length]; array = Arrays.copyOf(array, length);
// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);
// Copy the rest shifted over by one to make room // Copy the rest shifted over by one to make room
System.arraycopy(array, index, newArray, index + 1, size - index); System.arraycopy(array, index, array, index + 1, size - index);
array = newArray;
} }
array[index] = element; array[index] = element;

@ -197,10 +197,7 @@ final class DoubleArrayList extends AbstractProtobufList<Double>
if (size == array.length) { if (size == array.length) {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
double[] newArray = new double[length]; array = Arrays.copyOf(array, length);
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
} }
array[size++] = element; array[size++] = element;
@ -219,14 +216,10 @@ final class DoubleArrayList extends AbstractProtobufList<Double>
} else { } else {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
double[] newArray = new double[length]; array = Arrays.copyOf(array, length);
// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);
// Copy the rest shifted over by one to make room // Copy the rest shifted over by one to make room
System.arraycopy(array, index, newArray, index + 1, size - index); System.arraycopy(array, index, array, index + 1, size - index);
array = newArray;
} }
array[index] = element; array[index] = element;

@ -196,10 +196,7 @@ final class FloatArrayList extends AbstractProtobufList<Float>
if (size == array.length) { if (size == array.length) {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
float[] newArray = new float[length]; array = Arrays.copyOf(array, length);
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
} }
array[size++] = element; array[size++] = element;
@ -218,14 +215,10 @@ final class FloatArrayList extends AbstractProtobufList<Float>
} else { } else {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
float[] newArray = new float[length]; array = Arrays.copyOf(array, length);
// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);
// Copy the rest shifted over by one to make room // Copy the rest shifted over by one to make room
System.arraycopy(array, index, newArray, index + 1, size - index); System.arraycopy(array, index, array, index + 1, size - index);
array = newArray;
} }
array[index] = element; array[index] = element;

@ -196,10 +196,7 @@ final class IntArrayList extends AbstractProtobufList<Integer>
if (size == array.length) { if (size == array.length) {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
int[] newArray = new int[length]; array = Arrays.copyOf(array, length);
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
} }
array[size++] = element; array[size++] = element;
@ -218,14 +215,10 @@ final class IntArrayList extends AbstractProtobufList<Integer>
} else { } else {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
int[] newArray = new int[length]; array = Arrays.copyOf(array, length);
// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);
// Copy the rest shifted over by one to make room // Copy the rest shifted over by one to make room
System.arraycopy(array, index, newArray, index + 1, size - index); System.arraycopy(array, index, array, index + 1, size - index);
array = newArray;
} }
array[index] = element; array[index] = element;

@ -196,10 +196,7 @@ final class LongArrayList extends AbstractProtobufList<Long>
if (size == array.length) { if (size == array.length) {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
long[] newArray = new long[length]; array = Arrays.copyOf(array, length);
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
} }
array[size++] = element; array[size++] = element;
@ -218,14 +215,10 @@ final class LongArrayList extends AbstractProtobufList<Long>
} else { } else {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
long[] newArray = new long[length]; array = Arrays.copyOf(array, length);
// Copy the first part directly
System.arraycopy(array, 0, newArray, 0, index);
// Copy the rest shifted over by one to make room // Copy the rest shifted over by one to make room
System.arraycopy(array, index, newArray, index + 1, size - index); System.arraycopy(array, index, array, index + 1, size - index);
array = newArray;
} }
array[index] = element; array[index] = element;

@ -80,9 +80,7 @@ final class ProtobufArrayList<E> extends AbstractProtobufList<E> implements Rand
if (size == array.length) { if (size == array.length) {
// Resize to 1.5x the size // Resize to 1.5x the size
int length = ((size * 3) / 2) + 1; int length = ((size * 3) / 2) + 1;
E[] newArray = Arrays.copyOf(array, length); array = Arrays.copyOf(array, length);
array = newArray;
} }
array[size++] = element; array[size++] = element;

Loading…
Cancel
Save