31 package com.google.protobuf;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.RandomAccess;
45 final class LongArrayList
extends AbstractProtobufList<Long>
46 implements LongList, RandomAccess, PrimitiveNonBoxingCollection {
48 private static final LongArrayList EMPTY_LIST =
new LongArrayList(
new long[0], 0);
51 EMPTY_LIST.makeImmutable();
54 public static LongArrayList emptyList() {
69 this(
new long[DEFAULT_CAPACITY], 0);
75 private LongArrayList(
long[] other,
int size) {
81 protected void removeRange(
int fromIndex,
int toIndex) {
83 if (toIndex < fromIndex) {
84 throw new IndexOutOfBoundsException(
"toIndex < fromIndex");
87 System.arraycopy(
array, toIndex,
array, fromIndex,
size - toIndex);
88 size -= (toIndex - fromIndex);
93 public boolean equals(Object o) {
97 if (!(o instanceof LongArrayList)) {
98 return super.equals(o);
100 LongArrayList other = (LongArrayList) o;
101 if (
size != other.size) {
105 final long[] arr = other.array;
106 for (
int i = 0;
i <
size;
i++) {
116 public int hashCode() {
118 for (
int i = 0;
i <
size;
i++) {
119 result = (31 * result) + Internal.hashLong(
array[
i]);
125 public LongList mutableCopyWithCapacity(
int capacity) {
126 if (capacity <
size) {
127 throw new IllegalArgumentException();
129 return new LongArrayList(Arrays.copyOf(
array, capacity),
size);
133 public Long
get(
int index) {
134 return getLong(
index);
138 public long getLong(
int index) {
139 ensureIndexInRange(
index);
149 public Long
set(
int index, Long element) {
150 return setLong(
index, element);
154 public long setLong(
int index,
long element) {
156 ensureIndexInRange(
index);
159 return previousValue;
163 public void add(
int index, Long element) {
164 addLong(
index, element);
169 public void addLong(
long element) {
170 addLong(
size, element);
174 private void addLong(
int index,
long element) {
177 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(
index));
186 long[] newArray =
new long[
length];
189 System.arraycopy(
array, 0, newArray, 0,
index);
202 public boolean addAll(Collection<? extends Long> collection) {
205 checkNotNull(collection);
208 if (!(collection instanceof LongArrayList)) {
209 return super.addAll(collection);
212 LongArrayList list = (LongArrayList) collection;
213 if (list.size == 0) {
217 int overflow = Integer.MAX_VALUE -
size;
218 if (overflow < list.size) {
220 throw new OutOfMemoryError();
223 int newSize =
size + list.size;
224 if (newSize >
array.length) {
228 System.arraycopy(list.array, 0,
array,
size, list.size);
235 public boolean remove(Object o) {
237 for (
int i = 0;
i <
size;
i++) {
249 public Long
remove(
int index) {
251 ensureIndexInRange(
index);
267 private void ensureIndexInRange(
int index) {
269 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(
index));
273 private String makeOutOfBoundsExceptionMessage(
int index) {
274 return "Index:" +
index +
", Size:" +
size;