Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(474)

Side by Side Diff: third_party/protobuf/java/core/src/main/java/com/google/protobuf/IntArrayList.java

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 20 matching lines...) Expand all
31 package com.google.protobuf; 31 package com.google.protobuf;
32 32
33 import com.google.protobuf.Internal.IntList; 33 import com.google.protobuf.Internal.IntList;
34 34
35 import java.util.Arrays; 35 import java.util.Arrays;
36 import java.util.Collection; 36 import java.util.Collection;
37 import java.util.RandomAccess; 37 import java.util.RandomAccess;
38 38
39 /** 39 /**
40 * An implementation of {@link IntList} on top of a primitive array. 40 * An implementation of {@link IntList} on top of a primitive array.
41 * 41 *
42 * @author dweis@google.com (Daniel Weis) 42 * @author dweis@google.com (Daniel Weis)
43 */ 43 */
44 final class IntArrayList extends AbstractProtobufList<Integer> implements IntLis t, RandomAccess { 44 final class IntArrayList
45 45 extends AbstractProtobufList<Integer>
46 implements IntList, RandomAccess {
47
46 private static final IntArrayList EMPTY_LIST = new IntArrayList(); 48 private static final IntArrayList EMPTY_LIST = new IntArrayList();
47 static { 49 static {
48 EMPTY_LIST.makeImmutable(); 50 EMPTY_LIST.makeImmutable();
49 } 51 }
50 52
51 public static IntArrayList emptyList() { 53 public static IntArrayList emptyList() {
52 return EMPTY_LIST; 54 return EMPTY_LIST;
53 } 55 }
54 56
55 /** 57 /**
56 * The backing store for the list. 58 * The backing store for the list.
57 */ 59 */
58 private int[] array; 60 private int[] array;
59 61
60 /** 62 /**
61 * The size of the list distinct from the length of the array. That is, it is the number of 63 * The size of the list distinct from the length of the array. That is, it is the number of
62 * elements set in the list. 64 * elements set in the list.
63 */ 65 */
64 private int size; 66 private int size;
65 67
66 /** 68 /**
67 * Constructs a new mutable {@code IntArrayList} with default capacity. 69 * Constructs a new mutable {@code IntArrayList} with default capacity.
68 */ 70 */
69 IntArrayList() { 71 IntArrayList() {
70 this(new int[DEFAULT_CAPACITY], 0); 72 this(new int[DEFAULT_CAPACITY], 0);
71 } 73 }
72 74
73 /** 75 /**
74 * Constructs a new mutable {@code IntArrayList} containing the same elements as {@code other}. 76 * Constructs a new mutable {@code IntArrayList}
77 * containing the same elements as {@code other}.
75 */ 78 */
76 private IntArrayList(int[] array, int size) { 79 private IntArrayList(int[] other, int size) {
77 this.array = array; 80 array = other;
78 this.size = size; 81 this.size = size;
79 } 82 }
80 83
81 @Override 84 @Override
82 public boolean equals(Object o) { 85 public boolean equals(Object o) {
83 if (this == o) { 86 if (this == o) {
84 return true; 87 return true;
85 } 88 }
86 if (!(o instanceof IntArrayList)) { 89 if (!(o instanceof IntArrayList)) {
87 return super.equals(o); 90 return super.equals(o);
88 } 91 }
89 IntArrayList other = (IntArrayList) o; 92 IntArrayList other = (IntArrayList) o;
90 if (size != other.size) { 93 if (size != other.size) {
91 return false; 94 return false;
92 } 95 }
93 96
94 final int[] arr = other.array; 97 final int[] arr = other.array;
95 for (int i = 0; i < size; i++) { 98 for (int i = 0; i < size; i++) {
96 if (array[i] != arr[i]) { 99 if (array[i] != arr[i]) {
97 return false; 100 return false;
98 } 101 }
99 } 102 }
100 103
101 return true; 104 return true;
102 } 105 }
103 106
104 @Override 107 @Override
105 public int hashCode() { 108 public int hashCode() {
106 int result = 1; 109 int result = 1;
107 for (int i = 0; i < size; i++) { 110 for (int i = 0; i < size; i++) {
108 result = (31 * result) + array[i]; 111 result = (31 * result) + array[i];
109 } 112 }
110 return result; 113 return result;
111 } 114 }
112 115
113 @Override 116 @Override
114 public IntList mutableCopyWithCapacity(int capacity) { 117 public IntList mutableCopyWithCapacity(int capacity) {
115 if (capacity < size) { 118 if (capacity < size) {
116 throw new IllegalArgumentException(); 119 throw new IllegalArgumentException();
117 } 120 }
118 return new IntArrayList(Arrays.copyOf(array, capacity), size); 121 return new IntArrayList(Arrays.copyOf(array, capacity), size);
119 } 122 }
120 123
121 @Override 124 @Override
122 public Integer get(int index) { 125 public Integer get(int index) {
123 return getInt(index); 126 return getInt(index);
124 } 127 }
125 128
126 @Override 129 @Override
127 public int getInt(int index) { 130 public int getInt(int index) {
128 ensureIndexInRange(index); 131 ensureIndexInRange(index);
129 return array[index]; 132 return array[index];
130 } 133 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 } 165 }
163 166
164 /** 167 /**
165 * Like {@link #add(int, Integer)} but more efficient in that it doesn't box t he element. 168 * Like {@link #add(int, Integer)} but more efficient in that it doesn't box t he element.
166 */ 169 */
167 private void addInt(int index, int element) { 170 private void addInt(int index, int element) {
168 ensureIsMutable(); 171 ensureIsMutable();
169 if (index < 0 || index > size) { 172 if (index < 0 || index > size) {
170 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index) ); 173 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index) );
171 } 174 }
172 175
173 if (size < array.length) { 176 if (size < array.length) {
174 // Shift everything over to make room 177 // Shift everything over to make room
175 System.arraycopy(array, index, array, index + 1, size - index); 178 System.arraycopy(array, index, array, index + 1, size - index);
176 } else { 179 } else {
177 // Resize to 1.5x the size 180 // Resize to 1.5x the size
178 int length = ((size * 3) / 2) + 1; 181 int length = ((size * 3) / 2) + 1;
179 int[] newArray = new int[length]; 182 int[] newArray = new int[length];
180 183
181 // Copy the first part directly 184 // Copy the first part directly
182 System.arraycopy(array, 0, newArray, 0, index); 185 System.arraycopy(array, 0, newArray, 0, index);
183 186
184 // Copy the rest shifted over by one to make room 187 // Copy the rest shifted over by one to make room
185 System.arraycopy(array, index, newArray, index + 1, size - index); 188 System.arraycopy(array, index, newArray, index + 1, size - index);
186 array = newArray; 189 array = newArray;
187 } 190 }
188 191
189 array[index] = element; 192 array[index] = element;
190 size++; 193 size++;
191 modCount++; 194 modCount++;
192 } 195 }
193 196
194 @Override 197 @Override
195 public boolean addAll(Collection<? extends Integer> collection) { 198 public boolean addAll(Collection<? extends Integer> collection) {
196 ensureIsMutable(); 199 ensureIsMutable();
197 200
198 if (collection == null) { 201 if (collection == null) {
199 throw new NullPointerException(); 202 throw new NullPointerException();
200 } 203 }
201 204
202 // We specialize when adding another IntArrayList to avoid boxing elements. 205 // We specialize when adding another IntArrayList to avoid boxing elements.
203 if (!(collection instanceof IntArrayList)) { 206 if (!(collection instanceof IntArrayList)) {
204 return super.addAll(collection); 207 return super.addAll(collection);
205 } 208 }
206 209
207 IntArrayList list = (IntArrayList) collection; 210 IntArrayList list = (IntArrayList) collection;
208 if (list.size == 0) { 211 if (list.size == 0) {
209 return false; 212 return false;
210 } 213 }
211 214
212 int overflow = Integer.MAX_VALUE - size; 215 int overflow = Integer.MAX_VALUE - size;
213 if (overflow < list.size) { 216 if (overflow < list.size) {
214 // We can't actually represent a list this large. 217 // We can't actually represent a list this large.
215 throw new OutOfMemoryError(); 218 throw new OutOfMemoryError();
216 } 219 }
217 220
218 int newSize = size + list.size; 221 int newSize = size + list.size;
219 if (newSize > array.length) { 222 if (newSize > array.length) {
220 array = Arrays.copyOf(array, newSize); 223 array = Arrays.copyOf(array, newSize);
221 } 224 }
222 225
223 System.arraycopy(list.array, 0, array, size, list.size); 226 System.arraycopy(list.array, 0, array, size, list.size);
224 size = newSize; 227 size = newSize;
225 modCount++; 228 modCount++;
226 return true; 229 return true;
227 } 230 }
228 231
229 @Override 232 @Override
230 public boolean remove(Object o) { 233 public boolean remove(Object o) {
231 ensureIsMutable(); 234 ensureIsMutable();
232 for (int i = 0; i < size; i++) { 235 for (int i = 0; i < size; i++) {
233 if (o.equals(array[i])) { 236 if (o.equals(array[i])) {
234 System.arraycopy(array, i + 1, array, i, size - i); 237 System.arraycopy(array, i + 1, array, i, size - i);
235 size--; 238 size--;
236 modCount++; 239 modCount++;
237 return true; 240 return true;
238 } 241 }
239 } 242 }
240 return false; 243 return false;
241 } 244 }
242 245
243 @Override 246 @Override
244 public Integer remove(int index) { 247 public Integer remove(int index) {
245 ensureIsMutable(); 248 ensureIsMutable();
246 ensureIndexInRange(index); 249 ensureIndexInRange(index);
247 int value = array[index]; 250 int value = array[index];
248 System.arraycopy(array, index + 1, array, index, size - index); 251 System.arraycopy(array, index + 1, array, index, size - index);
249 size--; 252 size--;
250 modCount++; 253 modCount++;
251 return value; 254 return value;
252 } 255 }
253 256
254 /** 257 /**
255 * Ensures that the provided {@code index} is within the range of {@code [0, s ize]}. Throws an 258 * Ensures that the provided {@code index} is within the range of {@code [0, s ize]}. Throws an
256 * {@link IndexOutOfBoundsException} if it is not. 259 * {@link IndexOutOfBoundsException} if it is not.
257 * 260 *
258 * @param index the index to verify is in range 261 * @param index the index to verify is in range
259 */ 262 */
260 private void ensureIndexInRange(int index) { 263 private void ensureIndexInRange(int index) {
261 if (index < 0 || index >= size) { 264 if (index < 0 || index >= size) {
262 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index) ); 265 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index) );
263 } 266 }
264 } 267 }
265 268
266 private String makeOutOfBoundsExceptionMessage(int index) { 269 private String makeOutOfBoundsExceptionMessage(int index) {
267 return "Index:" + index + ", Size:" + size; 270 return "Index:" + index + ", Size:" + size;
268 } 271 }
269 } 272 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698