From 580aa0c9c5ed9753952993c42a6e0c09328adebf Mon Sep 17 00:00:00 2001 From: Alex Klymenko Date: Sun, 25 Aug 2024 22:14:33 +0200 Subject: [PATCH] refactor: `CheckVowels` (#5393) --- .../thealgorithms/strings/CheckVowels.java | 20 +++++++++---------- .../strings/CheckVowelsTest.java | 18 ++++++++--------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/thealgorithms/strings/CheckVowels.java b/src/main/java/com/thealgorithms/strings/CheckVowels.java index 44965cc9..21b536b5 100644 --- a/src/main/java/com/thealgorithms/strings/CheckVowels.java +++ b/src/main/java/com/thealgorithms/strings/CheckVowels.java @@ -1,7 +1,5 @@ package com.thealgorithms.strings; -import java.util.Arrays; -import java.util.HashSet; import java.util.Set; /** @@ -10,24 +8,24 @@ import java.util.Set; * alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order */ public final class CheckVowels { + private static final Set VOWELS = Set.of('a', 'e', 'i', 'o', 'u'); + private CheckVowels() { } - private static final Set VOWELS = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u')); - /** - * Check if a string is has vowels or not + * Checks if a string contains any vowels. * - * @param input a string - * @return {@code true} if given string has vowels, otherwise {@code false} + * @param input a string to check + * @return {@code true} if the given string contains at least one vowel, otherwise {@code false} */ public static boolean hasVowels(String input) { - if (input == null) { + if (input == null || input.isEmpty()) { return false; } - input = input.toLowerCase(); - for (int i = 0; i < input.length(); i++) { - if (VOWELS.contains(input.charAt(i))) { + + for (char c : input.toLowerCase().toCharArray()) { + if (VOWELS.contains(c)) { return true; } } diff --git a/src/test/java/com/thealgorithms/strings/CheckVowelsTest.java b/src/test/java/com/thealgorithms/strings/CheckVowelsTest.java index 713b53f0..ba510aa8 100644 --- a/src/test/java/com/thealgorithms/strings/CheckVowelsTest.java +++ b/src/test/java/com/thealgorithms/strings/CheckVowelsTest.java @@ -1,17 +1,15 @@ package com.thealgorithms.strings; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; -public class CheckVowelsTest { +class CheckVowelsTest { - @Test - public void isVowel() { - assertTrue(CheckVowels.hasVowels("foo")); - assertTrue(CheckVowels.hasVowels("bar")); - assertFalse(CheckVowels.hasVowels("why")); - assertFalse(CheckVowels.hasVowels("myths")); + @ParameterizedTest + @CsvSource({"'foo', true", "'bar', true", "'why', false", "'myths', false", "'', false", "'AEIOU', true", "'bcdfghjklmnpqrstvwxyz', false", "'AeIoU', true"}) + void testHasVowels(String input, boolean expected) { + assertEquals(CheckVowels.hasVowels(input), expected); } }