1. package org.apache.xalan.lib.sql;
  2. import java.util.Vector;
  3. /**
  4. * Provide a simple Array storage mechinsim where native Arrays will be use as
  5. * the basic storage mechinism but the Arrays will be stored as blocks.
  6. * The size of the Array blocks is determine during object construction.
  7. * This is intended to be a simple storage mechinsim where the storage only
  8. * can grow. Array elements can not be removed, only added to.
  9. */
  10. public class ObjectArray
  11. {
  12. /**
  13. */
  14. private int m_minArraySize = 10;
  15. /**
  16. * The container of all the sub arrays
  17. */
  18. private Vector m_Arrays = new Vector(200);
  19. /**
  20. * An index that porvides the Vector entry for the current Array that is
  21. * being appended to.
  22. */
  23. private _ObjectArray m_currentArray;
  24. /**
  25. * The next offset in the current Array to append a new object
  26. */
  27. private int m_nextSlot;
  28. /**
  29. */
  30. public ObjectArray( )
  31. {
  32. //
  33. // Default constructor will work with a minimal fixed size
  34. //
  35. init(10);
  36. }
  37. /**
  38. * @param minArraySize The size of the Arrays stored in the Vector
  39. */
  40. public ObjectArray( final int minArraySize )
  41. {
  42. init(minArraySize);
  43. }
  44. /**
  45. * @param size
  46. * @return
  47. */
  48. private void init( int size )
  49. {
  50. m_minArraySize = size;
  51. m_currentArray = new _ObjectArray(m_minArraySize);
  52. }
  53. /**
  54. * @param idx Index of the Object in the Array
  55. * @return
  56. */
  57. public Object getAt( final int idx )
  58. {
  59. int arrayIndx = idx / m_minArraySize;
  60. int arrayOffset = idx - (arrayIndx * m_minArraySize);
  61. //
  62. // If the array has been off loaded to the Vector Storage them
  63. // grab it from there.
  64. if (arrayIndx < m_Arrays.size())
  65. {
  66. _ObjectArray a = (_ObjectArray)m_Arrays.elementAt(arrayIndx);
  67. return a.objects[arrayOffset];
  68. }
  69. else
  70. {
  71. // We must be in the current array, so pull it from there
  72. // %REVIEW% We may want to check to see if arrayIndx is only
  73. // one freater that the m_Arrays.size(); This code is safe but
  74. // will repete if the index is greater than the array size.
  75. return m_currentArray.objects[arrayOffset];
  76. }
  77. }
  78. /**
  79. * @param idx Index of the Object in the Array
  80. * @param obj , The value to set in the Array
  81. * @return
  82. */
  83. public void setAt( final int idx, final Object obj )
  84. {
  85. int arrayIndx = idx / m_minArraySize;
  86. int arrayOffset = idx - (arrayIndx * m_minArraySize);
  87. //
  88. // If the array has been off loaded to the Vector Storage them
  89. // grab it from there.
  90. if (arrayIndx < m_Arrays.size())
  91. {
  92. _ObjectArray a = (_ObjectArray)m_Arrays.elementAt(arrayIndx);
  93. a.objects[arrayOffset] = obj;
  94. }
  95. else
  96. {
  97. // We must be in the current array, so pull it from there
  98. // %REVIEW% We may want to check to see if arrayIndx is only
  99. // one freater that the m_Arrays.size(); This code is safe but
  100. // will repete if the index is greater than the array size.
  101. m_currentArray.objects[arrayOffset] = obj;
  102. }
  103. }
  104. /**
  105. * @param o Object to be appended to the Array
  106. * @return
  107. */
  108. public int append( Object o )
  109. {
  110. if (m_nextSlot >= m_minArraySize)
  111. {
  112. m_Arrays.addElement(m_currentArray);
  113. m_nextSlot = 0;
  114. m_currentArray = new _ObjectArray(m_minArraySize);
  115. }
  116. m_currentArray.objects[m_nextSlot] = o;
  117. int pos = (m_Arrays.size() * m_minArraySize) + m_nextSlot;
  118. m_nextSlot++;
  119. return pos;
  120. }
  121. /**
  122. */
  123. class _ObjectArray
  124. {
  125. /**
  126. */
  127. public Object[] objects;
  128. /**
  129. * @param size
  130. */
  131. public _ObjectArray( int size )
  132. {
  133. objects = new Object[size];
  134. }
  135. }
  136. /**
  137. * @param args
  138. * @return
  139. */
  140. public static void main( String[] args )
  141. {
  142. String[] word={
  143. "Zero","One","Two","Three","Four","Five",
  144. "Six","Seven","Eight","Nine","Ten",
  145. "Eleven","Twelve","Thirteen","Fourteen","Fifteen",
  146. "Sixteen","Seventeen","Eighteen","Nineteen","Twenty",
  147. "Twenty-One","Twenty-Two","Twenty-Three","Twenty-Four",
  148. "Twenty-Five","Twenty-Six","Twenty-Seven","Twenty-Eight",
  149. "Twenty-Nine","Thirty","Thirty-One","Thirty-Two",
  150. "Thirty-Three","Thirty-Four","Thirty-Five","Thirty-Six",
  151. "Thirty-Seven","Thirty-Eight","Thirty-Nine"};
  152. ObjectArray m_ObjectArray = new ObjectArray();
  153. // Add them in, using the default block size
  154. for (int x =0; x< word.length; x++)
  155. {
  156. System.out.print(" - " + m_ObjectArray.append(word[x]));
  157. }
  158. System.out.println("\n");
  159. // Now let's read them out sequentally
  160. for (int x =0; x< word.length; x++)
  161. {
  162. String s = (String) m_ObjectArray.getAt(x);
  163. System.out.println(s);
  164. }
  165. // Some Random Access
  166. System.out.println((String) m_ObjectArray.getAt(5));
  167. System.out.println((String) m_ObjectArray.getAt(10));
  168. System.out.println((String) m_ObjectArray.getAt(20));
  169. System.out.println((String) m_ObjectArray.getAt(2));
  170. System.out.println((String) m_ObjectArray.getAt(15));
  171. System.out.println((String) m_ObjectArray.getAt(30));
  172. System.out.println((String) m_ObjectArray.getAt(6));
  173. System.out.println((String) m_ObjectArray.getAt(8));
  174. // Out of bounds
  175. System.out.println((String) m_ObjectArray.getAt(40));
  176. }
  177. }