1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements.  See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License.  You may obtain a copy of the License at
12 *
13 *      http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22package com.sun.org.apache.xml.internal.utils;
23
24/**
25 * A very simple table that stores a list of strings, optimized
26 * for small lists.
27 * @xsl.usage internal
28 */
29public class StringVector implements java.io.Serializable
30{
31    static final long serialVersionUID = 4995234972032919748L;
32
33  /** @serial Size of blocks to allocate           */
34  protected int m_blocksize;
35
36  /** @serial Array of strings this contains          */
37  protected String m_map[];
38
39  /** @serial Number of strings this contains          */
40  protected int m_firstFree = 0;
41
42  /** @serial Size of the array          */
43  protected int m_mapSize;
44
45  /**
46   * Default constructor.  Note that the default
47   * block size is very small, for small lists.
48   */
49  public StringVector()
50  {
51
52    m_blocksize = 8;
53    m_mapSize = m_blocksize;
54    m_map = new String[m_blocksize];
55  }
56
57  /**
58   * Construct a StringVector, using the given block size.
59   *
60   * @param blocksize Size of the blocks to allocate
61   */
62  public StringVector(int blocksize)
63  {
64
65    m_blocksize = blocksize;
66    m_mapSize = blocksize;
67    m_map = new String[blocksize];
68  }
69
70  /**
71   * Get the length of the list.
72   *
73   * @return Number of strings in the list
74   */
75  public int getLength()
76  {
77    return m_firstFree;
78  }
79
80  /**
81   * Get the length of the list.
82   *
83   * @return Number of strings in the list
84   */
85  public final int size()
86  {
87    return m_firstFree;
88  }
89
90  /**
91   * Append a string onto the vector.
92   *
93   * @param value Sting to add to the vector
94   */
95  public final void addElement(String value)
96  {
97
98    if ((m_firstFree + 1) >= m_mapSize)
99    {
100      m_mapSize += m_blocksize;
101
102      String newMap[] = new String[m_mapSize];
103
104      System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
105
106      m_map = newMap;
107    }
108
109    m_map[m_firstFree] = value;
110
111    m_firstFree++;
112  }
113
114  /**
115   * Get the nth element.
116   *
117   * @param i Index of string to find
118   *
119   * @return String at given index
120   */
121  public final String elementAt(int i)
122  {
123    return m_map[i];
124  }
125
126  /**
127   * Tell if the table contains the given string.
128   *
129   * @param s String to look for
130   *
131   * @return True if the string is in this table
132   */
133  public final boolean contains(String s)
134  {
135
136    if (null == s)
137      return false;
138
139    for (int i = 0; i < m_firstFree; i++)
140    {
141      if (m_map[i].equals(s))
142        return true;
143    }
144
145    return false;
146  }
147
148  /**
149   * Tell if the table contains the given string. Ignore case.
150   *
151   * @param s String to find
152   *
153   * @return True if the String is in this vector
154   */
155  public final boolean containsIgnoreCase(String s)
156  {
157
158    if (null == s)
159      return false;
160
161    for (int i = 0; i < m_firstFree; i++)
162    {
163      if (m_map[i].equalsIgnoreCase(s))
164        return true;
165    }
166
167    return false;
168  }
169
170  /**
171   * Tell if the table contains the given string.
172   *
173   * @param s String to push into the vector
174   */
175  public final void push(String s)
176  {
177
178    if ((m_firstFree + 1) >= m_mapSize)
179    {
180      m_mapSize += m_blocksize;
181
182      String newMap[] = new String[m_mapSize];
183
184      System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
185
186      m_map = newMap;
187    }
188
189    m_map[m_firstFree] = s;
190
191    m_firstFree++;
192  }
193
194  /**
195   * Pop the tail of this vector.
196   *
197   * @return The String last added to this vector or null not found.
198   * The string is removed from the vector.
199   */
200  public final String pop()
201  {
202
203    if (m_firstFree <= 0)
204      return null;
205
206    m_firstFree--;
207
208    String s = m_map[m_firstFree];
209
210    m_map[m_firstFree] = null;
211
212    return s;
213  }
214
215  /**
216   * Get the string at the tail of this vector without popping.
217   *
218   * @return The string at the tail of this vector.
219   */
220  public final String peek()
221  {
222    return (m_firstFree <= 0) ? null : m_map[m_firstFree - 1];
223  }
224}
225