Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Empty class #36

Merged
merged 10 commits into from
Sep 10, 2024
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.zeplinko</groupId>
<artifactId>commons-lang-ext</artifactId>
<version>1.1.0-BETA1</version>
<version>1.1.0</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down Expand Up @@ -73,6 +73,7 @@
<configuration>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<addOutputDirectory>false</addOutputDirectory>
<outputDirectory>${project.build.directory}/delombok</outputDirectory>
</configuration>
<executions>
<execution>
Expand All @@ -89,7 +90,7 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.3</version>
<configuration>
<sourcepath>${project.build.directory}/generated-sources/delombok</sourcepath>
<sourcepath>${project.build.directory}/delombok</sourcepath>
</configuration>
<executions>
<execution>
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/org/zeplinko/commons/lang/ext/core/Empty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.zeplinko.commons.lang.ext.core;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
* A singleton class representing an empty placeholder. This class is used as a
* substitute for {@link Void} when a non-null placeholder is required.
* <p>
* The {@link Empty} class provides a singleton instance that can be used in
* scenarios where a method or an operation requires a placeholder object, but
* the usage of {@code null} is not desired or allowed.
* </p>
* <p>
* Example usage:
* </p>
*
* <pre>
* public Result&lt;String, Empty&gt; performOperation() {
* // Perform some operation
* return Result.error(Empty.getInstance());
* }
* </pre>
*
* <p>
* This class cannot be instantiated from outside as its constructor is private.
* The only way to get an instance of this class is through the
* {@link #getInstance()} method.
* </p>
*
* @see Void
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Empty {
@SuppressWarnings("InstantiationOfUtilityClass")
private static final Empty INSTANCE = new Empty();

public static Empty getInstance() {
return Empty.INSTANCE;
}
}
47 changes: 47 additions & 0 deletions src/test/java/org/zeplinko/commons/lang/ext/core/EmptyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.zeplinko.commons.lang.ext.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class EmptyTest {
/**
* Test to ensure that the {@link Empty} class returns a non-null instance.
*/
@Test
void testGetInstanceIsNotNull() {
Assertions.assertNotNull(Empty.getInstance(), "Empty.getInstance() should not return null");
}

/**
* Test to ensure that {@link Empty#getInstance()} always returns the same
* instance (singleton pattern).
*/
@Test
void testGetInstanceReturnsSameInstance() {
Empty instance1 = Empty.getInstance();
Empty instance2 = Empty.getInstance();
Assertions.assertSame(instance1, instance2, "Empty.getInstance() should always return the same instance");
}

/**
* Test to ensure that the {@link Empty} class cannot be instantiated from
* outside.
*/
@Test
@SuppressWarnings("all")
void testPrivateConstructor() {
try {
Empty.class.getDeclaredConstructor().setAccessible(true);
Empty empty = Empty.class.getDeclaredConstructor().newInstance();
Assertions.fail("Instantiation via reflection should not be allowed");
} catch (Exception e) {
Assertions.assertTrue(
e instanceof IllegalAccessException ||
e.getCause() instanceof IllegalAccessException ||
e.getCause() instanceof SecurityException,
"Expected an IllegalAccessException when trying to instantiate via reflection"
);
}
}

}