001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: StringVector.java,v 1.8 2004/08/17 18:57:22 jycli Exp $
018: */
019: package org.apache.xml.utils;
020:
021: /**
022: * A very simple table that stores a list of strings, optimized
023: * for small lists.
024: * @xsl.usage internal
025: */
026: public class StringVector implements java.io.Serializable {
027: static final long serialVersionUID = 4995234972032919748L;
028:
029: /** @serial Size of blocks to allocate */
030: protected int m_blocksize;
031:
032: /** @serial Array of strings this contains */
033: protected String m_map[];
034:
035: /** @serial Number of strings this contains */
036: protected int m_firstFree = 0;
037:
038: /** @serial Size of the array */
039: protected int m_mapSize;
040:
041: /**
042: * Default constructor. Note that the default
043: * block size is very small, for small lists.
044: */
045: public StringVector() {
046:
047: m_blocksize = 8;
048: m_mapSize = m_blocksize;
049: m_map = new String[m_blocksize];
050: }
051:
052: /**
053: * Construct a StringVector, using the given block size.
054: *
055: * @param blocksize Size of the blocks to allocate
056: */
057: public StringVector(int blocksize) {
058:
059: m_blocksize = blocksize;
060: m_mapSize = blocksize;
061: m_map = new String[blocksize];
062: }
063:
064: /**
065: * Get the length of the list.
066: *
067: * @return Number of strings in the list
068: */
069: public int getLength() {
070: return m_firstFree;
071: }
072:
073: /**
074: * Get the length of the list.
075: *
076: * @return Number of strings in the list
077: */
078: public final int size() {
079: return m_firstFree;
080: }
081:
082: /**
083: * Append a string onto the vector.
084: *
085: * @param value Sting to add to the vector
086: */
087: public final void addElement(String value) {
088:
089: if ((m_firstFree + 1) >= m_mapSize) {
090: m_mapSize += m_blocksize;
091:
092: String newMap[] = new String[m_mapSize];
093:
094: System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
095:
096: m_map = newMap;
097: }
098:
099: m_map[m_firstFree] = value;
100:
101: m_firstFree++;
102: }
103:
104: /**
105: * Get the nth element.
106: *
107: * @param i Index of string to find
108: *
109: * @return String at given index
110: */
111: public final String elementAt(int i) {
112: return m_map[i];
113: }
114:
115: /**
116: * Tell if the table contains the given string.
117: *
118: * @param s String to look for
119: *
120: * @return True if the string is in this table
121: */
122: public final boolean contains(String s) {
123:
124: if (null == s)
125: return false;
126:
127: for (int i = 0; i < m_firstFree; i++) {
128: if (m_map[i].equals(s))
129: return true;
130: }
131:
132: return false;
133: }
134:
135: /**
136: * Tell if the table contains the given string. Ignore case.
137: *
138: * @param s String to find
139: *
140: * @return True if the String is in this vector
141: */
142: public final boolean containsIgnoreCase(String s) {
143:
144: if (null == s)
145: return false;
146:
147: for (int i = 0; i < m_firstFree; i++) {
148: if (m_map[i].equalsIgnoreCase(s))
149: return true;
150: }
151:
152: return false;
153: }
154:
155: /**
156: * Tell if the table contains the given string.
157: *
158: * @param s String to push into the vector
159: */
160: public final void push(String s) {
161:
162: if ((m_firstFree + 1) >= m_mapSize) {
163: m_mapSize += m_blocksize;
164:
165: String newMap[] = new String[m_mapSize];
166:
167: System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
168:
169: m_map = newMap;
170: }
171:
172: m_map[m_firstFree] = s;
173:
174: m_firstFree++;
175: }
176:
177: /**
178: * Pop the tail of this vector.
179: *
180: * @return The String last added to this vector or null not found.
181: * The string is removed from the vector.
182: */
183: public final String pop() {
184:
185: if (m_firstFree <= 0)
186: return null;
187:
188: m_firstFree--;
189:
190: String s = m_map[m_firstFree];
191:
192: m_map[m_firstFree] = null;
193:
194: return s;
195: }
196:
197: /**
198: * Get the string at the tail of this vector without popping.
199: *
200: * @return The string at the tail of this vector.
201: */
202: public final String peek() {
203: return (m_firstFree <= 0) ? null : m_map[m_firstFree - 1];
204: }
205: }
|