6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: StringToIntTable.java,v 1.1.4.1 2005/09/08 11:03:19 suresh_emailid Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xml.internal.serializer.utils;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* A very simple lookup table that stores a list of strings, the even
|
|
27 |
* number strings being keys, and the odd number strings being values.
|
|
28 |
*
|
|
29 |
* This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
|
|
30 |
* It exists to cut the serializers dependancy on that package.
|
|
31 |
*
|
|
32 |
* This class is not a public API, it is only public so it can be used
|
|
33 |
* in com.sun.org.apache.xml.internal.serializer.
|
|
34 |
*
|
|
35 |
* @xsl.usage internal
|
|
36 |
*/
|
|
37 |
public final class StringToIntTable
|
|
38 |
{
|
|
39 |
|
|
40 |
public static final int INVALID_KEY = -10000;
|
|
41 |
|
|
42 |
/** Block size to allocate */
|
|
43 |
private int m_blocksize;
|
|
44 |
|
|
45 |
/** Array of strings this table points to. Associated with ints
|
|
46 |
* in m_values */
|
|
47 |
private String m_map[];
|
|
48 |
|
|
49 |
/** Array of ints this table points. Associated with strings from
|
|
50 |
* m_map. */
|
|
51 |
private int m_values[];
|
|
52 |
|
|
53 |
/** Number of ints in the table */
|
|
54 |
private int m_firstFree = 0;
|
|
55 |
|
|
56 |
/** Size of this table */
|
|
57 |
private int m_mapSize;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Default constructor. Note that the default
|
|
61 |
* block size is very small, for small lists.
|
|
62 |
*/
|
|
63 |
public StringToIntTable()
|
|
64 |
{
|
|
65 |
|
|
66 |
m_blocksize = 8;
|
|
67 |
m_mapSize = m_blocksize;
|
|
68 |
m_map = new String[m_blocksize];
|
|
69 |
m_values = new int[m_blocksize];
|
|
70 |
}
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Construct a StringToIntTable, using the given block size.
|
|
74 |
*
|
|
75 |
* @param blocksize Size of block to allocate
|
|
76 |
*/
|
|
77 |
public StringToIntTable(int blocksize)
|
|
78 |
{
|
|
79 |
|
|
80 |
m_blocksize = blocksize;
|
|
81 |
m_mapSize = blocksize;
|
|
82 |
m_map = new String[blocksize];
|
|
83 |
m_values = new int[m_blocksize];
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Get the length of the list.
|
|
88 |
*
|
|
89 |
* @return the length of the list
|
|
90 |
*/
|
|
91 |
public final int getLength()
|
|
92 |
{
|
|
93 |
return m_firstFree;
|
|
94 |
}
|
|
95 |
|
|
96 |
/**
|
|
97 |
* Append a string onto the vector.
|
|
98 |
*
|
|
99 |
* @param key String to append
|
|
100 |
* @param value The int value of the string
|
|
101 |
*/
|
|
102 |
public final void put(String key, int value)
|
|
103 |
{
|
|
104 |
|
|
105 |
if ((m_firstFree + 1) >= m_mapSize)
|
|
106 |
{
|
|
107 |
m_mapSize += m_blocksize;
|
|
108 |
|
|
109 |
String newMap[] = new String[m_mapSize];
|
|
110 |
|
|
111 |
System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
|
|
112 |
|
|
113 |
m_map = newMap;
|
|
114 |
|
|
115 |
int newValues[] = new int[m_mapSize];
|
|
116 |
|
|
117 |
System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1);
|
|
118 |
|
|
119 |
m_values = newValues;
|
|
120 |
}
|
|
121 |
|
|
122 |
m_map[m_firstFree] = key;
|
|
123 |
m_values[m_firstFree] = value;
|
|
124 |
|
|
125 |
m_firstFree++;
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Tell if the table contains the given string.
|
|
130 |
*
|
|
131 |
* @param key String to look for
|
|
132 |
*
|
|
133 |
* @return The String's int value
|
|
134 |
*
|
|
135 |
*/
|
|
136 |
public final int get(String key)
|
|
137 |
{
|
|
138 |
|
|
139 |
for (int i = 0; i < m_firstFree; i++)
|
|
140 |
{
|
|
141 |
if (m_map[i].equals(key))
|
|
142 |
return m_values[i];
|
|
143 |
}
|
|
144 |
|
|
145 |
return INVALID_KEY;
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Tell if the table contains the given string. Ignore case.
|
|
150 |
*
|
|
151 |
* @param key String to look for
|
|
152 |
*
|
|
153 |
* @return The string's int value
|
|
154 |
*/
|
|
155 |
public final int getIgnoreCase(String key)
|
|
156 |
{
|
|
157 |
|
|
158 |
if (null == key)
|
|
159 |
return INVALID_KEY;
|
|
160 |
|
|
161 |
for (int i = 0; i < m_firstFree; i++)
|
|
162 |
{
|
|
163 |
if (m_map[i].equalsIgnoreCase(key))
|
|
164 |
return m_values[i];
|
|
165 |
}
|
|
166 |
|
|
167 |
return INVALID_KEY;
|
|
168 |
}
|
|
169 |
|
|
170 |
/**
|
|
171 |
* Tell if the table contains the given string.
|
|
172 |
*
|
|
173 |
* @param key String to look for
|
|
174 |
*
|
|
175 |
* @return True if the string is in the table
|
|
176 |
*/
|
|
177 |
public final boolean contains(String key)
|
|
178 |
{
|
|
179 |
|
|
180 |
for (int i = 0; i < m_firstFree; i++)
|
|
181 |
{
|
|
182 |
if (m_map[i].equals(key))
|
|
183 |
return true;
|
|
184 |
}
|
|
185 |
|
|
186 |
return false;
|
|
187 |
}
|
|
188 |
|
|
189 |
/**
|
|
190 |
* Return array of keys in the table.
|
|
191 |
*
|
|
192 |
* @return Array of strings
|
|
193 |
*/
|
|
194 |
public final String[] keys()
|
|
195 |
{
|
|
196 |
String [] keysArr = new String[m_firstFree];
|
|
197 |
|
|
198 |
for (int i = 0; i < m_firstFree; i++)
|
|
199 |
{
|
|
200 |
keysArr[i] = m_map[i];
|
|
201 |
}
|
|
202 |
|
|
203 |
return keysArr;
|
|
204 |
}
|
|
205 |
}
|