Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / util / IntList.java
blob30498cf439b45a6f2662152db275d4e00d7d1db3
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;
22 import java.util.*;
24 /**
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
27 * objects
29 * the mimicry of List is as follows:
30 * <ul>
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
34 * int
35 * <li> wherever the List interface refers to a Collection or List,
36 * substitute IntList
37 * </ul>
39 * the mimicry is not perfect, however:
40 * <ul>
41 * <li> operations involving Iterators or ListIterators are not
42 * supported
43 * <li> remove(Object) becomes removeValue to distinguish it from
44 * remove(int index)
45 * <li> subList is not supported
46 * </ul>
48 * @author Marc Johnson
51 public class IntList
53 private int[] _array;
54 private int _limit;
55 private int fillval = 0;
56 private static final int _default_size = 128;
58 /**
59 * create an IntList of default size
62 public IntList()
64 this(_default_size);
67 public IntList(final int initialCapacity)
69 this(initialCapacity,0);
73 /**
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);
83 _limit = list._limit;
86 /**
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 ];
95 if (fillval != 0) {
96 fillval = fillvalue;
97 fillArray(fillval, _array, 0);
99 _limit = 0;
102 private void fillArray(int val, int[] array, int index) {
103 for (int k = index; k < array.length; k++) {
104 array[k] = val;
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)
120 if (index > _limit)
122 throw new IndexOutOfBoundsException();
124 else if (index == _limit)
126 add(value);
128 else
131 // index < limit -- insert into the middle
132 if (_limit == _array.length)
134 growArray(_limit * 2);
136 System.arraycopy(_array, index, _array, index + 1,
137 _limit - index);
138 _array[ index ] = value;
139 _limit++;
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
149 * method).
152 public boolean add(final int value)
154 if (_limit == _array.length)
156 growArray(_limit * 2);
158 _array[ _limit++ ] = value;
159 return true;
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
169 * nonempty.)
171 * @param c collection whose elements are to be added to this
172 * list.
174 * @return true if this list changed as a result of the call.
177 public boolean addAll(final IntList c)
179 if (c._limit != 0)
181 if ((_limit + c._limit) > _array.length)
183 growArray(_limit + c._limit);
185 System.arraycopy(c._array, 0, _array, _limit, c._limit);
186 _limit += c._limit;
188 return true;
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
201 * nonempty.)
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)
215 if (index > _limit)
217 throw new IndexOutOfBoundsException();
219 if (c._limit != 0)
221 if ((_limit + c._limit) > _array.length)
223 growArray(_limit + c._limit);
226 // make a hole
227 System.arraycopy(_array, index, _array, index + c._limit,
228 _limit - index);
230 // fill it in
231 System.arraycopy(c._array, 0, _array, index, c._limit);
232 _limit += c._limit;
234 return true;
238 * Removes all of the elements from this list. This list will be
239 * empty after this call returns (unless it throws an exception).
242 public void clear()
244 _limit = 0;
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)
265 rval = true;
268 return rval;
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)
283 boolean rval = true;
285 if (this != c)
287 for (int j = 0; rval && (j < c._limit); j++)
289 if (!contains(c._array[ j ]))
291 rval = false;
295 return rval;
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
307 * interface.
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)
325 // assume match
326 rval = true;
327 for (int j = 0; rval && (j < _limit); j++)
329 rval = _array[ j ] == other._array[ j ];
333 return rval;
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)
349 if (index >= _limit)
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:
360 * <code>
361 * hashCode = 1;
362 * Iterator i = list.iterator();
363 * while (i.hasNext()) {
364 * Object obj = i.next();
365 * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
367 * </code>
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()
378 int hash = 0;
380 for (int j = 0; j < _limit; j++)
382 hash = (31 * hash) + _array[ j ];
384 return hash;
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
397 * this element.
400 public int indexOf(final int o)
402 int rval = 0;
404 for (; rval < _limit; rval++)
406 if (o == _array[ rval ])
408 break;
411 if (rval == _limit)
413 rval = -1; // didn't find it
415 return rval;
419 * Returns true if this list contains no elements.
421 * @return true if this list contains no elements.
424 public boolean isEmpty()
426 return _limit == 0;
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
439 * this element.
442 public int lastIndexOf(final int o)
444 int rval = _limit - 1;
446 for (; rval >= 0; rval--)
448 if (o == _array[ rval ])
450 break;
453 return 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
460 * list.
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)
472 if (index >= _limit)
474 throw new IndexOutOfBoundsException();
476 int rval = _array[ index ];
478 System.arraycopy(_array, index + 1, _array, index, _limit - index);
479 _limit--;
480 return rval;
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 ])
503 if (j+1 < _limit) {
504 System.arraycopy(_array, j + 1, _array, j, _limit - j);
506 _limit--;
507 rval = true;
510 return rval;
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
518 * from this list.
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 ]))
531 rval = true;
534 return rval;
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
541 * collection.
543 * @param c collection that defines which elements this set will
544 * retain.
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 ]))
557 remove(j);
558 rval = true;
560 else
562 j++;
565 return rval;
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)
583 if (index >= _limit)
585 throw new IndexOutOfBoundsException();
587 int rval = _array[ index ];
589 _array[ index ] = element;
590 return rval;
594 * Returns the number of elements in this list. If this list
595 * contains more than Integer.MAX_VALUE elements, returns
596 * Integer.MAX_VALUE.
598 * @return the number of elements in this IntList
601 public int size()
603 return _limit;
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
612 * proper sequence.
615 public int [] toArray()
617 int[] rval = new int[ _limit ];
619 System.arraycopy(_array, 0, rval, 0, _limit);
620 return rval;
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)
637 int[] rval;
639 if (a.length == _limit)
641 System.arraycopy(_array, 0, a, 0, _limit);
642 rval = a;
644 else
646 rval = toArray();
648 return rval;
651 private void growArray(final int new_size)
653 int size = (new_size == _array.length) ? new_size + 1
654 : new_size;
655 int[] new_array = new int[ size ];
657 if (fillval != 0) {
658 fillArray(fillval, new_array, _array.length);
661 System.arraycopy(_array, 0, new_array, 0, _limit);
662 _array = new_array;
664 } // end public class IntList