KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Iterator JavaDoc;
26
27 import com.sosnoski.util.ArrayRangeIterator;
28
29 /**
30  * Growable <code>Object</code> array with type specific access methods. This
31  * implementation is unsynchronized in order to provide the best possible
32  * performance for typical usage scenarios, so explicit synchronization must
33  * be implemented by a wrapper class or directly by the application in cases
34  * where instances are modified in a multithreaded environment. See the base
35  * classes for other details of the implementation.
36  *
37  * @author Dennis M. Sosnoski
38  * @version 1.1
39  */

40
41 public class ObjectArray extends ArrayBase
42 {
43     /** The underlying array used for storing the data. */
44     protected Object JavaDoc[] m_baseArray;
45
46     /**
47      * Constructor with full specification.
48      *
49      * @param size number of <code>Object</code> values initially allowed in
50      * array
51      * @param growth maximum size increment for growing array
52      */

53
54     public ObjectArray(int size, int growth) {
55         super(size, growth, Object JavaDoc.class);
56     }
57
58     /**
59      * Constructor with initial size specified.
60      *
61      * @param size number of chars to size array for initially
62      */

63
64     public ObjectArray(int size) {
65         super(size, Object JavaDoc.class);
66     }
67
68     /**
69      * Default constructor.
70      */

71
72     public ObjectArray() {
73         this(DEFAULT_SIZE);
74     }
75
76     /**
77      * Copy (clone) constructor.
78      *
79      * @param base instance being copied
80      */

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

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

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

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

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

139
140     public final Object JavaDoc get(int index) {
141         if (index < m_countPresent) {
142             return m_baseArray[index];
143         } else {
144             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
145         }
146     }
147
148     /**
149      * Set the value at an index position in the array. Note that this
150      * method can only be used for modifying an existing value in the
151      * array, not for appending to the end of the array.
152      *
153      * @param index index position to be set
154      * @param value value to be set
155      * @see #add
156      */

157
158     public final void set(int index, Object JavaDoc value) {
159         if (index < m_countPresent) {
160             m_baseArray[index] = value;
161         } else {
162             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
163         }
164     }
165
166     /**
167      * Return an iterator for the <code>Object</code>s in this array. The
168      * iterator returns all values in order, but is not "live". Values
169      * added to the array during iteration will not be returned by the
170      * iteration, and any other changes to the array while the iteration is
171      * in progress will give indeterminant results.
172      *
173      * @return iterator for values in array
174      */

175
176     public final Iterator JavaDoc iterator() {
177         return ArrayRangeIterator.buildIterator(m_baseArray, 0, m_countPresent);
178     }
179
180     /**
181      * Constructs and returns a simple array containing the same data as held
182      * in this growable array.
183      *
184      * @return array containing a copy of the data
185      */

186
187     public Object JavaDoc[] toArray() {
188         return (Object JavaDoc[]) buildArray(Object JavaDoc.class, 0, m_countPresent);
189     }
190
191     /**
192      * Constructs and returns a type-specific array containing the same data
193      * as held in this growable generic array. All values in this array must
194      * be assignment compatible with the specified type.
195      *
196      * @param type element type for constructed array
197      * @return array containing a copy of the data
198      */

199
200     public Object JavaDoc[] toArray(Class JavaDoc type) {
201         return (Object JavaDoc[]) buildArray(type, 0, m_countPresent);
202     }
203
204     /**
205      * Constructs and returns a simple array containing the same data as held
206      * in a portion of this growable array.
207      *
208      * @param offset start offset in array
209      * @param length number of characters to use
210      * @return array containing a copy of the data
211      */

212
213     public Object JavaDoc[] toArray(int offset, int length) {
214         return (Object JavaDoc[])buildArray(Object JavaDoc.class, offset, length);
215     }
216
217     /**
218      * Constructs and returns a type-specific array containing the same data
219      * as held in a portion of this growable generic array. All values in the
220      * requested portion of this array must be assignment compatible with the
221      * specified type.
222      *
223      * @param type element type for constructed array
224      * @param offset start offset in array
225      * @param length number of characters to use
226      * @return array containing a copy of the data
227      */

228
229     public Object JavaDoc[] toArray(Class JavaDoc type, int offset, int length) {
230         return (Object JavaDoc[])buildArray(type, offset, length);
231     }
232
233     /**
234      * Duplicates the object with the generic call.
235      *
236      * @return a copy of the object
237      */

238
239     public Object JavaDoc clone() {
240         return new ObjectArray(this);
241     }
242 }
243
Popular Tags