286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: StringToIntTable.java,v 1.2.4.1 2005/09/15 08:15:55 suresh_emailid Exp $
286N/A */
286N/Apackage com.sun.org.apache.xml.internal.utils;
286N/A
286N/A/**
286N/A * A very simple lookup table that stores a list of strings, the even
286N/A * number strings being keys, and the odd number strings being values.
286N/A * @xsl.usage internal
286N/A */
286N/Apublic class StringToIntTable
286N/A{
286N/A
286N/A public static final int INVALID_KEY = -10000;
286N/A
286N/A /** Block size to allocate */
286N/A private int m_blocksize;
286N/A
286N/A /** Array of strings this table points to. Associated with ints
286N/A * in m_values */
286N/A private String m_map[];
286N/A
286N/A /** Array of ints this table points. Associated with strings from
286N/A * m_map. */
286N/A private int m_values[];
286N/A
286N/A /** Number of ints in the table */
286N/A private int m_firstFree = 0;
286N/A
286N/A /** Size of this table */
286N/A private int m_mapSize;
286N/A
286N/A /**
286N/A * Default constructor. Note that the default
286N/A * block size is very small, for small lists.
286N/A */
286N/A public StringToIntTable()
286N/A {
286N/A
286N/A m_blocksize = 8;
286N/A m_mapSize = m_blocksize;
286N/A m_map = new String[m_blocksize];
286N/A m_values = new int[m_blocksize];
286N/A }
286N/A
286N/A /**
286N/A * Construct a StringToIntTable, using the given block size.
286N/A *
286N/A * @param blocksize Size of block to allocate
286N/A */
286N/A public StringToIntTable(int blocksize)
286N/A {
286N/A
286N/A m_blocksize = blocksize;
286N/A m_mapSize = blocksize;
286N/A m_map = new String[blocksize];
286N/A m_values = new int[m_blocksize];
286N/A }
286N/A
286N/A /**
286N/A * Get the length of the list.
286N/A *
286N/A * @return the length of the list
286N/A */
286N/A public final int getLength()
286N/A {
286N/A return m_firstFree;
286N/A }
286N/A
286N/A /**
286N/A * Append a string onto the vector.
286N/A *
286N/A * @param key String to append
286N/A * @param value The int value of the string
286N/A */
286N/A public final void put(String key, int value)
286N/A {
286N/A
286N/A if ((m_firstFree + 1) >= m_mapSize)
286N/A {
286N/A m_mapSize += m_blocksize;
286N/A
286N/A String newMap[] = new String[m_mapSize];
286N/A
286N/A System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
286N/A
286N/A m_map = newMap;
286N/A
286N/A int newValues[] = new int[m_mapSize];
286N/A
286N/A System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1);
286N/A
286N/A m_values = newValues;
286N/A }
286N/A
286N/A m_map[m_firstFree] = key;
286N/A m_values[m_firstFree] = value;
286N/A
286N/A m_firstFree++;
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given string.
286N/A *
286N/A * @param key String to look for
286N/A *
286N/A * @return The String's int value
286N/A *
286N/A */
286N/A public final int get(String key)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A if (m_map[i].equals(key))
286N/A return m_values[i];
286N/A }
286N/A
286N/A return INVALID_KEY;
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given string. Ignore case.
286N/A *
286N/A * @param key String to look for
286N/A *
286N/A * @return The string's int value
286N/A */
286N/A public final int getIgnoreCase(String key)
286N/A {
286N/A
286N/A if (null == key)
286N/A return INVALID_KEY;
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A if (m_map[i].equalsIgnoreCase(key))
286N/A return m_values[i];
286N/A }
286N/A
286N/A return INVALID_KEY;
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given string.
286N/A *
286N/A * @param key String to look for
286N/A *
286N/A * @return True if the string is in the table
286N/A */
286N/A public final boolean contains(String key)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A if (m_map[i].equals(key))
286N/A return true;
286N/A }
286N/A
286N/A return false;
286N/A }
286N/A
286N/A /**
286N/A * Return array of keys in the table.
286N/A *
286N/A * @return Array of strings
286N/A */
286N/A public final String[] keys()
286N/A {
286N/A String [] keysArr = new String[m_firstFree];
286N/A
286N/A for (int i = 0; i < m_firstFree; i++)
286N/A {
286N/A keysArr[i] = m_map[i];
286N/A }
286N/A
286N/A return keysArr;
286N/A }
286N/A}