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 double'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: * double
033: * <li> wherever the List interface refers to a Collection or List,
034: * substitute DoubleList
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(int index)
043: * <li> subList is not supported
044: * </ul>
045: *
046: * @author Marc Johnson
047: */
048:
049: public class DoubleList {
050: private double[] _array;
051: private int _limit;
052: private static final int _default_size = 128;
053:
054: /**
055: * create an DoubleList of default size
056: */
057:
058: public DoubleList() {
059: this (_default_size);
060: }
061:
062: /**
063: * create a copy of an existing DoubleList
064: *
065: * @param list the existing DoubleList
066: */
067:
068: public DoubleList(final DoubleList 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 DoubleList with a predefined initial size
076: *
077: * @param initialCapacity the size for the internal array
078: */
079:
080: public DoubleList(final int initialCapacity) {
081: _array = new double[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 double 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 double 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 DoubleList 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 DoubleList 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 double 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 DoubleList 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: DoubleList other = (DoubleList) 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 double get(final int index) {
297: if (index >= _limit) {
298: throw new IndexOutOfBoundsException();
299: }
300: return _array[index];
301: }
302:
303: /**
304: * THIS MOST LIKELY DOES NOT WORK
305: * Returns the hash code value for this list. The hash code of a
306: * list is defined to be the result of the following calculation:
307: *
308: * <code>
309: * hashCode = 1;
310: * Iterator i = list.iterator();
311: * while (i.hasNext()) {
312: * Object obj = i.next();
313: * hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
314: * }
315: * </code>
316: *
317: * This ensures that list1.equals(list2) implies that
318: * list1.hashCode()==list2.hashCode() for any two lists, list1 and
319: * list2, as required by the general contract of Object.hashCode.
320: *
321: * @return the hash code value for this list.
322: */
323:
324: public int hashCode() {
325: int hash = 0;
326:
327: for (int j = 0; j < _limit; j++) {
328: hash = (31 * hash) + ((int) _array[j]);
329: }
330: return hash;
331: }
332:
333: /**
334: * Returns the index in this list of the first occurrence of the
335: * specified element, or -1 if this list does not contain this
336: * element. More formally, returns the lowest index i such that
337: * (o == get(i)), or -1 if there is no such index.
338: *
339: * @param o element to search for.
340: *
341: * @return the index in this list of the first occurrence of the
342: * specified element, or -1 if this list does not contain
343: * this element.
344: */
345:
346: public int indexOf(final double o) {
347: int rval = 0;
348:
349: for (; rval < _limit; rval++) {
350: if (o == _array[rval]) {
351: break;
352: }
353: }
354: if (rval == _limit) {
355: rval = -1; // didn't find it
356: }
357: return rval;
358: }
359:
360: /**
361: * Returns true if this list contains no elements.
362: *
363: * @return true if this list contains no elements.
364: */
365:
366: public boolean isEmpty() {
367: return _limit == 0;
368: }
369:
370: /**
371: * Returns the index in this list of the last occurrence of the
372: * specified element, or -1 if this list does not contain this
373: * element. More formally, returns the highest index i such that
374: * (o == get(i)), or -1 if there is no such index.
375: *
376: * @param o element to search for.
377: *
378: * @return the index in this list of the last occurrence of the
379: * specified element, or -1 if this list does not contain
380: * this element.
381: */
382:
383: public int lastIndexOf(final double o) {
384: int rval = _limit - 1;
385:
386: for (; rval >= 0; rval--) {
387: if (o == _array[rval]) {
388: break;
389: }
390: }
391: return rval;
392: }
393:
394: /**
395: * Removes the element at the specified position in this list.
396: * Shifts any subsequent elements to the left (subtracts one from
397: * their indices). Returns the element that was removed from the
398: * list.
399: *
400: * @param index the index of the element to removed.
401: *
402: * @return the element previously at the specified position.
403: *
404: * @exception IndexOutOfBoundsException if the index is out of
405: * range (index < 0 || index >= size()).
406: */
407:
408: public double remove(final int index) {
409: if (index >= _limit) {
410: throw new IndexOutOfBoundsException();
411: }
412: double rval = _array[index];
413:
414: System.arraycopy(_array, index + 1, _array, index, _limit
415: - index);
416: _limit--;
417: return rval;
418: }
419:
420: /**
421: * Removes the first occurrence in this list of the specified
422: * element (optional operation). If this list does not contain
423: * the element, it is unchanged. More formally, removes the
424: * element with the lowest index i such that (o.equals(get(i)))
425: * (if such an element exists).
426: *
427: * @param o element to be removed from this list, if present.
428: *
429: * @return true if this list contained the specified element.
430: */
431:
432: public boolean removeValue(final double o) {
433: boolean rval = false;
434:
435: for (int j = 0; !rval && (j < _limit); j++) {
436: if (o == _array[j]) {
437: System.arraycopy(_array, j + 1, _array, j, _limit - j);
438: _limit--;
439: rval = true;
440: }
441: }
442: return rval;
443: }
444:
445: /**
446: * Removes from this list all the elements that are contained in
447: * the specified collection
448: *
449: * @param c collection that defines which elements will be removed
450: * from this list.
451: *
452: * @return true if this list changed as a result of the call.
453: */
454:
455: public boolean removeAll(final DoubleList c) {
456: boolean rval = false;
457:
458: for (int j = 0; j < c._limit; j++) {
459: if (removeValue(c._array[j])) {
460: rval = true;
461: }
462: }
463: return rval;
464: }
465:
466: /**
467: * Retains only the elements in this list that are contained in
468: * the specified collection. In other words, removes from this
469: * list all the elements that are not contained in the specified
470: * collection.
471: *
472: * @param c collection that defines which elements this set will
473: * retain.
474: *
475: * @return true if this list changed as a result of the call.
476: */
477:
478: public boolean retainAll(final DoubleList c) {
479: boolean rval = false;
480:
481: for (int j = 0; j < _limit;) {
482: if (!c.contains(_array[j])) {
483: remove(j);
484: rval = true;
485: } else {
486: j++;
487: }
488: }
489: return rval;
490: }
491:
492: /**
493: * Replaces the element at the specified position in this list
494: * with the specified element
495: *
496: * @param index index of element to replace.
497: * @param element element to be stored at the specified position.
498: *
499: * @return the element previously at the specified position.
500: *
501: * @exception IndexOutOfBoundsException if the index is out of
502: * range (index < 0 || index >= size()).
503: */
504:
505: public double set(final int index, final double element) {
506: if (index >= _limit) {
507: throw new IndexOutOfBoundsException();
508: }
509: double rval = _array[index];
510:
511: _array[index] = element;
512: return rval;
513: }
514:
515: /**
516: * Returns the number of elements in this list. If this list
517: * contains more than Doubleeger.MAX_VALUE elements, returns
518: * Doubleeger.MAX_VALUE.
519: *
520: * @return the number of elements in this DoubleList
521: */
522:
523: public int size() {
524: return _limit;
525: }
526:
527: /**
528: * Returns an array containing all of the elements in this list in
529: * proper sequence. Obeys the general contract of the
530: * Collection.toArray method.
531: *
532: * @return an array containing all of the elements in this list in
533: * proper sequence.
534: */
535:
536: public double[] toArray() {
537: double[] rval = new double[_limit];
538:
539: System.arraycopy(_array, 0, rval, 0, _limit);
540: return rval;
541: }
542:
543: /**
544: * Returns an array containing all of the elements in this list in
545: * proper sequence. Obeys the general contract of the
546: * Collection.toArray(Object[]) method.
547: *
548: * @param a the array into which the elements of this list are to
549: * be stored, if it is big enough; otherwise, a new array
550: * is allocated for this purpose.
551: *
552: * @return an array containing the elements of this list.
553: */
554:
555: public double[] toArray(final double[] a) {
556: double[] rval;
557:
558: if (a.length == _limit) {
559: System.arraycopy(_array, 0, a, 0, _limit);
560: rval = a;
561: } else {
562: rval = toArray();
563: }
564: return rval;
565: }
566:
567: private void growArray(final int new_size) {
568: int size = (new_size == _array.length) ? new_size + 1
569: : new_size;
570: double[] new_array = new double[size];
571:
572: System.arraycopy(_array, 0, new_array, 0, _limit);
573: _array = new_array;
574: }
575: } // end public class DoubleList
|