1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 package com.google.protobuf; 32 33 import com.google.protobuf.Internal.BooleanList; 34 35 import java.util.Arrays; 36 import java.util.Collection; 37 import java.util.RandomAccess; 38 39 /** 40 * An implementation of {@link BooleanList} on top of a primitive array. 41 * 42 * @author dweis@google.com (Daniel Weis) 43 */ 44 final class BooleanArrayList 45 extends AbstractProtobufList<Boolean> implements BooleanList, RandomAccess { 46 47 private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList(); 48 static { EMPTY_LIST.makeImmutable()49 EMPTY_LIST.makeImmutable(); 50 } 51 emptyList()52 public static BooleanArrayList emptyList() { 53 return EMPTY_LIST; 54 } 55 56 /** 57 * The backing store for the list. 58 */ 59 private boolean[] array; 60 61 /** 62 * The size of the list distinct from the length of the array. That is, it is the number of 63 * elements set in the list. 64 */ 65 private int size; 66 67 /** 68 * Constructs a new mutable {@code BooleanArrayList} with default capacity. 69 */ BooleanArrayList()70 BooleanArrayList() { 71 this(new boolean[DEFAULT_CAPACITY], 0); 72 } 73 74 /** 75 * Constructs a new mutable {@code BooleanArrayList}. 76 */ BooleanArrayList(boolean[] array, int size)77 private BooleanArrayList(boolean[] array, int size) { 78 this.array = array; 79 this.size = size; 80 } 81 82 @Override equals(Object o)83 public boolean equals(Object o) { 84 if (this == o) { 85 return true; 86 } 87 if (!(o instanceof BooleanArrayList)) { 88 return super.equals(o); 89 } 90 BooleanArrayList other = (BooleanArrayList) o; 91 if (size != other.size) { 92 return false; 93 } 94 95 final boolean[] arr = other.array; 96 for (int i = 0; i < size; i++) { 97 if (array[i] != arr[i]) { 98 return false; 99 } 100 } 101 102 return true; 103 } 104 105 @Override hashCode()106 public int hashCode() { 107 int result = 1; 108 for (int i = 0; i < size; i++) { 109 result = (31 * result) + Internal.hashBoolean(array[i]); 110 } 111 return result; 112 } 113 114 @Override mutableCopyWithCapacity(int capacity)115 public BooleanList mutableCopyWithCapacity(int capacity) { 116 if (capacity < size) { 117 throw new IllegalArgumentException(); 118 } 119 return new BooleanArrayList(Arrays.copyOf(array, capacity), size); 120 } 121 122 @Override get(int index)123 public Boolean get(int index) { 124 return getBoolean(index); 125 } 126 127 @Override getBoolean(int index)128 public boolean getBoolean(int index) { 129 ensureIndexInRange(index); 130 return array[index]; 131 } 132 133 @Override size()134 public int size() { 135 return size; 136 } 137 138 @Override set(int index, Boolean element)139 public Boolean set(int index, Boolean element) { 140 return setBoolean(index, element); 141 } 142 143 @Override setBoolean(int index, boolean element)144 public boolean setBoolean(int index, boolean element) { 145 ensureIsMutable(); 146 ensureIndexInRange(index); 147 boolean previousValue = array[index]; 148 array[index] = element; 149 return previousValue; 150 } 151 152 @Override add(int index, Boolean element)153 public void add(int index, Boolean element) { 154 addBoolean(index, element); 155 } 156 157 /** 158 * Like {@link #add(Boolean)} but more efficient in that it doesn't box the element. 159 */ 160 @Override addBoolean(boolean element)161 public void addBoolean(boolean element) { 162 addBoolean(size, element); 163 } 164 165 /** 166 * Like {@link #add(int, Boolean)} but more efficient in that it doesn't box the element. 167 */ addBoolean(int index, boolean element)168 private void addBoolean(int index, boolean element) { 169 ensureIsMutable(); 170 if (index < 0 || index > size) { 171 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)); 172 } 173 174 if (size < array.length) { 175 // Shift everything over to make room 176 System.arraycopy(array, index, array, index + 1, size - index); 177 } else { 178 // Resize to 1.5x the size 179 int length = ((size * 3) / 2) + 1; 180 boolean[] newArray = new boolean[length]; 181 182 // Copy the first part directly 183 System.arraycopy(array, 0, newArray, 0, index); 184 185 // Copy the rest shifted over by one to make room 186 System.arraycopy(array, index, newArray, index + 1, size - index); 187 array = newArray; 188 } 189 190 array[index] = element; 191 size++; 192 modCount++; 193 } 194 195 @Override addAll(Collection<? extends Boolean> collection)196 public boolean addAll(Collection<? extends Boolean> collection) { 197 ensureIsMutable(); 198 199 if (collection == null) { 200 throw new NullPointerException(); 201 } 202 203 // We specialize when adding another BooleanArrayList to avoid boxing elements. 204 if (!(collection instanceof BooleanArrayList)) { 205 return super.addAll(collection); 206 } 207 208 BooleanArrayList list = (BooleanArrayList) collection; 209 if (list.size == 0) { 210 return false; 211 } 212 213 int overflow = Integer.MAX_VALUE - size; 214 if (overflow < list.size) { 215 // We can't actually represent a list this large. 216 throw new OutOfMemoryError(); 217 } 218 219 int newSize = size + list.size; 220 if (newSize > array.length) { 221 array = Arrays.copyOf(array, newSize); 222 } 223 224 System.arraycopy(list.array, 0, array, size, list.size); 225 size = newSize; 226 modCount++; 227 return true; 228 } 229 230 @Override remove(Object o)231 public boolean remove(Object o) { 232 ensureIsMutable(); 233 for (int i = 0; i < size; i++) { 234 if (o.equals(array[i])) { 235 System.arraycopy(array, i + 1, array, i, size - i); 236 size--; 237 modCount++; 238 return true; 239 } 240 } 241 return false; 242 } 243 244 @Override remove(int index)245 public Boolean remove(int index) { 246 ensureIsMutable(); 247 ensureIndexInRange(index); 248 boolean value = array[index]; 249 System.arraycopy(array, index + 1, array, index, size - index); 250 size--; 251 modCount++; 252 return value; 253 } 254 255 /** 256 * Ensures that the provided {@code index} is within the range of {@code [0, size]}. Throws an 257 * {@link IndexOutOfBoundsException} if it is not. 258 * 259 * @param index the index to verify is in range 260 */ ensureIndexInRange(int index)261 private void ensureIndexInRange(int index) { 262 if (index < 0 || index >= size) { 263 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)); 264 } 265 } 266 makeOutOfBoundsExceptionMessage(int index)267 private String makeOutOfBoundsExceptionMessage(int index) { 268 return "Index:" + index + ", Size:" + size; 269 } 270 } 271