1 22 23 package org.xquark.mapper.util; 24 25 26 import java.util.ArrayList ; 27 28 32 33 public class RecyclingStack extends ArrayList 34 { 35 private static final String RCSRevision = "$Revision: 1.1 $"; 36 private static final String 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 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 toString() 91 { 92 StringBuffer dummy = new StringBuffer (); 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 |