2667 * method is called, or if this scanner is closed during stream pipeline execution.
2668 *
2669 * <p>This method might block waiting for more input.
2670 *
2671 * @apiNote
2672 * For example, the following code will create a list of
2673 * comma-delimited tokens from a string:
2674 *
2675 * <pre>{@code
2676 * List<String> result = new Scanner("abc,def,,ghi")
2677 * .useDelimiter(",")
2678 * .tokens()
2679 * .collect(Collectors.toList());
2680 * }</pre>
2681 *
2682 * <p>The resulting list would contain {@code "abc"}, {@code "def"},
2683 * the empty string, and {@code "ghi"}.
2684 *
2685 * @return a sequential stream of token strings
2686 * @throws IllegalStateException if this scanner is closed
2687 * @since 1.9
2688 */
2689 public Stream<String> tokens() {
2690 ensureOpen();
2691 Stream<String> stream = StreamSupport.stream(new TokenSpliterator(), false);
2692 return stream.onClose(this::close);
2693 }
2694
2695 class TokenSpliterator extends Spliterators.AbstractSpliterator<String> {
2696 int expectedCount = -1;
2697
2698 TokenSpliterator() {
2699 super(Long.MAX_VALUE,
2700 Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED);
2701 }
2702
2703 @Override
2704 public boolean tryAdvance(Consumer<? super String> cons) {
2705 if (expectedCount >= 0 && expectedCount != modCount) {
2706 throw new ConcurrentModificationException();
2707 }
2753 * input searching for a match.
2754 *
2755 * @apiNote
2756 * For example, the following code will read a file and return a list
2757 * of all sequences of characters consisting of seven or more Latin capital
2758 * letters:
2759 *
2760 * <pre>{@code
2761 * try (Scanner sc = new Scanner(Paths.get("input.txt"))) {
2762 * Pattern pat = Pattern.compile("[A-Z]{7,}");
2763 * List<String> capWords = sc.findAll(pat)
2764 * .map(MatchResult::group)
2765 * .collect(Collectors.toList());
2766 * }
2767 * }</pre>
2768 *
2769 * @param pattern the pattern to be matched
2770 * @return a sequential stream of match results
2771 * @throws NullPointerException if pattern is null
2772 * @throws IllegalStateException if this scanner is closed
2773 * @since 1.9
2774 */
2775 public Stream<MatchResult> findAll(Pattern pattern) {
2776 Objects.requireNonNull(pattern);
2777 ensureOpen();
2778 Stream<MatchResult> stream = StreamSupport.stream(new FindSpliterator(pattern), false);
2779 return stream.onClose(this::close);
2780 }
2781
2782 /**
2783 * Returns a stream of match results that match the provided pattern string.
2784 * The effect is equivalent to the following code:
2785 *
2786 * <pre>{@code
2787 * scanner.findAll(Pattern.compile(patString))
2788 * }</pre>
2789 *
2790 * @param patString the pattern string
2791 * @return a sequential stream of match results
2792 * @throws NullPointerException if patString is null
2793 * @throws IllegalStateException if this scanner is closed
2794 * @throws PatternSyntaxException if the regular expression's syntax is invalid
2795 * @since 1.9
2796 * @see java.util.regex.Pattern
2797 */
2798 public Stream<MatchResult> findAll(String patString) {
2799 Objects.requireNonNull(patString);
2800 ensureOpen();
2801 return findAll(patternCache.forName(patString));
2802 }
2803
2804 class FindSpliterator extends Spliterators.AbstractSpliterator<MatchResult> {
2805 final Pattern pattern;
2806 int expectedCount = -1;
2807
2808 FindSpliterator(Pattern pattern) {
2809 super(Long.MAX_VALUE,
2810 Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED);
2811 this.pattern = pattern;
2812 }
2813
2814 @Override
2815 public boolean tryAdvance(Consumer<? super MatchResult> cons) {
|
2667 * method is called, or if this scanner is closed during stream pipeline execution.
2668 *
2669 * <p>This method might block waiting for more input.
2670 *
2671 * @apiNote
2672 * For example, the following code will create a list of
2673 * comma-delimited tokens from a string:
2674 *
2675 * <pre>{@code
2676 * List<String> result = new Scanner("abc,def,,ghi")
2677 * .useDelimiter(",")
2678 * .tokens()
2679 * .collect(Collectors.toList());
2680 * }</pre>
2681 *
2682 * <p>The resulting list would contain {@code "abc"}, {@code "def"},
2683 * the empty string, and {@code "ghi"}.
2684 *
2685 * @return a sequential stream of token strings
2686 * @throws IllegalStateException if this scanner is closed
2687 * @since 9
2688 */
2689 public Stream<String> tokens() {
2690 ensureOpen();
2691 Stream<String> stream = StreamSupport.stream(new TokenSpliterator(), false);
2692 return stream.onClose(this::close);
2693 }
2694
2695 class TokenSpliterator extends Spliterators.AbstractSpliterator<String> {
2696 int expectedCount = -1;
2697
2698 TokenSpliterator() {
2699 super(Long.MAX_VALUE,
2700 Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED);
2701 }
2702
2703 @Override
2704 public boolean tryAdvance(Consumer<? super String> cons) {
2705 if (expectedCount >= 0 && expectedCount != modCount) {
2706 throw new ConcurrentModificationException();
2707 }
2753 * input searching for a match.
2754 *
2755 * @apiNote
2756 * For example, the following code will read a file and return a list
2757 * of all sequences of characters consisting of seven or more Latin capital
2758 * letters:
2759 *
2760 * <pre>{@code
2761 * try (Scanner sc = new Scanner(Paths.get("input.txt"))) {
2762 * Pattern pat = Pattern.compile("[A-Z]{7,}");
2763 * List<String> capWords = sc.findAll(pat)
2764 * .map(MatchResult::group)
2765 * .collect(Collectors.toList());
2766 * }
2767 * }</pre>
2768 *
2769 * @param pattern the pattern to be matched
2770 * @return a sequential stream of match results
2771 * @throws NullPointerException if pattern is null
2772 * @throws IllegalStateException if this scanner is closed
2773 * @since 9
2774 */
2775 public Stream<MatchResult> findAll(Pattern pattern) {
2776 Objects.requireNonNull(pattern);
2777 ensureOpen();
2778 Stream<MatchResult> stream = StreamSupport.stream(new FindSpliterator(pattern), false);
2779 return stream.onClose(this::close);
2780 }
2781
2782 /**
2783 * Returns a stream of match results that match the provided pattern string.
2784 * The effect is equivalent to the following code:
2785 *
2786 * <pre>{@code
2787 * scanner.findAll(Pattern.compile(patString))
2788 * }</pre>
2789 *
2790 * @param patString the pattern string
2791 * @return a sequential stream of match results
2792 * @throws NullPointerException if patString is null
2793 * @throws IllegalStateException if this scanner is closed
2794 * @throws PatternSyntaxException if the regular expression's syntax is invalid
2795 * @since 9
2796 * @see java.util.regex.Pattern
2797 */
2798 public Stream<MatchResult> findAll(String patString) {
2799 Objects.requireNonNull(patString);
2800 ensureOpen();
2801 return findAll(patternCache.forName(patString));
2802 }
2803
2804 class FindSpliterator extends Spliterators.AbstractSpliterator<MatchResult> {
2805 final Pattern pattern;
2806 int expectedCount = -1;
2807
2808 FindSpliterator(Pattern pattern) {
2809 super(Long.MAX_VALUE,
2810 Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED);
2811 this.pattern = pattern;
2812 }
2813
2814 @Override
2815 public boolean tryAdvance(Consumer<? super MatchResult> cons) {
|