diff --git a/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/delimiters/DelimiterDemo.java b/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/delimiters/DelimiterDemo.java new file mode 100644 index 0000000000..edc0b91d02 --- /dev/null +++ b/core-java-modules/core-java-9/src/main/java/com/baeldung/java9/delimiters/DelimiterDemo.java @@ -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 scannerWithDelimiter(String input, String delimiter) { + try (Scanner scan = new Scanner(input)) { + scan.useDelimiter(delimiter); + List result = new ArrayList(); + scan.forEachRemaining(result::add); + return result; + } + } + + public static List scannerWithDelimiterUsingPattern(String input, Pattern delimiter) { + try (Scanner scan = new Scanner(input)) { + scan.useDelimiter(delimiter); + List result = new ArrayList(); + scan.forEachRemaining(result::add); + return result; + } + } + + public static List baseScanner(String input) { + try (Scanner scan = new Scanner(input)) { + List result = new ArrayList(); + scan.forEachRemaining(result::add); + return result; + } + } + +} diff --git a/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/delimiters/DelimiterDemoUnitTest.java b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/delimiters/DelimiterDemoUnitTest.java new file mode 100644 index 0000000000..1c1fffe362 --- /dev/null +++ b/core-java-modules/core-java-9/src/test/java/com/baeldung/java9/delimiters/DelimiterDemoUnitTest.java @@ -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> function, String input, String delimiter, List expectedOutput) { + assertEquals(expectedOutput, function.apply(input, delimiter)); + } + + void checkOutput(BiFunction> function, String input, Pattern delimiter, List expectedOutput) { + assertEquals(expectedOutput, function.apply(input, delimiter)); + } + + @Test + void whenBaseScanner_ThenWhitespacesAreUsedAsDelimiters() { + assertEquals(List.of("Welcome", "at", "Baeldung"), DelimiterDemo.baseScanner("Welcome at Baeldung")); + } + +}