KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > StringVector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: StringVector.java,v 1.7 2004/02/17 04:21:14 minchau Exp $
18  */

19 package org.apache.xml.utils;
20
21 /**
22  * A very simple table that stores a list of strings, optimized
23  * for small lists.
24  * @xsl.usage internal
25  */

26 public class StringVector implements java.io.Serializable JavaDoc
27 {
28
29   /** @serial Size of blocks to allocate */
30   protected int m_blocksize;
31
32   /** @serial Array of strings this contains */
33   protected String JavaDoc m_map[];
34
35   /** @serial Number of strings this contains */
36   protected int m_firstFree = 0;
37
38   /** @serial Size of the array */
39   protected int m_mapSize;
40
41   /**
42    * Default constructor. Note that the default
43    * block size is very small, for small lists.
44    */

45   public StringVector()
46   {
47
48     m_blocksize = 8;
49     m_mapSize = m_blocksize;
50     m_map = new String JavaDoc[m_blocksize];
51   }
52
53   /**
54    * Construct a StringVector, using the given block size.
55    *
56    * @param blocksize Size of the blocks to allocate
57    */

58   public StringVector(int blocksize)
59   {
60
61     m_blocksize = blocksize;
62     m_mapSize = blocksize;
63     m_map = new String JavaDoc[blocksize];
64   }
65
66   /**
67    * Get the length of the list.
68    *
69    * @return Number of strings in the list
70    */

71   public int getLength()
72   {
73     return m_firstFree;
74   }
75
76   /**
77    * Get the length of the list.
78    *
79    * @return Number of strings in the list
80    */

81   public final int size()
82   {
83     return m_firstFree;
84   }
85
86   /**
87    * Append a string onto the vector.
88    *
89    * @param value Sting to add to the vector
90    */

91   public final void addElement(String JavaDoc value)
92   {
93
94     if ((m_firstFree + 1) >= m_mapSize)
95     {
96       m_mapSize += m_blocksize;
97
98       String JavaDoc newMap[] = new String JavaDoc[m_mapSize];
99
100       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
101
102       m_map = newMap;
103     }
104
105     m_map[m_firstFree] = value;
106
107     m_firstFree++;
108   }
109
110   /**
111    * Get the nth element.
112    *
113    * @param i Index of string to find
114    *
115    * @return String at given index
116    */

117   public final String JavaDoc elementAt(int i)
118   {
119     return m_map[i];
120   }
121
122   /**
123    * Tell if the table contains the given string.
124    *
125    * @param s String to look for
126    *
127    * @return True if the string is in this table
128    */

129   public final boolean contains(String JavaDoc s)
130   {
131
132     if (null == s)
133       return false;
134
135     for (int i = 0; i < m_firstFree; i++)
136     {
137       if (m_map[i].equals(s))
138         return true;
139     }
140
141     return false;
142   }
143
144   /**
145    * Tell if the table contains the given string. Ignore case.
146    *
147    * @param s String to find
148    *
149    * @return True if the String is in this vector
150    */

151   public final boolean containsIgnoreCase(String JavaDoc s)
152   {
153
154     if (null == s)
155       return false;
156
157     for (int i = 0; i < m_firstFree; i++)
158     {
159       if (m_map[i].equalsIgnoreCase(s))
160         return true;
161     }
162
163     return false;
164   }
165
166   /**
167    * Tell if the table contains the given string.
168    *
169    * @param s String to push into the vector
170    */

171   public final void push(String JavaDoc s)
172   {
173
174     if ((m_firstFree + 1) >= m_mapSize)
175     {
176       m_mapSize += m_blocksize;
177
178       String JavaDoc newMap[] = new String JavaDoc[m_mapSize];
179
180       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
181
182       m_map = newMap;
183     }
184
185     m_map[m_firstFree] = s;
186
187     m_firstFree++;
188   }
189
190   /**
191    * Pop the tail of this vector.
192    *
193    * @return The String last added to this vector or null not found.
194    * The string is removed from the vector.
195    */

196   public final String JavaDoc pop()
197   {
198
199     if (m_firstFree <= 0)
200       return null;
201
202     m_firstFree--;
203
204     String JavaDoc s = m_map[m_firstFree];
205
206     m_map[m_firstFree] = null;
207
208     return s;
209   }
210
211   /**
212    * Get the string at the tail of this vector without popping.
213    *
214    * @return The string at the tail of this vector.
215    */

216   public final String JavaDoc peek()
217   {
218     return (m_firstFree <= 0) ? null : m_map[m_firstFree - 1];
219   }
220 }
221
Popular Tags