Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / util / ShortList.java
blobc06a9c714b9e7555e6d9ce435ddd136f3ee124b6
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 short'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 * short
35 * <li> wherever the List interface refers to a Collection or List,
36 * substitute ShortList
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(short index)
45 * <li> subList is not supported
46 * </ul>
48 * @author Marc Johnson
51 public class ShortList
53 private short[] _array;
54 private int _limit;
55 private static final int _default_size = 128;
57 /**
58 * create an ShortList of default size
61 public ShortList()
63 this(_default_size);
66 /**
67 * create a copy of an existing ShortList
69 * @param list the existing ShortList
72 public ShortList(final ShortList list)
74 this(list._array.length);
75 System.arraycopy(list._array, 0, _array, 0, _array.length);
76 _limit = list._limit;
79 /**
80 * create an ShortList with a predefined initial size
82 * @param initialCapacity the size for the internal array
85 public ShortList(final int initialCapacity)
87 _array = new short[ initialCapacity ];
88 _limit = 0;
91 /**
92 * add the specfied value at the specified index
94 * @param index the index where the new value is to be added
95 * @param value the new value
97 * @exception IndexOutOfBoundsException if the index is out of
98 * range (index < 0 || index > size()).
101 public void add(final int index, final short value)
103 if (index > _limit)
105 throw new IndexOutOfBoundsException();
107 else if (index == _limit)
109 add(value);
111 else
114 // index < limit -- insert into the middle
115 if (_limit == _array.length)
117 growArray(_limit * 2);
119 System.arraycopy(_array, index, _array, index + 1,
120 _limit - index);
121 _array[ index ] = value;
122 _limit++;
127 * Appends the specified element to the end of this list
129 * @param value element to be appended to this list.
131 * @return true (as per the general contract of the Collection.add
132 * method).
135 public boolean add(final short value)
137 if (_limit == _array.length)
139 growArray(_limit * 2);
141 _array[ _limit++ ] = value;
142 return true;
146 * Appends all of the elements in the specified collection to the
147 * end of this list, in the order that they are returned by the
148 * specified collection's iterator. The behavior of this
149 * operation is unspecified if the specified collection is
150 * modified while the operation is in progress. (Note that this
151 * will occur if the specified collection is this list, and it's
152 * nonempty.)
154 * @param c collection whose elements are to be added to this
155 * list.
157 * @return true if this list changed as a result of the call.
160 public boolean addAll(final ShortList c)
162 if (c._limit != 0)
164 if ((_limit + c._limit) > _array.length)
166 growArray(_limit + c._limit);
168 System.arraycopy(c._array, 0, _array, _limit, c._limit);
169 _limit += c._limit;
171 return true;
175 * Inserts all of the elements in the specified collection into
176 * this list at the specified position. Shifts the element
177 * currently at that position (if any) and any subsequent elements
178 * to the right (increases their indices). The new elements will
179 * appear in this list in the order that they are returned by the
180 * specified collection's iterator. The behavior of this
181 * operation is unspecified if the specified collection is
182 * modified while the operation is in progress. (Note that this
183 * will occur if the specified collection is this list, and it's
184 * nonempty.)
186 * @param index index at which to insert first element from the
187 * specified collection.
188 * @param c elements to be inserted into this list.
190 * @return true if this list changed as a result of the call.
192 * @exception IndexOutOfBoundsException if the index is out of
193 * range (index < 0 || index > size())
196 public boolean addAll(final int index, final ShortList c)
198 if (index > _limit)
200 throw new IndexOutOfBoundsException();
202 if (c._limit != 0)
204 if ((_limit + c._limit) > _array.length)
206 growArray(_limit + c._limit);
209 // make a hole
210 System.arraycopy(_array, index, _array, index + c._limit,
211 _limit - index);
213 // fill it in
214 System.arraycopy(c._array, 0, _array, index, c._limit);
215 _limit += c._limit;
217 return true;
221 * Removes all of the elements from this list. This list will be
222 * empty after this call returns (unless it throws an exception).
225 public void clear()
227 _limit = 0;
231 * Returns true if this list contains the specified element. More
232 * formally, returns true if and only if this list contains at
233 * least one element e such that o == e
235 * @param o element whose presence in this list is to be tested.
237 * @return true if this list contains the specified element.
240 public boolean contains(final short o)
242 boolean rval = false;
244 for (int j = 0; !rval && (j < _limit); j++)
246 if (_array[ j ] == o)
248 rval = true;
251 return rval;
255 * Returns true if this list contains all of the elements of the
256 * specified collection.
258 * @param c collection to be checked for containment in this list.
260 * @return true if this list contains all of the elements of the
261 * specified collection.
264 public boolean containsAll(final ShortList c)
266 boolean rval = true;
268 if (this != c)
270 for (int j = 0; rval && (j < c._limit); j++)
272 if (!contains(c._array[ j ]))
274 rval = false;
278 return rval;
282 * Compares the specified object with this list for equality.
283 * Returns true if and only if the specified object is also a
284 * list, both lists have the same size, and all corresponding
285 * pairs of elements in the two lists are equal. (Two elements e1
286 * and e2 are equal if e1 == e2.) In other words, two lists are
287 * defined to be equal if they contain the same elements in the
288 * same order. This definition ensures that the equals method
289 * works properly across different implementations of the List
290 * interface.
292 * @param o the object to be compared for equality with this list.
294 * @return true if the specified object is equal to this list.
297 public boolean equals(final Object o)
299 boolean rval = this == o;
301 if (!rval && (o != null) && (o.getClass() == this.getClass()))
303 ShortList other = ( ShortList ) o;
305 if (other._limit == _limit)
308 // assume match
309 rval = true;
310 for (int j = 0; rval && (j < _limit); j++)
312 rval = _array[ j ] == other._array[ j ];
316 return rval;
320 * Returns the element at the specified position in this list.
322 * @param index index of element to return.
324 * @return the element at the specified position in this list.
326 * @exception IndexOutOfBoundsException if the index is out of
327 * range (index < 0 || index >= size()).
330 public short get(final int index)
332 if (index >= _limit)
334 throw new IndexOutOfBoundsException();
336 return _array[ index ];
340 * Returns the hash code value for this list. The hash code of a
341 * list is defined to be the result of the following calculation:
343 * <code>
344 * hashCode = 1;
345 * Iterator i = list.iterator();
346 * while (i.hasNext()) {
347 * Object obj = i.next();
348 * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
350 * </code>
352 * This ensures that list1.equals(list2) implies that
353 * list1.hashCode()==list2.hashCode() for any two lists, list1 and
354 * list2, as required by the general contract of Object.hashCode.
356 * @return the hash code value for this list.
359 public int hashCode()
361 int hash = 0;
363 for (int j = 0; j < _limit; j++)
365 hash = (31 * hash) + _array[ j ];
367 return hash;
371 * Returns the index in this list of the first occurrence of the
372 * specified element, or -1 if this list does not contain this
373 * element. More formally, returns the lowest index i such that
374 * (o == get(i)), or -1 if there is no such index.
376 * @param o element to search for.
378 * @return the index in this list of the first occurrence of the
379 * specified element, or -1 if this list does not contain
380 * this element.
383 public int indexOf(final short o)
385 int rval = 0;
387 for (; rval < _limit; rval++)
389 if (o == _array[ rval ])
391 break;
394 if (rval == _limit)
396 rval = -1; // didn't find it
398 return rval;
402 * Returns true if this list contains no elements.
404 * @return true if this list contains no elements.
407 public boolean isEmpty()
409 return _limit == 0;
413 * Returns the index in this list of the last occurrence of the
414 * specified element, or -1 if this list does not contain this
415 * element. More formally, returns the highest index i such that
416 * (o == get(i)), or -1 if there is no such index.
418 * @param o element to search for.
420 * @return the index in this list of the last occurrence of the
421 * specified element, or -1 if this list does not contain
422 * this element.
425 public int lastIndexOf(final short o)
427 int rval = _limit - 1;
429 for (; rval >= 0; rval--)
431 if (o == _array[ rval ])
433 break;
436 return rval;
440 * Removes the element at the specified position in this list.
441 * Shifts any subsequent elements to the left (subtracts one from
442 * their indices). Returns the element that was removed from the
443 * list.
445 * @param index the index of the element to removed.
447 * @return the element previously at the specified position.
449 * @exception IndexOutOfBoundsException if the index is out of
450 * range (index < 0 || index >= size()).
453 public short remove(final int index)
455 if (index >= _limit)
457 throw new IndexOutOfBoundsException();
459 short rval = _array[ index ];
461 System.arraycopy(_array, index + 1, _array, index, _limit - index);
462 _limit--;
463 return rval;
467 * Removes the first occurrence in this list of the specified
468 * element (optional operation). If this list does not contain
469 * the element, it is unchanged. More formally, removes the
470 * element with the lowest index i such that (o.equals(get(i)))
471 * (if such an element exists).
473 * @param o element to be removed from this list, if present.
475 * @return true if this list contained the specified element.
478 public boolean removeValue(final short o)
480 boolean rval = false;
482 for (int j = 0; !rval && (j < _limit); j++)
484 if (o == _array[ j ])
486 System.arraycopy(_array, j + 1, _array, j, _limit - j);
487 _limit--;
488 rval = true;
491 return rval;
495 * Removes from this list all the elements that are contained in
496 * the specified collection
498 * @param c collection that defines which elements will be removed
499 * from this list.
501 * @return true if this list changed as a result of the call.
504 public boolean removeAll(final ShortList c)
506 boolean rval = false;
508 for (int j = 0; j < c._limit; j++)
510 if (removeValue(c._array[ j ]))
512 rval = true;
515 return rval;
519 * Retains only the elements in this list that are contained in
520 * the specified collection. In other words, removes from this
521 * list all the elements that are not contained in the specified
522 * collection.
524 * @param c collection that defines which elements this set will
525 * retain.
527 * @return true if this list changed as a result of the call.
530 public boolean retainAll(final ShortList c)
532 boolean rval = false;
534 for (int j = 0; j < _limit; )
536 if (!c.contains(_array[ j ]))
538 remove(j);
539 rval = true;
541 else
543 j++;
546 return rval;
550 * Replaces the element at the specified position in this list
551 * with the specified element
553 * @param index index of element to replace.
554 * @param element element to be stored at the specified position.
556 * @return the element previously at the specified position.
558 * @exception IndexOutOfBoundsException if the index is out of
559 * range (index < 0 || index >= size()).
562 public short set(final int index, final short element)
564 if (index >= _limit)
566 throw new IndexOutOfBoundsException();
568 short rval = _array[ index ];
570 _array[ index ] = element;
571 return rval;
575 * Returns the number of elements in this list. If this list
576 * contains more than Integer.MAX_VALUE elements, returns
577 * Integer.MAX_VALUE.
579 * @return the number of elements in this ShortList
582 public int size()
584 return _limit;
588 * Returns an array containing all of the elements in this list in
589 * proper sequence. Obeys the general contract of the
590 * Collection.toArray method.
592 * @return an array containing all of the elements in this list in
593 * proper sequence.
596 public short [] toArray()
598 short[] rval = new short[ _limit ];
600 System.arraycopy(_array, 0, rval, 0, _limit);
601 return rval;
605 * Returns an array containing all of the elements in this list in
606 * proper sequence. Obeys the general contract of the
607 * Collection.toArray(Object[]) method.
609 * @param a the array into which the elements of this list are to
610 * be stored, if it is big enough; otherwise, a new array
611 * is allocated for this purpose.
613 * @return an array containing the elements of this list.
616 public short [] toArray(final short [] a)
618 short[] rval;
620 if (a.length == _limit)
622 System.arraycopy(_array, 0, a, 0, _limit);
623 rval = a;
625 else
627 rval = toArray();
629 return rval;
632 private void growArray(final int new_size)
634 int size = (new_size == _array.length) ? new_size + 1
635 : new_size;
636 short[] new_array = new short[ size ];
638 System.arraycopy(_array, 0, new_array, 0, _limit);
639 _array = new_array;
641 } // end public class ShortList