OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / main / java / org / apache / xml / utils / StringVector.java
1 /*
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18 /*
19  * $Id: StringVector.java 468655 2006-10-28 07:12:06Z minchau $
20  */
21 package org.apache.xml.utils;
22
23 /**
24  * A very simple table that stores a list of strings, optimized
25  * for small lists.
26  * @xsl.usage internal
27  */
28 public class StringVector implements java.io.Serializable
29 {
30     static final long serialVersionUID = 4995234972032919748L;
31
32   /** @serial Size of blocks to allocate           */
33   protected int m_blocksize;
34
35   /** @serial Array of strings this contains          */
36   protected String m_map[];
37
38   /** @serial Number of strings this contains          */
39   protected int m_firstFree = 0;
40
41   /** @serial Size of the array          */
42   protected int m_mapSize;
43
44   /**
45    * Default constructor.  Note that the default
46    * block size is very small, for small lists.
47    */
48   public StringVector()
49   {
50
51     m_blocksize = 8;
52     m_mapSize = m_blocksize;
53     m_map = new String[m_blocksize];
54   }
55
56   /**
57    * Construct a StringVector, using the given block size.
58    *
59    * @param blocksize Size of the blocks to allocate 
60    */
61   public StringVector(int blocksize)
62   {
63
64     m_blocksize = blocksize;
65     m_mapSize = blocksize;
66     m_map = new String[blocksize];
67   }
68
69   /**
70    * Get the length of the list.
71    *
72    * @return Number of strings in the list 
73    */
74   public int getLength()
75   {
76     return m_firstFree;
77   }
78
79   /**
80    * Get the length of the list.
81    *
82    * @return Number of strings in the list
83    */
84   public final int size()
85   {
86     return m_firstFree;
87   }
88
89   /**
90    * Append a string onto the vector.
91    *
92    * @param value Sting to add to the vector
93    */
94   public final void addElement(String value)
95   {
96
97     if ((m_firstFree + 1) >= m_mapSize)
98     {
99       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 }