KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > StringToStringTableVector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: StringToStringTableVector.java,v 1.5 2004/02/17 04:21:14 minchau Exp $
18  */

19 package org.apache.xml.utils;
20
21 /**
22  * A very simple table that stores a list of StringToStringTables, optimized
23  * for small lists.
24  * @xsl.usage internal
25  */

26 public class StringToStringTableVector
27 {
28
29   /** Size of blocks to allocate */
30   private int m_blocksize;
31
32   /** Array of StringToStringTable objects */
33   private StringToStringTable m_map[];
34
35   /** Number of StringToStringTable objects in this array */
36   private int m_firstFree = 0;
37
38   /** Size of this array */
39   private int m_mapSize;
40
41   /**
42    * Default constructor. Note that the default
43    * block size is very small, for small lists.
44    */

45   public StringToStringTableVector()
46   {
47
48     m_blocksize = 8;
49     m_mapSize = m_blocksize;
50     m_map = new StringToStringTable[m_blocksize];
51   }
52
53   /**
54    * Construct a StringToStringTableVector, using the given block size.
55    *
56    * @param blocksize Size of blocks to allocate
57    */

58   public StringToStringTableVector(int blocksize)
59   {
60
61     m_blocksize = blocksize;
62     m_mapSize = blocksize;
63     m_map = new StringToStringTable[blocksize];
64   }
65
66   /**
67    * Get the length of the list.
68    *
69    * @return Number of StringToStringTable objects in the list
70    */

71   public final int getLength()
72   {
73     return m_firstFree;
74   }
75
76   /**
77    * Get the length of the list.
78    *
79    * @return Number of StringToStringTable objects in the list
80    */

81   public final int size()
82   {
83     return m_firstFree;
84   }
85
86   /**
87    * Append a StringToStringTable object onto the vector.
88    *
89    * @param value StringToStringTable object to add
90    */

91   public final void addElement(StringToStringTable value)
92   {
93
94     if ((m_firstFree + 1) >= m_mapSize)
95     {
96       m_mapSize += m_blocksize;
97
98       StringToStringTable newMap[] = new StringToStringTable[m_mapSize];
99
100       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
101
102       m_map = newMap;
103     }
104
105     m_map[m_firstFree] = value;
106
107     m_firstFree++;
108   }
109
110   /**
111    * Given a string, find the last added occurance value
112    * that matches the key.
113    *
114    * @param key String to look up
115    *
116    * @return the last added occurance value that matches the key
117    * or null if not found.
118    */

119   public final String JavaDoc get(String JavaDoc key)
120   {
121
122     for (int i = m_firstFree - 1; i >= 0; --i)
123     {
124       String JavaDoc nsuri = m_map[i].get(key);
125
126       if (nsuri != null)
127         return nsuri;
128     }
129
130     return null;
131   }
132
133   /**
134    * Given a string, find out if there is a value in this table
135    * that matches the key.
136    *
137    * @param key String to look for
138    *
139    * @return True if the string was found in table, null if not
140    */

141   public final boolean containsKey(String JavaDoc key)
142   {
143
144     for (int i = m_firstFree - 1; i >= 0; --i)
145     {
146       if (m_map[i].get(key) != null)
147         return true;
148     }
149
150     return false;
151   }
152
153   /**
154    * Remove the last element.
155    */

156   public final void removeLastElem()
157   {
158
159     if (m_firstFree > 0)
160     {
161       m_map[m_firstFree] = null;
162
163       m_firstFree--;
164     }
165   }
166
167   /**
168    * Get the nth element.
169    *
170    * @param i Index of element to find
171    *
172    * @return The StringToStringTable object at the given index
173    */

174   public final StringToStringTable elementAt(int i)
175   {
176     return m_map[i];
177   }
178
179   /**
180    * Tell if the table contains the given StringToStringTable.
181    *
182    * @param s The StringToStringTable to find
183    *
184    * @return True if the StringToStringTable is found
185    */

186   public final boolean contains(StringToStringTable s)
187   {
188
189     for (int i = 0; i < m_firstFree; i++)
190     {
191       if (m_map[i].equals(s))
192         return true;
193     }
194
195     return false;
196   }
197 }
198
Popular Tags