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: IteratorPool.java,v 1.8 2004/02/17 04:32:08 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.axes;
  20. import java.util.Vector;
  21. import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  22. import com.sun.org.apache.xml.internal.utils.WrappedRuntimeException;
  23. /**
  24. * Pool of object of a given type to pick from to help memory usage
  25. * @xsl.usage internal
  26. */
  27. public class IteratorPool implements java.io.Serializable
  28. {
  29. /** Type of objects in this pool.
  30. * @serial */
  31. private final DTMIterator m_orig;
  32. /** Vector of given objects this points to.
  33. * @serial */
  34. private final Vector m_freeStack;
  35. /**
  36. * Constructor IteratorPool
  37. *
  38. * @param original The original iterator from which all others will be cloned.
  39. */
  40. public IteratorPool(DTMIterator original)
  41. {
  42. m_orig = original;
  43. m_freeStack = new Vector();
  44. }
  45. /**
  46. * Get an instance of the given object in this pool
  47. *
  48. * @return An instance of the given object
  49. */
  50. public synchronized DTMIterator getInstanceOrThrow()
  51. throws CloneNotSupportedException
  52. {
  53. // Check if the pool is empty.
  54. if (m_freeStack.isEmpty())
  55. {
  56. // Create a new object if so.
  57. return (DTMIterator)m_orig.clone();
  58. }
  59. else
  60. {
  61. // Remove object from end of free pool.
  62. DTMIterator result = (DTMIterator)m_freeStack.lastElement();
  63. m_freeStack.setSize(m_freeStack.size() - 1);
  64. return result;
  65. }
  66. }
  67. /**
  68. * Get an instance of the given object in this pool
  69. *
  70. * @return An instance of the given object
  71. */
  72. public synchronized DTMIterator getInstance()
  73. {
  74. // Check if the pool is empty.
  75. if (m_freeStack.isEmpty())
  76. {
  77. // Create a new object if so.
  78. try
  79. {
  80. return (DTMIterator)m_orig.clone();
  81. }
  82. catch (Exception ex)
  83. {
  84. throw new WrappedRuntimeException(ex);
  85. }
  86. }
  87. else
  88. {
  89. // Remove object from end of free pool.
  90. DTMIterator result = (DTMIterator)m_freeStack.lastElement();
  91. m_freeStack.setSize(m_freeStack.size() - 1);
  92. return result;
  93. }
  94. }
  95. /**
  96. * Add an instance of the given object to the pool
  97. *
  98. *
  99. * @param obj Object to add.
  100. */
  101. public synchronized void freeInstance(DTMIterator obj)
  102. {
  103. m_freeStack.addElement(obj);
  104. }
  105. }