KickJava   Java API By Example, From Geeks To Geeks.

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


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: StringToIntTable.java,v 1.6 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 StringToIntTable
27 {
28
29   public static final int INVALID_KEY = -10000;
30   
31   /** Block size to allocate */
32   private int m_blocksize;
33
34   /** Array of strings this table points to. Associated with ints
35    * in m_values */

36   private String JavaDoc m_map[];
37
38   /** Array of ints this table points. Associated with strings from
39    * m_map. */

40   private int m_values[];
41
42   /** Number of ints in the table */
43   private int m_firstFree = 0;
44
45   /** Size of this table */
46   private int m_mapSize;
47
48   /**
49    * Default constructor. Note that the default
50    * block size is very small, for small lists.
51    */

52   public StringToIntTable()
53   {
54
55     m_blocksize = 8;
56     m_mapSize = m_blocksize;
57     m_map = new String JavaDoc[m_blocksize];
58     m_values = new int[m_blocksize];
59   }
60
61   /**
62    * Construct a StringToIntTable, using the given block size.
63    *
64    * @param blocksize Size of block to allocate
65    */

66   public StringToIntTable(int blocksize)
67   {
68
69     m_blocksize = blocksize;
70     m_mapSize = blocksize;
71     m_map = new String JavaDoc[blocksize];
72     m_values = new int[m_blocksize];
73   }
74
75   /**
76    * Get the length of the list.
77    *
78    * @return the length of the list
79    */

80   public final int getLength()
81   {
82     return m_firstFree;
83   }
84
85   /**
86    * Append a string onto the vector.
87    *
88    * @param key String to append
89    * @param value The int value of the string
90    */

91   public final void put(String JavaDoc key, int value)
92   {
93
94     if ((m_firstFree + 1) >= m_mapSize)
95     {
96       m_mapSize += m_blocksize;
97
98       String JavaDoc newMap[] = new String JavaDoc[m_mapSize];
99
100       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
101
102       m_map = newMap;
103
104       int newValues[] = new int[m_mapSize];
105
106       System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1);
107
108       m_values = newValues;
109     }
110
111     m_map[m_firstFree] = key;
112     m_values[m_firstFree] = value;
113
114     m_firstFree++;
115   }
116
117   /**
118    * Tell if the table contains the given string.
119    *
120    * @param key String to look for
121    *
122    * @return The String's int value
123    *
124    */

125   public final int get(String JavaDoc key)
126   {
127
128     for (int i = 0; i < m_firstFree; i++)
129     {
130       if (m_map[i].equals(key))
131         return m_values[i];
132     }
133
134     return INVALID_KEY;
135   }
136
137   /**
138    * Tell if the table contains the given string. Ignore case.
139    *
140    * @param key String to look for
141    *
142    * @return The string's int value
143    */

144   public final int getIgnoreCase(String JavaDoc key)
145   {
146
147     if (null == key)
148         return INVALID_KEY;
149
150     for (int i = 0; i < m_firstFree; i++)
151     {
152       if (m_map[i].equalsIgnoreCase(key))
153         return m_values[i];
154     }
155
156     return INVALID_KEY;
157   }
158
159   /**
160    * Tell if the table contains the given string.
161    *
162    * @param key String to look for
163    *
164    * @return True if the string is in the table
165    */

166   public final boolean contains(String JavaDoc key)
167   {
168
169     for (int i = 0; i < m_firstFree; i++)
170     {
171       if (m_map[i].equals(key))
172         return true;
173     }
174
175     return false;
176   }
177   
178   /**
179    * Return array of keys in the table.
180    *
181    * @return Array of strings
182    */

183   public final String JavaDoc[] keys()
184   {
185     String JavaDoc [] keysArr = new String JavaDoc[m_firstFree];
186
187     for (int i = 0; i < m_firstFree; i++)
188     {
189       keysArr[i] = m_map[i];
190     }
191
192     return keysArr;
193   }
194 }
195
Popular Tags