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 <ashley@incredible.org.uk>
This commit is contained in:
parent
8ce908b85e
commit
615f3ba2bc
|
@ -2,6 +2,7 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-lang-oop-methods</artifactId>
|
||||
<name>core-java-lang-oop-methods</name>
|
||||
|
@ -33,7 +34,9 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<lombok.version>1.18.12</lombok.version>
|
||||
<lombok.version>1.18.22</lombok.version>
|
||||
<commons-lang.version>2.6</commons-lang.version>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<equalsverifier.version>3.0.3</equalsverifier.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue