Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / console / jedit / KeywordMap.java @ 40561

History | View | Annotate | Download (4.17 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils.console.jedit;
25
/*
26
 * KeywordMap.java - Fast keyword->id map
27
 * Copyright (C) 1998, 1999 Slava Pestov
28
 * Copyright (C) 1999 Mike Dillon
29
 *
30
 * You may use and modify this package for any purpose. Redistribution is
31
 * permitted, in both source and binary form, provided that this notice
32
 * remains intact in all source distributions of this package.
33
 */
34

    
35
import javax.swing.text.Segment;
36

    
37
/**
38
 * A <code>KeywordMap</code> is similar to a hashtable in that it maps keys
39
 * to values. However, the `keys' are Swing segments. This allows lookups of
40
 * text substrings without the overhead of creating a new string object.
41
 * <p>
42
 * This class is used by <code>CTokenMarker</code> to map keywords to ids.
43
 *
44
 * @author Slava Pestov, Mike Dillon
45
 * @version $Id$
46
 */
47
public class KeywordMap
48
{
49
        /**
50
         * Creates a new <code>KeywordMap</code>.
51
         * @param ignoreCase True if keys are case insensitive
52
         */
53
        public KeywordMap(boolean ignoreCase)
54
        {
55
                this(ignoreCase, 52);
56
                this.ignoreCase = ignoreCase;
57
        }
58

    
59
        /**
60
         * Creates a new <code>KeywordMap</code>.
61
         * @param ignoreCase True if the keys are case insensitive
62
         * @param mapLength The number of `buckets' to create.
63
         * A value of 52 will give good performance for most maps.
64
         */
65
        public KeywordMap(boolean ignoreCase, int mapLength)
66
        {
67
                this.mapLength = mapLength;
68
                this.ignoreCase = ignoreCase;
69
                map = new Keyword[mapLength];
70
        }
71

    
72
        /**
73
         * Looks up a key.
74
         * @param text The text segment
75
         * @param offset The offset of the substring within the text segment
76
         * @param length The length of the substring
77
         */
78
        public byte lookup(Segment text, int offset, int length)
79
        {
80
                if(length == 0)
81
                        return Token.NULL;
82
                Keyword k = map[getSegmentMapKey(text, offset, length)];
83
                while(k != null)
84
                {
85
                        if(length != k.keyword.length)
86
                        {
87
                                k = k.next;
88
                                continue;
89
                        }
90
                        if(SyntaxUtilities.regionMatches(ignoreCase,text,offset,
91
                                k.keyword))
92
                                return k.id;
93
                        k = k.next;
94
                }
95
                return Token.NULL;
96
        }
97

    
98
        /**
99
         * Adds a key-value mapping.
100
         * @param keyword The key
101
         * @Param id The value
102
         */
103
        public void add(String keyword, byte id)
104
        {
105
                int key = getStringMapKey(keyword);
106
                map[key] = new Keyword(keyword.toCharArray(),id,map[key]);
107
        }
108

    
109
        /**
110
         * Returns true if the keyword map is set to be case insensitive,
111
         * false otherwise.
112
         */
113
        public boolean getIgnoreCase()
114
        {
115
                return ignoreCase;
116
        }
117

    
118
        /**
119
         * Sets if the keyword map should be case insensitive.
120
         * @param ignoreCase True if the keyword map should be case
121
         * insensitive, false otherwise
122
         */
123
        public void setIgnoreCase(boolean ignoreCase)
124
        {
125
                this.ignoreCase = ignoreCase;
126
        }
127

    
128
        // protected members
129
        protected int mapLength;
130

    
131
        protected int getStringMapKey(String s)
132
        {
133
                return (Character.toUpperCase(s.charAt(0)) +
134
                                Character.toUpperCase(s.charAt(s.length()-1)))
135
                                % mapLength;
136
        }
137

    
138
        protected int getSegmentMapKey(Segment s, int off, int len)
139
        {
140
                return (Character.toUpperCase(s.array[off]) +
141
                                Character.toUpperCase(s.array[off + len - 1]))
142
                                % mapLength;
143
        }
144

    
145
        // private members
146
        class Keyword
147
        {
148
                public Keyword(char[] keyword, byte id, Keyword next)
149
                {
150
                        this.keyword = keyword;
151
                        this.id = id;
152
                        this.next = next;
153
                }
154

    
155
                public char[] keyword;
156
                public byte id;
157
                public Keyword next;
158
        }
159

    
160
        private Keyword[] map;
161
        private boolean ignoreCase;
162
}