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