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.util.Iterator; 58 import java.util.Stack; 59 60 /*** 61 * This class implements a Stack for String objects. 62 * 63 * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a> 64 * @version $Id: StringStackBuffer.java,v 1.9.10.2 2001/06/01 01:34:56 jvanzyl Exp $ 65 */ 66 public class StringStackBuffer 67 { 68 /*** The stack. */ 69 private Stack stk = null; 70 71 /*** 72 * Constructor. 73 */ 74 public StringStackBuffer() 75 { 76 stk = new Stack(); 77 } 78 79 /*** 80 * Adds the String to the collection if it does not already 81 * contain it. 82 * 83 * @param s A String. 84 * @return A StringStackBuffer. 85 */ 86 public StringStackBuffer add( String s ) 87 { 88 if ( s != null && !contains(s) ) 89 stk.push(s); 90 return this; 91 } 92 93 /*** 94 * Adds all Strings in the given StringStackBuffer to the collection 95 * (skipping those it already contains) 96 * 97 * @param s A StringStackBuffer. 98 * @return A StringStackBuffer. 99 */ 100 public StringStackBuffer addAll( StringStackBuffer s ) 101 { 102 Iterator it = s.stk.iterator(); 103 while (it.hasNext()) 104 add((String)it.next()); 105 return this; 106 } 107 108 /*** 109 * Clears the Stack. 110 * 111 */ 112 public void clear() 113 { 114 stk.clear(); 115 } 116 117 /*** 118 * Does the Stack contain this String? 119 * 120 * @param s A String. 121 * @return True if the Stack contains this String. 122 */ 123 public boolean contains( String s ) 124 { 125 return ( stk.search(s) != -1 ); 126 } 127 128 /*** 129 * Is the Stack empty? 130 * @return True if the Stack is empty. 131 */ 132 public boolean empty() 133 { 134 return stk.empty(); 135 } 136 137 /*** 138 * Get a String off the Stack at a certain position. 139 * 140 * @param i An int with the position. 141 * @return A String. 142 */ 143 public String get(int i) 144 { 145 return (String) stk.elementAt(i); 146 } 147 148 /*** 149 * What is the size of the Stack? 150 * 151 * @return An int, the size of the Stack. 152 */ 153 public int size() 154 { 155 return stk.size(); 156 } 157 158 /*** 159 * Converts the stack to a single {@link java.lang.String} with no 160 * separator. 161 * 162 * @return The stack elements as a single block of text. 163 */ 164 public String toString() 165 { 166 return toString(""); 167 } 168 169 /*** 170 * Converts the stack to a single {@link java.lang.String}. 171 * 172 * @param separator The text to use as glue between elements in 173 * the stack. 174 * @return The stack elements--glued together by 175 * <code>separator</code>--as a single block of text. 176 */ 177 public String toString( String separator ) 178 { 179 String s; 180 if ( size() > 0 ) 181 { 182 if ( separator == null ) 183 { 184 separator = ""; 185 } 186 187 // Determine what size to pre-allocate for the buffer. 188 int totalSize = 0; 189 for (int i = 0; i < stk.size(); i++) 190 { 191 totalSize += get(i).length(); 192 } 193 totalSize += (stk.size() - 1) * separator.length(); 194 195 StringBuffer sb = new StringBuffer(totalSize).append( get(0) ); 196 for (int i = 1; i < stk.size(); i++) 197 { 198 sb.append(separator).append(get(i)); 199 } 200 s = sb.toString(); 201 } 202 else 203 { 204 s = ""; 205 } 206 return s; 207 } 208 209 /*** 210 * Compares two StringStackBuffers. Considered equal if the toString() 211 * methods are equal. 212 * 213 */ 214 public boolean equals(Object ssbuf) 215 { 216 boolean isEquiv = false; 217 if ( ssbuf == null || !(ssbuf instanceof StringStackBuffer) ) 218 { 219 isEquiv = false; 220 } 221 222 else if ( ssbuf == this ) 223 { 224 isEquiv = true; 225 } 226 227 else if ( this.toString().equals(ssbuf.toString()) ) 228 { 229 isEquiv = true; 230 } 231 232 return isEquiv; 233 } 234 235 public String[] toStringArray() 236 { 237 String[] ss = new String[size()]; 238 for (int i=0; i<size(); i++) 239 { 240 ss[i]=get(i); 241 } 242 return ss; 243 } 244 } 245 246 247 248 249

This page was automatically generated by Maven