KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > lib > sql > ObjectArray


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

16 /*
17  * $Id: ObjectArray.java,v 1.7 2004/02/11 17:56:35 minchau Exp $
18  */

19 package org.apache.xalan.lib.sql;
20
21 import java.util.Vector JavaDoc;
22
23 /**
24  * Provide a simple Array storage mechinsim where native Arrays will be use as
25  * the basic storage mechinism but the Arrays will be stored as blocks.
26  * The size of the Array blocks is determine during object construction.
27  * This is intended to be a simple storage mechinsim where the storage only
28  * can grow. Array elements can not be removed, only added to.
29  */

30 public class ObjectArray
31 {
32   /**
33    */

34   private int m_minArraySize = 10;
35   /**
36    * The container of all the sub arrays
37    */

38   private Vector JavaDoc m_Arrays = new Vector JavaDoc(200);
39
40   /**
41    * An index that porvides the Vector entry for the current Array that is
42    * being appended to.
43    */

44   private _ObjectArray m_currentArray;
45
46
47   /**
48    * The next offset in the current Array to append a new object
49    */

50   private int m_nextSlot;
51
52
53   /**
54    */

55   public ObjectArray( )
56   {
57     //
58
// Default constructor will work with a minimal fixed size
59
//
60
init(10);
61   }
62
63   /**
64    * @param minArraySize The size of the Arrays stored in the Vector
65    */

66   public ObjectArray( final int minArraySize )
67   {
68     init(minArraySize);
69   }
70
71   /**
72    * @param size
73    *
74    */

75   private void init( int size )
76   {
77     m_minArraySize = size;
78     m_currentArray = new _ObjectArray(m_minArraySize);
79   }
80
81   /**
82    * @param idx Index of the Object in the Array
83    *
84    */

85   public Object JavaDoc getAt( final int idx )
86   {
87     int arrayIndx = idx / m_minArraySize;
88     int arrayOffset = idx - (arrayIndx * m_minArraySize);
89
90     //
91
// If the array has been off loaded to the Vector Storage them
92
// grab it from there.
93
if (arrayIndx < m_Arrays.size())
94     {
95       _ObjectArray a = (_ObjectArray)m_Arrays.elementAt(arrayIndx);
96       return a.objects[arrayOffset];
97     }
98     else
99     {
100       // We must be in the current array, so pull it from there
101

102       // %REVIEW% We may want to check to see if arrayIndx is only
103
// one freater that the m_Arrays.size(); This code is safe but
104
// will repete if the index is greater than the array size.
105
return m_currentArray.objects[arrayOffset];
106     }
107   }
108
109   /**
110    * @param idx Index of the Object in the Array
111    * @param obj , The value to set in the Array
112    *
113    */

114   public void setAt( final int idx, final Object JavaDoc obj )
115   {
116     int arrayIndx = idx / m_minArraySize;
117     int arrayOffset = idx - (arrayIndx * m_minArraySize);
118
119     //
120
// If the array has been off loaded to the Vector Storage them
121
// grab it from there.
122
if (arrayIndx < m_Arrays.size())
123     {
124       _ObjectArray a = (_ObjectArray)m_Arrays.elementAt(arrayIndx);
125       a.objects[arrayOffset] = obj;
126     }
127     else
128     {
129       // We must be in the current array, so pull it from there
130

131       // %REVIEW% We may want to check to see if arrayIndx is only
132
// one freater that the m_Arrays.size(); This code is safe but
133
// will repete if the index is greater than the array size.
134
m_currentArray.objects[arrayOffset] = obj;
135     }
136   }
137
138
139
140   /**
141    * @param o Object to be appended to the Array
142    *
143    */

144   public int append( Object JavaDoc o )
145   {
146     if (m_nextSlot >= m_minArraySize)
147     {
148       m_Arrays.addElement(m_currentArray);
149       m_nextSlot = 0;
150       m_currentArray = new _ObjectArray(m_minArraySize);
151     }
152
153     m_currentArray.objects[m_nextSlot] = o;
154
155     int pos = (m_Arrays.size() * m_minArraySize) + m_nextSlot;
156
157     m_nextSlot++;
158
159     return pos;
160   }
161
162
163   /**
164    */

165   class _ObjectArray
166   {
167     /**
168      */

169     public Object JavaDoc[] objects;
170     /**
171      * @param size
172      */

173     public _ObjectArray( int size )
174     {
175       objects = new Object JavaDoc[size];
176     }
177   }
178
179   /**
180    * @param args
181    *
182    */

183   public static void main( String JavaDoc[] args )
184   {
185     String JavaDoc[] word={
186       "Zero","One","Two","Three","Four","Five",
187       "Six","Seven","Eight","Nine","Ten",
188       "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
189       "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
190       "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
191       "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
192       "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
193       "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
194       "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
195
196     ObjectArray m_ObjectArray = new ObjectArray();
197     // Add them in, using the default block size
198
for (int x =0; x< word.length; x++)
199     {
200       System.out.print(" - " + m_ObjectArray.append(word[x]));
201     }
202
203     System.out.println("\n");
204     // Now let's read them out sequentally
205
for (int x =0; x< word.length; x++)
206     {
207       String JavaDoc s = (String JavaDoc) m_ObjectArray.getAt(x);
208       System.out.println(s);
209     }
210
211     // Some Random Access
212
System.out.println((String JavaDoc) m_ObjectArray.getAt(5));
213     System.out.println((String JavaDoc) m_ObjectArray.getAt(10));
214     System.out.println((String JavaDoc) m_ObjectArray.getAt(20));
215     System.out.println((String JavaDoc) m_ObjectArray.getAt(2));
216     System.out.println((String JavaDoc) m_ObjectArray.getAt(15));
217     System.out.println((String JavaDoc) m_ObjectArray.getAt(30));
218     System.out.println((String JavaDoc) m_ObjectArray.getAt(6));
219     System.out.println((String JavaDoc) m_ObjectArray.getAt(8));
220
221     // Out of bounds
222
System.out.println((String JavaDoc) m_ObjectArray.getAt(40));
223
224   }
225 }
226
Popular Tags