001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: IteratorPool.java,v 1.9 2004/08/17 19:25:34 jycli Exp $
018: */
019: package org.apache.xpath.axes;
020:
021: import java.util.Vector;
022:
023: import org.apache.xml.dtm.DTMIterator;
024: import org.apache.xml.utils.WrappedRuntimeException;
025:
026: /**
027: * Pool of object of a given type to pick from to help memory usage
028: * @xsl.usage internal
029: */
030: public class IteratorPool implements java.io.Serializable {
031: static final long serialVersionUID = -460927331149566998L;
032:
033: /** Type of objects in this pool.
034: * @serial */
035: private final DTMIterator m_orig;
036:
037: /** Vector of given objects this points to.
038: * @serial */
039: private final Vector m_freeStack;
040:
041: /**
042: * Constructor IteratorPool
043: *
044: * @param original The original iterator from which all others will be cloned.
045: */
046: public IteratorPool(DTMIterator original) {
047: m_orig = original;
048: m_freeStack = new Vector();
049: }
050:
051: /**
052: * Get an instance of the given object in this pool
053: *
054: * @return An instance of the given object
055: */
056: public synchronized DTMIterator getInstanceOrThrow()
057: throws CloneNotSupportedException {
058: // Check if the pool is empty.
059: if (m_freeStack.isEmpty()) {
060:
061: // Create a new object if so.
062: return (DTMIterator) m_orig.clone();
063: } else {
064: // Remove object from end of free pool.
065: DTMIterator result = (DTMIterator) m_freeStack
066: .lastElement();
067:
068: m_freeStack.setSize(m_freeStack.size() - 1);
069:
070: return result;
071: }
072: }
073:
074: /**
075: * Get an instance of the given object in this pool
076: *
077: * @return An instance of the given object
078: */
079: public synchronized DTMIterator getInstance() {
080: // Check if the pool is empty.
081: if (m_freeStack.isEmpty()) {
082:
083: // Create a new object if so.
084: try {
085: return (DTMIterator) m_orig.clone();
086: } catch (Exception ex) {
087: throw new WrappedRuntimeException(ex);
088: }
089: } else {
090: // Remove object from end of free pool.
091: DTMIterator result = (DTMIterator) m_freeStack
092: .lastElement();
093:
094: m_freeStack.setSize(m_freeStack.size() - 1);
095:
096: return result;
097: }
098: }
099:
100: /**
101: * Add an instance of the given object to the pool
102: *
103: *
104: * @param obj Object to add.
105: */
106: public synchronized void freeInstance(DTMIterator obj) {
107: m_freeStack.addElement(obj);
108: }
109: }
|