From 615f3ba2bc8fd93ef8d4fee954a2c4b1061b78a2 Mon Sep 17 00:00:00 2001 From: Daniel Strmecki Date: Thu, 2 Dec 2021 14:43:55 +0100 Subject: [PATCH] Feature/bael 5133 utility class constructor (#11421) * BAEL-5133: initial commit * BAEL-5133: better example * BAEL-5133: add Lombok examples * BAEL-5133: add interface and enum examples * BAEL-5133: add exception * BAEL-5133: add suppress warning * BAEL-5133: dummy Co-authored-by: ashleyfrieze --- .../core-java-lang-oop-methods/pom.xml | 5 +++- .../com/baeldung/utilities/StringUtils.java | 17 +++++++++++ .../alternatives/StringUtilsEnum.java | 13 +++++++++ .../alternatives/StringUtilsInterface.java | 13 +++++++++ .../StringUtilsWithNoArgsConstructor.java | 17 +++++++++++ .../lombok/StringUtilsWithUtilityClass.java | 16 ++++++++++ .../warning/StringUtilsSuppressWarning.java | 14 +++++++++ .../utilities/StringUtilsUnitTest.java | 28 ++++++++++++++++++ .../alternatives/StringUtilsEnumUnitTest.java | 29 +++++++++++++++++++ .../StringUtilsInterfaceUnitTest.java | 29 +++++++++++++++++++ ...ingUtilsWithNoArgsConstructorUnitTest.java | 29 +++++++++++++++++++ .../StringUtilsWithUtilityClassUnitTest.java | 29 +++++++++++++++++++ ...ilsStringUtilsSuppressWarningUnitTest.java | 29 +++++++++++++++++++ 13 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/StringUtils.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/alternatives/StringUtilsEnum.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/alternatives/StringUtilsInterface.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/lombok/StringUtilsWithNoArgsConstructor.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/lombok/StringUtilsWithUtilityClass.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/warning/StringUtilsSuppressWarning.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/StringUtilsUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/alternatives/StringUtilsEnumUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/alternatives/StringUtilsInterfaceUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/lombok/StringUtilsWithNoArgsConstructorUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/lombok/StringUtilsWithUtilityClassUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/warning/StringUtilsStringUtilsSuppressWarningUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-methods/pom.xml b/core-java-modules/core-java-lang-oop-methods/pom.xml index e57b51b7bd..e41c56fc6f 100644 --- a/core-java-modules/core-java-lang-oop-methods/pom.xml +++ b/core-java-modules/core-java-lang-oop-methods/pom.xml @@ -2,6 +2,7 @@ + 4.0.0 core-java-lang-oop-methods core-java-lang-oop-methods @@ -33,7 +34,9 @@ - 1.18.12 + 1.18.22 + 2.6 + 3.10.0 3.0.3 diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/StringUtils.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/StringUtils.java new file mode 100644 index 0000000000..1e94281d21 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/StringUtils.java @@ -0,0 +1,17 @@ +package com.baeldung.utilities; + +public final class StringUtils { + + private StringUtils() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + + public static boolean isEmpty(String source) { + return source == null || source.length() == 0; + } + + public static String wrap(String source, String wrapWith) { + return isEmpty(source) ? source : wrapWith + source + wrapWith; + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/alternatives/StringUtilsEnum.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/alternatives/StringUtilsEnum.java new file mode 100644 index 0000000000..2fa96874c0 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/alternatives/StringUtilsEnum.java @@ -0,0 +1,13 @@ +package com.baeldung.utilities.alternatives; + +public enum StringUtilsEnum {; + + public static boolean isEmpty(String source) { + return source == null || source.length() == 0; + } + + public static String wrap(String source, String wrapWith) { + return isEmpty(source) ? source : wrapWith + source + wrapWith; + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/alternatives/StringUtilsInterface.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/alternatives/StringUtilsInterface.java new file mode 100644 index 0000000000..b6afdfff5d --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/alternatives/StringUtilsInterface.java @@ -0,0 +1,13 @@ +package com.baeldung.utilities.alternatives; + +public interface StringUtilsInterface { + + static boolean isEmpty(String source) { + return source == null || source.length() == 0; + } + + static String wrap(String source, String wrapWith) { + return isEmpty(source) ? source : wrapWith + source + wrapWith; + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/lombok/StringUtilsWithNoArgsConstructor.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/lombok/StringUtilsWithNoArgsConstructor.java new file mode 100644 index 0000000000..38e60e0216 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/lombok/StringUtilsWithNoArgsConstructor.java @@ -0,0 +1,17 @@ +package com.baeldung.utilities.lombok; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access= AccessLevel.PRIVATE) +public final class StringUtilsWithNoArgsConstructor { + + public static boolean isEmpty(String source) { + return source == null || source.length() == 0; + } + + public static String wrap(String source, String wrapWith) { + return isEmpty(source) ? source : wrapWith + source + wrapWith; + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/lombok/StringUtilsWithUtilityClass.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/lombok/StringUtilsWithUtilityClass.java new file mode 100644 index 0000000000..56718f8ce4 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/lombok/StringUtilsWithUtilityClass.java @@ -0,0 +1,16 @@ +package com.baeldung.utilities.lombok; + +import lombok.experimental.UtilityClass; + +@UtilityClass +public class StringUtilsWithUtilityClass { + + public static boolean isEmpty(String source) { + return source == null || source.length() == 0; + } + + public static String wrap(String source, String wrapWith) { + return isEmpty(source) ? source : wrapWith + source + wrapWith; + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/warning/StringUtilsSuppressWarning.java b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/warning/StringUtilsSuppressWarning.java new file mode 100644 index 0000000000..d8354f993d --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/main/java/com/baeldung/utilities/warning/StringUtilsSuppressWarning.java @@ -0,0 +1,14 @@ +package com.baeldung.utilities.warning; + +@SuppressWarnings("java:S1118") +public final class StringUtilsSuppressWarning { + + public static boolean isEmpty(String source) { + return source == null || source.length() == 0; + } + + public static String wrap(String source, String wrapWith) { + return isEmpty(source) ? source : wrapWith + source + wrapWith; + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/StringUtilsUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/StringUtilsUnitTest.java new file mode 100644 index 0000000000..ca8b270d8d --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/StringUtilsUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.utilities; + +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.*; + +class StringUtilsUnitTest { + + @Test + void givenAnEmptyString_whenCallingIsEmpty_thenResultIsTrue() { + assertThat(StringUtils.isEmpty("")).isTrue(); + } + + @Test + void givenNonEmptyString_whenCallingIsEmpty_thenResultIsFalse() { + assertThat(StringUtils.isEmpty("asd")).isFalse(); + } + + @Test + void givenAnEmptyString_whenCallingWrap_thenResultIsAnEmptyString() { + assertThat(StringUtils.wrap("", "wrapper")).isEmpty(); + } + + @Test + void givenNonEmptyString_whenCallingWrap_thenResultIsWrappedString() { + assertThat(StringUtils.wrap("asd", "wrapper")).isEqualTo("wrapperasdwrapper"); + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/alternatives/StringUtilsEnumUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/alternatives/StringUtilsEnumUnitTest.java new file mode 100644 index 0000000000..4d948c44ae --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/alternatives/StringUtilsEnumUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.utilities.alternatives; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class StringUtilsEnumUnitTest { + + @Test + void givenAnEmptyString_whenCallingIsEmpty_thenResultIsTrue() { + assertThat(StringUtilsEnum.isEmpty("")).isTrue(); + } + + @Test + void givenNonEmptyString_whenCallingIsEmpty_thenResultIsFalse() { + assertThat(StringUtilsEnum.isEmpty("asd")).isFalse(); + } + + @Test + void givenAnEmptyString_whenCallingWrap_thenResultIsAnEmptyString() { + assertThat(StringUtilsEnum.wrap("", "wrapper")).isEmpty(); + } + + @Test + void givenNonEmptyString_whenCallingWrap_thenResultIsWrappedString() { + assertThat(StringUtilsEnum.wrap("asd", "wrapper")).isEqualTo("wrapperasdwrapper"); + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/alternatives/StringUtilsInterfaceUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/alternatives/StringUtilsInterfaceUnitTest.java new file mode 100644 index 0000000000..600f6c2156 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/alternatives/StringUtilsInterfaceUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.utilities.alternatives; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class StringUtilsInterfaceUnitTest { + + @Test + void givenAnEmptyString_whenCallingIsEmpty_thenResultIsTrue() { + assertThat(StringUtilsInterface.isEmpty("")).isTrue(); + } + + @Test + void givenNonEmptyString_whenCallingIsEmpty_thenResultIsFalse() { + assertThat(StringUtilsInterface.isEmpty("asd")).isFalse(); + } + + @Test + void givenAnEmptyString_whenCallingWrap_thenResultIsAnEmptyString() { + assertThat(StringUtilsInterface.wrap("", "wrapper")).isEmpty(); + } + + @Test + void givenNonEmptyString_whenCallingWrap_thenResultIsWrappedString() { + assertThat(StringUtilsInterface.wrap("asd", "wrapper")).isEqualTo("wrapperasdwrapper"); + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/lombok/StringUtilsWithNoArgsConstructorUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/lombok/StringUtilsWithNoArgsConstructorUnitTest.java new file mode 100644 index 0000000000..b110c31080 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/lombok/StringUtilsWithNoArgsConstructorUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.utilities.lombok; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class StringUtilsWithNoArgsConstructorUnitTest { + + @Test + void givenAnEmptyString_whenCallingIsEmpty_thenResultIsTrue() { + assertThat(StringUtilsWithNoArgsConstructor.isEmpty("")).isTrue(); + } + + @Test + void givenNonEmptyString_whenCallingIsEmpty_thenResultIsFalse() { + assertThat(StringUtilsWithNoArgsConstructor.isEmpty("asd")).isFalse(); + } + + @Test + void givenAnEmptyString_whenCallingWrap_thenResultIsAnEmptyString() { + assertThat(StringUtilsWithNoArgsConstructor.wrap("", "wrapper")).isEmpty(); + } + + @Test + void givenNonEmptyString_whenCallingWrap_thenResultIsWrappedString() { + assertThat(StringUtilsWithNoArgsConstructor.wrap("asd", "wrapper")).isEqualTo("wrapperasdwrapper"); + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/lombok/StringUtilsWithUtilityClassUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/lombok/StringUtilsWithUtilityClassUnitTest.java new file mode 100644 index 0000000000..c2f5003ada --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/lombok/StringUtilsWithUtilityClassUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.utilities.lombok; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class StringUtilsWithUtilityClassUnitTest { + + @Test + void givenAnEmptyString_whenCallingIsEmpty_thenResultIsTrue() { + assertThat(StringUtilsWithUtilityClass.isEmpty("")).isTrue(); + } + + @Test + void givenNonEmptyString_whenCallingIsEmpty_thenResultIsFalse() { + assertThat(StringUtilsWithUtilityClass.isEmpty("asd")).isFalse(); + } + + @Test + void givenAnEmptyString_whenCallingWrap_thenResultIsAnEmptyString() { + assertThat(StringUtilsWithUtilityClass.wrap("", "wrapper")).isEmpty(); + } + + @Test + void givenNonEmptyString_whenCallingWrap_thenResultIsWrappedString() { + assertThat(StringUtilsWithUtilityClass.wrap("asd", "wrapper")).isEqualTo("wrapperasdwrapper"); + } + +} diff --git a/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/warning/StringUtilsStringUtilsSuppressWarningUnitTest.java b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/warning/StringUtilsStringUtilsSuppressWarningUnitTest.java new file mode 100644 index 0000000000..646da08cc4 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-methods/src/test/java/com/baeldung/utilities/warning/StringUtilsStringUtilsSuppressWarningUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.utilities.warning; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class StringUtilsStringUtilsSuppressWarningUnitTest { + + @Test + void givenAnEmptyString_whenCallingIsEmpty_thenResultIsTrue() { + assertThat(StringUtilsSuppressWarning.isEmpty("")).isTrue(); + } + + @Test + void givenNonEmptyString_whenCallingIsEmpty_thenResultIsFalse() { + assertThat(StringUtilsSuppressWarning.isEmpty("asd")).isFalse(); + } + + @Test + void givenAnEmptyString_whenCallingWrap_thenResultIsAnEmptyString() { + assertThat(StringUtilsSuppressWarning.wrap("", "wrapper")).isEmpty(); + } + + @Test + void givenNonEmptyString_whenCallingWrap_thenResultIsWrappedString() { + assertThat(StringUtilsSuppressWarning.wrap("asd", "wrapper")).isEqualTo("wrapperasdwrapper"); + } + +}