1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.primitives.list.impl;
17
18 import java.util.Collection;
19
20 import org.joda.primitives.BooleanUtils;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class ArrayBooleanList extends AbstractBooleanList implements Cloneable {
38
39
40
41 private static final int MIN_GROWTH_SIZE = 4;
42
43 private static final int GROWTH_FACTOR_MULTIPLIER = 3;
44
45 private static final int GROWTH_FACTOR_DIVISOR = 2;
46
47
48 private boolean[] data;
49
50 private int size;
51
52
53
54
55 public ArrayBooleanList() {
56 super();
57 data = BooleanUtils.EMPTY_BOOLEAN_ARRAY;
58 }
59
60
61
62
63
64
65 public ArrayBooleanList(int initialSize) {
66 super();
67 if (initialSize <= 0) {
68 data = BooleanUtils.EMPTY_BOOLEAN_ARRAY;
69 } else {
70 data = new boolean[initialSize];
71 }
72 }
73
74
75
76
77
78
79 public ArrayBooleanList(boolean[] values) {
80 super();
81 if (values == null) {
82 data = BooleanUtils.EMPTY_BOOLEAN_ARRAY;
83 } else {
84 data = (boolean[]) values.clone();
85 size = values.length;
86 }
87 }
88
89
90
91
92
93
94 public ArrayBooleanList(Collection<Boolean> coll) {
95 super();
96 if (coll == null) {
97 data = BooleanUtils.EMPTY_BOOLEAN_ARRAY;
98 } else if (coll instanceof ArrayBooleanList) {
99 ArrayBooleanList c = (ArrayBooleanList) coll;
100 this.data = new boolean[c.size];
101 System.arraycopy(c.data, 0, this.data, 0, c.size);
102 size = c.size;
103 } else {
104 data = toPrimitiveArray(coll);
105 size = coll.size();
106 }
107 }
108
109
110
111
112
113
114
115
116 public int size() {
117 return size;
118 }
119
120
121
122
123
124
125
126
127 public boolean getBoolean(int index) {
128 checkIndexExists(index);
129 return data[index];
130 }
131
132
133
134
135
136
137
138
139
140 public boolean add(int index, boolean value) {
141 checkAddModifiable();
142 checkIndex(index);
143 ensureCapacity(size + 1);
144 System.arraycopy(data, index, data, index + 1, size - index);
145 data[index] = value;
146 size++;
147 return true;
148 }
149
150
151
152
153
154
155
156
157 public boolean removeBooleanAt(int index) {
158 checkRemoveModifiable();
159 checkIndexExists(index);
160 boolean result = data[index];
161 System.arraycopy(data, index + 1, data, index, size - 1 - index);
162 size--;
163 return result;
164 }
165
166
167
168
169
170
171
172
173 public boolean removeRange(int fromIndexInclusive, int toIndexExclusive) {
174 checkRemoveModifiable();
175 checkRange(fromIndexInclusive, toIndexExclusive);
176 if (fromIndexInclusive == toIndexExclusive) {
177 return false;
178 }
179 System.arraycopy(data, toIndexExclusive, data, fromIndexInclusive, size - toIndexExclusive);
180 size -= (toIndexExclusive - fromIndexInclusive);
181 return true;
182 }
183
184
185
186
187
188
189
190
191
192 public boolean set(int index, boolean value) {
193 checkSetModifiable();
194 checkIndexExists(index);
195 boolean result = data[index];
196 data[index] = value;
197 return result;
198 }
199
200
201
202
203
204
205
206
207
208 public void optimize() {
209 if (size < data.length) {
210 boolean[] array = new boolean[size];
211 System.arraycopy(data, 0, array, 0, size);
212 data = array;
213 }
214 }
215
216
217
218
219
220
221
222
223 public void clear() {
224 size = 0;
225 }
226
227
228
229
230
231
232
233
234
235 public boolean contains(boolean value) {
236 for (int i = 0; i < size; i++) {
237 if (data[i] == value) {
238 return true;
239 }
240 }
241 return false;
242 }
243
244
245
246
247
248
249
250
251
252 public boolean addAll(int index, boolean[] values) {
253 checkAddModifiable();
254 checkIndex(index);
255 if (values == null || values.length == 0) {
256 return false;
257 }
258 int len = values.length;
259 ensureCapacity(size + len);
260 System.arraycopy(data, index, data, index + len, size - index);
261 System.arraycopy(values, 0, data, index, len);
262 size += len;
263 return true;
264 }
265
266
267
268
269
270
271
272 protected boolean isAddModifiable() {
273 return true;
274 }
275
276
277
278
279
280
281 protected boolean isRemoveModifiable() {
282 return true;
283 }
284
285
286
287
288
289
290 protected boolean isSetModifiable() {
291 return true;
292 }
293
294
295
296
297
298
299 public boolean isModifiable() {
300 return true;
301 }
302
303
304
305
306
307
308 public Object clone() {
309 ArrayBooleanList cloned = (ArrayBooleanList) super.clone();
310 cloned.data = (boolean[]) data.clone();
311 return cloned;
312 }
313
314
315
316
317
318
319
320
321
322
323 protected void arrayCopy(int fromIndex, boolean[] dest, int destIndex, int size) {
324 System.arraycopy(data, fromIndex, dest, destIndex, size);
325 }
326
327
328
329
330
331
332
333
334 protected void ensureCapacity(int capacity) {
335 int len = data.length;
336 if (capacity <= len) {
337 return;
338 }
339 int newLen = len * GROWTH_FACTOR_MULTIPLIER / GROWTH_FACTOR_DIVISOR;
340 if (newLen < capacity) {
341 newLen = capacity;
342 }
343 if (newLen < MIN_GROWTH_SIZE) {
344 newLen = MIN_GROWTH_SIZE;
345 }
346 boolean[] newArray = new boolean[newLen];
347 System.arraycopy(data, 0, newArray, 0, len);
348 data = newArray;
349 }
350
351 }
352