001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: StringToIntTable.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xml.utils;
022    
023    /**
024     * A very simple lookup table that stores a list of strings, the even
025     * number strings being keys, and the odd number strings being values.
026     * @xsl.usage internal
027     */
028    public class StringToIntTable
029    {
030    
031      public static final int INVALID_KEY = -10000;
032      
033      /** Block size to allocate          */
034      private int m_blocksize;
035    
036      /** Array of strings this table points to. Associated with ints
037       * in m_values         */
038      private String m_map[];
039    
040      /** Array of ints this table points. Associated with strings from
041       * m_map.         */
042      private int m_values[];
043    
044      /** Number of ints in the table          */
045      private int m_firstFree = 0;
046    
047      /** Size of this table         */
048      private int m_mapSize;
049    
050      /**
051       * Default constructor.  Note that the default
052       * block size is very small, for small lists.
053       */
054      public StringToIntTable()
055      {
056    
057        m_blocksize = 8;
058        m_mapSize = m_blocksize;
059        m_map = new String[m_blocksize];
060        m_values = new int[m_blocksize];
061      }
062    
063      /**
064       * Construct a StringToIntTable, using the given block size.
065       *
066       * @param blocksize Size of block to allocate
067       */
068      public StringToIntTable(int blocksize)
069      {
070    
071        m_blocksize = blocksize;
072        m_mapSize = blocksize;
073        m_map = new String[blocksize];
074        m_values = new int[m_blocksize];
075      }
076    
077      /**
078       * Get the length of the list.
079       *
080       * @return the length of the list 
081       */
082      public final int getLength()
083      {
084        return m_firstFree;
085      }
086    
087      /**
088       * Append a string onto the vector.
089       *
090       * @param key String to append
091       * @param value The int value of the string
092       */
093      public final void put(String key, int value)
094      {
095    
096        if ((m_firstFree + 1) >= m_mapSize)
097        {
098          m_mapSize += m_blocksize;
099    
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       */
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       */
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       */
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       */
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    }