KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sosnoski > util > array > DoubleArray


1 /*
2  * Copyright (c) 2000-2001 Sosnoski Software Solutions, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */

22
23 package com.sosnoski.util.array;
24
25 /**
26  * Growable <code>double</code> array with type specific access methods. This
27  * implementation is unsynchronized in order to provide the best possible
28  * performance for typical usage scenarios, so explicit synchronization must
29  * be implemented by a wrapper class or directly by the application in cases
30  * where instances are modified in a multithreaded environment. See the base
31  * classes for other details of the implementation.
32  *
33  * @author Dennis M. Sosnoski
34  * @version 1.0
35  */

36
37 public class DoubleArray extends ArrayBase
38 {
39     /** The underlying array used for storing the data. */
40     protected double[] m_baseArray;
41
42     /**
43      * Constructor with full specification.
44      *
45      * @param size number of <code>double</code> values initially allowed in
46      * array
47      * @param growth maximum size increment for growing array
48      */

49     
50     public DoubleArray(int size, int growth) {
51         super(size, growth, double.class);
52     }
53
54     /**
55      * Constructor with initial size specified.
56      *
57      * @param size number of <code>double</code> values initially allowed in
58      * array
59      */

60     
61     public DoubleArray(int size) {
62         super(size, double.class);
63     }
64
65     /**
66      * Default constructor.
67      */

68     
69     public DoubleArray() {
70         this(DEFAULT_SIZE);
71     }
72
73     /**
74      * Copy (clone) constructor.
75      *
76      * @param base instance being copied
77      */

78     
79     public DoubleArray(DoubleArray base) {
80         super(base);
81     }
82
83     /**
84      * Get the backing array. This method is used by the type-agnostic base
85      * class code to access the array used for type-specific storage.
86      *
87      * @return backing array object
88      */

89     
90     protected final Object JavaDoc getArray() {
91         return m_baseArray;
92     }
93
94     /**
95      * Set the backing array. This method is used by the type-agnostic base
96      * class code to set the array used for type-specific storage.
97      *
98      * @param backing array object
99      */

100     
101     protected final void setArray(Object JavaDoc array) {
102         m_baseArray = (double[]) array;
103     }
104
105     /**
106      * Add a value to the array, appending it after the current values.
107      *
108      * @param value value to be added
109      * @return index number of added element
110      */

111     
112     public final int add(double value) {
113         int index = getAddIndex();
114         m_baseArray[index] = value;
115         return index;
116     }
117
118     /**
119      * Add a value at a specified index in the array.
120      *
121      * @param index index position at which to insert element
122      * @param value value to be inserted into array
123      */

124     
125     public void add(int index, double value) {
126         makeInsertSpace(index);
127         m_baseArray[index] = value;
128     }
129
130     /**
131      * Retrieve the value present at an index position in the array.
132      *
133      * @param index index position for value to be retrieved
134      * @return value from position in the array
135      */

136     
137     public final double get(int index) {
138         if (index < m_countPresent) {
139             return m_baseArray[index];
140         } else {
141             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
142         }
143     }
144
145     /**
146      * Set the value at an index position in the array.
147      *
148      * @param index index position to be set
149      * @param value value to be set
150      */

151     
152     public final void set(int index, double value) {
153         if (index < m_countPresent) {
154             m_baseArray[index] = value;
155         } else {
156             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
157         }
158     }
159
160     /**
161      * Constructs and returns a simple array containing the same data as held
162      * in this growable array.
163      *
164      * @return array containing a copy of the data
165      */

166     
167     public double[] toArray() {
168         return (double[])buildArray(double.class, 0, m_countPresent);
169     }
170
171     /**
172      * Constructs and returns a simple array containing the same data as held
173      * in a portion of this growable array.
174      *
175      * @param offset start offset in array
176      * @param length number of doubles to use
177      * @return array containing a copy of the data
178      */

179     
180     public double[] toArray(int offset, int length) {
181         return (double[])buildArray(double.class, offset, length);
182     }
183
184     /**
185      * Duplicates the object with the generic call.
186      *
187      * @return a copy of the object
188      */

189     
190     public Object JavaDoc clone() {
191         return new DoubleArray(this);
192     }
193 }
194
Popular Tags