KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jedit > syntax > KeywordMap


1 package org.jedit.syntax;
2
3 /*
4  * KeywordMap.java - Fast keyword->id map
5  * Copyright (C) 1998, 1999 Slava Pestov
6  * Copyright (C) 1999 Mike Dillon
7  *
8  * You may use and modify this package for any purpose. Redistribution is
9  * permitted, in both source and binary form, provided that this notice
10  * remains intact in all source distributions of this package.
11  */

12
13 import javax.swing.text.Segment JavaDoc;
14
15 /**
16  * A <code>KeywordMap</code> is similar to a hashtable in that it maps keys
17  * to values. However, the `keys' are Swing segments. This allows lookups of
18  * text substrings without the overhead of creating a new string object.
19  * <p>
20  * This class is used by <code>CTokenMarker</code> to map keywords to ids.
21  *
22  * @author Slava Pestov, Mike Dillon
23  * @version $Id: KeywordMap.java,v 1.1 2003/12/14 16:29:49 daggerrz Exp $
24  */

25 public class KeywordMap
26 {
27    /**
28     * Creates a new <code>KeywordMap</code>.
29     * @param ignoreCase True if keys are case insensitive
30     */

31    public KeywordMap(boolean ignoreCase)
32    {
33       this(ignoreCase, 52);
34       this.ignoreCase = ignoreCase;
35    }
36
37    /**
38     * Creates a new <code>KeywordMap</code>.
39     * @param ignoreCase True if the keys are case insensitive
40     * @param mapLength The number of `buckets' to create.
41     * A value of 52 will give good performance for most maps.
42     */

43    public KeywordMap(boolean ignoreCase, int mapLength)
44    {
45       this.mapLength = mapLength;
46       this.ignoreCase = ignoreCase;
47       map = new Keyword[mapLength];
48    }
49
50    /**
51     * Looks up a key.
52     * @param text The text segment
53     * @param offset The offset of the substring within the text segment
54     * @param length The length of the substring
55     */

56    public byte lookup(Segment JavaDoc text, int offset, int length)
57    {
58       if(length == 0)
59          return Token.NULL;
60       Keyword k = map[getSegmentMapKey(text, offset, length)];
61       while(k != null)
62       {
63          if(length != k.keyword.length)
64          {
65             k = k.next;
66             continue;
67          }
68          if(SyntaxUtilities.regionMatches(ignoreCase,text,offset,
69             k.keyword))
70             return k.id;
71          k = k.next;
72       }
73       return Token.NULL;
74    }
75
76    /**
77     * Adds a key-value mapping.
78     * @param keyword The key
79     * @Param id The value
80     */

81    public void add(String JavaDoc keyword, byte id)
82    {
83       int key = getStringMapKey(keyword);
84       map[key] = new Keyword(keyword.toCharArray(),id,map[key]);
85    }
86
87    /**
88     * Returns true if the keyword map is set to be case insensitive,
89     * false otherwise.
90     */

91    public boolean getIgnoreCase()
92    {
93       return ignoreCase;
94    }
95
96    /**
97     * Sets if the keyword map should be case insensitive.
98     * @param ignoreCase True if the keyword map should be case
99     * insensitive, false otherwise
100     */

101    public void setIgnoreCase(boolean ignoreCase)
102    {
103       this.ignoreCase = ignoreCase;
104    }
105
106    // protected members
107
protected int mapLength;
108
109    protected int getStringMapKey(String JavaDoc s)
110    {
111       return (Character.toUpperCase(s.charAt(0)) +
112             Character.toUpperCase(s.charAt(s.length()-1)))
113             % mapLength;
114    }
115
116    protected int getSegmentMapKey(Segment JavaDoc s, int off, int len)
117    {
118       return (Character.toUpperCase(s.array[off]) +
119             Character.toUpperCase(s.array[off + len - 1]))
120             % mapLength;
121    }
122
123    // private members
124
class Keyword
125    {
126       public Keyword(char[] keyword, byte id, Keyword next)
127       {
128          this.keyword = keyword;
129          this.id = id;
130          this.next = next;
131       }
132
133       public char[] keyword;
134       public byte id;
135       public Keyword next;
136    }
137
138    private Keyword[] map;
139    private boolean ignoreCase;
140 }
141
Popular Tags