jdk/src/share/classes/java/util/StringTokenizer.java
author sherman
Wed, 04 Mar 2009 09:26:41 -0800
changeset 2178 5a730fff4d0d
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6812879: Excess code line in ArrayList method Summary: Removed the line of "oldData" which is no longer used. Reviewed-by: martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * The string tokenizer class allows an application to break a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * string into tokens. The tokenization method is much simpler than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the one used by the <code>StreamTokenizer</code> class. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <code>StringTokenizer</code> methods do not distinguish among
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * identifiers, numbers, and quoted strings, nor do they recognize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and skip comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The set of delimiters (the characters that separate tokens) may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * be specified either at creation time or on a per-token basis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * An instance of <code>StringTokenizer</code> behaves in one of two
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * ways, depending on whether it was created with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>returnDelims</code> flag having the value <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * or <code>false</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <li>If the flag is <code>false</code>, delimiter characters serve to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *     separate tokens. A token is a maximal sequence of consecutive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *     characters that are not delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li>If the flag is <code>true</code>, delimiter characters are themselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *     considered to be tokens. A token is thus either one delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *     character, or a maximal sequence of consecutive characters that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *     not delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </ul><p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * A <tt>StringTokenizer</tt> object internally maintains a current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * position within the string to be tokenized. Some operations advance this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * current position past the characters processed.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * A token is returned by taking a substring of the string that was used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * create the <tt>StringTokenizer</tt> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * The following is one example of the use of the tokenizer. The code:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     StringTokenizer st = new StringTokenizer("this is a test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *         System.out.println(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * prints the following output:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <tt>StringTokenizer</tt> is a legacy class that is retained for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * compatibility reasons although its use is discouraged in new code. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * recommended that anyone seeking this functionality use the <tt>split</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * method of <tt>String</tt> or the java.util.regex package instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * The following example illustrates how the <tt>String.split</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * method can be used to break up a string into its basic tokens:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     String[] result = "this is a test".split("\\s");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     for (int x=0; x&lt;result.length; x++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *         System.out.println(result[x]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * prints the following output:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *     this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *     is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * @author  unascribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @see     java.io.StreamTokenizer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
class StringTokenizer implements Enumeration<Object> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int currentPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private int newPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private int maxPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private String delimiters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private boolean retDelims;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private boolean delimsChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * maxDelimCodePoint stores the value of the delimiter character with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * highest value. It is used to optimize the detection of delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * It is unlikely to provide any optimization benefit in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * hasSurrogates case because most string characters will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * smaller than the limit, but we keep it so that the two code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * paths remain similar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private int maxDelimCodePoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * If delimiters include any surrogates (including surrogate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * pairs), hasSurrogates is true and the tokenizer uses the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * different code path. This is because String.indexOf(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * doesn't handle unpaired surrogates as a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private boolean hasSurrogates = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * When hasSurrogates is true, delimiters are converted to code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * points and isDelimiter(int) is used to determine if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * codepoint is a delimiter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private int[] delimiterCodePoints;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Set maxDelimCodePoint to the highest char in the delimiter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private void setMaxDelimCodePoint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (delimiters == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            maxDelimCodePoint = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int m = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int i = 0; i < delimiters.length(); i += Character.charCount(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            c = delimiters.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (c >= Character.MIN_HIGH_SURROGATE && c <= Character.MAX_LOW_SURROGATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                c = delimiters.codePointAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                hasSurrogates = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (m < c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                m = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        maxDelimCodePoint = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (hasSurrogates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            delimiterCodePoints = new int[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            for (int i = 0, j = 0; i < count; i++, j += Character.charCount(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                c = delimiters.codePointAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                delimiterCodePoints[i] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Constructs a string tokenizer for the specified string. All
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * characters in the <code>delim</code> argument are the delimiters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * for separating tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * If the <code>returnDelims</code> flag is <code>true</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * the delimiter characters are also returned as tokens. Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * delimiter is returned as a string of length one. If the flag is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <code>false</code>, the delimiter characters are skipped and only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * serve as separators between tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Note that if <tt>delim</tt> is <tt>null</tt>, this constructor does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * not throw an exception. However, trying to invoke other methods on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * resulting <tt>StringTokenizer</tt> may result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <tt>NullPointerException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param   str            a string to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @param   delim          the delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @param   returnDelims   flag indicating whether to return the delimiters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *                         as tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * @exception NullPointerException if str is <CODE>null</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public StringTokenizer(String str, String delim, boolean returnDelims) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        currentPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        newPosition = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        delimsChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this.str = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        maxPosition = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        delimiters = delim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        retDelims = returnDelims;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        setMaxDelimCodePoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Constructs a string tokenizer for the specified string. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * characters in the <code>delim</code> argument are the delimiters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * for separating tokens. Delimiter characters themselves will not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * be treated as tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Note that if <tt>delim</tt> is <tt>null</tt>, this constructor does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * not throw an exception. However, trying to invoke other methods on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * resulting <tt>StringTokenizer</tt> may result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <tt>NullPointerException</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param   str     a string to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @param   delim   the delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception NullPointerException if str is <CODE>null</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public StringTokenizer(String str, String delim) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this(str, delim, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Constructs a string tokenizer for the specified string. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * tokenizer uses the default delimiter set, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <code>"&nbsp;&#92;t&#92;n&#92;r&#92;f"</code>: the space character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * the tab character, the newline character, the carriage-return character,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * and the form-feed character. Delimiter characters themselves will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * not be treated as tokens.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param   str   a string to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @exception NullPointerException if str is <CODE>null</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public StringTokenizer(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        this(str, " \t\n\r\f", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Skips delimiters starting from the specified position. If retDelims
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * is false, returns the index of the first non-delimiter character at or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * after startPos. If retDelims is true, startPos is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private int skipDelimiters(int startPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (delimiters == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int position = startPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        while (!retDelims && position < maxPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (!hasSurrogates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                char c = str.charAt(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if ((c > maxDelimCodePoint) || (delimiters.indexOf(c) < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                position++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                int c = str.codePointAt(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if ((c > maxDelimCodePoint) || !isDelimiter(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                position += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Skips ahead from startPos and returns the index of the next delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * character encountered, or maxPosition if no such delimiter is found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private int scanToken(int startPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int position = startPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        while (position < maxPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (!hasSurrogates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                char c = str.charAt(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                position++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                int c = str.codePointAt(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                if ((c <= maxDelimCodePoint) && isDelimiter(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                position += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (retDelims && (startPos == position)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (!hasSurrogates) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                char c = str.charAt(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                if ((c <= maxDelimCodePoint) && (delimiters.indexOf(c) >= 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    position++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                int c = str.codePointAt(position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if ((c <= maxDelimCodePoint) && isDelimiter(c))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    position += Character.charCount(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private boolean isDelimiter(int codePoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        for (int i = 0; i < delimiterCodePoints.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (delimiterCodePoints[i] == codePoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * Tests if there are more tokens available from this tokenizer's string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * If this method returns <tt>true</tt>, then a subsequent call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <tt>nextToken</tt> with no argument will successfully return a token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return  <code>true</code> if and only if there is at least one token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *          in the string after the current position; <code>false</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *          otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public boolean hasMoreTokens() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         * Temporarily store this position and use it in the following
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * nextToken() method only if the delimiters haven't been changed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * that nextToken() invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        newPosition = skipDelimiters(currentPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        return (newPosition < maxPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Returns the next token from this string tokenizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return     the next token from this string tokenizer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @exception  NoSuchElementException  if there are no more tokens in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *               tokenizer's string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public String nextToken() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * If next position already computed in hasMoreElements() and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * delimiters have changed between the computation and this invocation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         * then use the computed value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        currentPosition = (newPosition >= 0 && !delimsChanged) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            newPosition : skipDelimiters(currentPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /* Reset these anyway */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        delimsChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        newPosition = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (currentPosition >= maxPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int start = currentPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        currentPosition = scanToken(currentPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return str.substring(start, currentPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Returns the next token in this string tokenizer's string. First,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * the set of characters considered to be delimiters by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <tt>StringTokenizer</tt> object is changed to be the characters in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * the string <tt>delim</tt>. Then the next token in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * after the current position is returned. The current position is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * advanced beyond the recognized token.  The new delimiter set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * remains the default after this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @param      delim   the new delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @return     the next token, after switching to the new delimiter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @exception  NoSuchElementException  if there are no more tokens in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *               tokenizer's string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @exception NullPointerException if delim is <CODE>null</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public String nextToken(String delim) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        delimiters = delim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        /* delimiter string specified, so set the appropriate flag. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        delimsChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        setMaxDelimCodePoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Returns the same value as the <code>hasMoreTokens</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * method. It exists so that this class can implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * <code>Enumeration</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @return  <code>true</code> if there are more tokens;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *          <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @see     java.util.Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @see     java.util.StringTokenizer#hasMoreTokens()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        return hasMoreTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Returns the same value as the <code>nextToken</code> method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * except that its declared return value is <code>Object</code> rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <code>String</code>. It exists so that this class can implement the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * <code>Enumeration</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return     the next token in the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @exception  NoSuchElementException  if there are no more tokens in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *               tokenizer's string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @see        java.util.Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @see        java.util.StringTokenizer#nextToken()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Calculates the number of times that this tokenizer's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <code>nextToken</code> method can be called before it generates an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * exception. The current position is not advanced.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @return  the number of tokens remaining in the string using the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *          delimiter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @see     java.util.StringTokenizer#nextToken()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    public int countTokens() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int currpos = currentPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        while (currpos < maxPosition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            currpos = skipDelimiters(currpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (currpos >= maxPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            currpos = scanToken(currpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
}