KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > StringToStringTable


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: StringToStringTable.java,v 1.5 2004/02/17 04:21:14 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.utils;
20
21 /**
22  * A very simple lookup table that stores a list of strings, the even
23  * number strings being keys, and the odd number strings being values.
24  * @xsl.usage internal
25  */

26 public class StringToStringTable
27 {
28
29   /** Size of blocks to allocate */
30   private int m_blocksize;
31
32   /** Array of strings this contains */
33   private String JavaDoc m_map[];
34
35   /** Number of strings this contains */
36   private int m_firstFree = 0;
37
38   /** Size of this table */
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 StringToStringTable()
46   {
47
48     m_blocksize = 16;
49     m_mapSize = m_blocksize;
50     m_map = new String JavaDoc[m_blocksize];
51   }
52
53   /**
54    * Construct a StringToStringTable, using the given block size.
55    *
56    * @param blocksize Size of blocks to allocate
57    */

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

71   public final int getLength()
72   {
73     return m_firstFree;
74   }
75
76   /**
77    * Append a string onto the vector.
78    * The strings go to the even locations in the array
79    * and the values in the odd.
80    *
81    * @param key String to add to the list
82    * @param value Value of the string
83    */

84   public final void put(String JavaDoc key, String JavaDoc value)
85   {
86
87     if ((m_firstFree + 2) >= m_mapSize)
88     {
89       m_mapSize += m_blocksize;
90
91       String JavaDoc newMap[] = new String JavaDoc[m_mapSize];
92
93       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
94
95       m_map = newMap;
96     }
97
98     m_map[m_firstFree] = key;
99
100     m_firstFree++;
101
102     m_map[m_firstFree] = value;
103
104     m_firstFree++;
105   }
106
107   /**
108    * Tell if the table contains the given string.
109    *
110    * @param key String to look up
111    *
112    * @return return the value of the string or null if not found.
113    */

114   public final String JavaDoc get(String JavaDoc key)
115   {
116
117     for (int i = 0; i < m_firstFree; i += 2)
118     {
119       if (m_map[i].equals(key))
120         return m_map[i + 1];
121     }
122
123     return null;
124   }
125
126   /**
127    * Remove the given string and its value from this table.
128    *
129    * @param key String to remove from the table
130    */

131   public final void remove(String JavaDoc key)
132   {
133
134     for (int i = 0; i < m_firstFree; i += 2)
135     {
136       if (m_map[i].equals(key))
137       {
138         if ((i + 2) < m_firstFree)
139           System.arraycopy(m_map, i + 2, m_map, i, m_firstFree - (i + 2));
140
141         m_firstFree -= 2;
142         m_map[m_firstFree] = null;
143         m_map[m_firstFree + 1] = null;
144
145         break;
146       }
147     }
148   }
149
150   /**
151    * Tell if the table contains the given string. Ignore case
152    *
153    * @param key String to look up
154    *
155    * @return The value of the string or null if not found
156    */

157   public final String JavaDoc getIgnoreCase(String JavaDoc key)
158   {
159
160     if (null == key)
161       return null;
162
163     for (int i = 0; i < m_firstFree; i += 2)
164     {
165       if (m_map[i].equalsIgnoreCase(key))
166         return m_map[i + 1];
167     }
168
169     return null;
170   }
171
172   /**
173    * Tell if the table contains the given string in the value.
174    *
175    * @param val Value of the string to look up
176    *
177    * @return the string associated with the given value or null if not found
178    */

179   public final String JavaDoc getByValue(String JavaDoc val)
180   {
181
182     for (int i = 1; i < m_firstFree; i += 2)
183     {
184       if (m_map[i].equals(val))
185         return m_map[i - 1];
186     }
187
188     return null;
189   }
190
191   /**
192    * Get the nth element.
193    *
194    * @param i index of the string to look up.
195    *
196    * @return The string at the given index.
197    */

198   public final String JavaDoc elementAt(int i)
199   {
200     return m_map[i];
201   }
202
203   /**
204    * Tell if the table contains the given string.
205    *
206    * @param key String to look up
207    *
208    * @return True if the given string is in this table
209    */

210   public final boolean contains(String JavaDoc key)
211   {
212
213     for (int i = 0; i < m_firstFree; i += 2)
214     {
215       if (m_map[i].equals(key))
216         return true;
217     }
218
219     return false;
220   }
221
222   /**
223    * Tell if the table contains the given string.
224    *
225    * @param val value to look up
226    *
227    * @return True if the given value is in the table.
228    */

229   public final boolean containsValue(String JavaDoc val)
230   {
231
232     for (int i = 1; i < m_firstFree; i += 2)
233     {
234       if (m_map[i].equals(val))
235         return true;
236     }
237
238     return false;
239   }
240 }
241
Popular Tags