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