KickJava   Java API By Example, From Geeks To Geeks.

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


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>int</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 IntArray extends ArrayBase
38 {
39     /** The underlying array used for storing the data. */
40     protected int[] m_baseArray;
41
42     /**
43      * Constructor with full specification.
44      *
45      * @param size number of <code>int</code> values initially allowed in array
46      * @param growth maximum size increment for growing array
47      */

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

58     
59     public IntArray(int size) {
60         super(size, int.class);
61     }
62
63     /**
64      * Default constructor.
65      */

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

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

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

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

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

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

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

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

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

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

187     
188     public Object JavaDoc clone() {
189         return new IntArray(this);
190     }
191 }
192
Popular Tags