001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor;
015:
016: import java.util.Map;
017:
018: /**
019: * Support for comparing part of char array to hash map with strings as keys.
020: *
021: * @author Miloslav Metelka
022: * @version 1.00
023: */
024:
025: public class StringMap extends java.util.HashMap {
026:
027: char[] testChars;
028:
029: int testOffset;
030:
031: int testLen;
032:
033: static final long serialVersionUID = 967608225972123714L;
034:
035: public StringMap() {
036: super ();
037: }
038:
039: public StringMap(int initialCapacity) {
040: super (initialCapacity);
041: }
042:
043: public StringMap(int initialCapacity, float loadFactor) {
044: super (initialCapacity, loadFactor);
045: }
046:
047: public StringMap(Map t) {
048: super (t);
049: }
050:
051: public Object get(char[] chars, int offset, int len) {
052: testChars = chars;
053: testOffset = offset;
054: testLen = len;
055: Object o = get(this );
056: testChars = null; // enable possible GC
057: return o;
058: }
059:
060: public boolean containsKey(char[] chars, int offset, int len) {
061: testChars = chars;
062: testOffset = offset;
063: testLen = len;
064: boolean b = containsKey(this );
065: testChars = null; // enable possible GC
066: return b;
067: }
068:
069: public Object remove(char[] chars, int offset, int len) {
070: testChars = chars;
071: testOffset = offset;
072: testLen = len;
073: Object o = remove(this );
074: testChars = null;
075: return o;
076: }
077:
078: public boolean equals(Object o) {
079: if (this == o) {
080: return true;
081: }
082:
083: if (o instanceof String) {
084: String s = (String) o;
085: if (testLen == s.length()) {
086: for (int i = testLen - 1; i >= 0; i--) {
087: if (testChars[testOffset + i] != s.charAt(i)) {
088: return false;
089: }
090: }
091: return true;
092: }
093: return false;
094: }
095:
096: if (o instanceof char[]) {
097: char[] chars = (char[]) o;
098: if (testLen == chars.length) {
099: for (int i = testLen - 1; i >= 0; i--) {
100: if (testChars[testOffset + i] != chars[i]) {
101: return false;
102: }
103: }
104: return true;
105: }
106: return false;
107: }
108:
109: return false;
110: }
111:
112: public int hashCode() {
113: int h = 0;
114: char[] chars = testChars;
115: int off = testOffset;
116:
117: for (int i = testLen; i > 0; i--) {
118: h = 31 * h + chars[off++];
119: }
120:
121: return h;
122: }
123:
124: }
|