31 package com.google.protobuf;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.RandomAccess;
45 final class BooleanArrayList
extends AbstractProtobufList<Boolean>
46 implements BooleanList, RandomAccess, PrimitiveNonBoxingCollection {
48 private static final BooleanArrayList EMPTY_LIST =
new BooleanArrayList(
new boolean[0], 0);
51 EMPTY_LIST.makeImmutable();
54 public static BooleanArrayList emptyList() {
59 private boolean[]
array;
69 this(
new boolean[DEFAULT_CAPACITY], 0);
76 private BooleanArrayList(
boolean[] other,
int size) {
82 protected void removeRange(
int fromIndex,
int toIndex) {
84 if (toIndex < fromIndex) {
85 throw new IndexOutOfBoundsException(
"toIndex < fromIndex");
88 System.arraycopy(
array, toIndex,
array, fromIndex,
size - toIndex);
89 size -= (toIndex - fromIndex);
94 public boolean equals(Object o) {
98 if (!(o instanceof BooleanArrayList)) {
99 return super.equals(o);
101 BooleanArrayList other = (BooleanArrayList) o;
102 if (
size != other.size) {
106 final boolean[] arr = other.array;
107 for (
int i = 0;
i <
size;
i++) {
117 public int hashCode() {
119 for (
int i = 0;
i <
size;
i++) {
120 result = (31 * result) + Internal.hashBoolean(
array[
i]);
126 public BooleanList mutableCopyWithCapacity(
int capacity) {
127 if (capacity <
size) {
128 throw new IllegalArgumentException();
130 return new BooleanArrayList(Arrays.copyOf(
array, capacity),
size);
134 public Boolean
get(
int index) {
135 return getBoolean(
index);
139 public boolean getBoolean(
int index) {
140 ensureIndexInRange(
index);
150 public Boolean
set(
int index, Boolean element) {
151 return setBoolean(
index, element);
155 public boolean setBoolean(
int index,
boolean element) {
157 ensureIndexInRange(
index);
160 return previousValue;
164 public void add(
int index, Boolean element) {
165 addBoolean(
index, element);
170 public void addBoolean(
boolean element) {
171 addBoolean(
size, element);
175 private void addBoolean(
int index,
boolean element) {
178 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(
index));
187 boolean[] newArray =
new boolean[
length];
190 System.arraycopy(
array, 0, newArray, 0,
index);
203 public boolean addAll(Collection<? extends Boolean> collection) {
206 checkNotNull(collection);
209 if (!(collection instanceof BooleanArrayList)) {
210 return super.addAll(collection);
213 BooleanArrayList list = (BooleanArrayList) collection;
214 if (list.size == 0) {
218 int overflow = Integer.MAX_VALUE -
size;
219 if (overflow < list.size) {
221 throw new OutOfMemoryError();
224 int newSize =
size + list.size;
225 if (newSize >
array.length) {
229 System.arraycopy(list.array, 0,
array,
size, list.size);
236 public boolean remove(Object o) {
238 for (
int i = 0;
i <
size;
i++) {
250 public Boolean
remove(
int index) {
252 ensureIndexInRange(
index);
268 private void ensureIndexInRange(
int index) {
270 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(
index));
274 private String makeOutOfBoundsExceptionMessage(
int index) {
275 return "Index:" +
index +
", Size:" +
size;