diff options
Diffstat (limited to 'java/core/src/main/java/com/google/protobuf/BooleanArrayList.java')
-rw-r--r-- | java/core/src/main/java/com/google/protobuf/BooleanArrayList.java | 125 |
1 files changed, 79 insertions, 46 deletions
diff --git a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java index 70e042f5..4d7a9727 100644 --- a/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java +++ b/java/core/src/main/java/com/google/protobuf/BooleanArrayList.java @@ -30,37 +30,35 @@ package com.google.protobuf; -import com.google.protobuf.Internal.BooleanList; +import static com.google.protobuf.Internal.checkNotNull; +import com.google.protobuf.Internal.BooleanList; import java.util.Arrays; import java.util.Collection; -import java.util.List; import java.util.RandomAccess; /** * An implementation of {@link BooleanList} on top of a primitive array. - * + * * @author dweis@google.com (Daniel Weis) */ -final class BooleanArrayList - extends AbstractProtobufList<Boolean> implements BooleanList, RandomAccess { - - private static final int DEFAULT_CAPACITY = 10; - +final class BooleanArrayList extends AbstractProtobufList<Boolean> + implements BooleanList, RandomAccess, PrimitiveNonBoxingCollection { + private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList(); static { EMPTY_LIST.makeImmutable(); } - + public static BooleanArrayList emptyList() { return EMPTY_LIST; } - + /** * The backing store for the list. */ private boolean[] array; - + /** * The size of the list distinct from the length of the array. That is, it is the number of * elements set in the list. @@ -71,35 +69,70 @@ final class BooleanArrayList * Constructs a new mutable {@code BooleanArrayList} with default capacity. */ BooleanArrayList() { - this(DEFAULT_CAPACITY); + this(new boolean[DEFAULT_CAPACITY], 0); } /** - * Constructs a new mutable {@code BooleanArrayList} with the provided capacity. + * Constructs a new mutable {@code BooleanArrayList} + * containing the same elements as {@code other}. */ - BooleanArrayList(int capacity) { - array = new boolean[capacity]; - size = 0; + private BooleanArrayList(boolean[] other, int size) { + array = other; + this.size = size; } - /** - * Constructs a new mutable {@code BooleanArrayList} containing the same elements as - * {@code other}. - */ - BooleanArrayList(List<Boolean> other) { - if (other instanceof BooleanArrayList) { - BooleanArrayList list = (BooleanArrayList) other; - array = list.array.clone(); - size = list.size; - } else { - size = other.size(); - array = new boolean[size]; - for (int i = 0; i < size; i++) { - array[i] = other.get(i); + @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 BooleanArrayList)) { + return super.equals(o); + } + BooleanArrayList other = (BooleanArrayList) o; + if (size != other.size) { + return false; + } + + final boolean[] arr = other.array; + for (int i = 0; i < size; i++) { + if (array[i] != arr[i]) { + return false; } } + + return true; + } + + @Override + public int hashCode() { + int result = 1; + for (int i = 0; i < size; i++) { + result = (31 * result) + Internal.hashBoolean(array[i]); + } + return result; } - + + @Override + public BooleanList mutableCopyWithCapacity(int capacity) { + if (capacity < size) { + throw new IllegalArgumentException(); + } + return new BooleanArrayList(Arrays.copyOf(array, capacity), size); + } + @Override public Boolean get(int index) { return getBoolean(index); @@ -151,7 +184,7 @@ final class BooleanArrayList if (index < 0 || index > size) { throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)); } - + if (size < array.length) { // Shift everything over to make room System.arraycopy(array, index, array, index + 1, size - index); @@ -159,10 +192,10 @@ final class BooleanArrayList // Resize to 1.5x the size int length = ((size * 3) / 2) + 1; boolean[] newArray = new boolean[length]; - + // Copy the first part directly System.arraycopy(array, 0, newArray, 0, index); - + // Copy the rest shifted over by one to make room System.arraycopy(array, index, newArray, index + 1, size - index); array = newArray; @@ -176,38 +209,36 @@ final class BooleanArrayList @Override public boolean addAll(Collection<? extends Boolean> collection) { ensureIsMutable(); - - if (collection == null) { - throw new NullPointerException(); - } - + + checkNotNull(collection); + // We specialize when adding another BooleanArrayList to avoid boxing elements. if (!(collection instanceof BooleanArrayList)) { return super.addAll(collection); } - + BooleanArrayList list = (BooleanArrayList) collection; if (list.size == 0) { return false; } - + int overflow = Integer.MAX_VALUE - size; if (overflow < list.size) { // We can't actually represent a list this large. 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(); @@ -227,7 +258,9 @@ final class BooleanArrayList ensureIsMutable(); ensureIndexInRange(index); boolean value = array[index]; - System.arraycopy(array, index + 1, array, index, size - index); + if (index < size - 1) { + System.arraycopy(array, index + 1, array, index, size - index); + } size--; modCount++; return value; @@ -236,7 +269,7 @@ final class BooleanArrayList /** * Ensures that the provided {@code index} is within the range of {@code [0, size]}. Throws an * {@link IndexOutOfBoundsException} if it is not. - * + * * @param index the index to verify is in range */ private void ensureIndexInRange(int index) { |