View Javadoc
1 package org.apache.turbine.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.Serializable; 58 import java.util.Iterator; 59 import java.util.Stack; 60 61 /*** 62 * This class implements a Stack for String objects. 63 * 64 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 65 * @version $Id: StringStackBuffer.java,v 1.2 2001/10/27 16:50:41 dlr Exp $ 66 */ 67 public class StringStackBuffer implements Serializable 68 { 69 /*** The stack. */ 70 private Stack stk = null; 71 72 /*** 73 * Constructor. 74 */ 75 public StringStackBuffer() 76 { 77 stk = new Stack(); 78 } 79 80 /*** 81 * Adds the String to the collection if it does not already 82 * contain it. 83 * 84 * @param s A String. 85 * @return A StringStackBuffer. 86 */ 87 public StringStackBuffer add( String s ) 88 { 89 if ( s != null && !contains(s) ) 90 stk.push(s); 91 return this; 92 } 93 94 /*** 95 * Adds all Strings in the given StringStackBuffer to the collection 96 * (skipping those it already contains) 97 * 98 * @param s A StringStackBuffer. 99 * @return A StringStackBuffer. 100 */ 101 public StringStackBuffer addAll( StringStackBuffer s ) 102 { 103 Iterator it = s.stk.iterator(); 104 while (it.hasNext()) 105 add((String)it.next()); 106 return this; 107 } 108 109 /*** 110 * Clears the Stack. 111 * 112 */ 113 public void clear() 114 { 115 stk.clear(); 116 } 117 118 /*** 119 * Does the Stack contain this String? 120 * 121 * @param s A String. 122 * @return True if the Stack contains this String. 123 */ 124 public boolean contains( String s ) 125 { 126 return ( stk.search(s) != -1 ); 127 } 128 129 /*** 130 * Is the Stack empty? 131 * @return True if the Stack is empty. 132 */ 133 public boolean empty() 134 { 135 return stk.empty(); 136 } 137 138 /*** 139 * Get a String off the Stack at a certain position. 140 * 141 * @param i An int with the position. 142 * @return A String. 143 */ 144 public String get(int i) 145 { 146 return (String) stk.elementAt(i); 147 } 148 149 /*** 150 * What is the size of the Stack? 151 * 152 * @return An int, the size of the Stack. 153 */ 154 public int size() 155 { 156 return stk.size(); 157 } 158 159 /*** 160 * Converts the stack to a single {@link java.lang.String} with no 161 * separator. 162 * 163 * @return The stack elements as a single block of text. 164 */ 165 public String toString() 166 { 167 return toString(""); 168 } 169 170 /*** 171 * Converts the stack to a single {@link java.lang.String}. 172 * 173 * @param separator The text to use as glue between elements in 174 * the stack. 175 * @return The stack elements--glued together by 176 * <code>separator</code>--as a single block of text. 177 */ 178 public String toString( String separator ) 179 { 180 String s; 181 if ( size() > 0 ) 182 { 183 if ( separator == null ) 184 { 185 separator = ""; 186 } 187 188 // Determine what size to pre-allocate for the buffer. 189 int totalSize = 0; 190 for (int i = 0; i < stk.size(); i++) 191 { 192 totalSize += get(i).length(); 193 } 194 totalSize += (stk.size() - 1) * separator.length(); 195 196 StringBuffer sb = new StringBuffer(totalSize).append( get(0) ); 197 for (int i = 1; i < stk.size(); i++) 198 { 199 sb.append(separator).append(get(i)); 200 } 201 s = sb.toString(); 202 } 203 else 204 { 205 s = ""; 206 } 207 return s; 208 } 209 210 /*** 211 * Compares two StringStackBuffers. Considered equal if the toString() 212 * methods are equal. 213 * 214 */ 215 public boolean equals(Object ssbuf) 216 { 217 boolean isEquiv = false; 218 if ( ssbuf == null || !(ssbuf instanceof StringStackBuffer) ) 219 { 220 isEquiv = false; 221 } 222 223 else if ( ssbuf == this ) 224 { 225 isEquiv = true; 226 } 227 228 else if ( this.toString().equals(ssbuf.toString()) ) 229 { 230 isEquiv = true; 231 } 232 233 return isEquiv; 234 } 235 236 public String[] toStringArray() 237 { 238 String[] ss = new String[size()]; 239 for (int i=0; i<size(); i++) 240 { 241 ss[i]=get(i); 242 } 243 return ss; 244 } 245 } 246 247 248 249 250

This page was automatically generated by Maven