2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
20 package org
.apache
.poi
.util
;
25 * A List of int's; as full an implementation of the java.util.List
26 * interface as possible, with an eye toward minimal creation of
29 * the mimicry of List is as follows:
31 * <li> if possible, operations designated 'optional' in the List
32 * interface are attempted
33 * <li> wherever the List interface refers to an Object, substitute
35 * <li> wherever the List interface refers to a Collection or List,
39 * the mimicry is not perfect, however:
41 * <li> operations involving Iterators or ListIterators are not
43 * <li> remove(Object) becomes removeValue to distinguish it from
45 * <li> subList is not supported
48 * @author Marc Johnson
55 private int fillval
= 0;
56 private static final int _default_size
= 128;
59 * create an IntList of default size
67 public IntList(final int initialCapacity
)
69 this(initialCapacity
,0);
74 * create a copy of an existing IntList
76 * @param list the existing IntList
79 public IntList(final IntList list
)
81 this(list
._array
.length
);
82 System
.arraycopy(list
._array
, 0, _array
, 0, _array
.length
);
87 * create an IntList with a predefined initial size
89 * @param initialCapacity the size for the internal array
92 public IntList(final int initialCapacity
, int fillvalue
)
94 _array
= new int[ initialCapacity
];
97 fillArray(fillval
, _array
, 0);
102 private void fillArray(int val
, int[] array
, int index
) {
103 for (int k
= index
; k
< array
.length
; k
++) {
109 * add the specfied value at the specified index
111 * @param index the index where the new value is to be added
112 * @param value the new value
114 * @exception IndexOutOfBoundsException if the index is out of
115 * range (index < 0 || index > size()).
118 public void add(final int index
, final int value
)
122 throw new IndexOutOfBoundsException();
124 else if (index
== _limit
)
131 // index < limit -- insert into the middle
132 if (_limit
== _array
.length
)
134 growArray(_limit
* 2);
136 System
.arraycopy(_array
, index
, _array
, index
+ 1,
138 _array
[ index
] = value
;
144 * Appends the specified element to the end of this list
146 * @param value element to be appended to this list.
148 * @return true (as per the general contract of the Collection.add
152 public boolean add(final int value
)
154 if (_limit
== _array
.length
)
156 growArray(_limit
* 2);
158 _array
[ _limit
++ ] = value
;
163 * Appends all of the elements in the specified collection to the
164 * end of this list, in the order that they are returned by the
165 * specified collection's iterator. The behavior of this
166 * operation is unspecified if the specified collection is
167 * modified while the operation is in progress. (Note that this
168 * will occur if the specified collection is this list, and it's
171 * @param c collection whose elements are to be added to this
174 * @return true if this list changed as a result of the call.
177 public boolean addAll(final IntList c
)
181 if ((_limit
+ c
._limit
) > _array
.length
)
183 growArray(_limit
+ c
._limit
);
185 System
.arraycopy(c
._array
, 0, _array
, _limit
, c
._limit
);
192 * Inserts all of the elements in the specified collection into
193 * this list at the specified position. Shifts the element
194 * currently at that position (if any) and any subsequent elements
195 * to the right (increases their indices). The new elements will
196 * appear in this list in the order that they are returned by the
197 * specified collection's iterator. The behavior of this
198 * operation is unspecified if the specified collection is
199 * modified while the operation is in progress. (Note that this
200 * will occur if the specified collection is this list, and it's
203 * @param index index at which to insert first element from the
204 * specified collection.
205 * @param c elements to be inserted into this list.
207 * @return true if this list changed as a result of the call.
209 * @exception IndexOutOfBoundsException if the index is out of
210 * range (index < 0 || index > size())
213 public boolean addAll(final int index
, final IntList c
)
217 throw new IndexOutOfBoundsException();
221 if ((_limit
+ c
._limit
) > _array
.length
)
223 growArray(_limit
+ c
._limit
);
227 System
.arraycopy(_array
, index
, _array
, index
+ c
._limit
,
231 System
.arraycopy(c
._array
, 0, _array
, index
, c
._limit
);
238 * Removes all of the elements from this list. This list will be
239 * empty after this call returns (unless it throws an exception).
248 * Returns true if this list contains the specified element. More
249 * formally, returns true if and only if this list contains at
250 * least one element e such that o == e
252 * @param o element whose presence in this list is to be tested.
254 * @return true if this list contains the specified element.
257 public boolean contains(final int o
)
259 boolean rval
= false;
261 for (int j
= 0; !rval
&& (j
< _limit
); j
++)
263 if (_array
[ j
] == o
)
272 * Returns true if this list contains all of the elements of the
273 * specified collection.
275 * @param c collection to be checked for containment in this list.
277 * @return true if this list contains all of the elements of the
278 * specified collection.
281 public boolean containsAll(final IntList c
)
287 for (int j
= 0; rval
&& (j
< c
._limit
); j
++)
289 if (!contains(c
._array
[ j
]))
299 * Compares the specified object with this list for equality.
300 * Returns true if and only if the specified object is also a
301 * list, both lists have the same size, and all corresponding
302 * pairs of elements in the two lists are equal. (Two elements e1
303 * and e2 are equal if e1 == e2.) In other words, two lists are
304 * defined to be equal if they contain the same elements in the
305 * same order. This definition ensures that the equals method
306 * works properly across different implementations of the List
309 * @param o the object to be compared for equality with this list.
311 * @return true if the specified object is equal to this list.
314 public boolean equals(final Object o
)
316 boolean rval
= this == o
;
318 if (!rval
&& (o
!= null) && (o
.getClass() == this.getClass()))
320 IntList other
= ( IntList
) o
;
322 if (other
._limit
== _limit
)
327 for (int j
= 0; rval
&& (j
< _limit
); j
++)
329 rval
= _array
[ j
] == other
._array
[ j
];
337 * Returns the element at the specified position in this list.
339 * @param index index of element to return.
341 * @return the element at the specified position in this list.
343 * @exception IndexOutOfBoundsException if the index is out of
344 * range (index < 0 || index >= size()).
347 public int get(final int index
)
351 throw new IndexOutOfBoundsException();
353 return _array
[ index
];
357 * Returns the hash code value for this list. The hash code of a
358 * list is defined to be the result of the following calculation:
362 * Iterator i = list.iterator();
363 * while (i.hasNext()) {
364 * Object obj = i.next();
365 * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
369 * This ensures that list1.equals(list2) implies that
370 * list1.hashCode()==list2.hashCode() for any two lists, list1 and
371 * list2, as required by the general contract of Object.hashCode.
373 * @return the hash code value for this list.
376 public int hashCode()
380 for (int j
= 0; j
< _limit
; j
++)
382 hash
= (31 * hash
) + _array
[ j
];
388 * Returns the index in this list of the first occurrence of the
389 * specified element, or -1 if this list does not contain this
390 * element. More formally, returns the lowest index i such that
391 * (o == get(i)), or -1 if there is no such index.
393 * @param o element to search for.
395 * @return the index in this list of the first occurrence of the
396 * specified element, or -1 if this list does not contain
400 public int indexOf(final int o
)
404 for (; rval
< _limit
; rval
++)
406 if (o
== _array
[ rval
])
413 rval
= -1; // didn't find it
419 * Returns true if this list contains no elements.
421 * @return true if this list contains no elements.
424 public boolean isEmpty()
430 * Returns the index in this list of the last occurrence of the
431 * specified element, or -1 if this list does not contain this
432 * element. More formally, returns the highest index i such that
433 * (o == get(i)), or -1 if there is no such index.
435 * @param o element to search for.
437 * @return the index in this list of the last occurrence of the
438 * specified element, or -1 if this list does not contain
442 public int lastIndexOf(final int o
)
444 int rval
= _limit
- 1;
446 for (; rval
>= 0; rval
--)
448 if (o
== _array
[ rval
])
457 * Removes the element at the specified position in this list.
458 * Shifts any subsequent elements to the left (subtracts one from
459 * their indices). Returns the element that was removed from the
462 * @param index the index of the element to removed.
464 * @return the element previously at the specified position.
466 * @exception IndexOutOfBoundsException if the index is out of
467 * range (index < 0 || index >= size()).
470 public int remove(final int index
)
474 throw new IndexOutOfBoundsException();
476 int rval
= _array
[ index
];
478 System
.arraycopy(_array
, index
+ 1, _array
, index
, _limit
- index
);
484 * Removes the first occurrence in this list of the specified
485 * element (optional operation). If this list does not contain
486 * the element, it is unchanged. More formally, removes the
487 * element with the lowest index i such that (o.equals(get(i)))
488 * (if such an element exists).
490 * @param o element to be removed from this list, if present.
492 * @return true if this list contained the specified element.
495 public boolean removeValue(final int o
)
497 boolean rval
= false;
499 for (int j
= 0; !rval
&& (j
< _limit
); j
++)
501 if (o
== _array
[ j
])
504 System
.arraycopy(_array
, j
+ 1, _array
, j
, _limit
- j
);
514 * Removes from this list all the elements that are contained in
515 * the specified collection
517 * @param c collection that defines which elements will be removed
520 * @return true if this list changed as a result of the call.
523 public boolean removeAll(final IntList c
)
525 boolean rval
= false;
527 for (int j
= 0; j
< c
._limit
; j
++)
529 if (removeValue(c
._array
[ j
]))
538 * Retains only the elements in this list that are contained in
539 * the specified collection. In other words, removes from this
540 * list all the elements that are not contained in the specified
543 * @param c collection that defines which elements this set will
546 * @return true if this list changed as a result of the call.
549 public boolean retainAll(final IntList c
)
551 boolean rval
= false;
553 for (int j
= 0; j
< _limit
; )
555 if (!c
.contains(_array
[ j
]))
569 * Replaces the element at the specified position in this list
570 * with the specified element
572 * @param index index of element to replace.
573 * @param element element to be stored at the specified position.
575 * @return the element previously at the specified position.
577 * @exception IndexOutOfBoundsException if the index is out of
578 * range (index < 0 || index >= size()).
581 public int set(final int index
, final int element
)
585 throw new IndexOutOfBoundsException();
587 int rval
= _array
[ index
];
589 _array
[ index
] = element
;
594 * Returns the number of elements in this list. If this list
595 * contains more than Integer.MAX_VALUE elements, returns
598 * @return the number of elements in this IntList
607 * Returns an array containing all of the elements in this list in
608 * proper sequence. Obeys the general contract of the
609 * Collection.toArray method.
611 * @return an array containing all of the elements in this list in
615 public int [] toArray()
617 int[] rval
= new int[ _limit
];
619 System
.arraycopy(_array
, 0, rval
, 0, _limit
);
624 * Returns an array containing all of the elements in this list in
625 * proper sequence. Obeys the general contract of the
626 * Collection.toArray(Object[]) method.
628 * @param a the array into which the elements of this list are to
629 * be stored, if it is big enough; otherwise, a new array
630 * is allocated for this purpose.
632 * @return an array containing the elements of this list.
635 public int [] toArray(final int [] a
)
639 if (a
.length
== _limit
)
641 System
.arraycopy(_array
, 0, a
, 0, _limit
);
651 private void growArray(final int new_size
)
653 int size
= (new_size
== _array
.length
) ? new_size
+ 1
655 int[] new_array
= new int[ size
];
658 fillArray(fillval
, new_array
, _array
.length
);
661 System
.arraycopy(_array
, 0, new_array
, 0, _limit
);
664 } // end public class IntList