KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > StringMap


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor;
21
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 /** Support for comparing part of char array
26 * to hash map with strings as keys.
27 *
28 * @author Miloslav Metelka
29 * @version 1.00
30 */

31
32 public class StringMap extends java.util.HashMap JavaDoc {
33
34     char[] testChars;
35
36     int testOffset;
37
38     int testLen;
39
40     static final long serialVersionUID =967608225972123714L;
41     public StringMap() {
42         super();
43     }
44
45     public StringMap(int initialCapacity) {
46         super(initialCapacity);
47     }
48
49     public StringMap(int initialCapacity, float loadFactor) {
50         super(initialCapacity, loadFactor);
51     }
52
53     public StringMap(Map JavaDoc t) {
54         super(t);
55     }
56
57     public Object JavaDoc get(char[] chars, int offset, int len) {
58         testChars = chars;
59         testOffset = offset;
60         testLen = len;
61         Object JavaDoc o = get(this);
62         testChars = null; // enable possible GC
63
return o;
64     }
65
66     public boolean containsKey(char[] chars, int offset, int len) {
67         testChars = chars;
68         testOffset = offset;
69         testLen = len;
70         boolean b = containsKey(this);
71         testChars = null; // enable possible GC
72
return b;
73     }
74
75     public Object JavaDoc remove(char[] chars, int offset, int len) {
76         testChars = chars;
77         testOffset = offset;
78         testLen = len;
79         Object JavaDoc o = remove(this);
80         testChars = null;
81         return o;
82     }
83
84     public boolean equals(Object JavaDoc o) {
85         if (this == o) {
86             return true;
87         }
88
89         if (o instanceof String JavaDoc) {
90             String JavaDoc s = (String JavaDoc)o;
91             if (testLen == s.length()) {
92                 for (int i = testLen - 1; i >= 0; i--) {
93                     if (testChars[testOffset + i] != s.charAt(i)) {
94                         return false;
95                     }
96                 }
97                 return true;
98             }
99             return false;
100         }
101
102         if (o instanceof char[]) {
103             char[] chars = (char[])o;
104             if (testLen == chars.length) {
105                 for (int i = testLen - 1; i >= 0; i--) {
106                     if (testChars[testOffset + i] != chars[i]) {
107                         return false;
108                     }
109                 }
110                 return true;
111             }
112             return false;
113         }
114
115         return false;
116     }
117
118     public int hashCode() {
119         int h = 0;
120         char[] chars = testChars;
121         int off = testOffset;
122
123         for (int i = testLen; i > 0; i--) {
124             h = 31 * h + chars[off++];
125         }
126
127         return h;
128     }
129
130 }
131
Popular Tags