1. package org.apache.xpath.axes;
  2. import java.util.*;
  3. import org.apache.xml.dtm.DTMIterator;
  4. import org.apache.xml.utils.WrappedRuntimeException;
  5. /**
  6. * <meta name="usage" content="internal"/>
  7. * Pool of object of a given type to pick from to help memory usage
  8. */
  9. public class IteratorPool implements java.io.Serializable
  10. {
  11. /** Type of objects in this pool.
  12. * @serial */
  13. private final DTMIterator m_orig;
  14. /** Vector of given objects this points to.
  15. * @serial */
  16. private final Vector m_freeStack;
  17. /**
  18. * Constructor IteratorPool
  19. *
  20. * @param original The original iterator from which all others will be cloned.
  21. */
  22. public IteratorPool(DTMIterator original)
  23. {
  24. m_orig = original;
  25. m_freeStack = new Vector();
  26. }
  27. /**
  28. * Get an instance of the given object in this pool
  29. *
  30. * @return An instance of the given object
  31. */
  32. public synchronized DTMIterator getInstanceOrThrow()
  33. throws CloneNotSupportedException
  34. {
  35. // Check if the pool is empty.
  36. if (m_freeStack.isEmpty())
  37. {
  38. // Create a new object if so.
  39. return (DTMIterator)m_orig.clone();
  40. }
  41. else
  42. {
  43. // Remove object from end of free pool.
  44. DTMIterator result = (DTMIterator)m_freeStack.lastElement();
  45. m_freeStack.setSize(m_freeStack.size() - 1);
  46. return result;
  47. }
  48. }
  49. /**
  50. * Get an instance of the given object in this pool
  51. *
  52. * @return An instance of the given object
  53. */
  54. public synchronized DTMIterator getInstance()
  55. {
  56. // Check if the pool is empty.
  57. if (m_freeStack.isEmpty())
  58. {
  59. // Create a new object if so.
  60. try
  61. {
  62. return (DTMIterator)m_orig.clone();
  63. }
  64. catch (Exception ex)
  65. {
  66. throw new WrappedRuntimeException(ex);
  67. }
  68. }
  69. else
  70. {
  71. // Remove object from end of free pool.
  72. DTMIterator result = (DTMIterator)m_freeStack.lastElement();
  73. m_freeStack.setSize(m_freeStack.size() - 1);
  74. return result;
  75. }
  76. }
  77. /**
  78. * Add an instance of the given object to the pool
  79. *
  80. *
  81. * @param obj Object to add.
  82. */
  83. public synchronized void freeInstance(DTMIterator obj)
  84. {
  85. m_freeStack.addElement(obj);
  86. }
  87. }