1 package org.apache.turbine.util; 2 3 18 19 import java.io.Serializable ; 20 import java.util.Iterator ; 21 import java.util.Stack ; 22 23 32 public class StringStackBuffer implements Serializable 33 { 34 35 private Stack stk = null; 36 37 40 public StringStackBuffer() 41 { 42 stk = new Stack (); 43 } 44 45 52 public StringStackBuffer add(String s) 53 { 54 if (s != null && !contains(s)) 55 stk.push(s); 56 return this; 57 } 58 59 66 public StringStackBuffer addAll(StringStackBuffer s) 67 { 68 for (Iterator it = s.stk.iterator(); it.hasNext();) 69 { 70 add((String ) it.next()); 71 } 72 return this; 73 } 74 75 79 public void clear() 80 { 81 stk.clear(); 82 } 83 84 90 public boolean contains(String s) 91 { 92 return (stk.search(s) != -1); 93 } 94 95 99 public boolean empty() 100 { 101 return stk.empty(); 102 } 103 104 110 public String get(int i) 111 { 112 return (String ) stk.elementAt(i); 113 } 114 115 120 public int size() 121 { 122 return stk.size(); 123 } 124 125 131 public String toString() 132 { 133 return toString(""); 134 } 135 136 144 public String toString(String separator) 145 { 146 String s; 147 if (size() > 0) 148 { 149 if (separator == null) 150 { 151 separator = ""; 152 } 153 154 int totalSize = 0; 156 for (int i = 0; i < stk.size(); i++) 157 { 158 totalSize += get(i).length(); 159 } 160 totalSize += (stk.size() - 1) * separator.length(); 161 162 StringBuffer sb = new StringBuffer (totalSize).append(get(0)); 163 for (int i = 1; i < stk.size(); i++) 164 { 165 sb.append(separator).append(get(i)); 166 } 167 s = sb.toString(); 168 } 169 else 170 { 171 s = ""; 172 } 173 return s; 174 } 175 176 181 public boolean equals(Object ssbuf) 182 { 183 boolean isEquiv = false; 184 if (ssbuf == null || !(ssbuf instanceof StringStackBuffer)) 185 { 186 isEquiv = false; 187 } 188 189 else if (ssbuf == this) 190 { 191 isEquiv = true; 192 } 193 194 else if (this.toString().equals(ssbuf.toString())) 195 { 196 isEquiv = true; 197 } 198 199 return isEquiv; 200 } 201 202 public String [] toStringArray() 203 { 204 String [] ss = new String [size()]; 205 for (int i = 0; i < size(); i++) 206 { 207 ss[i] = get(i); 208 } 209 return ss; 210 } 211 } 212 213 214 215 216 217 | Popular Tags |