Source Code Cross Referenced for StringTokenizer.java in  » Apache-Harmony-Java-SE » java-package » java » util » 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 » Apache Harmony Java SE » java package » java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package java.util;
019:
020:        /**
021:         * String tokenizer is used to break a string apart into tokens.
022:         * 
023:         * If returnDelimiters is false, successive calls to nextToken() return maximal
024:         * blocks of characters that do not contain a delimiter.
025:         * 
026:         * If returnDelimiters is true, delimiters are considered to be tokens, and
027:         * successive calls to nextToken() return either a one character delimiter, or a
028:         * maximal block of text between delimiters.
029:         */
030:        public class StringTokenizer implements  Enumeration<Object> {
031:
032:            private String string;
033:
034:            private String delimiters;
035:
036:            private boolean returnDelimiters;
037:
038:            private int position;
039:
040:            /**
041:             * Constructs a new StringTokenizer for string using whitespace as the
042:             * delimiter, returnDelimiters is false.
043:             * 
044:             * @param string
045:             *            the string to be tokenized
046:             */
047:            public StringTokenizer(String string) {
048:                this (string, " \t\n\r\f", false); //$NON-NLS-1$
049:            }
050:
051:            /**
052:             * Constructs a new StringTokenizer for string using the specified
053:             * delimiters, returnDelimiters is false.
054:             * 
055:             * @param string
056:             *            the string to be tokenized
057:             * @param delimiters
058:             *            the delimiters to use
059:             */
060:            public StringTokenizer(String string, String delimiters) {
061:                this (string, delimiters, false);
062:            }
063:
064:            /**
065:             * Constructs a new StringTokenizer for string using the specified
066:             * delimiters and returning delimiters as tokens when specified.
067:             * 
068:             * @param string
069:             *            the string to be tokenized
070:             * @param delimiters
071:             *            the delimiters to use
072:             * @param returnDelimiters
073:             *            true to return each delimiter as a token
074:             */
075:            public StringTokenizer(String string, String delimiters,
076:                    boolean returnDelimiters) {
077:                if (string != null) {
078:                    this .string = string;
079:                    this .delimiters = delimiters;
080:                    this .returnDelimiters = returnDelimiters;
081:                    this .position = 0;
082:                } else
083:                    throw new NullPointerException();
084:            }
085:
086:            /**
087:             * Returns the number of unprocessed tokens remaining in the string.
088:             * 
089:             * @return number of tokens that can be retreived before an exception will
090:             *         result
091:             */
092:            public int countTokens() {
093:                int count = 0;
094:                boolean inToken = false;
095:                for (int i = position, length = string.length(); i < length; i++) {
096:                    if (delimiters.indexOf(string.charAt(i), 0) >= 0) {
097:                        if (returnDelimiters)
098:                            count++;
099:                        if (inToken) {
100:                            count++;
101:                            inToken = false;
102:                        }
103:                    } else {
104:                        inToken = true;
105:                    }
106:                }
107:                if (inToken)
108:                    count++;
109:                return count;
110:            }
111:
112:            /**
113:             * Returns true if unprocessed tokens remain.
114:             * 
115:             * @return true if unprocessed tokens remain
116:             */
117:            public boolean hasMoreElements() {
118:                return hasMoreTokens();
119:            }
120:
121:            /**
122:             * Returns true if unprocessed tokens remain.
123:             * 
124:             * @return true if unprocessed tokens remain
125:             */
126:            public boolean hasMoreTokens() {
127:                int length = string.length();
128:                if (position < length) {
129:                    if (returnDelimiters)
130:                        return true; // there is at least one character and even if
131:                    // it is a delimiter it is a token
132:
133:                    // otherwise find a character which is not a delimiter
134:                    for (int i = position; i < length; i++)
135:                        if (delimiters.indexOf(string.charAt(i), 0) == -1)
136:                            return true;
137:                }
138:                return false;
139:            }
140:
141:            /**
142:             * Returns the next token in the string as an Object.
143:             * 
144:             * @return next token in the string as an Object
145:             * @exception NoSuchElementException
146:             *                if no tokens remain
147:             */
148:            public Object nextElement() {
149:                return nextToken();
150:            }
151:
152:            /**
153:             * Returns the next token in the string as a String.
154:             * 
155:             * @return next token in the string as a String
156:             * @exception NoSuchElementException
157:             *                if no tokens remain
158:             */
159:            public String nextToken() {
160:                int i = position;
161:                int length = string.length();
162:
163:                if (i < length) {
164:                    if (returnDelimiters) {
165:                        if (delimiters.indexOf(string.charAt(position), 0) >= 0)
166:                            return String.valueOf(string.charAt(position++));
167:                        for (position++; position < length; position++)
168:                            if (delimiters.indexOf(string.charAt(position), 0) >= 0)
169:                                return string.substring(i, position);
170:                        return string.substring(i);
171:                    }
172:
173:                    while (i < length
174:                            && delimiters.indexOf(string.charAt(i), 0) >= 0)
175:                        i++;
176:                    position = i;
177:                    if (i < length) {
178:                        for (position++; position < length; position++)
179:                            if (delimiters.indexOf(string.charAt(position), 0) >= 0)
180:                                return string.substring(i, position);
181:                        return string.substring(i);
182:                    }
183:                }
184:                throw new NoSuchElementException();
185:            }
186:
187:            /**
188:             * Returns the next token in the string as a String. The delimiters used are
189:             * changed to the specified delimiters.
190:             * 
191:             * @param delims
192:             *            the new delimiters to use
193:             * @return next token in the string as a String
194:             * @exception NoSuchElementException
195:             *                if no tokens remain
196:             */
197:            public String nextToken(String delims) {
198:                this.delimiters = delims;
199:                return nextToken();
200:            }
201:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.