Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ArrayIntList |
|
| 2.0;2 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
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 | * An {@link IntList} backed by an array of <code>int</code>s. | |
26 | * This implementation supports all optional methods. | |
27 | * | |
28 | * @since Commons Primitives 1.0 | |
29 | * @version $Revision: 480460 $ $Date: 2006-11-29 03:14:21 -0500 (Wed, 29 Nov 2006) $ | |
30 | * | |
31 | * @author Rodney Waldhoff | |
32 | * @author Robert Fischer | |
33 | */ | |
34 | public class ArrayIntList extends RandomAccessIntList implements IntList, Serializable { | |
35 | ||
36 | // constructors | |
37 | //------------------------------------------------------------------------- | |
38 | ||
39 | /** | |
40 | * Construct an empty list with the default | |
41 | * initial capacity. | |
42 | */ | |
43 | public ArrayIntList() { | |
44 | 600 | this(8); |
45 | 600 | } |
46 | ||
47 | /** | |
48 | * Construct an empty list with the given | |
49 | * initial capacity. | |
50 | * @throws IllegalArgumentException when <i>initialCapacity</i> is negative | |
51 | */ | |
52 | 608 | public ArrayIntList(int initialCapacity) { |
53 | 608 | if(initialCapacity < 0) { |
54 | 1 | throw new IllegalArgumentException("capacity " + initialCapacity); |
55 | } | |
56 | 607 | _data = new int[initialCapacity]; |
57 | 607 | _size = 0; |
58 | 607 | } |
59 | ||
60 | /** | |
61 | * Constructs a list containing the elements of the given collection, | |
62 | * in the order they are returned by that collection's iterator. | |
63 | * | |
64 | * @see ArrayIntList#addAll(org.apache.commons.collections.primitives.IntCollection) | |
65 | * @param that the non-<code>null</code> collection of <code>int</code>s | |
66 | * to add | |
67 | * @throws NullPointerException if <i>that</i> is <code>null</code> | |
68 | */ | |
69 | public ArrayIntList(IntCollection that) { | |
70 | 2 | this(that.size()); |
71 | 1 | addAll(that); |
72 | 1 | } |
73 | ||
74 | /** | |
75 | * Constructs a list by copying the specified array. | |
76 | * | |
77 | * @param array the array to initialize the collection with | |
78 | * @throws NullPointerException if the array is <code>null</code> | |
79 | */ | |
80 | public ArrayIntList(int[] array) { | |
81 | 2 | this(array.length); |
82 | 1 | System.arraycopy(array, 0, _data, 0, array.length); |
83 | 1 | _size = array.length; |
84 | 1 | } |
85 | ||
86 | // IntList methods | |
87 | //------------------------------------------------------------------------- | |
88 | ||
89 | public int get(int index) { | |
90 | 123081 | checkRange(index); |
91 | 123063 | return _data[index]; |
92 | } | |
93 | ||
94 | public int size() { | |
95 | 183854 | return _size; |
96 | } | |
97 | ||
98 | /** | |
99 | * Removes the element at the specified position in | |
100 | * (optional operation). Any subsequent elements | |
101 | * are shifted to the left, subtracting one from their | |
102 | * indices. Returns the element that was removed. | |
103 | * | |
104 | * @param index the index of the element to remove | |
105 | * @return the value of the element that was removed | |
106 | * | |
107 | * @throws UnsupportedOperationException when this operation is not | |
108 | * supported | |
109 | * @throws IndexOutOfBoundsException if the specified index is out of range | |
110 | */ | |
111 | public int removeElementAt(int index) { | |
112 | 434 | checkRange(index); |
113 | 416 | incrModCount(); |
114 | 416 | int oldval = _data[index]; |
115 | 416 | int numtomove = _size - index - 1; |
116 | 416 | if(numtomove > 0) { |
117 | 382 | System.arraycopy(_data,index+1,_data,index,numtomove); |
118 | } | |
119 | 416 | _size--; |
120 | 416 | return oldval; |
121 | } | |
122 | ||
123 | /** | |
124 | * Replaces the element at the specified | |
125 | * position in me with the specified element | |
126 | * (optional operation). | |
127 | * | |
128 | * @param index the index of the element to change | |
129 | * @param element the value to be stored at the specified position | |
130 | * @return the value previously stored at the specified position | |
131 | * | |
132 | * @throws UnsupportedOperationException when this operation is not | |
133 | * supported | |
134 | * @throws IndexOutOfBoundsException if the specified index is out of range | |
135 | */ | |
136 | public int set(int index, int element) { | |
137 | 110 | checkRange(index); |
138 | 92 | incrModCount(); |
139 | 92 | int oldval = _data[index]; |
140 | 92 | _data[index] = element; |
141 | 92 | return oldval; |
142 | } | |
143 | ||
144 | /** | |
145 | * Inserts the specified element at the specified position | |
146 | * (optional operation). Shifts the element currently | |
147 | * at that position (if any) and any subsequent elements to the | |
148 | * right, increasing their indices. | |
149 | * | |
150 | * @param index the index at which to insert the element | |
151 | * @param element the value to insert | |
152 | * | |
153 | * @throws UnsupportedOperationException when this operation is not | |
154 | * supported | |
155 | * @throws IllegalArgumentException if some aspect of the specified element | |
156 | * prevents it from being added to me | |
157 | * @throws IndexOutOfBoundsException if the specified index is out of range | |
158 | */ | |
159 | public void add(int index, int element) { | |
160 | 3718 | checkRangeIncludingEndpoint(index); |
161 | 3702 | incrModCount(); |
162 | 3702 | ensureCapacity(_size+1); |
163 | 3702 | int numtomove = _size-index; |
164 | 3702 | System.arraycopy(_data,index,_data,index+1,numtomove); |
165 | 3702 | _data[index] = element; |
166 | 3702 | _size++; |
167 | 3702 | } |
168 | ||
169 | public void clear() { | |
170 | 8 | incrModCount(); |
171 | 8 | _size = 0; |
172 | 8 | } |
173 | ||
174 | public boolean addAll(IntCollection collection) { | |
175 | 384 | return addAll(size(), collection); |
176 | } | |
177 | ||
178 | public boolean addAll(int index, IntCollection collection) { | |
179 | 387 | if (collection.size() == 0) { |
180 | 0 | return false; |
181 | } | |
182 | 387 | checkRangeIncludingEndpoint(index); |
183 | 387 | incrModCount(); |
184 | 387 | ensureCapacity(_size + collection.size()); |
185 | 387 | if (index != _size) { |
186 | // Need to move some elements | |
187 | 3 | System.arraycopy(_data, index, _data, index + collection.size(), _size - index); |
188 | } | |
189 | 387 | for (IntIterator it = collection.iterator(); it.hasNext();) { |
190 | 6414 | _data[index] = it.next(); |
191 | 6414 | index++; |
192 | } | |
193 | 387 | _size += collection.size(); |
194 | 387 | return true; |
195 | } | |
196 | ||
197 | // capacity methods | |
198 | //------------------------------------------------------------------------- | |
199 | ||
200 | /** | |
201 | * Increases my capacity, if necessary, to ensure that I can hold at | |
202 | * least the number of elements specified by the minimum capacity | |
203 | * argument without growing. | |
204 | */ | |
205 | public void ensureCapacity(int mincap) { | |
206 | 4089 | incrModCount(); |
207 | 4089 | if(mincap > _data.length) { |
208 | 630 | int newcap = (_data.length * 3)/2 + 1; |
209 | 630 | int[] olddata = _data; |
210 | 630 | _data = new int[newcap < mincap ? mincap : newcap]; |
211 | 630 | System.arraycopy(olddata,0,_data,0,_size); |
212 | } | |
213 | 4089 | } |
214 | ||
215 | /** | |
216 | * Reduce my capacity, if necessary, to match my | |
217 | * current {@link #size size}. | |
218 | */ | |
219 | public void trimToSize() { | |
220 | 12 | incrModCount(); |
221 | 12 | if(_size < _data.length) { |
222 | 9 | int[] olddata = _data; |
223 | 9 | _data = new int[_size]; |
224 | 9 | System.arraycopy(olddata,0,_data,0,_size); |
225 | } | |
226 | 12 | } |
227 | ||
228 | // private methods | |
229 | //------------------------------------------------------------------------- | |
230 | ||
231 | private void writeObject(ObjectOutputStream out) throws IOException{ | |
232 | 13 | out.defaultWriteObject(); |
233 | 13 | out.writeInt(_data.length); |
234 | 119 | for(int i=0;i<_size;i++) { |
235 | 106 | out.writeInt(_data[i]); |
236 | } | |
237 | 13 | } |
238 | ||
239 | private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { | |
240 | 13 | in.defaultReadObject(); |
241 | 13 | _data = new int[in.readInt()]; |
242 | 119 | for(int i=0;i<_size;i++) { |
243 | 106 | _data[i] = in.readInt(); |
244 | } | |
245 | 13 | } |
246 | ||
247 | private final void checkRange(int index) { | |
248 | 123625 | if(index < 0 || index >= _size) { |
249 | 54 | throw new IndexOutOfBoundsException("Should be at least 0 and less than " + _size + ", found " + index); |
250 | } | |
251 | 123571 | } |
252 | ||
253 | private final void checkRangeIncludingEndpoint(int index) { | |
254 | 4105 | if(index < 0 || index > _size) { |
255 | 16 | throw new IndexOutOfBoundsException("Should be at least 0 and at most " + _size + ", found " + index); |
256 | } | |
257 | 4089 | } |
258 | ||
259 | // attributes | |
260 | //------------------------------------------------------------------------- | |
261 | ||
262 | 608 | private transient int[] _data = null; |
263 | 608 | private int _size = 0; |
264 | ||
265 | } |