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: StringToStringTableVector.java,v 1.5 2004/02/17 04:21:14 minchau Exp $
018: */
019: package org.apache.xml.utils;
020:
021: /**
022: * A very simple table that stores a list of StringToStringTables, optimized
023: * for small lists.
024: * @xsl.usage internal
025: */
026: public class StringToStringTableVector {
027:
028: /** Size of blocks to allocate */
029: private int m_blocksize;
030:
031: /** Array of StringToStringTable objects */
032: private StringToStringTable m_map[];
033:
034: /** Number of StringToStringTable objects in this array */
035: private int m_firstFree = 0;
036:
037: /** Size of this array */
038: private int m_mapSize;
039:
040: /**
041: * Default constructor. Note that the default
042: * block size is very small, for small lists.
043: */
044: public StringToStringTableVector() {
045:
046: m_blocksize = 8;
047: m_mapSize = m_blocksize;
048: m_map = new StringToStringTable[m_blocksize];
049: }
050:
051: /**
052: * Construct a StringToStringTableVector, using the given block size.
053: *
054: * @param blocksize Size of blocks to allocate
055: */
056: public StringToStringTableVector(int blocksize) {
057:
058: m_blocksize = blocksize;
059: m_mapSize = blocksize;
060: m_map = new StringToStringTable[blocksize];
061: }
062:
063: /**
064: * Get the length of the list.
065: *
066: * @return Number of StringToStringTable objects in the list
067: */
068: public final int getLength() {
069: return m_firstFree;
070: }
071:
072: /**
073: * Get the length of the list.
074: *
075: * @return Number of StringToStringTable objects in the list
076: */
077: public final int size() {
078: return m_firstFree;
079: }
080:
081: /**
082: * Append a StringToStringTable object onto the vector.
083: *
084: * @param value StringToStringTable object to add
085: */
086: public final void addElement(StringToStringTable value) {
087:
088: if ((m_firstFree + 1) >= m_mapSize) {
089: m_mapSize += m_blocksize;
090:
091: StringToStringTable newMap[] = new StringToStringTable[m_mapSize];
092:
093: System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
094:
095: m_map = newMap;
096: }
097:
098: m_map[m_firstFree] = value;
099:
100: m_firstFree++;
101: }
102:
103: /**
104: * Given a string, find the last added occurance value
105: * that matches the key.
106: *
107: * @param key String to look up
108: *
109: * @return the last added occurance value that matches the key
110: * or null if not found.
111: */
112: public final String get(String key) {
113:
114: for (int i = m_firstFree - 1; i >= 0; --i) {
115: String nsuri = m_map[i].get(key);
116:
117: if (nsuri != null)
118: return nsuri;
119: }
120:
121: return null;
122: }
123:
124: /**
125: * Given a string, find out if there is a value in this table
126: * that matches the key.
127: *
128: * @param key String to look for
129: *
130: * @return True if the string was found in table, null if not
131: */
132: public final boolean containsKey(String key) {
133:
134: for (int i = m_firstFree - 1; i >= 0; --i) {
135: if (m_map[i].get(key) != null)
136: return true;
137: }
138:
139: return false;
140: }
141:
142: /**
143: * Remove the last element.
144: */
145: public final void removeLastElem() {
146:
147: if (m_firstFree > 0) {
148: m_map[m_firstFree] = null;
149:
150: m_firstFree--;
151: }
152: }
153:
154: /**
155: * Get the nth element.
156: *
157: * @param i Index of element to find
158: *
159: * @return The StringToStringTable object at the given index
160: */
161: public final StringToStringTable elementAt(int i) {
162: return m_map[i];
163: }
164:
165: /**
166: * Tell if the table contains the given StringToStringTable.
167: *
168: * @param s The StringToStringTable to find
169: *
170: * @return True if the StringToStringTable is found
171: */
172: public final boolean contains(StringToStringTable s) {
173:
174: for (int i = 0; i < m_firstFree; i++) {
175: if (m_map[i].equals(s))
176: return true;
177: }
178:
179: return false;
180: }
181: }
|