KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > DoubleList


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.util;
20
21 import java.util.*;
22
23 /**
24  * A List of double's; as full an implementation of the java.util.List
25  * interface as possible, with an eye toward minimal creation of
26  * objects
27  *
28  * the mimicry of List is as follows:
29  * <ul>
30  * <li> if possible, operations designated 'optional' in the List
31  * interface are attempted
32  * <li> wherever the List interface refers to an Object, substitute
33  * double
34  * <li> wherever the List interface refers to a Collection or List,
35  * substitute DoubleList
36  * </ul>
37  *
38  * the mimicry is not perfect, however:
39  * <ul>
40  * <li> operations involving Iterators or ListIterators are not
41  * supported
42  * <li> remove(Object) becomes removeValue to distinguish it from
43  * remove(int index)
44  * <li> subList is not supported
45  * </ul>
46  *
47  * @author Marc Johnson
48  */

49
50 public class DoubleList
51 {
52     private double[] _array;
53     private int _limit;
54     private static final int _default_size = 128;
55
56     /**
57      * create an DoubleList of default size
58      */

59
60     public DoubleList()
61     {
62         this(_default_size);
63     }
64
65     /**
66      * create a copy of an existing DoubleList
67      *
68      * @param list the existing DoubleList
69      */

70
71     public DoubleList(final DoubleList list)
72     {
73         this(list._array.length);
74         System.arraycopy(list._array, 0, _array, 0, _array.length);
75         _limit = list._limit;
76     }
77
78     /**
79      * create an DoubleList with a predefined initial size
80      *
81      * @param initialCapacity the size for the internal array
82      */

83
84     public DoubleList(final int initialCapacity)
85     {
86         _array = new double[ initialCapacity ];
87         _limit = 0;
88     }
89
90     /**
91      * add the specfied value at the specified index
92      *
93      * @param index the index where the new value is to be added
94      * @param value the new value
95      *
96      * @exception IndexOutOfBoundsException if the index is out of
97      * range (index < 0 || index > size()).
98      */

99
100     public void add(final int index, final double value)
101     {
102         if (index > _limit)
103         {
104             throw new IndexOutOfBoundsException JavaDoc();
105         }
106         else if (index == _limit)
107         {
108             add(value);
109         }
110         else
111         {
112
113             // index < limit -- insert into the middle
114
if (_limit == _array.length)
115             {
116                 growArray(_limit * 2);
117             }
118             System.arraycopy(_array, index, _array, index + 1,
119                              _limit - index);
120             _array[ index ] = value;
121             _limit++;
122         }
123     }
124
125     /**
126      * Appends the specified element to the end of this list
127      *
128      * @param value element to be appended to this list.
129      *
130      * @return true (as per the general contract of the Collection.add
131      * method).
132      */

133
134     public boolean add(final double value)
135     {
136         if (_limit == _array.length)
137         {
138             growArray(_limit * 2);
139         }
140         _array[ _limit++ ] = value;
141         return true;
142     }
143
144     /**
145      * Appends all of the elements in the specified collection to the
146      * end of this list, in the order that they are returned by the
147      * specified collection's iterator. The behavior of this
148      * operation is unspecified if the specified collection is
149      * modified while the operation is in progress. (Note that this
150      * will occur if the specified collection is this list, and it's
151      * nonempty.)
152      *
153      * @param c collection whose elements are to be added to this
154      * list.
155      *
156      * @return true if this list changed as a result of the call.
157      */

158
159     public boolean addAll(final DoubleList c)
160     {
161         if (c._limit != 0)
162         {
163             if ((_limit + c._limit) > _array.length)
164             {
165                 growArray(_limit + c._limit);
166             }
167             System.arraycopy(c._array, 0, _array, _limit, c._limit);
168             _limit += c._limit;
169         }
170         return true;
171     }
172
173     /**
174      * Inserts all of the elements in the specified collection into
175      * this list at the specified position. Shifts the element
176      * currently at that position (if any) and any subsequent elements
177      * to the right (increases their indices). The new elements will
178      * appear in this list in the order that they are returned by the
179      * specified collection's iterator. The behavior of this
180      * operation is unspecified if the specified collection is
181      * modified while the operation is in progress. (Note that this
182      * will occur if the specified collection is this list, and it's
183      * nonempty.)
184      *
185      * @param index index at which to insert first element from the
186      * specified collection.
187      * @param c elements to be inserted into this list.
188      *
189      * @return true if this list changed as a result of the call.
190      *
191      * @exception IndexOutOfBoundsException if the index is out of
192      * range (index < 0 || index > size())
193      */

194
195     public boolean addAll(final int index, final DoubleList c)
196     {
197         if (index > _limit)
198         {
199             throw new IndexOutOfBoundsException JavaDoc();
200         }
201         if (c._limit != 0)
202         {
203             if ((_limit + c._limit) > _array.length)
204             {
205                 growArray(_limit + c._limit);
206             }
207
208             // make a hole
209
System.arraycopy(_array, index, _array, index + c._limit,
210                              _limit - index);
211
212             // fill it in
213
System.arraycopy(c._array, 0, _array, index, c._limit);
214             _limit += c._limit;
215         }
216         return true;
217     }
218
219     /**
220      * Removes all of the elements from this list. This list will be
221      * empty after this call returns (unless it throws an exception).
222      */

223
224     public void clear()
225     {
226         _limit = 0;
227     }
228
229     /**
230      * Returns true if this list contains the specified element. More
231      * formally, returns true if and only if this list contains at
232      * least one element e such that o == e
233      *
234      * @param o element whose presence in this list is to be tested.
235      *
236      * @return true if this list contains the specified element.
237      */

238
239     public boolean contains(final double o)
240     {
241         boolean rval = false;
242
243         for (int j = 0; !rval && (j < _limit); j++)
244         {
245             if (_array[ j ] == o)
246             {
247                 rval = true;
248             }
249         }
250         return rval;
251     }
252
253     /**
254      * Returns true if this list contains all of the elements of the
255      * specified collection.
256      *
257      * @param c collection to be checked for containment in this list.
258      *
259      * @return true if this list contains all of the elements of the
260      * specified collection.
261      */

262
263     public boolean containsAll(final DoubleList c)
264     {
265         boolean rval = true;
266
267         if (this != c)
268         {
269             for (int j = 0; rval && (j < c._limit); j++)
270             {
271                 if (!contains(c._array[ j ]))
272                 {
273                     rval = false;
274                 }
275             }
276         }
277         return rval;
278     }
279
280     /**
281      * Compares the specified object with this list for equality.
282      * Returns true if and only if the specified object is also a
283      * list, both lists have the same size, and all corresponding
284      * pairs of elements in the two lists are equal. (Two elements e1
285      * and e2 are equal if e1 == e2.) In other words, two lists are
286      * defined to be equal if they contain the same elements in the
287      * same order. This definition ensures that the equals method
288      * works properly across different implementations of the List
289      * interface.
290      *
291      * @param o the object to be compared for equality with this list.
292      *
293      * @return true if the specified object is equal to this list.
294      */

295
296     public boolean equals(final Object JavaDoc o)
297     {
298         boolean rval = this == o;
299
300         if (!rval && (o != null) && (o.getClass() == this.getClass()))
301         {
302             DoubleList other = ( DoubleList ) o;
303
304             if (other._limit == _limit)
305             {
306
307                 // assume match
308
rval = true;
309                 for (int j = 0; rval && (j < _limit); j++)
310                 {
311                     rval = _array[ j ] == other._array[ j ];
312                 }
313             }
314         }
315         return rval;
316     }
317
318     /**
319      * Returns the element at the specified position in this list.
320      *
321      * @param index index of element to return.
322      *
323      * @return the element at the specified position in this list.
324      *
325      * @exception IndexOutOfBoundsException if the index is out of
326      * range (index < 0 || index >= size()).
327      */

328
329     public double get(final int index)
330     {
331         if (index >= _limit)
332         {
333             throw new IndexOutOfBoundsException JavaDoc();
334         }
335         return _array[ index ];
336     }
337
338     /**
339      * THIS MOST LIKELY DOES NOT WORK
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:
342      *
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());
349      * }
350      * </code>
351      *
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.
355      *
356      * @return the hash code value for this list.
357      */

358
359     public int hashCode()
360     {
361         int hash = 0;
362
363         for (int j = 0; j < _limit; j++)
364         {
365             hash = (31 * hash) + ((int) _array[ j ]);
366         }
367         return hash;
368     }
369
370     /**
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.
375      *
376      * @param o element to search for.
377      *
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.
381      */

382
383     public int indexOf(final double o)
384     {
385         int rval = 0;
386
387         for (; rval < _limit; rval++)
388         {
389             if (o == _array[ rval ])
390             {
391                 break;
392             }
393         }
394         if (rval == _limit)
395         {
396             rval = -1; // didn't find it
397
}
398         return rval;
399     }
400
401     /**
402      * Returns true if this list contains no elements.
403      *
404      * @return true if this list contains no elements.
405      */

406
407     public boolean isEmpty()
408     {
409         return _limit == 0;
410     }
411
412     /**
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.
417      *
418      * @param o element to search for.
419      *
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.
423      */

424
425     public int lastIndexOf(final double o)
426     {
427         int rval = _limit - 1;
428
429         for (; rval >= 0; rval--)
430         {
431             if (o == _array[ rval ])
432             {
433                 break;
434             }
435         }
436         return rval;
437     }
438
439     /**
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.
444      *
445      * @param index the index of the element to removed.
446      *
447      * @return the element previously at the specified position.
448      *
449      * @exception IndexOutOfBoundsException if the index is out of
450      * range (index < 0 || index >= size()).
451      */

452
453     public double remove(final int index)
454     {
455         if (index >= _limit)
456         {
457             throw new IndexOutOfBoundsException JavaDoc();
458         }
459         double rval = _array[ index ];
460
461         System.arraycopy(_array, index + 1, _array, index, _limit - index);
462         _limit--;
463         return rval;
464     }
465
466     /**
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).
472      *
473      * @param o element to be removed from this list, if present.
474      *
475      * @return true if this list contained the specified element.
476      */

477
478     public boolean removeValue(final double o)
479     {
480         boolean rval = false;
481
482         for (int j = 0; !rval && (j < _limit); j++)
483         {
484             if (o == _array[ j ])
485             {
486                 System.arraycopy(_array, j + 1, _array, j, _limit - j);
487                 _limit--;
488                 rval = true;
489             }
490         }
491         return rval;
492     }
493
494     /**
495      * Removes from this list all the elements that are contained in
496      * the specified collection
497      *
498      * @param c collection that defines which elements will be removed
499      * from this list.
500      *
501      * @return true if this list changed as a result of the call.
502      */

503
504     public boolean removeAll(final DoubleList c)
505     {
506         boolean rval = false;
507
508         for (int j = 0; j < c._limit; j++)
509         {
510             if (removeValue(c._array[ j ]))
511             {
512                 rval = true;
513             }
514         }
515         return rval;
516     }
517
518     /**
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.
523      *
524      * @param c collection that defines which elements this set will
525      * retain.
526      *
527      * @return true if this list changed as a result of the call.
528      */

529
530     public boolean retainAll(final DoubleList c)
531     {
532         boolean rval = false;
533
534         for (int j = 0; j < _limit; )
535         {
536             if (!c.contains(_array[ j ]))
537             {
538                 remove(j);
539                 rval = true;
540             }
541             else
542             {
543                 j++;
544             }
545         }
546         return rval;
547     }
548
549     /**
550      * Replaces the element at the specified position in this list
551      * with the specified element
552      *
553      * @param index index of element to replace.
554      * @param element element to be stored at the specified position.
555      *
556      * @return the element previously at the specified position.
557      *
558      * @exception IndexOutOfBoundsException if the index is out of
559      * range (index < 0 || index >= size()).
560      */

561
562     public double set(final int index, final double element)
563     {
564         if (index >= _limit)
565         {
566             throw new IndexOutOfBoundsException JavaDoc();
567         }
568         double rval = _array[ index ];
569
570         _array[ index ] = element;
571         return rval;
572     }
573
574     /**
575      * Returns the number of elements in this list. If this list
576      * contains more than Doubleeger.MAX_VALUE elements, returns
577      * Doubleeger.MAX_VALUE.
578      *
579      * @return the number of elements in this DoubleList
580      */

581
582     public int size()
583     {
584         return _limit;
585     }
586
587     /**
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.
591      *
592      * @return an array containing all of the elements in this list in
593      * proper sequence.
594      */

595
596     public double [] toArray()
597     {
598         double[] rval = new double[ _limit ];
599
600         System.arraycopy(_array, 0, rval, 0, _limit);
601         return rval;
602     }
603
604     /**
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.
608      *
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.
612      *
613      * @return an array containing the elements of this list.
614      */

615
616     public double [] toArray(final double [] a)
617     {
618         double[] rval;
619
620         if (a.length == _limit)
621         {
622             System.arraycopy(_array, 0, a, 0, _limit);
623             rval = a;
624         }
625         else
626         {
627             rval = toArray();
628         }
629         return rval;
630     }
631
632     private void growArray(final int new_size)
633     {
634         int size = (new_size == _array.length) ? new_size + 1
635                                                       : new_size;
636         double[] new_array = new double[ size ];
637
638         System.arraycopy(_array, 0, new_array, 0, _limit);
639         _array = new_array;
640     }
641 } // end public class DoubleList
642
Popular Tags