Home | History | Annotate | Download | only in utils
      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 468654 2006-10-28 07:09:23Z minchau $
     20  */
     21 package org.apache.xml.serializer.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  *
     27  * This class is a copy of the one in org.apache.xml.utils.
     28  * It exists to cut the serializers dependancy on that package.
     29  *
     30  * This class is not a public API, it is only public so it can be used
     31  * in org.apache.xml.serializer.
     32  *
     33  * @xsl.usage internal
     34  */
     35 public final class StringToIntTable
     36 {
     37 
     38   public static final int INVALID_KEY = -10000;
     39 
     40   /** Block size to allocate          */
     41   private int m_blocksize;
     42 
     43   /** Array of strings this table points to. Associated with ints
     44    * in m_values         */
     45   private String m_map[];
     46 
     47   /** Array of ints this table points. Associated with strings from
     48    * m_map.         */
     49   private int m_values[];
     50 
     51   /** Number of ints in the table          */
     52   private int m_firstFree = 0;
     53 
     54   /** Size of this table         */
     55   private int m_mapSize;
     56 
     57   /**
     58    * Default constructor.  Note that the default
     59    * block size is very small, for small lists.
     60    */
     61   public StringToIntTable()
     62   {
     63 
     64     m_blocksize = 8;
     65     m_mapSize = m_blocksize;
     66     m_map = new String[m_blocksize];
     67     m_values = new int[m_blocksize];
     68   }
     69 
     70   /**
     71    * Construct a StringToIntTable, using the given block size.
     72    *
     73    * @param blocksize Size of block to allocate
     74    */
     75   public StringToIntTable(int blocksize)
     76   {
     77 
     78     m_blocksize = blocksize;
     79     m_mapSize = blocksize;
     80     m_map = new String[blocksize];
     81     m_values = new int[m_blocksize];
     82   }
     83 
     84   /**
     85    * Get the length of the list.
     86    *
     87    * @return the length of the list
     88    */
     89   public final int getLength()
     90   {
     91     return m_firstFree;
     92   }
     93 
     94   /**
     95    * Append a string onto the vector.
     96    *
     97    * @param key String to append
     98    * @param value The int value of the string
     99    */
    100   public final void put(String key, int value)
    101   {
    102 
    103     if ((m_firstFree + 1) >= m_mapSize)
    104     {
    105       m_mapSize += m_blocksize;
    106 
    107       String newMap[] = new String[m_mapSize];
    108 
    109       System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
    110 
    111       m_map = newMap;
    112 
    113       int newValues[] = new int[m_mapSize];
    114 
    115       System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1);
    116 
    117       m_values = newValues;
    118     }
    119 
    120     m_map[m_firstFree] = key;
    121     m_values[m_firstFree] = value;
    122 
    123     m_firstFree++;
    124   }
    125 
    126   /**
    127    * Tell if the table contains the given string.
    128    *
    129    * @param key String to look for
    130    *
    131    * @return The String's int value
    132    *
    133    */
    134   public final int get(String key)
    135   {
    136 
    137     for (int i = 0; i < m_firstFree; i++)
    138     {
    139       if (m_map[i].equals(key))
    140         return m_values[i];
    141     }
    142 
    143     return INVALID_KEY;
    144   }
    145 
    146   /**
    147    * Tell if the table contains the given string. Ignore case.
    148    *
    149    * @param key String to look for
    150    *
    151    * @return The string's int value
    152    */
    153   public final int getIgnoreCase(String key)
    154   {
    155 
    156     if (null == key)
    157         return INVALID_KEY;
    158 
    159     for (int i = 0; i < m_firstFree; i++)
    160     {
    161       if (m_map[i].equalsIgnoreCase(key))
    162         return m_values[i];
    163     }
    164 
    165     return INVALID_KEY;
    166   }
    167 
    168   /**
    169    * Tell if the table contains the given string.
    170    *
    171    * @param key String to look for
    172    *
    173    * @return True if the string is in the table
    174    */
    175   public final boolean contains(String key)
    176   {
    177 
    178     for (int i = 0; i < m_firstFree; i++)
    179     {
    180       if (m_map[i].equals(key))
    181         return true;
    182     }
    183 
    184     return false;
    185   }
    186 
    187   /**
    188    * Return array of keys in the table.
    189    *
    190    * @return Array of strings
    191    */
    192   public final String[] keys()
    193   {
    194     String [] keysArr = new String[m_firstFree];
    195 
    196     for (int i = 0; i < m_firstFree; i++)
    197     {
    198       keysArr[i] = m_map[i];
    199     }
    200 
    201     return keysArr;
    202   }
    203 }
    204