1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections.primitives;
18
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
23
24
25
26
27
28
29
30
31
32
33
34 public class ArrayIntList extends RandomAccessIntList implements IntList, Serializable {
35
36
37
38
39
40
41
42
43 public ArrayIntList() {
44 this(8);
45 }
46
47
48
49
50
51
52 public ArrayIntList(int initialCapacity) {
53 if(initialCapacity < 0) {
54 throw new IllegalArgumentException("capacity " + initialCapacity);
55 }
56 _data = new int[initialCapacity];
57 _size = 0;
58 }
59
60
61
62
63
64
65
66
67
68
69 public ArrayIntList(IntCollection that) {
70 this(that.size());
71 addAll(that);
72 }
73
74
75
76
77
78
79
80 public ArrayIntList(int[] array) {
81 this(array.length);
82 System.arraycopy(array, 0, _data, 0, array.length);
83 _size = array.length;
84 }
85
86
87
88
89 public int get(int index) {
90 checkRange(index);
91 return _data[index];
92 }
93
94 public int size() {
95 return _size;
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public int removeElementAt(int index) {
112 checkRange(index);
113 incrModCount();
114 int oldval = _data[index];
115 int numtomove = _size - index - 1;
116 if(numtomove > 0) {
117 System.arraycopy(_data,index+1,_data,index,numtomove);
118 }
119 _size--;
120 return oldval;
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public int set(int index, int element) {
137 checkRange(index);
138 incrModCount();
139 int oldval = _data[index];
140 _data[index] = element;
141 return oldval;
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public void add(int index, int element) {
160 checkRangeIncludingEndpoint(index);
161 incrModCount();
162 ensureCapacity(_size+1);
163 int numtomove = _size-index;
164 System.arraycopy(_data,index,_data,index+1,numtomove);
165 _data[index] = element;
166 _size++;
167 }
168
169 public void clear() {
170 incrModCount();
171 _size = 0;
172 }
173
174 public boolean addAll(IntCollection collection) {
175 return addAll(size(), collection);
176 }
177
178 public boolean addAll(int index, IntCollection collection) {
179 if (collection.size() == 0) {
180 return false;
181 }
182 checkRangeIncludingEndpoint(index);
183 incrModCount();
184 ensureCapacity(_size + collection.size());
185 if (index != _size) {
186
187 System.arraycopy(_data, index, _data, index + collection.size(), _size - index);
188 }
189 for (IntIterator it = collection.iterator(); it.hasNext();) {
190 _data[index] = it.next();
191 index++;
192 }
193 _size += collection.size();
194 return true;
195 }
196
197
198
199
200
201
202
203
204
205 public void ensureCapacity(int mincap) {
206 incrModCount();
207 if(mincap > _data.length) {
208 int newcap = (_data.length * 3)/2 + 1;
209 int[] olddata = _data;
210 _data = new int[newcap < mincap ? mincap : newcap];
211 System.arraycopy(olddata,0,_data,0,_size);
212 }
213 }
214
215
216
217
218
219 public void trimToSize() {
220 incrModCount();
221 if(_size < _data.length) {
222 int[] olddata = _data;
223 _data = new int[_size];
224 System.arraycopy(olddata,0,_data,0,_size);
225 }
226 }
227
228
229
230
231 private void writeObject(ObjectOutputStream out) throws IOException{
232 out.defaultWriteObject();
233 out.writeInt(_data.length);
234 for(int i=0;i<_size;i++) {
235 out.writeInt(_data[i]);
236 }
237 }
238
239 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
240 in.defaultReadObject();
241 _data = new int[in.readInt()];
242 for(int i=0;i<_size;i++) {
243 _data[i] = in.readInt();
244 }
245 }
246
247 private final void checkRange(int index) {
248 if(index < 0 || index >= _size) {
249 throw new IndexOutOfBoundsException("Should be at least 0 and less than " + _size + ", found " + index);
250 }
251 }
252
253 private final void checkRangeIncludingEndpoint(int index) {
254 if(index < 0 || index > _size) {
255 throw new IndexOutOfBoundsException("Should be at least 0 and at most " + _size + ", found " + index);
256 }
257 }
258
259
260
261
262 private transient int[] _data = null;
263 private int _size = 0;
264
265 }