KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > axes > IteratorPool


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
21 import java.util.Vector JavaDoc;
22
23 import com.sun.org.apache.xml.internal.dtm.DTMIterator;
24 import com.sun.org.apache.xml.internal.utils.WrappedRuntimeException;
25
26 /**
27  * Pool of object of a given type to pick from to help memory usage
28  * @xsl.usage internal
29  */

30 public class IteratorPool implements java.io.Serializable JavaDoc
31 {
32
33   /** Type of objects in this pool.
34    * @serial */

35   private final DTMIterator m_orig;
36
37   /** Vector of given objects this points to.
38    * @serial */

39   private final Vector JavaDoc m_freeStack;
40
41   /**
42    * Constructor IteratorPool
43    *
44    * @param original The original iterator from which all others will be cloned.
45    */

46   public IteratorPool(DTMIterator original)
47   {
48     m_orig = original;
49     m_freeStack = new Vector JavaDoc();
50   }
51   
52   /**
53    * Get an instance of the given object in this pool
54    *
55    * @return An instance of the given object
56    */

57   public synchronized DTMIterator getInstanceOrThrow()
58     throws CloneNotSupportedException JavaDoc
59   {
60     // Check if the pool is empty.
61
if (m_freeStack.isEmpty())
62     {
63
64       // Create a new object if so.
65
return (DTMIterator)m_orig.clone();
66     }
67     else
68     {
69       // Remove object from end of free pool.
70
DTMIterator result = (DTMIterator)m_freeStack.lastElement();
71
72       m_freeStack.setSize(m_freeStack.size() - 1);
73
74       return result;
75     }
76   }
77   
78   /**
79    * Get an instance of the given object in this pool
80    *
81    * @return An instance of the given object
82    */

83   public synchronized DTMIterator getInstance()
84   {
85     // Check if the pool is empty.
86
if (m_freeStack.isEmpty())
87     {
88
89       // Create a new object if so.
90
try
91       {
92         return (DTMIterator)m_orig.clone();
93       }
94       catch (Exception JavaDoc ex)
95       {
96         throw new WrappedRuntimeException(ex);
97       }
98     }
99     else
100     {
101       // Remove object from end of free pool.
102
DTMIterator result = (DTMIterator)m_freeStack.lastElement();
103
104       m_freeStack.setSize(m_freeStack.size() - 1);
105
106       return result;
107     }
108   }
109
110   /**
111    * Add an instance of the given object to the pool
112    *
113    *
114    * @param obj Object to add.
115    */

116   public synchronized void freeInstance(DTMIterator obj)
117   {
118     m_freeStack.addElement(obj);
119   }
120 }
121
Popular Tags