From 6bbef27bc74d343ec0f9cba60f72a5707fc3071a Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Wed, 31 Jan 2024 23:27:16 +0100 Subject: [PATCH 1/5] [count-upper-lower] count upper/lowercase letters --- .../core-java-string-operations-8/README.md | 4 +- .../core-java-string-operations-8/pom.xml | 106 ++++++------------ .../CountUpperAndLowercaseCharsUnitTest.java | 85 ++++++++++++++ 3 files changed, 121 insertions(+), 74 deletions(-) create mode 100644 core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java diff --git a/core-java-modules/core-java-string-operations-8/README.md b/core-java-modules/core-java-string-operations-8/README.md index 2dce44d217..6598a4b9cc 100644 --- a/core-java-modules/core-java-string-operations-8/README.md +++ b/core-java-modules/core-java-string-operations-8/README.md @@ -1,2 +1,2 @@ - -### Relevant Articles: +### Relevant Articles: +>>>>>>> be317465c ([count-upper-lower] count upper/lowercase letters) diff --git a/core-java-modules/core-java-string-operations-8/pom.xml b/core-java-modules/core-java-string-operations-8/pom.xml index 2495abccf8..a2f97a93d9 100644 --- a/core-java-modules/core-java-string-operations-8/pom.xml +++ b/core-java-modules/core-java-string-operations-8/pom.xml @@ -1,72 +1,34 @@ - - - 4.0.0 - core-java-string-operations-8 - core-java-string-operations-8 - jar - - - com.baeldung.core-java-modules - core-java-modules - 0.0.1-SNAPSHOT - - - - - org.apache.commons - commons-lang3 - ${apache.commons.lang3.version} - - - org.apache.commons - commons-text - ${commons-text.version} - - - org.liquibase - liquibase-core - 4.9.1 - test - - - org.junit.jupiter - junit-jupiter - 5.8.1 - test - - - org.liquibase - liquibase-core - 4.9.1 - test - - - junit - junit - 4.13.2 - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - ${maven.compiler.source} - ${maven.compiler.target} - - - - - - - 11 - 11 - 3.13.0 - 1.10.0 - - - \ No newline at end of file + + + 4.0.0 + core-java-string-operations-8 + core-java-string-operations-8 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + 11 + 11 + + + diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java new file mode 100644 index 0000000000..cfef8d53ad --- /dev/null +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java @@ -0,0 +1,85 @@ +package com.baeldung.countupperandlowercasechars; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CountUpperAndLowercaseCharsUnitTest { + private static final String MY_STRING = "Hi, Welcome to Baeldung! Let's count letters!"; + private static final LetterCount EXPECTED = new LetterCount(4, 31); + + @Test + void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() { + int upperCnt = 0; + int lowerCnt = 0; + for (char c : MY_STRING.toCharArray()) { + if (c >= 'A' && c <= 'Z') { + upperCnt++; + } + if (c >= 'a' && c <= 'z') { + lowerCnt++; + } + } + LetterCount result = new LetterCount(upperCnt, lowerCnt); + assertEquals(EXPECTED, result); + } + + @Test + void whenUsingCharacterIsLowerOrUpperCase_thenGetExpectedResult() { + int upperCnt = 0; + int lowerCnt = 0; + for (char c : MY_STRING.toCharArray()) { + if (Character.isUpperCase(c)) { + upperCnt++; + } + if (Character.isLowerCase(c)) { + lowerCnt++; + } + } + LetterCount result = new LetterCount(upperCnt, lowerCnt); + assertEquals(EXPECTED, result); + } + + @Test + void whenUsingStreamFilterAndCount_thenGetExpectedResult() { + LetterCount result = new LetterCount( + (int) MY_STRING.chars().filter(Character::isUpperCase).count(), + (int) MY_STRING.chars().filter(Character::isLowerCase).count() + ); + assertEquals(EXPECTED, result); + } +} + +class LetterCount { + private int uppercaseCnt; + private int lowercaseCnt; + + public LetterCount(int uppercaseCnt, int lowercaseCnt) { + this.uppercaseCnt = uppercaseCnt; + this.lowercaseCnt = lowercaseCnt; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof LetterCount)) { + return false; + } + + LetterCount that = (LetterCount) o; + + if (uppercaseCnt != that.uppercaseCnt) { + return false; + } + return lowercaseCnt == that.lowercaseCnt; + } + + @Override + public int hashCode() { + int result = uppercaseCnt; + result = 31 * result + lowercaseCnt; + return result; + } +} \ No newline at end of file From ee1225360970d9a7da49b9599b8f80fbfd86c609 Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Fri, 2 Feb 2024 17:13:57 +0100 Subject: [PATCH 2/5] [count-upper-lower] review comments related changes --- .../CountUpperAndLowercaseCharsUnitTest.java | 44 +++++++------------ 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java index cfef8d53ad..a05b067f5a 100644 --- a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java @@ -6,7 +6,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class CountUpperAndLowercaseCharsUnitTest { private static final String MY_STRING = "Hi, Welcome to Baeldung! Let's count letters!"; - private static final LetterCount EXPECTED = new LetterCount(4, 31); @Test void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() { @@ -21,7 +20,8 @@ public class CountUpperAndLowercaseCharsUnitTest { } } LetterCount result = new LetterCount(upperCnt, lowerCnt); - assertEquals(EXPECTED, result); + assertEquals(4, result.getUppercaseCount()); + assertEquals(31, result.getLowercaseCount()); } @Test @@ -37,7 +37,8 @@ public class CountUpperAndLowercaseCharsUnitTest { } } LetterCount result = new LetterCount(upperCnt, lowerCnt); - assertEquals(EXPECTED, result); + assertEquals(4, result.getUppercaseCount()); + assertEquals(31, result.getLowercaseCount()); } @Test @@ -46,40 +47,25 @@ public class CountUpperAndLowercaseCharsUnitTest { (int) MY_STRING.chars().filter(Character::isUpperCase).count(), (int) MY_STRING.chars().filter(Character::isLowerCase).count() ); - assertEquals(EXPECTED, result); + assertEquals(4, result.getUppercaseCount()); + assertEquals(31, result.getLowercaseCount()); } } class LetterCount { - private int uppercaseCnt; - private int lowercaseCnt; + private int uppercaseCount; + private int lowercaseCount; - public LetterCount(int uppercaseCnt, int lowercaseCnt) { - this.uppercaseCnt = uppercaseCnt; - this.lowercaseCnt = lowercaseCnt; + public LetterCount(int uppercaseCount, int lowercaseCount) { + this.uppercaseCount = uppercaseCount; + this.lowercaseCount = lowercaseCount; } - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof LetterCount)) { - return false; - } - - LetterCount that = (LetterCount) o; - - if (uppercaseCnt != that.uppercaseCnt) { - return false; - } - return lowercaseCnt == that.lowercaseCnt; + public int getUppercaseCount() { + return uppercaseCount; } - @Override - public int hashCode() { - int result = uppercaseCnt; - result = 31 * result + lowercaseCnt; - return result; + public int getLowercaseCount() { + return lowercaseCount; } } \ No newline at end of file From 746c6f5083ef041dbaaf5d8dbc68721a5b3fc4b0 Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Fri, 2 Feb 2024 17:30:15 +0100 Subject: [PATCH 3/5] [count-upper-lower] some renamings were missing --- .../CountUpperAndLowercaseCharsUnitTest.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java index a05b067f5a..b1ed82ea34 100644 --- a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java @@ -9,34 +9,34 @@ public class CountUpperAndLowercaseCharsUnitTest { @Test void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() { - int upperCnt = 0; - int lowerCnt = 0; + int upperCount = 0; + int lowerCount = 0; for (char c : MY_STRING.toCharArray()) { if (c >= 'A' && c <= 'Z') { - upperCnt++; + upperCount++; } if (c >= 'a' && c <= 'z') { - lowerCnt++; + lowerCount++; } } - LetterCount result = new LetterCount(upperCnt, lowerCnt); + LetterCount result = new LetterCount(upperCount, lowerCount); assertEquals(4, result.getUppercaseCount()); assertEquals(31, result.getLowercaseCount()); } @Test void whenUsingCharacterIsLowerOrUpperCase_thenGetExpectedResult() { - int upperCnt = 0; - int lowerCnt = 0; + int upperCount = 0; + int lowerCount = 0; for (char c : MY_STRING.toCharArray()) { if (Character.isUpperCase(c)) { - upperCnt++; + upperCount++; } if (Character.isLowerCase(c)) { - lowerCnt++; + lowerCount++; } } - LetterCount result = new LetterCount(upperCnt, lowerCnt); + LetterCount result = new LetterCount(upperCount, lowerCount); assertEquals(4, result.getUppercaseCount()); assertEquals(31, result.getLowercaseCount()); } From a7a97689ba5d4e530c0f7ec3c1602a5d94dbc329 Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Sat, 3 Feb 2024 00:28:22 +0100 Subject: [PATCH 4/5] [count-upper-lower] add unicode char tests --- .../CountUpperAndLowercaseCharsUnitTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java index b1ed82ea34..9137906a5a 100644 --- a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java @@ -3,6 +3,7 @@ package com.baeldung.countupperandlowercasechars; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; public class CountUpperAndLowercaseCharsUnitTest { private static final String MY_STRING = "Hi, Welcome to Baeldung! Let's count letters!"; @@ -50,6 +51,13 @@ public class CountUpperAndLowercaseCharsUnitTest { assertEquals(4, result.getUppercaseCount()); assertEquals(31, result.getLowercaseCount()); } + + + @Test + void whenUsingIsUpperCaseAndIsLowerCase_thenUnicodeCharactersCanBeChecked() { + assertTrue(Character.isLowerCase('ä')); + assertTrue(Character.isUpperCase('Ä')); + } } class LetterCount { From 9cbccd4b2fb982e4e1c152fca3a1a30ace3a5766 Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Fri, 9 Feb 2024 11:19:53 +0100 Subject: [PATCH 5/5] [count-upper-lower] extract the impl. from the test methods --- .../core-java-string-operations-8/README.md | 1 - .../CountUpperAndLowercaseCharsUnitTest.java | 73 +++++++++++-------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/core-java-modules/core-java-string-operations-8/README.md b/core-java-modules/core-java-string-operations-8/README.md index 6598a4b9cc..7d843af9ea 100644 --- a/core-java-modules/core-java-string-operations-8/README.md +++ b/core-java-modules/core-java-string-operations-8/README.md @@ -1,2 +1 @@ ### Relevant Articles: ->>>>>>> be317465c ([count-upper-lower] count upper/lowercase letters) diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java index 9137906a5a..64e8b7ebbd 100644 --- a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java @@ -9,50 +9,26 @@ public class CountUpperAndLowercaseCharsUnitTest { private static final String MY_STRING = "Hi, Welcome to Baeldung! Let's count letters!"; @Test - void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() { - int upperCount = 0; - int lowerCount = 0; - for (char c : MY_STRING.toCharArray()) { - if (c >= 'A' && c <= 'Z') { - upperCount++; - } - if (c >= 'a' && c <= 'z') { - lowerCount++; - } - } - LetterCount result = new LetterCount(upperCount, lowerCount); + void whenUsingCountByCharacterRange_thenGetExpectedResult() { + LetterCount result = LetterCount.countByCharacterRange(MY_STRING); assertEquals(4, result.getUppercaseCount()); assertEquals(31, result.getLowercaseCount()); } @Test - void whenUsingCharacterIsLowerOrUpperCase_thenGetExpectedResult() { - int upperCount = 0; - int lowerCount = 0; - for (char c : MY_STRING.toCharArray()) { - if (Character.isUpperCase(c)) { - upperCount++; - } - if (Character.isLowerCase(c)) { - lowerCount++; - } - } - LetterCount result = new LetterCount(upperCount, lowerCount); + void whenUsingCountByCharacterIsLowerOrUpperCase_thenGetExpectedResult() { + LetterCount result = LetterCount.countByCharacterIsUpperLower(MY_STRING); assertEquals(4, result.getUppercaseCount()); assertEquals(31, result.getLowercaseCount()); } @Test - void whenUsingStreamFilterAndCount_thenGetExpectedResult() { - LetterCount result = new LetterCount( - (int) MY_STRING.chars().filter(Character::isUpperCase).count(), - (int) MY_STRING.chars().filter(Character::isLowerCase).count() - ); + void whenUsingCountByStreamApi_thenGetExpectedResult() { + LetterCount result = LetterCount.countByStreamAPI(MY_STRING); assertEquals(4, result.getUppercaseCount()); assertEquals(31, result.getLowercaseCount()); } - @Test void whenUsingIsUpperCaseAndIsLowerCase_thenUnicodeCharactersCanBeChecked() { assertTrue(Character.isLowerCase('ä')); @@ -64,11 +40,46 @@ class LetterCount { private int uppercaseCount; private int lowercaseCount; - public LetterCount(int uppercaseCount, int lowercaseCount) { + private LetterCount(int uppercaseCount, int lowercaseCount) { this.uppercaseCount = uppercaseCount; this.lowercaseCount = lowercaseCount; } + public static LetterCount countByCharacterRange(String input) { + int upperCount = 0; + int lowerCount = 0; + for (char c : input.toCharArray()) { + if (c >= 'A' && c <= 'Z') { + upperCount++; + } + if (c >= 'a' && c <= 'z') { + lowerCount++; + } + } + return new LetterCount(upperCount, lowerCount); + } + + public static LetterCount countByCharacterIsUpperLower(String input) { + int upperCount = 0; + int lowerCount = 0; + for (char c : input.toCharArray()) { + if (Character.isUpperCase(c)) { + upperCount++; + } + if (Character.isLowerCase(c)) { + lowerCount++; + } + } + return new LetterCount(upperCount, lowerCount); + } + + public static LetterCount countByStreamAPI(String input) { + return new LetterCount( + (int) input.chars().filter(Character::isUpperCase).count(), + (int) input.chars().filter(Character::isLowerCase).count() + ); + } + public int getUppercaseCount() { return uppercaseCount; }