Java Stream Operation stringPositions(StringBuilder toTest, Stream strings)

Here you can find the source of stringPositions(StringBuilder toTest, Stream strings)

Description

Tests the supplied StringBuilder for the presence of strings , and returns the positions of matching characters.

License

Apache License

Parameter

Parameter Description
toTest the StringBuilder being tested
strings the strings being tested for

Return

a Stream containing the positions (zero-indexed) of the characters matched by the strings

Declaration

static Stream<Integer> stringPositions(StringBuilder toTest, Stream<String> strings) 

Method Source Code

    //package com.java2s;
    /*/*  w w  w. j  a  v a2s  .  co m*/
     * Copyright 2017 Johns Hopkins University
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *     http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    import java.util.stream.Stream;

    public class Main {
        /**
         * Tests the supplied {@code StringBuilder} for the presence of {@code strings}, and returns the positions of
         * matching characters.
         *
         * @param toTest the {@code StringBuilder} being tested
         * @param strings the strings being tested for
         * @return a {@code Stream<Integer>} containing the positions (zero-indexed) of the characters matched by the
         *         {@code strings}
         */
        static Stream<Integer> stringPositions(StringBuilder toTest, Stream<String> strings) {
    if (toTest == null) {
        throw new IllegalArgumentException("StringBuilder must not be null.");
    }

    if (strings == null) {
        throw new IllegalArgumentException("Invalid strings must not be null");

    }
    Stream.Builder<Integer> positionStream = Stream.builder();
    strings.forEach(invalidString -> {
        int index = -1;
        while ((index = toTest.indexOf(invalidString, index)) > -1) {
            int endIndex = -1;
            for (int i = index; i < (index + invalidString.length()); i++) {
                positionStream.accept(i);
                endIndex = i;
            }
            index = endIndex + 1;
        }
    });

    return positionStream.build();
}
    }

Related

  1. replaceStr(StringBuilder original, char replacementChar, Stream stringsToReplace)
  2. reverse(Stream stream)
  3. reverse(Stream input)
  4. startsWith(Stream stream, Iterable iterable)
  5. startsWith(Stream stream, Stream stream2)
  6. toList(final Stream stream)
  7. toParameter(Stream input, Class exp)
  8. toString(IntStream s)
  9. toString(LongStream ls)