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
10
* http://www.apache.org/licenses/LICENSE-2.0
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.
19
* $Id: IteratorPool.java,v 1.2 2009/12/10 03:18:40 matthewoliver Exp $
21
package org.apache.xpath.axes;
23
import java.util.ArrayList;
25
import org.apache.xml.dtm.DTMIterator;
26
import org.apache.xml.utils.WrappedRuntimeException;
29
* Pool of object of a given type to pick from to help memory usage
32
public final class IteratorPool implements java.io.Serializable
34
static final long serialVersionUID = -460927331149566998L;
37
* Type of objects in this pool.
39
private final DTMIterator m_orig;
42
* Stack of given objects this points to.
44
private final ArrayList m_freeStack;
47
* Constructor IteratorPool
49
* @param original The original iterator from which all others will be cloned.
51
public IteratorPool(DTMIterator original)
54
m_freeStack = new ArrayList();
58
* Get an instance of the given object in this pool
60
* @return An instance of the given object
62
public synchronized DTMIterator getInstanceOrThrow()
63
throws CloneNotSupportedException
65
// Check if the pool is empty.
66
if (m_freeStack.isEmpty())
69
// Create a new object if so.
70
return (DTMIterator)m_orig.clone();
74
// Remove object from end of free pool.
75
DTMIterator result = (DTMIterator)m_freeStack.remove(m_freeStack.size() - 1);
81
* Get an instance of the given object in this pool
83
* @return An instance of the given object
85
public synchronized DTMIterator getInstance()
87
// Check if the pool is empty.
88
if (m_freeStack.isEmpty())
91
// Create a new object if so.
94
return (DTMIterator)m_orig.clone();
98
throw new WrappedRuntimeException(ex);
103
// Remove object from end of free pool.
104
DTMIterator result = (DTMIterator)m_freeStack.remove(m_freeStack.size() - 1);
110
* Add an instance of the given object to the pool
113
* @param obj Object to add.
115
public synchronized void freeInstance(DTMIterator obj)
117
m_freeStack.add(obj);