1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id: IteratorPool.java 475981 2006-11-16 23:35:53Z minchau $
20 */
21package org.apache.xpath.axes;
22
23import java.util.ArrayList;
24
25import org.apache.xml.dtm.DTMIterator;
26import org.apache.xml.utils.WrappedRuntimeException;
27
28/**
29 * Pool of object of a given type to pick from to help memory usage
30 * @xsl.usage internal
31 */
32public final class IteratorPool implements java.io.Serializable
33{
34    static final long serialVersionUID = -460927331149566998L;
35
36  /**
37   * Type of objects in this pool.
38   */
39  private final DTMIterator m_orig;
40
41  /**
42   * Stack of given objects this points to.
43   */
44  private final ArrayList m_freeStack;
45
46  /**
47   * Constructor IteratorPool
48   *
49   * @param original The original iterator from which all others will be cloned.
50   */
51  public IteratorPool(DTMIterator original)
52  {
53    m_orig = original;
54    m_freeStack = new ArrayList();
55  }
56
57  /**
58   * Get an instance of the given object in this pool
59   *
60   * @return An instance of the given object
61   */
62  public synchronized DTMIterator getInstanceOrThrow()
63    throws CloneNotSupportedException
64  {
65    // Check if the pool is empty.
66    if (m_freeStack.isEmpty())
67    {
68
69      // Create a new object if so.
70      return (DTMIterator)m_orig.clone();
71    }
72    else
73    {
74      // Remove object from end of free pool.
75      DTMIterator result = (DTMIterator)m_freeStack.remove(m_freeStack.size() - 1);
76      return result;
77    }
78  }
79
80  /**
81   * Get an instance of the given object in this pool
82   *
83   * @return An instance of the given object
84   */
85  public synchronized DTMIterator getInstance()
86  {
87    // Check if the pool is empty.
88    if (m_freeStack.isEmpty())
89    {
90
91      // Create a new object if so.
92      try
93      {
94        return (DTMIterator)m_orig.clone();
95      }
96      catch (Exception ex)
97      {
98        throw new WrappedRuntimeException(ex);
99      }
100    }
101    else
102    {
103      // Remove object from end of free pool.
104      DTMIterator result = (DTMIterator)m_freeStack.remove(m_freeStack.size() - 1);
105      return result;
106    }
107  }
108
109  /**
110   * Add an instance of the given object to the pool
111   *
112   *
113   * @param obj Object to add.
114   */
115  public synchronized void freeInstance(DTMIterator obj)
116  {
117    m_freeStack.add(obj);
118  }
119}
120