Here you can find the source of stringPositions(StringBuilder toTest, Stream
Parameter | Description |
---|---|
toTest | the StringBuilder being tested |
strings | the strings being tested for |
static Stream<Integer> stringPositions(StringBuilder toTest, Stream<String> strings)
//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(); } }