OLD | NEW |
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 Loading... |
31 package com.google.protobuf; | 31 package com.google.protobuf; |
32 | 32 |
33 import com.google.protobuf.Internal.BooleanList; | 33 import com.google.protobuf.Internal.BooleanList; |
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 BooleanList} on top of a primitive array. | 40 * An implementation of {@link BooleanList} 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 BooleanArrayList | 44 final class BooleanArrayList |
45 extends AbstractProtobufList<Boolean> implements BooleanList, RandomAccess { | 45 extends AbstractProtobufList<Boolean> |
46 | 46 implements BooleanList, RandomAccess { |
| 47 |
47 private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList(); | 48 private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList(); |
48 static { | 49 static { |
49 EMPTY_LIST.makeImmutable(); | 50 EMPTY_LIST.makeImmutable(); |
50 } | 51 } |
51 | 52 |
52 public static BooleanArrayList emptyList() { | 53 public static BooleanArrayList emptyList() { |
53 return EMPTY_LIST; | 54 return EMPTY_LIST; |
54 } | 55 } |
55 | 56 |
56 /** | 57 /** |
57 * The backing store for the list. | 58 * The backing store for the list. |
58 */ | 59 */ |
59 private boolean[] array; | 60 private boolean[] array; |
60 | 61 |
61 /** | 62 /** |
62 * 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 |
63 * elements set in the list. | 64 * elements set in the list. |
64 */ | 65 */ |
65 private int size; | 66 private int size; |
66 | 67 |
67 /** | 68 /** |
68 * Constructs a new mutable {@code BooleanArrayList} with default capacity. | 69 * Constructs a new mutable {@code BooleanArrayList} with default capacity. |
69 */ | 70 */ |
70 BooleanArrayList() { | 71 BooleanArrayList() { |
71 this(new boolean[DEFAULT_CAPACITY], 0); | 72 this(new boolean[DEFAULT_CAPACITY], 0); |
72 } | 73 } |
73 | 74 |
74 /** | 75 /** |
75 * Constructs a new mutable {@code BooleanArrayList}. | 76 * Constructs a new mutable {@code BooleanArrayList} |
| 77 * containing the same elements as {@code other}. |
76 */ | 78 */ |
77 private BooleanArrayList(boolean[] array, int size) { | 79 private BooleanArrayList(boolean[] other, int size) { |
78 this.array = array; | 80 array = other; |
79 this.size = size; | 81 this.size = size; |
80 } | 82 } |
81 | 83 |
82 @Override | 84 @Override |
83 public boolean equals(Object o) { | 85 public boolean equals(Object o) { |
84 if (this == o) { | 86 if (this == o) { |
85 return true; | 87 return true; |
86 } | 88 } |
87 if (!(o instanceof BooleanArrayList)) { | 89 if (!(o instanceof BooleanArrayList)) { |
88 return super.equals(o); | 90 return super.equals(o); |
89 } | 91 } |
90 BooleanArrayList other = (BooleanArrayList) o; | 92 BooleanArrayList other = (BooleanArrayList) o; |
91 if (size != other.size) { | 93 if (size != other.size) { |
92 return false; | 94 return false; |
93 } | 95 } |
94 | 96 |
95 final boolean[] arr = other.array; | 97 final boolean[] arr = other.array; |
96 for (int i = 0; i < size; i++) { | 98 for (int i = 0; i < size; i++) { |
97 if (array[i] != arr[i]) { | 99 if (array[i] != arr[i]) { |
98 return false; | 100 return false; |
99 } | 101 } |
100 } | 102 } |
101 | 103 |
102 return true; | 104 return true; |
103 } | 105 } |
104 | 106 |
105 @Override | 107 @Override |
106 public int hashCode() { | 108 public int hashCode() { |
107 int result = 1; | 109 int result = 1; |
108 for (int i = 0; i < size; i++) { | 110 for (int i = 0; i < size; i++) { |
109 result = (31 * result) + Internal.hashBoolean(array[i]); | 111 result = (31 * result) + Internal.hashBoolean(array[i]); |
110 } | 112 } |
111 return result; | 113 return result; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 } | 165 } |
164 | 166 |
165 /** | 167 /** |
166 * Like {@link #add(int, Boolean)} but more efficient in that it doesn't box t
he element. | 168 * Like {@link #add(int, Boolean)} but more efficient in that it doesn't box t
he element. |
167 */ | 169 */ |
168 private void addBoolean(int index, boolean element) { | 170 private void addBoolean(int index, boolean element) { |
169 ensureIsMutable(); | 171 ensureIsMutable(); |
170 if (index < 0 || index > size) { | 172 if (index < 0 || index > size) { |
171 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)
); | 173 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)
); |
172 } | 174 } |
173 | 175 |
174 if (size < array.length) { | 176 if (size < array.length) { |
175 // Shift everything over to make room | 177 // Shift everything over to make room |
176 System.arraycopy(array, index, array, index + 1, size - index); | 178 System.arraycopy(array, index, array, index + 1, size - index); |
177 } else { | 179 } else { |
178 // Resize to 1.5x the size | 180 // Resize to 1.5x the size |
179 int length = ((size * 3) / 2) + 1; | 181 int length = ((size * 3) / 2) + 1; |
180 boolean[] newArray = new boolean[length]; | 182 boolean[] newArray = new boolean[length]; |
181 | 183 |
182 // Copy the first part directly | 184 // Copy the first part directly |
183 System.arraycopy(array, 0, newArray, 0, index); | 185 System.arraycopy(array, 0, newArray, 0, index); |
184 | 186 |
185 // Copy the rest shifted over by one to make room | 187 // Copy the rest shifted over by one to make room |
186 System.arraycopy(array, index, newArray, index + 1, size - index); | 188 System.arraycopy(array, index, newArray, index + 1, size - index); |
187 array = newArray; | 189 array = newArray; |
188 } | 190 } |
189 | 191 |
190 array[index] = element; | 192 array[index] = element; |
191 size++; | 193 size++; |
192 modCount++; | 194 modCount++; |
193 } | 195 } |
194 | 196 |
195 @Override | 197 @Override |
196 public boolean addAll(Collection<? extends Boolean> collection) { | 198 public boolean addAll(Collection<? extends Boolean> collection) { |
197 ensureIsMutable(); | 199 ensureIsMutable(); |
198 | 200 |
199 if (collection == null) { | 201 if (collection == null) { |
200 throw new NullPointerException(); | 202 throw new NullPointerException(); |
201 } | 203 } |
202 | 204 |
203 // We specialize when adding another BooleanArrayList to avoid boxing elemen
ts. | 205 // We specialize when adding another BooleanArrayList to avoid boxing elemen
ts. |
204 if (!(collection instanceof BooleanArrayList)) { | 206 if (!(collection instanceof BooleanArrayList)) { |
205 return super.addAll(collection); | 207 return super.addAll(collection); |
206 } | 208 } |
207 | 209 |
208 BooleanArrayList list = (BooleanArrayList) collection; | 210 BooleanArrayList list = (BooleanArrayList) collection; |
209 if (list.size == 0) { | 211 if (list.size == 0) { |
210 return false; | 212 return false; |
211 } | 213 } |
212 | 214 |
213 int overflow = Integer.MAX_VALUE - size; | 215 int overflow = Integer.MAX_VALUE - size; |
214 if (overflow < list.size) { | 216 if (overflow < list.size) { |
215 // We can't actually represent a list this large. | 217 // We can't actually represent a list this large. |
216 throw new OutOfMemoryError(); | 218 throw new OutOfMemoryError(); |
217 } | 219 } |
218 | 220 |
219 int newSize = size + list.size; | 221 int newSize = size + list.size; |
220 if (newSize > array.length) { | 222 if (newSize > array.length) { |
221 array = Arrays.copyOf(array, newSize); | 223 array = Arrays.copyOf(array, newSize); |
222 } | 224 } |
223 | 225 |
224 System.arraycopy(list.array, 0, array, size, list.size); | 226 System.arraycopy(list.array, 0, array, size, list.size); |
225 size = newSize; | 227 size = newSize; |
226 modCount++; | 228 modCount++; |
227 return true; | 229 return true; |
228 } | 230 } |
229 | 231 |
230 @Override | 232 @Override |
231 public boolean remove(Object o) { | 233 public boolean remove(Object o) { |
232 ensureIsMutable(); | 234 ensureIsMutable(); |
233 for (int i = 0; i < size; i++) { | 235 for (int i = 0; i < size; i++) { |
234 if (o.equals(array[i])) { | 236 if (o.equals(array[i])) { |
235 System.arraycopy(array, i + 1, array, i, size - i); | 237 System.arraycopy(array, i + 1, array, i, size - i); |
236 size--; | 238 size--; |
237 modCount++; | 239 modCount++; |
238 return true; | 240 return true; |
239 } | 241 } |
240 } | 242 } |
241 return false; | 243 return false; |
242 } | 244 } |
243 | 245 |
244 @Override | 246 @Override |
245 public Boolean remove(int index) { | 247 public Boolean remove(int index) { |
246 ensureIsMutable(); | 248 ensureIsMutable(); |
247 ensureIndexInRange(index); | 249 ensureIndexInRange(index); |
248 boolean value = array[index]; | 250 boolean value = array[index]; |
249 System.arraycopy(array, index + 1, array, index, size - index); | 251 System.arraycopy(array, index + 1, array, index, size - index); |
250 size--; | 252 size--; |
251 modCount++; | 253 modCount++; |
252 return value; | 254 return value; |
253 } | 255 } |
254 | 256 |
255 /** | 257 /** |
256 * 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 |
257 * {@link IndexOutOfBoundsException} if it is not. | 259 * {@link IndexOutOfBoundsException} if it is not. |
258 * | 260 * |
259 * @param index the index to verify is in range | 261 * @param index the index to verify is in range |
260 */ | 262 */ |
261 private void ensureIndexInRange(int index) { | 263 private void ensureIndexInRange(int index) { |
262 if (index < 0 || index >= size) { | 264 if (index < 0 || index >= size) { |
263 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)
); | 265 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)
); |
264 } | 266 } |
265 } | 267 } |
266 | 268 |
267 private String makeOutOfBoundsExceptionMessage(int index) { | 269 private String makeOutOfBoundsExceptionMessage(int index) { |
268 return "Index:" + index + ", Size:" + size; | 270 return "Index:" + index + ", Size:" + size; |
269 } | 271 } |
270 } | 272 } |
OLD | NEW |