001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042:
043: ============================================================================
044: The Apache Software License, Version 1.1
045: ============================================================================
046:
047: Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
048:
049: Redistribution and use in source and binary forms, with or without modifica-
050: tion, are permitted provided that the following conditions are met:
051:
052: 1. Redistributions of source code must retain the above copyright notice,
053: this list of conditions and the following disclaimer.
054:
055: 2. Redistributions in binary form must reproduce the above copyright notice,
056: this list of conditions and the following disclaimer in the documentation
057: and/or other materials provided with the distribution.
058:
059: 3. The end-user documentation included with the redistribution, if any, must
060: include the following acknowledgment: "This product includes software
061: developed by the Apache Software Foundation (http://www.apache.org/)."
062: Alternately, this acknowledgment may appear in the software itself, if
063: and wherever such third-party acknowledgments normally appear.
064:
065: 4. The names "Batik" and "Apache Software Foundation" must not be
066: used to endorse or promote products derived from this software without
067: prior written permission. For written permission, please contact
068: apache@apache.org.
069:
070: 5. Products derived from this software may not be called "Apache", nor may
071: "Apache" appear in their name, without prior written permission of the
072: Apache Software Foundation.
073:
074: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
075: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
076: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
077: APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
078: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
079: DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
080: OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
081: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
082: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
083: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
084:
085: This software consists of voluntary contributions made by many individuals
086: on behalf of the Apache Software Foundation. For more information on the
087: Apache Software Foundation, please see <http://www.apache.org/>.
088:
089: */
090:
091: package org.netbeans.modules.visualweb.designer.cssengine;
092:
093: import org.openide.ErrorManager;
094:
095: /**
096: * DOH! I NOW REALIZE THERE ALREADY WAS A STRINGINTMAP IN BATIK.
097: * Try to merge the two versions!
098: * <p>
099: *
100: * ORIGINALLY COPIED FROM Batik's org.apache.batik.css.engine.value.StringMap.
101: * I tweaked it to reference ints rather than Value objects and made capacity
102: * a parameter and ripped out some methods we won't use.
103: * <p>
104: * A simple hashtable, not synchronized, with fixed load factor and with
105: * equality test made with '=='.
106: *
107: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
108: * @author Tor Norbye
109: */
110: public class StringIntMap {
111:
112: /**
113: * The underlying array
114: */
115: protected Entry[] table;
116:
117: /**
118: * The number of entries
119: */
120: protected int count;
121:
122: /**
123: * Creates a new table.
124: */
125: public StringIntMap(int capacity) {
126: table = new Entry[capacity];
127: }
128:
129: /**
130: * Gets the int corresponding to the given string.
131: * Return -1 if the int is not in the table.
132: * @return the value or null
133: */
134: public int get(String key) {
135: int hash = key.hashCode() & 0x7FFFFFFF;
136: int index = hash % table.length;
137:
138: for (Entry e = table[index]; e != null; e = e.next) {
139: if ((e.hash == hash) && e.key == key) {
140: return e.value;
141: }
142: }
143: return -1;
144: }
145:
146: /**
147: * Sets a new value for the given variable
148: * @return the old value or null
149: */
150: public int put(String key, int value) {
151: int hash = key.hashCode() & 0x7FFFFFFF;
152: int index = hash % table.length;
153:
154: for (Entry e = table[index]; e != null; e = e.next) {
155: if ((e.hash == hash) && e.key == key) {
156: int old = e.value;
157: e.value = value;
158: return old;
159: }
160: }
161:
162: // The key is not in the hash table
163: int len = table.length;
164: if (count++ >= (len * 3) >>> 2) {
165: // This shouldn't happen, we're using it for internal tables
166: // whose sizes I control, so log a warning so that I look at this
167: // and change the default sizes
168: ErrorManager.getDefault().log("Had to rehash StringIntMap");
169: rehash();
170: index = hash % table.length;
171: }
172: Entry e = new Entry(hash, key, value, table[index]);
173: table[index] = e;
174: return -1;
175: }
176:
177: /**
178: * Rehash the table
179: */
180: protected void rehash() {
181: Entry[] oldTable = table;
182:
183: table = new Entry[oldTable.length * 2 + 1];
184:
185: for (int i = oldTable.length - 1; i >= 0; i--) {
186: for (Entry old = oldTable[i]; old != null;) {
187: Entry e = old;
188: old = old.next;
189:
190: int index = e.hash % table.length;
191: e.next = table[index];
192: table[index] = e;
193: }
194: }
195: }
196:
197: /**
198: * To manage collisions
199: */
200: protected static class Entry {
201: /**
202: * The hash code
203: */
204: public int hash;
205:
206: /**
207: * The key
208: */
209: public String key;
210:
211: /**
212: * The value
213: */
214: public int value;
215:
216: /**
217: * The next entry
218: */
219: public Entry next;
220:
221: /**
222: * Creates a new entry
223: */
224: public Entry(int hash, String key, int value, Entry next) {
225: this.hash = hash;
226: this.key = key;
227: this.value = value;
228: this.next = next;
229: }
230: }
231: }
|