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 static com.google.protobuf.Internal.checkNotNull; 34 35 import com.google.protobuf.Internal.LongList; 36 import java.util.Arrays; 37 import java.util.Collection; 38 import java.util.RandomAccess; 39 40 /** 41 * An implementation of {@link LongList} on top of a primitive array. 42 * 43 * @author dweis@google.com (Daniel Weis) 44 */ 45 final class LongArrayList extends AbstractProtobufList<Long> 46 implements LongList, RandomAccess, PrimitiveNonBoxingCollection { 47 48 private static final LongArrayList EMPTY_LIST = new LongArrayList(new long[0], 0); 49 50 static { EMPTY_LIST.makeImmutable()51 EMPTY_LIST.makeImmutable(); 52 } 53 emptyList()54 public static LongArrayList emptyList() { 55 return EMPTY_LIST; 56 } 57 58 /** The backing store for the list. */ 59 private long[] 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 /** Constructs a new mutable {@code LongArrayList} with default capacity. */ LongArrayList()68 LongArrayList() { 69 this(new long[DEFAULT_CAPACITY], 0); 70 } 71 72 /** 73 * Constructs a new mutable {@code LongArrayList} containing the same elements as {@code other}. 74 */ LongArrayList(long[] other, int size)75 private LongArrayList(long[] other, int size) { 76 array = other; 77 this.size = size; 78 } 79 80 @Override removeRange(int fromIndex, int toIndex)81 protected void removeRange(int fromIndex, int toIndex) { 82 ensureIsMutable(); 83 if (toIndex < fromIndex) { 84 throw new IndexOutOfBoundsException("toIndex < fromIndex"); 85 } 86 87 System.arraycopy(array, toIndex, array, fromIndex, size - toIndex); 88 size -= (toIndex - fromIndex); 89 modCount++; 90 } 91 92 @Override equals(Object o)93 public boolean equals(Object o) { 94 if (this == o) { 95 return true; 96 } 97 if (!(o instanceof LongArrayList)) { 98 return super.equals(o); 99 } 100 LongArrayList other = (LongArrayList) o; 101 if (size != other.size) { 102 return false; 103 } 104 105 final long[] arr = other.array; 106 for (int i = 0; i < size; i++) { 107 if (array[i] != arr[i]) { 108 return false; 109 } 110 } 111 112 return true; 113 } 114 115 @Override hashCode()116 public int hashCode() { 117 int result = 1; 118 for (int i = 0; i < size; i++) { 119 result = (31 * result) + Internal.hashLong(array[i]); 120 } 121 return result; 122 } 123 124 @Override mutableCopyWithCapacity(int capacity)125 public LongList mutableCopyWithCapacity(int capacity) { 126 if (capacity < size) { 127 throw new IllegalArgumentException(); 128 } 129 return new LongArrayList(Arrays.copyOf(array, capacity), size); 130 } 131 132 @Override get(int index)133 public Long get(int index) { 134 return getLong(index); 135 } 136 137 @Override getLong(int index)138 public long getLong(int index) { 139 ensureIndexInRange(index); 140 return array[index]; 141 } 142 143 @Override size()144 public int size() { 145 return size; 146 } 147 148 @Override set(int index, Long element)149 public Long set(int index, Long element) { 150 return setLong(index, element); 151 } 152 153 @Override setLong(int index, long element)154 public long setLong(int index, long element) { 155 ensureIsMutable(); 156 ensureIndexInRange(index); 157 long previousValue = array[index]; 158 array[index] = element; 159 return previousValue; 160 } 161 162 @Override add(int index, Long element)163 public void add(int index, Long element) { 164 addLong(index, element); 165 } 166 167 /** Like {@link #add(Long)} but more efficient in that it doesn't box the element. */ 168 @Override addLong(long element)169 public void addLong(long element) { 170 addLong(size, element); 171 } 172 173 /** Like {@link #add(int, Long)} but more efficient in that it doesn't box the element. */ addLong(int index, long element)174 private void addLong(int index, long element) { 175 ensureIsMutable(); 176 if (index < 0 || index > size) { 177 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)); 178 } 179 180 if (size < array.length) { 181 // Shift everything over to make room 182 System.arraycopy(array, index, array, index + 1, size - index); 183 } else { 184 // Resize to 1.5x the size 185 int length = ((size * 3) / 2) + 1; 186 long[] newArray = new long[length]; 187 188 // Copy the first part directly 189 System.arraycopy(array, 0, newArray, 0, index); 190 191 // Copy the rest shifted over by one to make room 192 System.arraycopy(array, index, newArray, index + 1, size - index); 193 array = newArray; 194 } 195 196 array[index] = element; 197 size++; 198 modCount++; 199 } 200 201 @Override addAll(Collection<? extends Long> collection)202 public boolean addAll(Collection<? extends Long> collection) { 203 ensureIsMutable(); 204 205 checkNotNull(collection); 206 207 // We specialize when adding another LongArrayList to avoid boxing elements. 208 if (!(collection instanceof LongArrayList)) { 209 return super.addAll(collection); 210 } 211 212 LongArrayList list = (LongArrayList) collection; 213 if (list.size == 0) { 214 return false; 215 } 216 217 int overflow = Integer.MAX_VALUE - size; 218 if (overflow < list.size) { 219 // We can't actually represent a list this large. 220 throw new OutOfMemoryError(); 221 } 222 223 int newSize = size + list.size; 224 if (newSize > array.length) { 225 array = Arrays.copyOf(array, newSize); 226 } 227 228 System.arraycopy(list.array, 0, array, size, list.size); 229 size = newSize; 230 modCount++; 231 return true; 232 } 233 234 @Override remove(Object o)235 public boolean remove(Object o) { 236 ensureIsMutable(); 237 for (int i = 0; i < size; i++) { 238 if (o.equals(array[i])) { 239 System.arraycopy(array, i + 1, array, i, size - i - 1); 240 size--; 241 modCount++; 242 return true; 243 } 244 } 245 return false; 246 } 247 248 @Override remove(int index)249 public Long remove(int index) { 250 ensureIsMutable(); 251 ensureIndexInRange(index); 252 long value = array[index]; 253 if (index < size - 1) { 254 System.arraycopy(array, index + 1, array, index, size - index - 1); 255 } 256 size--; 257 modCount++; 258 return value; 259 } 260 261 /** 262 * Ensures that the provided {@code index} is within the range of {@code [0, size]}. Throws an 263 * {@link IndexOutOfBoundsException} if it is not. 264 * 265 * @param index the index to verify is in range 266 */ ensureIndexInRange(int index)267 private void ensureIndexInRange(int index) { 268 if (index < 0 || index >= size) { 269 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)); 270 } 271 } 272 makeOutOfBoundsExceptionMessage(int index)273 private String makeOutOfBoundsExceptionMessage(int index) { 274 return "Index:" + index + ", Size:" + size; 275 } 276 } 277