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