001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: StringToIntTable.java,v 1.6 2004/02/17 04:21:14 minchau Exp $
018: */
019: package org.apache.xml.utils;
020:
021: /**
022: * A very simple lookup table that stores a list of strings, the even
023: * number strings being keys, and the odd number strings being values.
024: * @xsl.usage internal
025: */
026: public class StringToIntTable {
027:
028: public static final int INVALID_KEY = -10000;
029:
030: /** Block size to allocate */
031: private int m_blocksize;
032:
033: /** Array of strings this table points to. Associated with ints
034: * in m_values */
035: private String m_map[];
036:
037: /** Array of ints this table points. Associated with strings from
038: * m_map. */
039: private int m_values[];
040:
041: /** Number of ints in the table */
042: private int m_firstFree = 0;
043:
044: /** Size of this table */
045: private int m_mapSize;
046:
047: /**
048: * Default constructor. Note that the default
049: * block size is very small, for small lists.
050: */
051: public StringToIntTable() {
052:
053: m_blocksize = 8;
054: m_mapSize = m_blocksize;
055: m_map = new String[m_blocksize];
056: m_values = new int[m_blocksize];
057: }
058:
059: /**
060: * Construct a StringToIntTable, using the given block size.
061: *
062: * @param blocksize Size of block to allocate
063: */
064: public StringToIntTable(int blocksize) {
065:
066: m_blocksize = blocksize;
067: m_mapSize = blocksize;
068: m_map = new String[blocksize];
069: m_values = new int[m_blocksize];
070: }
071:
072: /**
073: * Get the length of the list.
074: *
075: * @return the length of the list
076: */
077: public final int getLength() {
078: return m_firstFree;
079: }
080:
081: /**
082: * Append a string onto the vector.
083: *
084: * @param key String to append
085: * @param value The int value of the string
086: */
087: public final void put(String key, int value) {
088:
089: if ((m_firstFree + 1) >= m_mapSize) {
090: m_mapSize += m_blocksize;
091:
092: String newMap[] = new String[m_mapSize];
093:
094: System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
095:
096: m_map = newMap;
097:
098: int newValues[] = new int[m_mapSize];
099:
100: System
101: .arraycopy(m_values, 0, newValues, 0,
102: m_firstFree + 1);
103:
104: m_values = newValues;
105: }
106:
107: m_map[m_firstFree] = key;
108: m_values[m_firstFree] = value;
109:
110: m_firstFree++;
111: }
112:
113: /**
114: * Tell if the table contains the given string.
115: *
116: * @param key String to look for
117: *
118: * @return The String's int value
119: *
120: */
121: public final int get(String key) {
122:
123: for (int i = 0; i < m_firstFree; i++) {
124: if (m_map[i].equals(key))
125: return m_values[i];
126: }
127:
128: return INVALID_KEY;
129: }
130:
131: /**
132: * Tell if the table contains the given string. Ignore case.
133: *
134: * @param key String to look for
135: *
136: * @return The string's int value
137: */
138: public final int getIgnoreCase(String key) {
139:
140: if (null == key)
141: return INVALID_KEY;
142:
143: for (int i = 0; i < m_firstFree; i++) {
144: if (m_map[i].equalsIgnoreCase(key))
145: return m_values[i];
146: }
147:
148: return INVALID_KEY;
149: }
150:
151: /**
152: * Tell if the table contains the given string.
153: *
154: * @param key String to look for
155: *
156: * @return True if the string is in the table
157: */
158: public final boolean contains(String key) {
159:
160: for (int i = 0; i < m_firstFree; i++) {
161: if (m_map[i].equals(key))
162: return true;
163: }
164:
165: return false;
166: }
167:
168: /**
169: * Return array of keys in the table.
170: *
171: * @return Array of strings
172: */
173: public final String[] keys() {
174: String[] keysArr = new String[m_firstFree];
175:
176: for (int i = 0; i < m_firstFree; i++) {
177: keysArr[i] = m_map[i];
178: }
179:
180: return keysArr;
181: }
182: }
|