Source Code Cross Referenced for TextTrieMap.java in  » Internationalization-Localization » icu4j » com » ibm » icu » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Internationalization Localization » icu4j » com.ibm.icu.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * *****************************************************************************
003:         * Copyright (C) 2006, International Business Machines Corporation and others.
004:         * All Rights Reserved.
005:         * *****************************************************************************
006:         */
007:        package com.ibm.icu.impl;
008:
009:        import java.util.ArrayList;
010:        import java.util.List;
011:
012:        import com.ibm.icu.lang.UCharacter;
013:        import com.ibm.icu.text.UTF16;
014:
015:        /**
016:         * TextTrieMap is a trie implementation for supporting
017:         * fast prefix match for the key.
018:         */
019:        public class TextTrieMap {
020:            /**
021:             * Costructs a TextTrieMap object.
022:             * 
023:             * @param ignoreCase true to use case insensitive match
024:             */
025:            public TextTrieMap(boolean ignoreCase) {
026:                this .ignoreCase = ignoreCase;
027:            }
028:
029:            /**
030:             * Adds the text key and its associated object in this object.
031:             * 
032:             * @param text The text.
033:             * @param o The object associated with the text.
034:             * @return The previous value associated with specified text,
035:             * or null if there was no mapping for the text.
036:             */
037:            public synchronized Object put(String text, Object o) {
038:                CharacterNode node = root;
039:                for (int i = 0; i < text.length(); i++) {
040:                    int ch = UTF16.charAt(text, i);
041:                    node = node.addChildNode(ch);
042:                    if (UTF16.getCharCount(ch) == 2) {
043:                        i++;
044:                    }
045:                }
046:                Object prevObj = node.getObject();
047:                node.setObject(o);
048:                return prevObj;
049:            }
050:
051:            /**
052:             * Gets the object associated with the longest prefix
053:             * matching string key.
054:             * 
055:             * @param text The text to be matched with prefixes.
056:             * @return The object associated with the longet prefix matching
057:             * matching key, or null if no matching entry is found.
058:             */
059:            public Object get(String text) {
060:                return get(root, text, 0);
061:            }
062:
063:            /**
064:             * Gets the object associated with the longest prefix
065:             * matching string key starting at the specified position.
066:             * 
067:             * @param text The text to be matched with prefixes.
068:             * @param start The start index of of the text
069:             * @return The object associated with the longet prefix matching
070:             * matching key, or null if no matching entry is found.
071:             */
072:            public Object get(String text, int start) {
073:                return get(root, text, start);
074:            }
075:
076:            /**
077:             * Gets the object associated with the longet prefix
078:             * matching string key under the specified node.
079:             * 
080:             * @param node The character node in this trie.
081:             * @param text The text to be matched with prefixes.
082:             * @param index The current index within the text.
083:             * @return The object associated with the longest prefix
084:             * match under the node.
085:             */
086:            private synchronized Object get(CharacterNode node, String text,
087:                    int index) {
088:                Object obj = node.getObject();
089:                if (index < text.length()) {
090:                    List childNodes = node.getChildNodes();
091:                    if (childNodes == null) {
092:                        return obj;
093:                    }
094:                    int ch = UTF16.charAt(text, index);
095:                    int chLen = UTF16.getCharCount(ch);
096:                    for (int i = 0; i < childNodes.size(); i++) {
097:                        CharacterNode child = (CharacterNode) childNodes.get(i);
098:                        if (compare(ch, child.getCharacter())) {
099:                            Object tmp = get(child, text, index + chLen);
100:                            if (tmp != null) {
101:                                obj = tmp;
102:                            }
103:                            break;
104:                        }
105:                    }
106:                }
107:                return obj;
108:            }
109:
110:            /**
111:             * A private method used for comparing two characters.
112:             * 
113:             * @param ch1 The first character.
114:             * @param ch2 The second character.
115:             * @return true if the first character matches the second.
116:             */
117:            private boolean compare(int ch1, int ch2) {
118:                if (ch1 == ch2) {
119:                    return true;
120:                } else if (ignoreCase) {
121:                    if (UCharacter.toLowerCase(ch1) == UCharacter
122:                            .toLowerCase(ch2)) {
123:                        return true;
124:                    } else if (UCharacter.toUpperCase(ch1) == UCharacter
125:                            .toUpperCase(ch2)) {
126:                        return true;
127:                    }
128:                }
129:                return false;
130:            }
131:
132:            // The root node of this trie
133:            private CharacterNode root = new CharacterNode(0);
134:
135:            // Character matching option
136:            boolean ignoreCase;
137:
138:            /**
139:             * Inner class representing a character node in the trie.
140:             */
141:            private class CharacterNode {
142:                int character;
143:                List children;
144:                Object obj;
145:
146:                /**
147:                 * Constructs a node for the character.
148:                 * 
149:                 * @param ch The character associated with this node.
150:                 */
151:                public CharacterNode(int ch) {
152:                    character = ch;
153:                }
154:
155:                /**
156:                 * Gets the character associated with this node.
157:                 * 
158:                 * @return The character
159:                 */
160:                public int getCharacter() {
161:                    return character;
162:                }
163:
164:                /**
165:                 * Sets the object to the node.  Only a leaf node has
166:                 * the reference of an object associated with a key.
167:                 * 
168:                 * @param obj The object set in the leaf node.
169:                 */
170:                public void setObject(Object obj) {
171:                    this .obj = obj;
172:                }
173:
174:                /**
175:                 * Gets the object associated the leaf node.
176:                 * 
177:                 * @return The object.
178:                 */
179:                public Object getObject() {
180:                    return obj;
181:                }
182:
183:                /**
184:                 * Adds a child node for the characer under this character
185:                 * node in the trie.  When the matching child node already
186:                 * exists, the reference of the existing child node is
187:                 * returned.
188:                 * 
189:                 * @param ch The character associated with a child node.
190:                 * @return The child node.
191:                 */
192:                public CharacterNode addChildNode(int ch) {
193:                    if (children == null) {
194:                        children = new ArrayList();
195:                        CharacterNode newNode = new CharacterNode(ch);
196:                        children.add(newNode);
197:                        return newNode;
198:                    }
199:                    CharacterNode node = null;
200:                    for (int i = 0; i < children.size(); i++) {
201:                        CharacterNode cur = (CharacterNode) children.get(i);
202:                        if (compare(ch, cur.getCharacter())) {
203:                            node = cur;
204:                            break;
205:                        }
206:                    }
207:                    if (node == null) {
208:                        node = new CharacterNode(ch);
209:                        children.add(node);
210:                    }
211:                    return node;
212:                }
213:
214:                /**
215:                 * Gets the list of child nodes under this node.
216:                 * 
217:                 * @return The list of child nodes.
218:                 */
219:                public List getChildNodes() {
220:                    return children;
221:                }
222:            }
223:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.