001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import java.util.*;
021:
022: /**
023: * A List of short's; as full an implementation of the java.util.List
024: * interface as possible, with an eye toward minimal creation of
025: * objects
026: *
027: * the mimicry of List is as follows:
028: * <ul>
029: * <li> if possible, operations designated 'optional' in the List
030: * interface are attempted
031: * <li> wherever the List interface refers to an Object, substitute
032: * short
033: * <li> wherever the List interface refers to a Collection or List,
034: * substitute ShortList
035: * </ul>
036: *
037: * the mimicry is not perfect, however:
038: * <ul>
039: * <li> operations involving Iterators or ListIterators are not
040: * supported
041: * <li> remove(Object) becomes removeValue to distinguish it from
042: * remove(short index)
043: * <li> subList is not supported
044: * </ul>
045: *
046: * @author Marc Johnson
047: */
048:
049: public class ShortList {
050: private short[] _array;
051: private int _limit;
052: private static final int _default_size = 128;
053:
054: /**
055: * create an ShortList of default size
056: */
057:
058: public ShortList() {
059: this (_default_size);
060: }
061:
062: /**
063: * create a copy of an existing ShortList
064: *
065: * @param list the existing ShortList
066: */
067:
068: public ShortList(final ShortList list) {
069: this (list._array.length);
070: System.arraycopy(list._array, 0, _array, 0, _array.length);
071: _limit = list._limit;
072: }
073:
074: /**
075: * create an ShortList with a predefined initial size
076: *
077: * @param initialCapacity the size for the internal array
078: */
079:
080: public ShortList(final int initialCapacity) {
081: _array = new short[initialCapacity];
082: _limit = 0;
083: }
084:
085: /**
086: * add the specfied value at the specified index
087: *
088: * @param index the index where the new value is to be added
089: * @param value the new value
090: *
091: * @exception IndexOutOfBoundsException if the index is out of
092: * range (index < 0 || index > size()).
093: */
094:
095: public void add(final int index, final short value) {
096: if (index > _limit) {
097: throw new IndexOutOfBoundsException();
098: } else if (index == _limit) {
099: add(value);
100: } else {
101:
102: // index < limit -- insert into the middle
103: if (_limit == _array.length) {
104: growArray(_limit * 2);
105: }
106: System.arraycopy(_array, index, _array, index + 1, _limit
107: - index);
108: _array[index] = value;
109: _limit++;
110: }
111: }
112:
113: /**
114: * Appends the specified element to the end of this list
115: *
116: * @param value element to be appended to this list.
117: *
118: * @return true (as per the general contract of the Collection.add
119: * method).
120: */
121:
122: public boolean add(final short value) {
123: if (_limit == _array.length) {
124: growArray(_limit * 2);
125: }
126: _array[_limit++] = value;
127: return true;
128: }
129:
130: /**
131: * Appends all of the elements in the specified collection to the
132: * end of this list, in the order that they are returned by the
133: * specified collection's iterator. The behavior of this
134: * operation is unspecified if the specified collection is
135: * modified while the operation is in progress. (Note that this
136: * will occur if the specified collection is this list, and it's
137: * nonempty.)
138: *
139: * @param c collection whose elements are to be added to this
140: * list.
141: *
142: * @return true if this list changed as a result of the call.
143: */
144:
145: public boolean addAll(final ShortList c) {
146: if (c._limit != 0) {
147: if ((_limit + c._limit) > _array.length) {
148: growArray(_limit + c._limit);
149: }
150: System.arraycopy(c._array, 0, _array, _limit, c._limit);
151: _limit += c._limit;
152: }
153: return true;
154: }
155:
156: /**
157: * Inserts all of the elements in the specified collection into
158: * this list at the specified position. Shifts the element
159: * currently at that position (if any) and any subsequent elements
160: * to the right (increases their indices). The new elements will
161: * appear in this list in the order that they are returned by the
162: * specified collection's iterator. The behavior of this
163: * operation is unspecified if the specified collection is
164: * modified while the operation is in progress. (Note that this
165: * will occur if the specified collection is this list, and it's
166: * nonempty.)
167: *
168: * @param index index at which to insert first element from the
169: * specified collection.
170: * @param c elements to be inserted into this list.
171: *
172: * @return true if this list changed as a result of the call.
173: *
174: * @exception IndexOutOfBoundsException if the index is out of
175: * range (index < 0 || index > size())
176: */
177:
178: public boolean addAll(final int index, final ShortList c) {
179: if (index > _limit) {
180: throw new IndexOutOfBoundsException();
181: }
182: if (c._limit != 0) {
183: if ((_limit + c._limit) > _array.length) {
184: growArray(_limit + c._limit);
185: }
186:
187: // make a hole
188: System.arraycopy(_array, index, _array, index + c._limit,
189: _limit - index);
190:
191: // fill it in
192: System.arraycopy(c._array, 0, _array, index, c._limit);
193: _limit += c._limit;
194: }
195: return true;
196: }
197:
198: /**
199: * Removes all of the elements from this list. This list will be
200: * empty after this call returns (unless it throws an exception).
201: */
202:
203: public void clear() {
204: _limit = 0;
205: }
206:
207: /**
208: * Returns true if this list contains the specified element. More
209: * formally, returns true if and only if this list contains at
210: * least one element e such that o == e
211: *
212: * @param o element whose presence in this list is to be tested.
213: *
214: * @return true if this list contains the specified element.
215: */
216:
217: public boolean contains(final short o) {
218: boolean rval = false;
219:
220: for (int j = 0; !rval && (j < _limit); j++) {
221: if (_array[j] == o) {
222: rval = true;
223: }
224: }
225: return rval;
226: }
227:
228: /**
229: * Returns true if this list contains all of the elements of the
230: * specified collection.
231: *
232: * @param c collection to be checked for containment in this list.
233: *
234: * @return true if this list contains all of the elements of the
235: * specified collection.
236: */
237:
238: public boolean containsAll(final ShortList c) {
239: boolean rval = true;
240:
241: if (this != c) {
242: for (int j = 0; rval && (j < c._limit); j++) {
243: if (!contains(c._array[j])) {
244: rval = false;
245: }
246: }
247: }
248: return rval;
249: }
250:
251: /**
252: * Compares the specified object with this list for equality.
253: * Returns true if and only if the specified object is also a
254: * list, both lists have the same size, and all corresponding
255: * pairs of elements in the two lists are equal. (Two elements e1
256: * and e2 are equal if e1 == e2.) In other words, two lists are
257: * defined to be equal if they contain the same elements in the
258: * same order. This definition ensures that the equals method
259: * works properly across different implementations of the List
260: * interface.
261: *
262: * @param o the object to be compared for equality with this list.
263: *
264: * @return true if the specified object is equal to this list.
265: */
266:
267: public boolean equals(final Object o) {
268: boolean rval = this == o;
269:
270: if (!rval && (o != null) && (o.getClass() == this .getClass())) {
271: ShortList other = (ShortList) o;
272:
273: if (other._limit == _limit) {
274:
275: // assume match
276: rval = true;
277: for (int j = 0; rval && (j < _limit); j++) {
278: rval = _array[j] == other._array[j];
279: }
280: }
281: }
282: return rval;
283: }
284:
285: /**
286: * Returns the element at the specified position in this list.
287: *
288: * @param index index of element to return.
289: *
290: * @return the element at the specified position in this list.
291: *
292: * @exception IndexOutOfBoundsException if the index is out of
293: * range (index < 0 || index >= size()).
294: */
295:
296: public short get(final int index) {
297: if (index >= _limit) {
298: throw new IndexOutOfBoundsException();
299: }
300: return _array[index];
301: }
302:
303: /**
304: * Returns the hash code value for this list. The hash code of a
305: * list is defined to be the result of the following calculation:
306: *
307: * <code>
308: * hashCode = 1;
309: * Iterator i = list.iterator();
310: * while (i.hasNext()) {
311: * Object obj = i.next();
312: * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
313: * }
314: * </code>
315: *
316: * This ensures that list1.equals(list2) implies that
317: * list1.hashCode()==list2.hashCode() for any two lists, list1 and
318: * list2, as required by the general contract of Object.hashCode.
319: *
320: * @return the hash code value for this list.
321: */
322:
323: public int hashCode() {
324: int hash = 0;
325:
326: for (int j = 0; j < _limit; j++) {
327: hash = (31 * hash) + _array[j];
328: }
329: return hash;
330: }
331:
332: /**
333: * Returns the index in this list of the first occurrence of the
334: * specified element, or -1 if this list does not contain this
335: * element. More formally, returns the lowest index i such that
336: * (o == get(i)), or -1 if there is no such index.
337: *
338: * @param o element to search for.
339: *
340: * @return the index in this list of the first occurrence of the
341: * specified element, or -1 if this list does not contain
342: * this element.
343: */
344:
345: public int indexOf(final short o) {
346: int rval = 0;
347:
348: for (; rval < _limit; rval++) {
349: if (o == _array[rval]) {
350: break;
351: }
352: }
353: if (rval == _limit) {
354: rval = -1; // didn't find it
355: }
356: return rval;
357: }
358:
359: /**
360: * Returns true if this list contains no elements.
361: *
362: * @return true if this list contains no elements.
363: */
364:
365: public boolean isEmpty() {
366: return _limit == 0;
367: }
368:
369: /**
370: * Returns the index in this list of the last occurrence of the
371: * specified element, or -1 if this list does not contain this
372: * element. More formally, returns the highest index i such that
373: * (o == get(i)), or -1 if there is no such index.
374: *
375: * @param o element to search for.
376: *
377: * @return the index in this list of the last occurrence of the
378: * specified element, or -1 if this list does not contain
379: * this element.
380: */
381:
382: public int lastIndexOf(final short o) {
383: int rval = _limit - 1;
384:
385: for (; rval >= 0; rval--) {
386: if (o == _array[rval]) {
387: break;
388: }
389: }
390: return rval;
391: }
392:
393: /**
394: * Removes the element at the specified position in this list.
395: * Shifts any subsequent elements to the left (subtracts one from
396: * their indices). Returns the element that was removed from the
397: * list.
398: *
399: * @param index the index of the element to removed.
400: *
401: * @return the element previously at the specified position.
402: *
403: * @exception IndexOutOfBoundsException if the index is out of
404: * range (index < 0 || index >= size()).
405: */
406:
407: public short remove(final int index) {
408: if (index >= _limit) {
409: throw new IndexOutOfBoundsException();
410: }
411: short rval = _array[index];
412:
413: System.arraycopy(_array, index + 1, _array, index, _limit
414: - index);
415: _limit--;
416: return rval;
417: }
418:
419: /**
420: * Removes the first occurrence in this list of the specified
421: * element (optional operation). If this list does not contain
422: * the element, it is unchanged. More formally, removes the
423: * element with the lowest index i such that (o.equals(get(i)))
424: * (if such an element exists).
425: *
426: * @param o element to be removed from this list, if present.
427: *
428: * @return true if this list contained the specified element.
429: */
430:
431: public boolean removeValue(final short o) {
432: boolean rval = false;
433:
434: for (int j = 0; !rval && (j < _limit); j++) {
435: if (o == _array[j]) {
436: System.arraycopy(_array, j + 1, _array, j, _limit - j);
437: _limit--;
438: rval = true;
439: }
440: }
441: return rval;
442: }
443:
444: /**
445: * Removes from this list all the elements that are contained in
446: * the specified collection
447: *
448: * @param c collection that defines which elements will be removed
449: * from this list.
450: *
451: * @return true if this list changed as a result of the call.
452: */
453:
454: public boolean removeAll(final ShortList c) {
455: boolean rval = false;
456:
457: for (int j = 0; j < c._limit; j++) {
458: if (removeValue(c._array[j])) {
459: rval = true;
460: }
461: }
462: return rval;
463: }
464:
465: /**
466: * Retains only the elements in this list that are contained in
467: * the specified collection. In other words, removes from this
468: * list all the elements that are not contained in the specified
469: * collection.
470: *
471: * @param c collection that defines which elements this set will
472: * retain.
473: *
474: * @return true if this list changed as a result of the call.
475: */
476:
477: public boolean retainAll(final ShortList c) {
478: boolean rval = false;
479:
480: for (int j = 0; j < _limit;) {
481: if (!c.contains(_array[j])) {
482: remove(j);
483: rval = true;
484: } else {
485: j++;
486: }
487: }
488: return rval;
489: }
490:
491: /**
492: * Replaces the element at the specified position in this list
493: * with the specified element
494: *
495: * @param index index of element to replace.
496: * @param element element to be stored at the specified position.
497: *
498: * @return the element previously at the specified position.
499: *
500: * @exception IndexOutOfBoundsException if the index is out of
501: * range (index < 0 || index >= size()).
502: */
503:
504: public short set(final int index, final short element) {
505: if (index >= _limit) {
506: throw new IndexOutOfBoundsException();
507: }
508: short rval = _array[index];
509:
510: _array[index] = element;
511: return rval;
512: }
513:
514: /**
515: * Returns the number of elements in this list. If this list
516: * contains more than Integer.MAX_VALUE elements, returns
517: * Integer.MAX_VALUE.
518: *
519: * @return the number of elements in this ShortList
520: */
521:
522: public int size() {
523: return _limit;
524: }
525:
526: /**
527: * Returns an array containing all of the elements in this list in
528: * proper sequence. Obeys the general contract of the
529: * Collection.toArray method.
530: *
531: * @return an array containing all of the elements in this list in
532: * proper sequence.
533: */
534:
535: public short[] toArray() {
536: short[] rval = new short[_limit];
537:
538: System.arraycopy(_array, 0, rval, 0, _limit);
539: return rval;
540: }
541:
542: /**
543: * Returns an array containing all of the elements in this list in
544: * proper sequence. Obeys the general contract of the
545: * Collection.toArray(Object[]) method.
546: *
547: * @param a the array into which the elements of this list are to
548: * be stored, if it is big enough; otherwise, a new array
549: * is allocated for this purpose.
550: *
551: * @return an array containing the elements of this list.
552: */
553:
554: public short[] toArray(final short[] a) {
555: short[] rval;
556:
557: if (a.length == _limit) {
558: System.arraycopy(_array, 0, a, 0, _limit);
559: rval = a;
560: } else {
561: rval = toArray();
562: }
563: return rval;
564: }
565:
566: private void growArray(final int new_size) {
567: int size = (new_size == _array.length) ? new_size + 1
568: : new_size;
569: short[] new_array = new short[size];
570:
571: System.arraycopy(_array, 0, new_array, 0, _limit);
572: _array = new_array;
573: }
574: } // end public class ShortList
|