KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > IntegerArray


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 import com.sosnoski.util.array.ArrayBase;
24
25 /**
26  * Growable <code>Integer</code> array providing the same functionality as
27  * <code>java.util.Vector</code> but with type specific methods. This
28  * implementation is unsynchronized in order to provide the best possible
29  * performance for typical usage scenarios, so explicit synchronization must
30  * be implemented by a wrapper class or directly by the application in cases
31  * where instances are modified in a multithreaded environment. See the base
32  * classes for other details of the implementation.<p>
33  *
34  * This class is used for timing comparison tests but is not considered of
35  * general enough usefulness to be included in the standard set of growable
36  * arrays.
37  *
38  * @author Dennis M. Sosnoski
39  * @version 1.0
40  */

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

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

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

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

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

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

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

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

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

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

158     
159     public final void set(int index, Integer JavaDoc value) {
160         if (index < m_countPresent) {
161             m_baseArray[index] = value;
162         } else {
163             throw new ArrayIndexOutOfBoundsException JavaDoc("Invalid index value");
164         }
165     }
166
167     /**
168      * Constructs and returns a simple array containing the same data as held
169      * in this growable array.
170      *
171      * @return array containing a copy of the data
172      */

173     
174     public Integer JavaDoc[] toArray() {
175         return (Integer JavaDoc[]) buildArray(Integer JavaDoc.class, 0, m_countPresent);
176     }
177
178     /**
179      * Constructs and returns a simple array containing the same data as held
180      * in a portion of this growable array.
181      *
182      * @param offset start offset in array
183      * @param length number of characters to use
184      * @return array containing a copy of the data
185      */

186     
187     public Integer JavaDoc[] toArray(int offset, int length) {
188         return (Integer JavaDoc[]) buildArray(Integer JavaDoc.class, offset, length);
189     }
190
191     /**
192      * Duplicates the object with the generic call.
193      *
194      * @return a copy of the object
195      */

196     
197     public Object JavaDoc clone() {
198         return new IntegerArray(this);
199     }
200 }
201
Popular Tags