BAEL-5383 code for the use delimiters article

This commit is contained in:
Thibault Faure 2022-04-29 22:13:19 +02:00 committed by thibault.faure
parent 1ce8895891
commit 8fc971657a
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package com.baeldung.java9.delimiters;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
public class DelimiterDemo {
public static List<String> scannerWithDelimiter(String input, String delimiter) {
try (Scanner scan = new Scanner(input)) {
scan.useDelimiter(delimiter);
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
public static List<String> scannerWithDelimiterUsingPattern(String input, Pattern delimiter) {
try (Scanner scan = new Scanner(input)) {
scan.useDelimiter(delimiter);
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
public static List<String> baseScanner(String input) {
try (Scanner scan = new Scanner(input)) {
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
}

View File

@ -0,0 +1,67 @@
package com.baeldung.java9.delimiters;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
class DelimiterDemoUnitTest {
@Test
void givenSimpleCharacterDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiter, "Welcome to Baeldung", "\\s", Arrays.asList("Welcome", "to", "Baeldung"));
}
@Test
void givenStringDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiter, "HelloBaeldungHelloWorld", "Hello", Arrays.asList("Baeldung", "World"));
}
@Test
void givenVariousPossibleDelimiters_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiter, "Welcome to Baeldung.\nThank you for reading.\nThe team", "\n|\\s", Arrays.asList("Welcome", "to", "Baeldung.", "Thank", "you", "for", "reading.", "The", "team"));
}
@Test
void givenWildcardRegexDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiter, "1aaaaaaa2aa3aaa4", "a+", Arrays.asList("1", "2", "3", "4"));
}
@Test
void givenSimpleCharacterDelimiter_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "Welcome to Baeldung", Pattern.compile("\\s"), Arrays.asList("Welcome", "to", "Baeldung"));
}
@Test
void givenStringDelimiter_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "HelloBaeldungHelloWorld", Pattern.compile("Hello"), Arrays.asList("Baeldung", "World"));
}
@Test
void givenVariousPossibleDelimiters_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "Welcome to Baeldung.\nThank you for reading.\nThe team", Pattern.compile("\n|\\s"), Arrays.asList("Welcome", "to", "Baeldung.", "Thank", "you", "for", "reading.", "The", "team"));
}
@Test
void givenWildcardRegexDelimiters_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
checkOutput(DelimiterDemo::scannerWithDelimiterUsingPattern, "1aaaaaaa2aa3aaa4", Pattern.compile("a*"), Arrays.asList("1", "2", "3", "4"));
}
void checkOutput(BiFunction<String, String, List<String>> function, String input, String delimiter, List<String> expectedOutput) {
assertEquals(expectedOutput, function.apply(input, delimiter));
}
void checkOutput(BiFunction<String, Pattern, List<String>> function, String input, Pattern delimiter, List<String> expectedOutput) {
assertEquals(expectedOutput, function.apply(input, delimiter));
}
@Test
void whenBaseScanner_ThenWhitespacesAreUsedAsDelimiters() {
assertEquals(List.of("Welcome", "at", "Baeldung"), DelimiterDemo.baseScanner("Welcome at Baeldung"));
}
}