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