KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > util > RecyclingStack


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.mapper.util;
24
25
26 import java.util.ArrayList JavaDoc;
27
28 /** This class is a stack that never release object when popping
29  * and handles object allocation or recycling when pushing.
30  *
31  */

32
33 public class RecyclingStack extends ArrayList JavaDoc
34 {
35 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
36 private static final String JavaDoc RCSName = "$Name: $";
37     private int stackSize = 0;
38     private StackObjectFactory factory;
39     
40     public RecyclingStack(StackObjectFactory factory)
41     {
42         this.factory = factory;
43     }
44     
45     public StackObject push()
46     {
47         StackObject o;
48         if (stackSize < super.size())
49         {
50             o = (StackObject)get(stackSize);
51             o.clear();
52         }
53         else
54         {
55             o = factory.newStackObject();
56             add(o);
57         }
58         stackSize++;
59         return o;
60     }
61     
62     public int size()
63     {
64         return stackSize;
65     }
66     
67     public StackObject pop()
68     {
69         StackObject o = null;
70         if (stackSize > 0)
71             o = (StackObject)get(--stackSize);
72         // clear() is performed when resued
73
return o;
74     }
75     
76     public StackObject top()
77     {
78         if (stackSize > 0)
79             return (StackObject)get(stackSize-1);
80         else
81             return null;
82     }
83     
84     public boolean isEmpty()
85     {
86         return (stackSize == 0);
87     }
88     
89     
90     public String JavaDoc toString()
91     {
92         StringBuffer JavaDoc dummy = new StringBuffer JavaDoc();
93         for(int i=0; i<size(); i++)
94             dummy.append("\n"+get(i));
95         return dummy.toString();
96     }
97     
98     public void clear()
99     {
100         for (int i = 0; i < stackSize; i++)
101         {
102             ((StackObject)get(i)).clear();
103         }
104         stackSize = 0;
105     }
106     
107     public interface StackObject
108     {
109         void clear();
110     }
111     
112     public interface StackObjectFactory
113     {
114         StackObject newStackObject();
115     }
116     
117 }
118
Popular Tags