package com.google.protobuf;
import static com.google.protobuf.Internal.checkNotNull;
import com.google.protobuf.Internal.DoubleList;
import java.util.Arrays;
import java.util.Collection;
import java.util.RandomAccess;
final class DoubleArrayList extends AbstractProtobufList<Double>
implements DoubleList, RandomAccess, PrimitiveNonBoxingCollection {
private static final DoubleArrayList EMPTY_LIST = new DoubleArrayList(new double[0], 0);
static {
EMPTY_LIST.makeImmutable();
}
public static DoubleArrayList emptyList() {
return EMPTY_LIST;
}
private double[] array;
private int size;
DoubleArrayList() {
this(new double[DEFAULT_CAPACITY], 0);
}
private DoubleArrayList(double[] other, int size) {
array = other;
this.size = size;
}
@Override
protected void removeRange(int fromIndex, int toIndex) {
ensureIsMutable();
if (toIndex < fromIndex) {
throw new IndexOutOfBoundsException("toIndex < fromIndex");
}
System.arraycopy(array, toIndex, array, fromIndex, size - toIndex);
size -= (toIndex - fromIndex);
modCount++;
}
@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 (Double.doubleToLongBits(array[i]) != Double.doubleToLongBits(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
public Double get(int index) {
return getDouble(index);
}
@Override
public double getDouble(int index) {
ensureIndexInRange(index);
return array[index];
}
@Override
public int size() {
return size;
}
@Override
public Double set(int index, Double element) {
return setDouble(index, element);
}
@Override
public double setDouble(int index, double element) {
ensureIsMutable();
ensureIndexInRange(index);
double previousValue = array[index];
array[index] = element;
return previousValue;
}
@Override
public boolean add(Double element) {
addDouble(element);
return true;
}
@Override
public void add(int index, Double element) {
addDouble(index, element);
}
@Override
public void addDouble(double element) {
ensureIsMutable();
if (size == array.length) {
int length = ((size * 3) / 2) + 1;
double[] newArray = new double[length];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}
array[size++] = element;
}
private void addDouble(int index, double element) {
ensureIsMutable();
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
}
if (size < array.length) {
System.arraycopy(array, index, array, index + 1, size - index);
} else {
int length = ((size * 3) / 2) + 1;
double[] newArray = new double[length];
System.arraycopy(array, 0, newArray, 0, index);
System.arraycopy(array, index, newArray, index + 1, size - index);
array = newArray;
}
array[index] = element;
size++;
modCount++;
}
@Override
public boolean addAll(Collection<? extends Double> collection) {
ensureIsMutable();
checkNotNull(collection);
if (!(collection instanceof DoubleArrayList)) {
return super.addAll(collection);
}
DoubleArrayList list = (DoubleArrayList) collection;
if (list.size == 0) {
return false;
}
int overflow = Integer.MAX_VALUE - size;
if (overflow < list.size) {
throw new OutOfMemoryError();
}
int newSize = size + list.size;
if (newSize > array.length) {
array = Arrays.copyOf(array, newSize);
}
System.arraycopy(list.array, 0, array, size, list.size);
size = newSize;
modCount++;
return true;
}
@Override
public boolean remove(Object o) {
ensureIsMutable();
for (int i = 0; i < size; i++) {
if (o.equals(array[i])) {
System.arraycopy(array, i + 1, array, i, size - i - 1);
size--;
modCount++;
return true;
}
}
return false;
}
@Override
public Double remove(int index) {
ensureIsMutable();
ensureIndexInRange(index);
double value = array[index];
if (index < size - 1) {
System.arraycopy(array, index + 1, array, index, size - index - 1);
}
size--;
modCount++;
return value;
}
private void ensureIndexInRange(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
}
}
private String makeOutOfBoundsExceptionMessage(int index) {
return "Index:" + index + ", Size:" + size;
}
}