View Javadoc

1   package org.apache.turbine.util;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.Serializable;
20  import java.util.Iterator;
21  import java.util.Stack;
22  
23  /***
24   * This class implements a Stack for String objects.
25   *
26   * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
27   * @version $Id: StringStackBuffer.java 264148 2005-08-29 14:21:04Z henning $
28   * @deprecated This class will be removed after the 2.3 release. It is
29   *             not part of the Web Framework scope. If you need this class,
30   *             please lobby for inclusion in the <a href="http://jakarta.apache.org/commons/">commons-collections</a> component repository.
31   */
32  public class StringStackBuffer implements Serializable
33  {
34      /*** The stack. */
35      private Stack stk = null;
36  
37      /***
38       * Constructor.
39       */
40      public StringStackBuffer()
41      {
42          stk = new Stack();
43      }
44  
45      /***
46       * Adds the String to the collection if it does not already
47       * contain it.
48       *
49       * @param s A String.
50       * @return A StringStackBuffer.
51       */
52      public StringStackBuffer add(String s)
53      {
54          if (s != null && !contains(s))
55              stk.push(s);
56          return this;
57      }
58  
59      /***
60       * Adds all Strings in the given StringStackBuffer to the collection
61       * (skipping those it already contains)
62       *
63       * @param s A StringStackBuffer.
64       * @return A StringStackBuffer.
65       */
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      /***
76       * Clears the Stack.
77       *
78       */
79      public void clear()
80      {
81          stk.clear();
82      }
83  
84      /***
85       * Does the Stack contain this String?
86       *
87       * @param s A String.
88       * @return True if the Stack contains this String.
89       */
90      public boolean contains(String s)
91      {
92          return (stk.search(s) != -1);
93      }
94  
95      /***
96       * Is the Stack empty?
97       * @return True if the Stack is empty.
98       */
99      public boolean empty()
100     {
101         return stk.empty();
102     }
103 
104     /***
105      * Get a String off the Stack at a certain position.
106      *
107      * @param i An int with the position.
108      * @return A String.
109      */
110     public String get(int i)
111     {
112         return (String) stk.elementAt(i);
113     }
114 
115     /***
116      * What is the size of the Stack?
117      *
118      * @return An int, the size of the Stack.
119      */
120     public int size()
121     {
122         return stk.size();
123     }
124 
125     /***
126      * Converts the stack to a single {@link java.lang.String} with no
127      * separator.
128      *
129      * @return The stack elements as a single block of text.
130      */
131     public String toString()
132     {
133         return toString("");
134     }
135 
136     /***
137      * Converts the stack to a single {@link java.lang.String}.
138      *
139      * @param separator The text to use as glue between elements in
140      * the stack.
141      * @return The stack elements--glued together by
142      * <code>separator</code>--as a single block of text.
143      */
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             // Determine what size to pre-allocate for the buffer.
155             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     /***
177      * Compares two StringStackBuffers.  Considered equal if the toString()
178      * methods are equal.
179      *
180      */
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