KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > HTMLTagHashSet


1 package net.sf.saxon.event;
2
3 /**
4 * A simple class for testing membership of a fixed set of casse-insensitive ASCII strings.
5 * The class must be initialised with enough space for all the strings,
6 * it will go into an infinite loop if it fills. The string matching is case-blind,
7 * using an algorithm that works only for ASCII.
8 *
9 * The class implements part of the java.util.Set interface; it could be replaced with
10 * an implementation of java.util.Set together with a class that implemented a customized
11 * equals() method.
12 */

13
14 public class HTMLTagHashSet {
15
16     String JavaDoc[] strings;
17     int size;
18
19     public HTMLTagHashSet(int size) {
20         strings = new String JavaDoc[size];
21         this.size = size;
22     }
23
24     public void add(String JavaDoc s) {
25         int hash = (hashCode(s) & 0x7fffffff) % size;
26         while(true) {
27             if (strings[hash]==null) {
28                 strings[hash] = s;
29                 return;
30             }
31             if (strings[hash].equalsIgnoreCase(s)) {
32                 return;
33             }
34             hash = (hash + 1) % size;
35         }
36     }
37
38     public boolean contains(String JavaDoc s) {
39         int hash = (hashCode(s) & 0x7fffffff) % size;
40         while(true) {
41             if (strings[hash]==null) {
42                 return false;
43             }
44             if (strings[hash].equalsIgnoreCase(s)) {
45                 return true;
46             }
47             hash = (hash + 1) % size;
48         }
49     }
50
51     private int hashCode(String JavaDoc s) {
52         // get a hashcode that doesn't depend on the case of characters.
53
// This relies on the fact that char & 0xDF is case-blind in ASCII
54
int hash = 0;
55         int limit = s.length();
56         if (limit>24) limit = 24;
57         for (int i=0; i<limit; i++) {
58             hash = (hash<<1) + (s.charAt(i) & 0xdf);
59         }
60         return hash;
61     }
62 }
63
64 //
65
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
66
// you may not use this file except in compliance with the License. You may obtain a copy of the
67
// License at http://www.mozilla.org/MPL/
68
//
69
// Software distributed under the License is distributed on an "AS IS" basis,
70
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
71
// See the License for the specific language governing rights and limitations under the License.
72
//
73
// The Original Code is: all this file.
74
//
75
// The Initial Developer of this module is Michael H. Kay.
76
//
77
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
78
//
79
// Contributor(s): none.
80
//
Popular Tags