KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serializer > 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.1.4.1 2005/09/08 11:03:19 suresh_emailid Exp $
18  */

19 package com.sun.org.apache.xml.internal.serializer.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  *
25  * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
26  * It exists to cut the serializers dependancy on that package.
27  *
28  * This class is not a public API, it is only public so it can be used
29  * in com.sun.org.apache.xml.internal.serializer.
30  *
31  * @xsl.usage internal
32  */

33 public final class StringToIntTable
34 {
35
36   public static final int INVALID_KEY = -10000;
37   
38   /** Block size to allocate */
39   private int m_blocksize;
40
41   /** Array of strings this table points to. Associated with ints
42    * in m_values */

43   private String JavaDoc m_map[];
44
45   /** Array of ints this table points. Associated with strings from
46    * m_map. */

47   private int m_values[];
48
49   /** Number of ints in the table */
50   private int m_firstFree = 0;
51
52   /** Size of this table */
53   private int m_mapSize;
54
55   /**
56    * Default constructor. Note that the default
57    * block size is very small, for small lists.
58    */

59   public StringToIntTable()
60   {
61
62     m_blocksize = 8;
63     m_mapSize = m_blocksize;
64     m_map = new String JavaDoc[m_blocksize];
65     m_values = new int[m_blocksize];
66   }
67
68   /**
69    * Construct a StringToIntTable, using the given block size.
70    *
71    * @param blocksize Size of block to allocate
72    */

73   public StringToIntTable(int blocksize)
74   {
75
76     m_blocksize = blocksize;
77     m_mapSize = blocksize;
78     m_map = new String JavaDoc[blocksize];
79     m_values = new int[m_blocksize];
80   }
81
82   /**
83    * Get the length of the list.
84    *
85    * @return the length of the list
86    */

87   public final int getLength()
88   {
89     return m_firstFree;
90   }
91
92   /**
93    * Append a string onto the vector.
94    *
95    * @param key String to append
96    * @param value The int value of the string
97    */

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

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

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

173   public final boolean contains(String JavaDoc key)
174   {
175
176     for (int i = 0; i < m_firstFree; i++)
177     {
178       if (m_map[i].equals(key))
179         return true;
180     }
181
182     return false;
183   }
184   
185   /**
186    * Return array of keys in the table.
187    *
188    * @return Array of strings
189    */

190   public final String JavaDoc[] keys()
191   {
192     String JavaDoc [] keysArr = new String JavaDoc[m_firstFree];
193
194     for (int i = 0; i < m_firstFree; i++)
195     {
196       keysArr[i] = m_map[i];
197     }
198
199     return keysArr;
200   }
201 }
202
Popular Tags