jdk/src/share/classes/java/util/Scanner.java
author peytoia
Thu, 11 Apr 2013 12:22:23 +0900
changeset 16850 f6f6c2182678
parent 16010 2727163b5df5
child 18156 edb590d448c5
permissions -rw-r--r--
8009638: Wrong comment for PL in LocaleISOData, 1989 forward Poland is Republic of Poland Reviewed-by: okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14014
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    23
 * questions.
2
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
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
    28
import java.nio.file.Path;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
    29
import java.nio.file.Files;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.regex.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.math.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Locale;
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
    38
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.misc.LRUCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * A simple text scanner which can parse primitive types and strings using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * regular expressions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p>A <code>Scanner</code> breaks its input into tokens using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * delimiter pattern, which by default matches whitespace. The resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * tokens may then be converted into values of different types using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * various <tt>next</tt> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p>For example, this code allows a user to read a number from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <tt>System.in</tt>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *     Scanner sc = new Scanner(System.in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *     int i = sc.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>As another example, this code allows <code>long</code> types to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * assigned from entries in a file <code>myNumbers</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *      Scanner sc = new Scanner(new File("myNumbers"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      while (sc.hasNextLong()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *          long aLong = sc.nextLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      }</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p>The scanner can also use delimiters other than whitespace. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * example reads several items in from a string:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *     String input = "1 fish 2 fish red fish blue fish";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     System.out.println(s.nextInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     System.out.println(s.nextInt());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     System.out.println(s.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     System.out.println(s.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *     s.close(); </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * prints the following output:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *     2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     red
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *     blue </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p>The same output can be generated with this code, which uses a regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * expression to parse all four tokens at once:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *<blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *     String input = "1 fish 2 fish red fish blue fish";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *     Scanner s = new Scanner(input);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *     MatchResult result = s.match();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *     for (int i=1; i<=result.groupCount(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *         System.out.println(result.group(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *     s.close(); </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>The <a name="default-delimiter">default whitespace delimiter</a> used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * by a scanner is as recognized by {@link java.lang.Character}.{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * java.lang.Character#isWhitespace(char) isWhitespace}. The {@link #reset}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * method will reset the value of the scanner's delimiter to the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * whitespace delimiter regardless of whether it was previously changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>A scanning operation may block waiting for input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <p>The {@link #next} and {@link #hasNext} methods and their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * primitive-type companion methods (such as {@link #nextInt} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * {@link #hasNextInt}) first skip any input that matches the delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * pattern, and then attempt to return the next token. Both <tt>hasNext</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * and <tt>next</tt> methods may block waiting for further input.  Whether a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <tt>hasNext</tt> method blocks has no connection to whether or not its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * associated <tt>next</tt> method will block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p> The {@link #findInLine}, {@link #findWithinHorizon}, and {@link #skip}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * methods operate independently of the delimiter pattern. These methods will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * attempt to match the specified pattern with no regard to delimiters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * input and thus can be used in special circumstances where delimiters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * not relevant. These methods may block waiting for more input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <p>When a scanner throws an {@link InputMismatchException}, the scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * will not pass the token that caused the exception, so that it may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * retrieved or skipped via some other method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p>Depending upon the type of delimiting pattern, empty tokens may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * returned. For example, the pattern <tt>"\\s+"</tt> will return no empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * tokens since it matches multiple instances of the delimiter. The delimiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * pattern <tt>"\\s"</tt> could return empty tokens since it only passes one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * space at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <p> A scanner can read text from any object which implements the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * java.lang.Readable} interface.  If an invocation of the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * readable's {@link java.lang.Readable#read} method throws an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * java.io.IOException} then the scanner assumes that the end of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * has been reached.  The most recent <tt>IOException</tt> thrown by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * underlying readable can be retrieved via the {@link #ioException} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <p>When a <code>Scanner</code> is closed, it will close its input source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * if the source implements the {@link java.io.Closeable} interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <p>A <code>Scanner</code> is not safe for multithreaded use without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * external synchronization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <p>Unless otherwise mentioned, passing a <code>null</code> parameter into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * any method of a <code>Scanner</code> will cause a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * <code>NullPointerException</code> to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <p>A scanner will default to interpreting numbers as decimal unless a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * different radix has been set by using the {@link #useRadix} method. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * {@link #reset} method will reset the value of the scanner's radix to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * <code>10</code> regardless of whether it was previously changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <a name="localized-numbers">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <h4> Localized numbers </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <p> An instance of this class is capable of scanning numbers in the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * formats as well as in the formats of the scanner's locale. A scanner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <a name="initial-locale">initial locale </a>is the value returned by the {@link
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 14342
diff changeset
   154
 * java.util.Locale#getDefault(Locale.Category)
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 14342
diff changeset
   155
 * Locale.getDefault(Locale.Category.FORMAT)} method; it may be changed via the {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * #useLocale} method. The {@link #reset} method will reset the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * scanner's locale to the initial locale regardless of whether it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * previously changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * <p>The localized formats are defined in terms of the following parameters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * which for a particular locale are taken from that locale's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * java.text.DecimalFormat DecimalFormat} object, <tt>df</tt>, and its and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * {@link java.text.DecimalFormatSymbols DecimalFormatSymbols} object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <tt>dfs</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * <blockquote><table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <tr><td valign="top"><i>LocalGroupSeparator&nbsp;&nbsp;</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *     <td valign="top">The character used to separate thousands groups,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *                      <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *                      java.text.DecimalFormatSymbols#getGroupingSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *                      getGroupingSeparator()}</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * <tr><td valign="top"><i>LocalDecimalSeparator&nbsp;&nbsp;</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *     <td valign="top">The character used for the decimal point,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *                      <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *                      java.text.DecimalFormatSymbols#getDecimalSeparator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *                      getDecimalSeparator()}</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 * <tr><td valign="top"><i>LocalPositivePrefix&nbsp;&nbsp;</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *     <td valign="top">The string that appears before a positive number (may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *                      be empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *                      java.text.DecimalFormat#getPositivePrefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *                      getPositivePrefix()}</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * <tr><td valign="top"><i>LocalPositiveSuffix&nbsp;&nbsp;</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *     <td valign="top">The string that appears after a positive number (may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *                      empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *                      java.text.DecimalFormat#getPositiveSuffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *                      getPositiveSuffix()}</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * <tr><td valign="top"><i>LocalNegativePrefix&nbsp;&nbsp;</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *     <td valign="top">The string that appears before a negative number (may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *                      be empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *                      java.text.DecimalFormat#getNegativePrefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *                      getNegativePrefix()}</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * <tr><td valign="top"><i>LocalNegativeSuffix&nbsp;&nbsp;</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *     <td valign="top">The string that appears after a negative number (may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 *                      empty), <i>i.e.,</i>&nbsp;<tt>df.</tt>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *                      java.text.DecimalFormat#getNegativeSuffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *                      getNegativeSuffix()}</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * <tr><td valign="top"><i>LocalNaN&nbsp;&nbsp;</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *     <td valign="top">The string that represents not-a-number for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *                      floating-point values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *                      <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 *                      java.text.DecimalFormatSymbols#getNaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 *                      getNaN()}</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * <tr><td valign="top"><i>LocalInfinity&nbsp;&nbsp;</i></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 *     <td valign="top">The string that represents infinity for floating-point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 *                      values, <i>i.e.,</i>&nbsp;<tt>dfs.</tt>{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 *                      java.text.DecimalFormatSymbols#getInfinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *                      getInfinity()}</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * </table></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * <a name="number-syntax">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * <h4> Number syntax </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * <p> The strings that can be parsed as numbers by an instance of this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * are specified in terms of the following regular-expression grammar, where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * Rmax is the highest digit in the radix being used (for example, Rmax is 9
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * in base 10).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * <table cellspacing=0 cellpadding=0 align=center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *   <tr><td valign=top align=right><i>NonASCIIDigit</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 *       <td valign=top>= A non-ASCII character c for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *            {@link java.lang.Character#isDigit Character.isDigit}<tt>(c)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 *                        returns&nbsp;true</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 *   <tr><td align=right><i>Non0Digit</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 *   <td><tt>= [1-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *   <tr><td align=right><i>Digit</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *   <td><tt>= [0-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *   <tr><td valign=top align=right><i>GroupedNumeral</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *       <td valign=top>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *         <table cellpadding=0 cellspacing=0>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 *           <tr><td><tt>= (&nbsp;</tt></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *               <td><i>Non0Digit</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *                   </tt><i>Digit</i><tt>?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *                   </tt><i>Digit</i><tt>?</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *           <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *               <td><tt>(&nbsp;</tt><i>LocalGroupSeparator</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *                         </tt><i>Digit</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *                         </tt><i>Digit</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *                         </tt><i>Digit</i><tt> )+ )</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *         </table></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *   <tr><td align=right><i>Numeral</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *       <td><tt>= ( ( </tt><i>Digit</i><tt>+ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *               | </tt><i>GroupedNumeral</i><tt> )</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *   <tr><td valign=top align=right>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 *         <a name="Integer-regex"><i>Integer</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *       <td valign=top><tt>= ( [-+]? ( </tt><i>Numeral</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 *                               ) )</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 *   <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 *       <td><tt>| </tt><i>LocalPositivePrefix</i><tt> </tt><i>Numeral</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 *                      </tt><i>LocalPositiveSuffix</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *   <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *       <td><tt>| </tt><i>LocalNegativePrefix</i><tt> </tt><i>Numeral</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 *                 </tt><i>LocalNegativeSuffix</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 *   <tr><td align=right><i>DecimalNumeral</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 *       <td><tt>= </tt><i>Numeral</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 *   <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 *       <td><tt>| </tt><i>Numeral</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 *                 </tt><i>LocalDecimalSeparator</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *                 </tt><i>Digit</i><tt>*</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 *   <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 *       <td><tt>| </tt><i>LocalDecimalSeparator</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 *                 </tt><i>Digit</i><tt>+</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 *   <tr><td align=right><i>Exponent</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 *       <td><tt>= ( [eE] [+-]? </tt><i>Digit</i><tt>+ )</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *   <tr><td align=right>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 *         <a name="Decimal-regex"><i>Decimal</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *       <td><tt>= ( [-+]? </tt><i>DecimalNumeral</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 *                         </tt><i>Exponent</i><tt>? )</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 *   <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 *       <td><tt>| </tt><i>LocalPositivePrefix</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *                 </tt><i>DecimalNumeral</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 *                 </tt><i>LocalPositiveSuffix</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 *                 </tt><i>Exponent</i><tt>?</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 *   <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 *       <td><tt>| </tt><i>LocalNegativePrefix</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 *                 </tt><i>DecimalNumeral</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 *                 </tt><i>LocalNegativeSuffix</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 *                 </tt><i>Exponent</i><tt>?</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 *   <tr><td align=right><i>HexFloat</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 *       <td><tt>= [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 *                 ([pP][-+]?[0-9]+)?</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 *   <tr><td align=right><i>NonNumber</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 *       <td valign=top><tt>= NaN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 *                          | </tt><i>LocalNan</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 *                          | Infinity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 *                          | </tt><i>LocalInfinity</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
 *   <tr><td align=right><i>SignedNonNumber</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
 *       <td><tt>= ( [-+]? </tt><i>NonNumber</i><tt> )</tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 *   <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 *       <td><tt>| </tt><i>LocalPositivePrefix</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 *                 </tt><i>NonNumber</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 *                 </tt><i>LocalPositiveSuffix</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 *   <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 *       <td><tt>| </tt><i>LocalNegativePrefix</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 *                 </tt><i>NonNumber</i><tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 *                 </tt><i>LocalNegativeSuffix</i></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 *   <tr><td>&nbsp;</td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 *   <tr><td valign=top align=right>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 *         <a name="Float-regex"><i>Float</i>&nbsp;&nbsp;::</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
 *       <td valign=top><tt>= </tt><i>Decimal</i><tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 *       <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 *           <td><tt>| </tt><i>HexFloat</i><tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 *       <tr><td></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 *           <td><tt>| </tt><i>SignedNonNumber</i><tt></td></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
 * </center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
 * <p> Whitespace is not significant in the above regular expressions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 * @since   1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
 */
5998
586c1b1dbb10 6963723: Project Coin: Retrofit more JDK classes for ARM
darcy
parents: 5506
diff changeset
   349
public final class Scanner implements Iterator<String>, Closeable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    // Internal buffer used to hold input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private CharBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    // Size of internal character buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private static final int BUFFER_SIZE = 1024; // change to 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    // The index into the buffer currently held by the Scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private int position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    // Internal matcher used for finding delimiters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private Matcher matcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    // Pattern used to delimit tokens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private Pattern delimPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    // Pattern found in last hasNext operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private Pattern hasNextPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    // Position after last hasNext operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private int hasNextPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    // Result after last hasNext operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private String hasNextResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    // The input source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    private Readable source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    // Boolean is true if source is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    private boolean sourceClosed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    // Boolean indicating more input is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    private boolean needInput = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    // Boolean indicating if a delim has been skipped this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    private boolean skipped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    // A store of a position that the scanner may fall back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private int savedScannerPosition = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    // A cache of the last primitive type scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    private Object typeCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    // Boolean indicating if a match result is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    private boolean matchValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    // Boolean indicating if this scanner has been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    // The current radix used by this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    private int radix = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    // The default radix for this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    private int defaultRadix = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    // The locale used by this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    private Locale locale = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    // A cache of the last few recently used Patterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private LRUCache<String,Pattern> patternCache =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    new LRUCache<String,Pattern>(7) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        protected Pattern create(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return Pattern.compile(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        protected boolean hasName(Pattern p, String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            return p.pattern().equals(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    // A holder of the last IOException encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private IOException lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    // A pattern for java whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    private static Pattern WHITESPACE_PATTERN = Pattern.compile(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                                                "\\p{javaWhitespace}+");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    // A pattern for any token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    private static Pattern FIND_ANY_PATTERN = Pattern.compile("(?s).*");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    // A pattern for non-ASCII digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private static Pattern NON_ASCII_DIGIT = Pattern.compile(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        "[\\p{javaDigit}&&[^0-9]]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    // Fields and methods to support scanning primitive types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Locale dependent values used to scan numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    private String groupSeparator = "\\,";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    private String decimalSeparator = "\\.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    private String nanString = "NaN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private String infinityString = "Infinity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    private String positivePrefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    private String negativePrefix = "\\-";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    private String positiveSuffix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    private String negativeSuffix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Fields and an accessor method to match booleans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private static volatile Pattern boolPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private static final String BOOLEAN_PATTERN = "true|false";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    private static Pattern boolPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        Pattern bp = boolPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        if (bp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            boolPattern = bp = Pattern.compile(BOOLEAN_PATTERN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                          Pattern.CASE_INSENSITIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        return bp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Fields and methods to match bytes, shorts, ints, and longs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    private Pattern integerPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    private String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    private String non0Digit = "[\\p{javaDigit}&&[^0]]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private int SIMPLE_GROUP_INDEX = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    private String buildIntegerPatternString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        String radixDigits = digits.substring(0, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // \\p{javaDigit} is not guaranteed to be appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        // here but what can we do? The final authority will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // whatever parse method is invoked, so ultimately the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        // Scanner will do the right thing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        String digit = "((?i)["+radixDigits+"]|\\p{javaDigit})";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                groupSeparator+digit+digit+digit+")+)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        // digit++ is the possessive form which is necessary for reducing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // backtracking that would otherwise cause unacceptable performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        String numeral = "(("+ digit+"++)|"+groupedNumeral+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        String javaStyleInteger = "([-+]?(" + numeral + "))";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        String negativeInteger = negativePrefix + numeral + negativeSuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        String positiveInteger = positivePrefix + numeral + positiveSuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return "("+ javaStyleInteger + ")|(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            positiveInteger + ")|(" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            negativeInteger + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    private Pattern integerPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        if (integerPattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            integerPattern = patternCache.forName(buildIntegerPatternString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return integerPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * Fields and an accessor method to match line separators
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private static volatile Pattern separatorPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    private static volatile Pattern linePattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private static final String LINE_SEPARATOR_PATTERN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                                           "\r\n|[\n\r\u2028\u2029\u0085]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    private static final String LINE_PATTERN = ".*("+LINE_SEPARATOR_PATTERN+")|.+$";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    private static Pattern separatorPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        Pattern sp = separatorPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        if (sp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            separatorPattern = sp = Pattern.compile(LINE_SEPARATOR_PATTERN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        return sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private static Pattern linePattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        Pattern lp = linePattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        if (lp == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            linePattern = lp = Pattern.compile(LINE_PATTERN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        return lp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * Fields and methods to match floats and doubles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    private Pattern floatPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    private Pattern decimalPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    private void buildFloatAndDecimalPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        // \\p{javaDigit} may not be perfect, see above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        String digit = "([0-9]|(\\p{javaDigit}))";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        String exponent = "([eE][+-]?"+digit+"+)?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                groupSeparator+digit+digit+digit+")+)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        // Once again digit++ is used for performance, as above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        String numeral = "(("+digit+"++)|"+groupedNumeral+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        String decimalNumeral = "("+numeral+"|"+numeral +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            decimalSeparator + digit + "*+|"+ decimalSeparator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            digit + "++)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        String nonNumber = "(NaN|"+nanString+"|Infinity|"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                               infinityString+")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        String positiveFloat = "(" + positivePrefix + decimalNumeral +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                            positiveSuffix + exponent + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        String negativeFloat = "(" + negativePrefix + decimalNumeral +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                            negativeSuffix + exponent + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        String decimal = "(([-+]?" + decimalNumeral + exponent + ")|"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            positiveFloat + "|" + negativeFloat + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        String hexFloat =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            "[-+]?0[xX][0-9a-fA-F]*\\.[0-9a-fA-F]+([pP][-+]?[0-9]+)?";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        String positiveNonNumber = "(" + positivePrefix + nonNumber +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                            positiveSuffix + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        String negativeNonNumber = "(" + negativePrefix + nonNumber +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                            negativeSuffix + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        String signedNonNumber = "(([-+]?"+nonNumber+")|" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                 positiveNonNumber + "|" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                                 negativeNonNumber + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        floatPattern = Pattern.compile(decimal + "|" + hexFloat + "|" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                                       signedNonNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        decimalPattern = Pattern.compile(decimal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    private Pattern floatPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        if (floatPattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            buildFloatAndDecimalPattern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return floatPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    private Pattern decimalPattern() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (decimalPattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            buildFloatAndDecimalPattern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return decimalPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    // Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Constructs a <code>Scanner</code> that returns values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * from the specified source delimited by the specified pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 8166
diff changeset
   572
     * @param source A character source implementing the Readable interface
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param pattern A delimiting pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    private Scanner(Readable source, Pattern pattern) {
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   576
        assert source != null : "source should not be null";
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   577
        assert pattern != null : "pattern should not be null";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        this.source = source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        delimPattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        buf = CharBuffer.allocate(BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        buf.limit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        matcher = delimPattern.matcher(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        matcher.useTransparentBounds(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        matcher.useAnchoringBounds(false);
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5998
diff changeset
   585
        useLocale(Locale.getDefault(Locale.Category.FORMAT));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * Constructs a new <code>Scanner</code> that produces values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * from the specified source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * @param  source A character source implementing the {@link Readable}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *         interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public Scanner(Readable source) {
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
   596
        this(Objects.requireNonNull(source, "source"), WHITESPACE_PATTERN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * Constructs a new <code>Scanner</code> that produces values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * from the specified input stream. Bytes from the stream are converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * into characters using the underlying platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param  source An input stream to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    public Scanner(InputStream source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        this(new InputStreamReader(source), WHITESPACE_PATTERN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * Constructs a new <code>Scanner</code> that produces values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * from the specified input stream. Bytes from the stream are converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * into characters using the specified charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @param  source An input stream to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * @param charsetName The encoding type used to convert bytes from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *        stream into characters to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @throws IllegalArgumentException if the specified character set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *         does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    public Scanner(InputStream source, String charsetName) {
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
   623
        this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   624
             WHITESPACE_PATTERN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   627
    /**
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   628
     * Returns a charset object for the given charset name.
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   629
     * @throws NullPointerException          is csn is null
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   630
     * @throws IllegalArgumentException      if the charset is not supported
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   631
     */
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   632
    private static Charset toCharset(String csn) {
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
   633
        Objects.requireNonNull(csn, "charsetName");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        try {
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   635
            return Charset.forName(csn);
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   636
        } catch (IllegalCharsetNameException|UnsupportedCharsetException e) {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   637
            // IllegalArgumentException should be thrown
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   638
            throw new IllegalArgumentException(e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        }
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   640
    }
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   641
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   642
    private static Readable makeReadable(InputStream source, Charset charset) {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   643
        return new InputStreamReader(source, charset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Constructs a new <code>Scanner</code> that produces values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * from the specified file. Bytes from the file are converted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * characters using the underlying platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param  source A file to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @throws FileNotFoundException if source is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     */
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   655
    public Scanner(File source) throws FileNotFoundException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * Constructs a new <code>Scanner</code> that produces values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * from the specified file. Bytes from the file are converted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * characters using the specified charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @param  source A file to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @param charsetName The encoding type used to convert bytes from the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *        into characters to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @throws FileNotFoundException if source is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @throws IllegalArgumentException if the specified encoding is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *         not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public Scanner(File source, String charsetName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        throws FileNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    {
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
   674
        this(Objects.requireNonNull(source), toDecoder(charsetName));
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   675
    }
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   676
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   677
    private Scanner(File source, CharsetDecoder dec)
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   678
        throws FileNotFoundException
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   679
    {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   680
        this(makeReadable((ReadableByteChannel)(new FileInputStream(source).getChannel()), dec));
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   681
    }
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   682
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   683
    private static CharsetDecoder toDecoder(String charsetName) {
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
   684
        Objects.requireNonNull(charsetName, "charsetName");
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   685
        try {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   686
            return Charset.forName(charsetName).newDecoder();
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   687
        } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   688
            throw new IllegalArgumentException(charsetName);
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   689
        }
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   690
    }
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   691
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   692
    private static Readable makeReadable(ReadableByteChannel source,
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   693
                                         CharsetDecoder dec) {
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   694
        return Channels.newReader(source, dec, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   698
     * Constructs a new <code>Scanner</code> that produces values scanned
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   699
     * from the specified file. Bytes from the file are converted into
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   700
     * characters using the underlying platform's
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   701
     * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   702
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   703
     * @param   source
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
   704
     *          the path to the file to be scanned
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   705
     * @throws  IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   706
     *          if an I/O error occurs opening source
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   707
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   708
     * @since   1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   709
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
   710
    public Scanner(Path source)
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   711
        throws IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   712
    {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
   713
        this(Files.newInputStream(source));
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   714
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   715
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   716
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   717
     * Constructs a new <code>Scanner</code> that produces values scanned
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   718
     * from the specified file. Bytes from the file are converted into
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   719
     * characters using the specified charset.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   720
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   721
     * @param   source
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
   722
     *          the path to the file to be scanned
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   723
     * @param   charsetName
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   724
     *          The encoding type used to convert bytes from the file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   725
     *          into characters to be scanned
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   726
     * @throws  IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   727
     *          if an I/O error occurs opening source
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   728
     * @throws  IllegalArgumentException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   729
     *          if the specified encoding is not found
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   730
     * @since   1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   731
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
   732
    public Scanner(Path source, String charsetName) throws IOException {
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
   733
        this(Objects.requireNonNull(source), toCharset(charsetName));
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   734
    }
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   735
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
   736
    private Scanner(Path source, Charset charset)  throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7966
diff changeset
   737
        this(makeReadable(Files.newInputStream(source), charset));
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   738
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   739
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   740
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * Constructs a new <code>Scanner</code> that produces values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * from the specified string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param  source A string to scan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    public Scanner(String source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        this(new StringReader(source), WHITESPACE_PATTERN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * Constructs a new <code>Scanner</code> that produces values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * from the specified channel. Bytes from the source are converted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * characters using the underlying platform's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * @param  source A channel to scan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public Scanner(ReadableByteChannel source) {
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
   759
        this(makeReadable(Objects.requireNonNull(source, "source")),
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   760
             WHITESPACE_PATTERN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    private static Readable makeReadable(ReadableByteChannel source) {
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   764
        return makeReadable(source, Charset.defaultCharset().newDecoder());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * Constructs a new <code>Scanner</code> that produces values scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * from the specified channel. Bytes from the source are converted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * characters using the specified charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @param  source A channel to scan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @param charsetName The encoding type used to convert bytes from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *        channel into characters to be scanned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @throws IllegalArgumentException if the specified character set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     *         does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public Scanner(ReadableByteChannel source, String charsetName) {
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
   779
        this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)),
7966
a23e3f47c5a8 7000511: PrintStream, PrintWriter, Formatter, Scanner leave files open when exception thrown
chegar
parents: 7668
diff changeset
   780
             WHITESPACE_PATTERN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    // Private primitives used to support scanning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    private void saveState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        savedScannerPosition = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private void revertState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        this.position = savedScannerPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        savedScannerPosition = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        skipped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    private boolean revertState(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        this.position = savedScannerPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        savedScannerPosition = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        skipped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    private void cacheResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        hasNextResult = matcher.group();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        hasNextPosition = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        hasNextPattern = matcher.pattern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    private void cacheResult(String result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        hasNextResult = result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        hasNextPosition = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        hasNextPattern = matcher.pattern();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    // Clears both regular cache and type cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    private void clearCaches() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        hasNextPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        typeCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    // Also clears both the regular cache and the type cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    private String getCachedResult() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        position = hasNextPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        hasNextPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        typeCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        return hasNextResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    // Also clears both the regular cache and the type cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    private void useTypeCache() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            throw new IllegalStateException("Scanner closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        position = hasNextPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        hasNextPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        typeCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    // Tries to read more input. May block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    private void readInput() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        if (buf.limit() == buf.capacity())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            makeSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        // Prepare to receive data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        int p = buf.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        buf.position(buf.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        buf.limit(buf.capacity());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            n = source.read(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            lastException = ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            n = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            sourceClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            needInput = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (n > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            needInput = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        // Restore current position and limit for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        buf.limit(buf.position());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        buf.position(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    // After this method is called there will either be an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    // or else there will be space in the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    private boolean makeSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        int offset = savedScannerPosition == -1 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            position : savedScannerPosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        buf.position(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        // Gain space by compacting buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if (offset > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            buf.compact();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            translateSavedIndexes(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            position -= offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            buf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        // Gain space by growing buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        int newSize = buf.capacity() * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        CharBuffer newBuf = CharBuffer.allocate(newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        newBuf.put(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        newBuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        translateSavedIndexes(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        position -= offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        buf = newBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        matcher.reset(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    // When a buffer compaction/reallocation occurs the saved indexes must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    // be modified appropriately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    private void translateSavedIndexes(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (savedScannerPosition != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            savedScannerPosition -= offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    // If we are at the end of input then NoSuchElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
    // If there is still input left then InputMismatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    private void throwFor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        skipped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        if ((sourceClosed) && (position == buf.limit()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            throw new InputMismatchException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    // Returns true if a complete token or partial token is in the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    // It is not necessary to find a complete token since a partial token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    // means that there will be another token with or without more input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    private boolean hasTokenInBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        matchValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        matcher.usePattern(delimPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        matcher.region(position, buf.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        // Skip delims first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        if (matcher.lookingAt())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            position = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        // If we are sitting at the end, no more tokens in buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        if (position == buf.limit())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * Returns a "complete token" that matches the specified pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * A token is complete if surrounded by delims; a partial token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * is prefixed by delims but not postfixed by them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * The position is advanced to the end of that complete token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Pattern == null means accept any token at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * Triple return:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * 1. valid string means it was found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * 2. null with needInput=false means we won't ever find it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * 3. null with needInput=true means try again after readInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    private String getCompleteTokenInBuffer(Pattern pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        matchValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        // Skip delims first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        matcher.usePattern(delimPattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        if (!skipped) { // Enforcing only one skip of leading delims
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            matcher.region(position, buf.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            if (matcher.lookingAt()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                // If more input could extend the delimiters then we must wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                // for more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                if (matcher.hitEnd() && !sourceClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                // The delims were whole and the matcher should skip them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                skipped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                position = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        // If we are sitting at the end, no more tokens in buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        if (position == buf.limit()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            if (sourceClosed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // Must look for next delims. Simply attempting to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        // pattern at this point may find a match but it might not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        // the first longest match because of missing input, or it might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        // match a partial token instead of the whole thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        // Then look for next delims
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        matcher.region(position, buf.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        boolean foundNextDelim = matcher.find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        if (foundNextDelim && (matcher.end() == position)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            // Zero length delimiter match; we should find the next one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            // using the automatic advance past a zero length match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            // Otherwise we have just found the same one we just skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            foundNextDelim = matcher.find();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        if (foundNextDelim) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            // In the rare case that more input could cause the match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            // to be lost and there is more input coming we must wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            // for more input. Note that hitting the end is okay as long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            // as the match cannot go away. It is the beginning of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            // next delims we want to be sure about, we don't care if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            // they potentially extend further.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            if (matcher.requireEnd() && !sourceClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            int tokenEnd = matcher.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            // There is a complete token.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            if (pattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                // Must continue with match to provide valid MatchResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                pattern = FIND_ANY_PATTERN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            //  Attempt to match against the desired pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            matcher.usePattern(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            matcher.region(position, tokenEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            if (matcher.matches()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                String s = matcher.group();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                position = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            } else { // Complete token but it does not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        // If we can't find the next delims but no more input is coming,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        // then we can treat the remainder as a whole token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if (sourceClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            if (pattern == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                // Must continue with match to provide valid MatchResult
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                pattern = FIND_ANY_PATTERN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            // Last token; Match the pattern here or throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            matcher.usePattern(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            matcher.region(position, buf.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            if (matcher.matches()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                String s = matcher.group();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                position = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            // Last piece does not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        // There is a partial token in the buffer; must read more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        // to complete it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    // Finds the specified pattern in the buffer up to horizon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    // Returns a match for the specified input pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    private String findPatternInBuffer(Pattern pattern, int horizon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        matchValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        matcher.usePattern(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        int bufferLimit = buf.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        int horizonLimit = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        int searchLimit = bufferLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if (horizon > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            horizonLimit = position + horizon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            if (horizonLimit < bufferLimit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                searchLimit = horizonLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        matcher.region(position, searchLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        if (matcher.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
            if (matcher.hitEnd() && (!sourceClosed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                // The match may be longer if didn't hit horizon or real end
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                if (searchLimit != horizonLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                     // Hit an artificial end; try to extend the match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                // The match could go away depending on what is next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                if ((searchLimit == horizonLimit) && matcher.requireEnd()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                    // Rare case: we hit the end of input and it happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    // that it is at the horizon and the end of input is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    // required for the match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                    needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            // Did not hit end, or hit real end, or hit horizon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            position = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            return matcher.group();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        if (sourceClosed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        // If there is no specified horizon, or if we have not searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        // to the specified horizon yet, get more input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        if ((horizon == 0) || (searchLimit != horizonLimit))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    // Returns a match for the specified input pattern anchored at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    // the current position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    private String matchPatternInBuffer(Pattern pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        matchValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        matcher.usePattern(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        matcher.region(position, buf.limit());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if (matcher.lookingAt()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            if (matcher.hitEnd() && (!sourceClosed)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                // Get more input and try again
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            position = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            return matcher.group();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        if (sourceClosed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        // Read more to find pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        needInput = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    // Throws if the scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    private void ensureOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            throw new IllegalStateException("Scanner closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    // Public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * Closes this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * <p> If this scanner has not yet been closed then if its underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * {@linkplain java.lang.Readable readable} also implements the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * java.io.Closeable} interface then the readable's <tt>close</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * will be invoked.  If this scanner is already closed then invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * method will have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * <p>Attempting to perform search operations after a scanner has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * been closed will result in an {@link IllegalStateException}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        if (source instanceof Closeable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                ((Closeable)source).close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                lastException = ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        sourceClosed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        source = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * Returns the <code>IOException</code> last thrown by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * <code>Scanner</code>'s underlying <code>Readable</code>. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * returns <code>null</code> if no such exception exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * @return the last exception thrown by this scanner's readable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    public IOException ioException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        return lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * Returns the <code>Pattern</code> this <code>Scanner</code> is currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * using to match delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * @return this scanner's delimiting pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    public Pattern delimiter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        return delimPattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * Sets this scanner's delimiting pattern to the specified pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @param pattern A delimiting pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @return this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    public Scanner useDelimiter(Pattern pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        delimPattern = pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     * Sets this scanner's delimiting pattern to a pattern constructed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * the specified <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * <tt>useDelimiter(pattern)</tt> behaves in exactly the same way as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * invocation <tt>useDelimiter(Pattern.compile(pattern))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * <p> Invoking the {@link #reset} method will set the scanner's delimiter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * to the <a href= "#default-delimiter">default</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * @param pattern A string specifying a delimiting pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @return this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    public Scanner useDelimiter(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        delimPattern = patternCache.forName(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * Returns this scanner's locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * <p>A scanner's locale affects many elements of its default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * primitive matching regular expressions; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * <a href= "#localized-numbers">localized numbers</a> above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * @return this scanner's locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
    public Locale locale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        return this.locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * Sets this scanner's locale to the specified locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * <p>A scanner's locale affects many elements of its default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * primitive matching regular expressions; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * <a href= "#localized-numbers">localized numbers</a> above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * <p>Invoking the {@link #reset} method will set the scanner's locale to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * the <a href= "#initial-locale">initial locale</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     * @param locale A string specifying the locale to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     * @return this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
    public Scanner useLocale(Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        if (locale.equals(this.locale))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        this.locale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        DecimalFormat df =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            (DecimalFormat)NumberFormat.getNumberInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        // These must be literalized to avoid collision with regex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        // metacharacters such as dot or parenthesis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        groupSeparator =   "\\" + dfs.getGroupingSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        decimalSeparator = "\\" + dfs.getDecimalSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        // Quoting the nonzero length locale-specific things
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        // to avoid potential conflict with metacharacters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        nanString = "\\Q" + dfs.getNaN() + "\\E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        infinityString = "\\Q" + dfs.getInfinity() + "\\E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        positivePrefix = df.getPositivePrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        if (positivePrefix.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            positivePrefix = "\\Q" + positivePrefix + "\\E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        negativePrefix = df.getNegativePrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
        if (negativePrefix.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            negativePrefix = "\\Q" + negativePrefix + "\\E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
        positiveSuffix = df.getPositiveSuffix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        if (positiveSuffix.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            positiveSuffix = "\\Q" + positiveSuffix + "\\E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        negativeSuffix = df.getNegativeSuffix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        if (negativeSuffix.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            negativeSuffix = "\\Q" + negativeSuffix + "\\E";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        // Force rebuilding and recompilation of locale dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        // primitive patterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        integerPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        floatPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * Returns this scanner's default radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * <p>A scanner's radix affects elements of its default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * number matching regular expressions; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     * <a href= "#localized-numbers">localized numbers</a> above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * @return the default radix of this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
    public int radix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        return this.defaultRadix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * Sets this scanner's default radix to the specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * <p>A scanner's radix affects elements of its default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * number matching regular expressions; see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * <a href= "#localized-numbers">localized numbers</a> above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * <p>If the radix is less than <code>Character.MIN_RADIX</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * or greater than <code>Character.MAX_RADIX</code>, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * <code>IllegalArgumentException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * <p>Invoking the {@link #reset} method will set the scanner's radix to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * <code>10</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * @param radix The radix to use when scanning numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @return this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * @throws IllegalArgumentException if radix is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
    public Scanner useRadix(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            throw new IllegalArgumentException("radix:"+radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        if (this.defaultRadix == radix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        this.defaultRadix = radix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        // Force rebuilding and recompilation of radix dependent patterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
        integerPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    // The next operation should occur in the specified radix but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    // the default is left untouched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    private void setRadix(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if (this.radix != radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            // Force rebuilding and recompilation of radix dependent patterns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            integerPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
            this.radix = radix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * Returns the match result of the last scanning operation performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * by this scanner. This method throws <code>IllegalStateException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * if no match has been performed, or if the last match was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * not successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * <p>The various <code>next</code>methods of <code>Scanner</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * make a match result available if they complete without throwing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     * exception. For instance, after an invocation of the {@link #nextInt}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * method that returned an int, this method returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * <code>MatchResult</code> for the search of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     * <a href="#Integer-regex"><i>Integer</i></a> regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * defined above. Similarly the {@link #findInLine},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * {@link #findWithinHorizon}, and {@link #skip} methods will make a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * match available if they succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * @return a match result for the last match operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * @throws IllegalStateException  If no match result is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    public MatchResult match() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        if (!matchValid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            throw new IllegalStateException("No match result available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        return matcher.toMatchResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * <p>Returns the string representation of this <code>Scanner</code>. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * string representation of a <code>Scanner</code> contains information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * that may be useful for debugging. The exact format is unspecified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * @return  The string representation of this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        sb.append("java.util.Scanner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        sb.append("[delimiters=" + delimPattern + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        sb.append("[position=" + position + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        sb.append("[match valid=" + matchValid + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        sb.append("[need input=" + needInput + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        sb.append("[source closed=" + sourceClosed + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        sb.append("[skipped=" + skipped + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        sb.append("[group separator=" + groupSeparator + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        sb.append("[decimal separator=" + decimalSeparator + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        sb.append("[positive prefix=" + positivePrefix + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        sb.append("[negative prefix=" + negativePrefix + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        sb.append("[positive suffix=" + positiveSuffix + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        sb.append("[negative suffix=" + negativeSuffix + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        sb.append("[NaN string=" + nanString + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        sb.append("[infinity string=" + infinityString + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * Returns true if this scanner has another token in its input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * This method may block while waiting for input to scan.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @return true if and only if this scanner has another token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * @see java.util.Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
    public boolean hasNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        saveState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        while (!sourceClosed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            if (hasTokenInBuffer())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                return revertState(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            readInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        boolean result = hasTokenInBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        return revertState(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * Finds and returns the next complete token from this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * A complete token is preceded and followed by input that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * the delimiter pattern. This method may block while waiting for input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * to scan, even if a previous invocation of {@link #hasNext} returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @return the next token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @throws NoSuchElementException if no more tokens are available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * @see java.util.Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    public String next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            String token = getCompleteTokenInBuffer(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            if (token != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
                matchValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
                skipped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
                return token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            if (needInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
                readInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
                throwFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * The remove operation is not supported by this implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * <code>Iterator</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * @throws UnsupportedOperationException if this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @see java.util.Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    public void remove() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * Returns true if the next token matches the pattern constructed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * specified string. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * <p> An invocation of this method of the form <tt>hasNext(pattern)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * <tt>hasNext(Pattern.compile(pattern))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @param pattern a string specifying the pattern to scan
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * @return true if and only if this scanner has another token matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     *         the specified pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    public boolean hasNext(String pattern)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        return hasNext(patternCache.forName(pattern));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * Returns the next token if it matches the pattern constructed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * specified string.  If the match is successful, the scanner advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * past the input that matched the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * <p> An invocation of this method of the form <tt>next(pattern)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * <tt>next(Pattern.compile(pattern))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * @param pattern a string specifying the pattern to scan
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * @return the next token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * @throws NoSuchElementException if no such tokens are available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    public String next(String pattern)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        return next(patternCache.forName(pattern));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
     * Returns true if the next complete token matches the specified pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * A complete token is prefixed and postfixed by input that matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     * the delimiter pattern. This method may block while waiting for input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * @param pattern the pattern to scan for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * @return true if and only if this scanner has another token matching
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *         the specified pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    public boolean hasNext(Pattern pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        if (pattern == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
        hasNextPattern = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        saveState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
            if (getCompleteTokenInBuffer(pattern) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                matchValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                cacheResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                return revertState(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
            if (needInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                readInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                return revertState(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * Returns the next token if it matches the specified pattern. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     * method may block while waiting for input to scan, even if a previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     * invocation of {@link #hasNext(Pattern)} returned <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * If the match is successful, the scanner advances past the input that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * matched the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * @param pattern the pattern to scan for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * @return the next token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * @throws NoSuchElementException if no more tokens are available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
    public String next(Pattern pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        if (pattern == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        // Did we already find this pattern?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        if (hasNextPattern == pattern)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            return getCachedResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        // Search for the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            String token = getCompleteTokenInBuffer(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            if (token != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                matchValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                skipped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                return token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            if (needInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                readInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                throwFor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * Returns true if there is another line in the input of this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * This method may block while waiting for input. The scanner does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * @return true if and only if this scanner has another line of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    public boolean hasNextLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        saveState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        String result = findWithinHorizon(linePattern(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
            MatchResult mr = this.match();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            String lineSep = mr.group(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
            if (lineSep != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                result = result.substring(0, result.length() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                                          lineSep.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                cacheResult(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                cacheResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        revertState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        return (result != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * Advances this scanner past the current line and returns the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * that was skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
     * This method returns the rest of the current line, excluding any line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
     * separator at the end. The position is set to the beginning of the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * <p>Since this method continues to search through the input looking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * for a line separator, it may buffer all of the input searching for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * the line to skip if no line separators are present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * @return the line that was skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * @throws NoSuchElementException if no line was found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    public String nextLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        if (hasNextPattern == linePattern())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
            return getCachedResult();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        String result = findWithinHorizon(linePattern, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
            throw new NoSuchElementException("No line found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        MatchResult mr = this.match();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
        String lineSep = mr.group(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        if (lineSep != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            result = result.substring(0, result.length() - lineSep.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        if (result == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    // Public methods that ignore delimiters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * Attempts to find the next occurrence of a pattern constructed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * specified string, ignoring delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * <p>An invocation of this method of the form <tt>findInLine(pattern)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * <tt>findInLine(Pattern.compile(pattern))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * @param pattern a string specifying the pattern to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * @return the text that matched the specified pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
    public String findInLine(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        return findInLine(patternCache.forName(pattern));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * Attempts to find the next occurrence of the specified pattern ignoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * delimiters. If the pattern is found before the next line separator, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * scanner advances past the input that matched and returns the string that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * matched the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * If no such pattern is detected in the input up to the next line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * separator, then <code>null</code> is returned and the scanner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * position is unchanged. This method may block waiting for input that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * matches the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * <p>Since this method continues to search through the input looking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     * for the specified pattern, it may buffer all of the input searching for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * the desired token if no line separators are present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * @param pattern the pattern to scan for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * @return the text that matched the specified pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
    public String findInLine(Pattern pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        if (pattern == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        // Expand buffer to include the next newline or end of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        int endPosition = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        saveState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            String token = findPatternInBuffer(separatorPattern(), 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            if (token != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                endPosition = matcher.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
                break; // up to next newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            if (needInput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                readInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                endPosition = buf.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                break; // up to end of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        revertState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        int horizonForLine = endPosition - position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        // If there is nothing between the current pos and the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        // newline simply return null, invoking findWithinHorizon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        // with "horizon=0" will scan beyond the line bound.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        if (horizonForLine == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        // Search for the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
        return findWithinHorizon(pattern, horizonForLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * Attempts to find the next occurrence of a pattern constructed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     * specified string, ignoring delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
     * <p>An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
     * <tt>findWithinHorizon(pattern)</tt> behaves in exactly the same way as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     * <tt>findWithinHorizon(Pattern.compile(pattern, horizon))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * @param pattern a string specifying the pattern to search for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * @return the text that matched the specified pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @throws IllegalArgumentException if horizon is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    public String findWithinHorizon(String pattern, int horizon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        return findWithinHorizon(patternCache.forName(pattern), horizon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * Attempts to find the next occurrence of the specified pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * <p>This method searches through the input up to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * search horizon, ignoring delimiters. If the pattern is found the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * scanner advances past the input that matched and returns the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * that matched the pattern. If no such pattern is detected then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * null is returned and the scanner's position remains unchanged. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     * method may block waiting for input that matches the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * <p>A scanner will never search more than <code>horizon</code> code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * points beyond its current position. Note that a match may be clipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * by the horizon; that is, an arbitrary match result may have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * different if the horizon had been larger. The scanner treats the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * horizon as a transparent, non-anchoring bound (see {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * Matcher#useTransparentBounds} and {@link Matcher#useAnchoringBounds}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * <p>If horizon is <code>0</code>, then the horizon is ignored and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * this method continues to search through the input looking for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * specified pattern without bound. In this case it may buffer all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * the input searching for the pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * <p>If horizon is negative, then an IllegalArgumentException is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * @param pattern the pattern to scan for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * @return the text that matched the specified pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * @throws IllegalArgumentException if horizon is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    public String findWithinHorizon(Pattern pattern, int horizon) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        if (pattern == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        if (horizon < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            throw new IllegalArgumentException("horizon < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        // Search for the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
            String token = findPatternInBuffer(pattern, horizon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            if (token != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                matchValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                return token;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            if (needInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
                readInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                break; // up to end of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * Skips input that matches the specified pattern, ignoring delimiters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * This method will skip input if an anchored match of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * pattern succeeds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * <p>If a match to the specified pattern is not found at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * current position, then no input is skipped and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * <tt>NoSuchElementException</tt> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * <p>Since this method seeks to match the specified pattern starting at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * the scanner's current position, patterns that can match a lot of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * input (".*", for example) may cause the scanner to buffer a large
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * amount of input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     * <p>Note that it is possible to skip something without risking a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * <code>NoSuchElementException</code> by using a pattern that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * match nothing, e.g., <code>sc.skip("[ \t]*")</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * @param pattern a string specifying the pattern to skip over
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * @return this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * @throws NoSuchElementException if the specified pattern is not found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    public Scanner skip(Pattern pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        if (pattern == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        // Search for the pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            String token = matchPatternInBuffer(pattern);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            if (token != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                matchValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                position = matcher.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            if (needInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
                readInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
                throw new NoSuchElementException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * Skips input that matches a pattern constructed from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * <p> An invocation of this method of the form <tt>skip(pattern)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * <tt>skip(Pattern.compile(pattern))</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * @param pattern a string specifying the pattern to skip over
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     * @return this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    public Scanner skip(String pattern) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        return skip(patternCache.forName(pattern));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    // Convenience methods for scanning primitives
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * interpreted as a boolean value using a case insensitive pattern
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * created from the string "true|false".  The scanner does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * advance past the input that matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     *         boolean value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    public boolean hasNextBoolean()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        return hasNext(boolPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * Scans the next token of the input into a boolean value and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * that value. This method will throw <code>InputMismatchException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * if the next token cannot be translated into a valid boolean value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * If the match is successful, the scanner advances past the input that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @return the boolean scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * @throws InputMismatchException if the next token is not a valid boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    public boolean nextBoolean()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        return Boolean.parseBoolean(next(boolPattern()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * interpreted as a byte value in the default radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     * {@link #nextByte} method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     *         byte value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
    public boolean hasNextByte() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        return hasNextByte(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * interpreted as a byte value in the specified radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * {@link #nextByte} method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     * @param radix the radix used to interpret the token as a byte value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     *         byte value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
    public boolean hasNextByte(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        boolean result = hasNext(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        if (result) { // Cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                    processIntegerToken(hasNextResult) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                    hasNextResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                typeCache = Byte.parseByte(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     * Scans the next token of the input as a <tt>byte</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * <tt>nextByte()</tt> behaves in exactly the same way as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     * invocation <tt>nextByte(radix)</tt>, where <code>radix</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
     * is the default radix of this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     * @return the <tt>byte</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    public byte nextByte() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
         return nextByte(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     * Scans the next token of the input as a <tt>byte</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * This method will throw <code>InputMismatchException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * if the next token cannot be translated into a valid byte value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * described below. If the translation is successful, the scanner advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * past the input that matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     * <p> If the next token matches the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     * href="#Integer-regex"><i>Integer</i></a> regular expression defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * above then the token is converted into a <tt>byte</tt> value as if by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * removing all locale specific prefixes, group separators, and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * specific suffixes, then mapping non-ASCII digits into ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * digits via {@link Character#digit Character.digit}, prepending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * negative sign (-) if the locale specific negative prefixes and suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * were present, and passing the resulting string to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     * {@link Byte#parseByte(String, int) Byte.parseByte} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     * specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * @param radix the radix used to interpret the token as a byte value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * @return the <tt>byte</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    public byte nextByte(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        // Check cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
        if ((typeCache != null) && (typeCache instanceof Byte)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
            && this.radix == radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
            byte val = ((Byte)typeCache).byteValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            useTypeCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        // Search for next byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            String s = next(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                s = processIntegerToken(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            return Byte.parseByte(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            position = matcher.start(); // don't skip bad token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            throw new InputMismatchException(nfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * interpreted as a short value in the default radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     * {@link #nextShort} method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
     *         short value in the default radix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    public boolean hasNextShort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        return hasNextShort(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * interpreted as a short value in the specified radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     * {@link #nextShort} method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
     * @param radix the radix used to interpret the token as a short value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
     *         short value in the specified radix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    public boolean hasNextShort(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        boolean result = hasNext(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
        if (result) { // Cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                    processIntegerToken(hasNextResult) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                    hasNextResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
                typeCache = Short.parseShort(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * Scans the next token of the input as a <tt>short</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * <tt>nextShort()</tt> behaves in exactly the same way as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     * invocation <tt>nextShort(radix)</tt>, where <code>radix</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * is the default radix of this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * @return the <tt>short</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    public short nextShort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        return nextShort(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
     * Scans the next token of the input as a <tt>short</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * This method will throw <code>InputMismatchException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
     * if the next token cannot be translated into a valid short value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     * described below. If the translation is successful, the scanner advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
     * past the input that matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * <p> If the next token matches the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * href="#Integer-regex"><i>Integer</i></a> regular expression defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * above then the token is converted into a <tt>short</tt> value as if by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * removing all locale specific prefixes, group separators, and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * specific suffixes, then mapping non-ASCII digits into ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * digits via {@link Character#digit Character.digit}, prepending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * negative sign (-) if the locale specific negative prefixes and suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * were present, and passing the resulting string to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * {@link Short#parseShort(String, int) Short.parseShort} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     * specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * @param radix the radix used to interpret the token as a short value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * @return the <tt>short</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    public short nextShort(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        // Check cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        if ((typeCache != null) && (typeCache instanceof Short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            && this.radix == radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            short val = ((Short)typeCache).shortValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            useTypeCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        // Search for next short
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            String s = next(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                s = processIntegerToken(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            return Short.parseShort(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            position = matcher.start(); // don't skip bad token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            throw new InputMismatchException(nfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
     * interpreted as an int value in the default radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * {@link #nextInt} method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     *         int value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
    public boolean hasNextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        return hasNextInt(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * interpreted as an int value in the specified radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * {@link #nextInt} method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     * @param radix the radix used to interpret the token as an int value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
     *         int value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    public boolean hasNextInt(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        boolean result = hasNext(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
        if (result) { // Cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                    processIntegerToken(hasNextResult) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                    hasNextResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                typeCache = Integer.parseInt(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * The integer token must be stripped of prefixes, group separators,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * and suffixes, non ascii digits must be converted into ascii digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * before parse will accept it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
    private String processIntegerToken(String token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        String result = token.replaceAll(""+groupSeparator, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        boolean isNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        int preLen = negativePrefix.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        if ((preLen > 0) && result.startsWith(negativePrefix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            isNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
            result = result.substring(preLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        int sufLen = negativeSuffix.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            isNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
            result = result.substring(result.length() - sufLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                                      result.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        if (isNegative)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
            result = "-" + result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     * Scans the next token of the input as an <tt>int</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     * <tt>nextInt()</tt> behaves in exactly the same way as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     * invocation <tt>nextInt(radix)</tt>, where <code>radix</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     * is the default radix of this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * @return the <tt>int</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    public int nextInt() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        return nextInt(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * Scans the next token of the input as an <tt>int</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * This method will throw <code>InputMismatchException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * if the next token cannot be translated into a valid int value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * described below. If the translation is successful, the scanner advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * past the input that matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * <p> If the next token matches the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * href="#Integer-regex"><i>Integer</i></a> regular expression defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * above then the token is converted into an <tt>int</tt> value as if by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * removing all locale specific prefixes, group separators, and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     * specific suffixes, then mapping non-ASCII digits into ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * digits via {@link Character#digit Character.digit}, prepending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * negative sign (-) if the locale specific negative prefixes and suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * were present, and passing the resulting string to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * {@link Integer#parseInt(String, int) Integer.parseInt} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     * specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * @param radix the radix used to interpret the token as an int value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * @return the <tt>int</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
    public int nextInt(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
        // Check cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
        if ((typeCache != null) && (typeCache instanceof Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
            && this.radix == radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
            int val = ((Integer)typeCache).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
            useTypeCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        // Search for next int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            String s = next(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                s = processIntegerToken(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
            return Integer.parseInt(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
            position = matcher.start(); // don't skip bad token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            throw new InputMismatchException(nfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     * interpreted as a long value in the default radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     * {@link #nextLong} method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     *         long value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
    public boolean hasNextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        return hasNextLong(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * interpreted as a long value in the specified radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * {@link #nextLong} method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     * @param radix the radix used to interpret the token as a long value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     *         long value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
    public boolean hasNextLong(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        boolean result = hasNext(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        if (result) { // Cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                    processIntegerToken(hasNextResult) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                    hasNextResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                typeCache = Long.parseLong(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * Scans the next token of the input as a <tt>long</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     * <tt>nextLong()</tt> behaves in exactly the same way as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * invocation <tt>nextLong(radix)</tt>, where <code>radix</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * is the default radix of this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * @return the <tt>long</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
    public long nextLong() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        return nextLong(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
     * Scans the next token of the input as a <tt>long</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * This method will throw <code>InputMismatchException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     * if the next token cannot be translated into a valid long value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * described below. If the translation is successful, the scanner advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * past the input that matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     * <p> If the next token matches the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
     * href="#Integer-regex"><i>Integer</i></a> regular expression defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * above then the token is converted into a <tt>long</tt> value as if by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * removing all locale specific prefixes, group separators, and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     * specific suffixes, then mapping non-ASCII digits into ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * digits via {@link Character#digit Character.digit}, prepending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * negative sign (-) if the locale specific negative prefixes and suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * were present, and passing the resulting string to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * {@link Long#parseLong(String, int) Long.parseLong} with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
     * specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * @param radix the radix used to interpret the token as an int value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * @return the <tt>long</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    public long nextLong(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        // Check cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        if ((typeCache != null) && (typeCache instanceof Long)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            && this.radix == radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
            long val = ((Long)typeCache).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            useTypeCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            String s = next(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                s = processIntegerToken(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
            return Long.parseLong(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            position = matcher.start(); // don't skip bad token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            throw new InputMismatchException(nfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * The float token must be stripped of prefixes, group separators,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     * and suffixes, non ascii digits must be converted into ascii digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
     * before parseFloat will accept it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * If there are non-ascii digits in the token these digits must
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     * be processed before the token is passed to parseFloat.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
    private String processFloatToken(String token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
        String result = token.replaceAll(groupSeparator, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
        if (!decimalSeparator.equals("\\."))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            result = result.replaceAll(decimalSeparator, ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        boolean isNegative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
        int preLen = negativePrefix.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        if ((preLen > 0) && result.startsWith(negativePrefix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
            isNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            result = result.substring(preLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
        int sufLen = negativeSuffix.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
        if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
            isNegative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
            result = result.substring(result.length() - sufLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                                      result.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
        if (result.equals(nanString))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
            result = "NaN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
        if (result.equals(infinityString))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            result = "Infinity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        if (isNegative)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
            result = "-" + result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
        // Translate non-ASCII digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        Matcher m = NON_ASCII_DIGIT.matcher(result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        if (m.find()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
            StringBuilder inASCII = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
            for (int i=0; i<result.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                char nextChar = result.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                if (Character.isDigit(nextChar)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                    int d = Character.digit(nextChar, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                    if (d != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                        inASCII.append(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                        inASCII.append(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                    inASCII.append(nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
            result = inASCII.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     * interpreted as a float value using the {@link #nextFloat}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     *         float value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
    public boolean hasNextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        setRadix(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        boolean result = hasNext(floatPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
        if (result) { // Cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                String s = processFloatToken(hasNextResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                typeCache = Float.valueOf(Float.parseFloat(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * Scans the next token of the input as a <tt>float</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     * This method will throw <code>InputMismatchException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
     * if the next token cannot be translated into a valid float value as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
     * described below. If the translation is successful, the scanner advances
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
     * past the input that matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
     * <p> If the next token matches the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
     * href="#Float-regex"><i>Float</i></a> regular expression defined above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
     * then the token is converted into a <tt>float</tt> value as if by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
     * removing all locale specific prefixes, group separators, and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
     * specific suffixes, then mapping non-ASCII digits into ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * digits via {@link Character#digit Character.digit}, prepending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * negative sign (-) if the locale specific negative prefixes and suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * were present, and passing the resulting string to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     * {@link Float#parseFloat Float.parseFloat}. If the token matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
     * the localized NaN or infinity strings, then either "Nan" or "Infinity"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     * is passed to {@link Float#parseFloat(String) Float.parseFloat} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
     * @return the <tt>float</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     *         if the next token does not match the <i>Float</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * @throws NoSuchElementException if input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    public float nextFloat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
        // Check cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        if ((typeCache != null) && (typeCache instanceof Float)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
            float val = ((Float)typeCache).floatValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            useTypeCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        setRadix(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            return Float.parseFloat(processFloatToken(next(floatPattern())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
            position = matcher.start(); // don't skip bad token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
            throw new InputMismatchException(nfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * interpreted as a double value using the {@link #nextDouble}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * method. The scanner does not advance past any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     *         double value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
    public boolean hasNextDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
        setRadix(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        boolean result = hasNext(floatPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
        if (result) { // Cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                String s = processFloatToken(hasNextResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                typeCache = Double.valueOf(Double.parseDouble(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     * Scans the next token of the input as a <tt>double</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     * This method will throw <code>InputMismatchException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     * if the next token cannot be translated into a valid double value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * If the translation is successful, the scanner advances past the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * that matched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * <p> If the next token matches the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     * href="#Float-regex"><i>Float</i></a> regular expression defined above
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
     * then the token is converted into a <tt>double</tt> value as if by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * removing all locale specific prefixes, group separators, and locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     * specific suffixes, then mapping non-ASCII digits into ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * digits via {@link Character#digit Character.digit}, prepending a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * negative sign (-) if the locale specific negative prefixes and suffixes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * were present, and passing the resulting string to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * {@link Double#parseDouble Double.parseDouble}. If the token matches
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * the localized NaN or infinity strings, then either "Nan" or "Infinity"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     * is passed to {@link Double#parseDouble(String) Double.parseDouble} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     * appropriate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     * @return the <tt>double</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     *         if the next token does not match the <i>Float</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
     * @throws NoSuchElementException if the input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    public double nextDouble() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
        // Check cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        if ((typeCache != null) && (typeCache instanceof Double)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
            double val = ((Double)typeCache).doubleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
            useTypeCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        setRadix(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        // Search for next float
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
            return Double.parseDouble(processFloatToken(next(floatPattern())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
            position = matcher.start(); // don't skip bad token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
            throw new InputMismatchException(nfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
    // Convenience methods for scanning multi precision numbers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * interpreted as a <code>BigInteger</code> in the default radix using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * {@link #nextBigInteger} method. The scanner does not advance past any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     *         <code>BigInteger</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
    public boolean hasNextBigInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
        return hasNextBigInteger(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     * interpreted as a <code>BigInteger</code> in the specified radix using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     * the {@link #nextBigInteger} method. The scanner does not advance past
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     * any input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
     * @param radix the radix used to interpret the token as an integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     *         <code>BigInteger</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
    public boolean hasNextBigInteger(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
        boolean result = hasNext(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
        if (result) { // Cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                    processIntegerToken(hasNextResult) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                    hasNextResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                typeCache = new BigInteger(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     * Scans the next token of the input as a {@link java.math.BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     * BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     * <tt>nextBigInteger()</tt> behaves in exactly the same way as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     * invocation <tt>nextBigInteger(radix)</tt>, where <code>radix</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     * is the default radix of this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     * @return the <tt>BigInteger</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
     * @throws NoSuchElementException if the input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
    public BigInteger nextBigInteger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
        return nextBigInteger(defaultRadix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * Scans the next token of the input as a {@link java.math.BigInteger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     * BigInteger}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
     * <p> If the next token matches the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
     * href="#Integer-regex"><i>Integer</i></a> regular expression defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
     * above then the token is converted into a <tt>BigInteger</tt> value as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
     * by removing all group separators, mapping non-ASCII digits into ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
     * digits via the {@link Character#digit Character.digit}, and passing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
     * resulting string to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
     * java.math.BigInteger#BigInteger(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
     * BigInteger(String, int)} constructor with the specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
     * @param radix the radix used to interpret the token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
     * @return the <tt>BigInteger</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     *         if the next token does not match the <i>Integer</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
     * @throws NoSuchElementException if the input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
    public BigInteger nextBigInteger(int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        // Check cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
        if ((typeCache != null) && (typeCache instanceof BigInteger)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            && this.radix == radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
            BigInteger val = (BigInteger)typeCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
            useTypeCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        setRadix(radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
        // Search for next int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
            String s = next(integerPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            if (matcher.group(SIMPLE_GROUP_INDEX) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                s = processIntegerToken(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            return new BigInteger(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
            position = matcher.start(); // don't skip bad token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
            throw new InputMismatchException(nfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     * Returns true if the next token in this scanner's input can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
     * interpreted as a <code>BigDecimal</code> using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
     * {@link #nextBigDecimal} method. The scanner does not advance past any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
     * input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
     * @return true if and only if this scanner's next token is a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
     *         <code>BigDecimal</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
    public boolean hasNextBigDecimal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        setRadix(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        boolean result = hasNext(decimalPattern());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
        if (result) { // Cache it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
                String s = processFloatToken(hasNextResult);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                typeCache = new BigDecimal(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
                result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
     * Scans the next token of the input as a {@link java.math.BigDecimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
     * BigDecimal}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     * <p> If the next token matches the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     * href="#Decimal-regex"><i>Decimal</i></a> regular expression defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     * above then the token is converted into a <tt>BigDecimal</tt> value as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
     * by removing all group separators, mapping non-ASCII digits into ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
     * digits via the {@link Character#digit Character.digit}, and passing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
     * resulting string to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
     * java.math.BigDecimal#BigDecimal(java.lang.String) BigDecimal(String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
     * constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
     * @return the <tt>BigDecimal</tt> scanned from the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
     * @throws InputMismatchException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
     *         if the next token does not match the <i>Decimal</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
     *         regular expression, or is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
     * @throws NoSuchElementException if the input is exhausted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
     * @throws IllegalStateException if this scanner is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
    public BigDecimal nextBigDecimal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        // Check cached result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
        if ((typeCache != null) && (typeCache instanceof BigDecimal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
            BigDecimal val = (BigDecimal)typeCache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
            useTypeCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        setRadix(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
        // Search for next float
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
            String s = processFloatToken(next(decimalPattern()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
            return new BigDecimal(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
            position = matcher.start(); // don't skip bad token
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
            throw new InputMismatchException(nfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
     * Resets this scanner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
     * <p> Resetting a scanner discards all of its explicit state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
     * information which may have been changed by invocations of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     * #useDelimiter}, {@link #useLocale}, or {@link #useRadix}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
     * <tt>scanner.reset()</tt> behaves in exactly the same way as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
     * invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
     *   scanner.useDelimiter("\\p{javaWhitespace}+")
16010
2727163b5df5 7092447: Clarify the default locale used in each locale sensitive operation
naoto
parents: 14342
diff changeset
  2645
     *          .useLocale(Locale.getDefault(Locale.Category.FORMAT))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
     *          .useRadix(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
     * @return this scanner
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
    public Scanner reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        delimPattern = WHITESPACE_PATTERN;
6489
9e7015635425 4700857: RFE: separating user locale and user interface locale
naoto
parents: 5998
diff changeset
  2655
        useLocale(Locale.getDefault(Locale.Category.FORMAT));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
        useRadix(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
        clearCaches();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
}