2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * $Id: StringVector.java 468655 2006-10-28 07:12:06Z minchau $
21 package org.apache.xml.utils;
24 * A very simple table that stores a list of strings, optimized
28 public class StringVector implements java.io.Serializable
30 static final long serialVersionUID = 4995234972032919748L;
32 /** @serial Size of blocks to allocate */
33 protected int m_blocksize;
35 /** @serial Array of strings this contains */
36 protected String m_map[];
38 /** @serial Number of strings this contains */
39 protected int m_firstFree = 0;
41 /** @serial Size of the array */
42 protected int m_mapSize;
45 * Default constructor. Note that the default
46 * block size is very small, for small lists.
52 m_mapSize = m_blocksize;
53 m_map = new String[m_blocksize];
57 * Construct a StringVector, using the given block size.
59 * @param blocksize Size of the blocks to allocate
61 public StringVector(int blocksize)
64 m_blocksize = blocksize;
65 m_mapSize = blocksize;
66 m_map = new String[blocksize];
70 * Get the length of the list.
72 * @return Number of strings in the list
74 public int getLength()
80 * Get the length of the list.
82 * @return Number of strings in the list
84 public final int size()
90 * Append a string onto the vector.
92 * @param value Sting to add to the vector
94 public final void addElement(String value)
97 if ((m_firstFree + 1) >= m_mapSize)
99 m_mapSize += m_blocksize;
101 String newMap[] = new String[m_mapSize];
103 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
108 m_map[m_firstFree] = value;
114 * Get the nth element.
116 * @param i Index of string to find
118 * @return String at given index
120 public final String elementAt(int i)
126 * Tell if the table contains the given string.
128 * @param s String to look for
130 * @return True if the string is in this table
132 public final boolean contains(String s)
138 for (int i = 0; i < m_firstFree; i++)
140 if (m_map[i].equals(s))
148 * Tell if the table contains the given string. Ignore case.
150 * @param s String to find
152 * @return True if the String is in this vector
154 public final boolean containsIgnoreCase(String s)
160 for (int i = 0; i < m_firstFree; i++)
162 if (m_map[i].equalsIgnoreCase(s))
170 * Tell if the table contains the given string.
172 * @param s String to push into the vector
174 public final void push(String s)
177 if ((m_firstFree + 1) >= m_mapSize)
179 m_mapSize += m_blocksize;
181 String newMap[] = new String[m_mapSize];
183 System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
188 m_map[m_firstFree] = s;
194 * Pop the tail of this vector.
196 * @return The String last added to this vector or null not found.
197 * The string is removed from the vector.
199 public final String pop()
202 if (m_firstFree <= 0)
207 String s = m_map[m_firstFree];
209 m_map[m_firstFree] = null;
215 * Get the string at the tail of this vector without popping.
217 * @return The string at the tail of this vector.
219 public final String peek()
221 return (m_firstFree <= 0) ? null : m_map[m_firstFree - 1];