Skip to content

Commit

Permalink
Merge pull request xyzsd#3 from fdelsert/empty_instance
Browse files Browse the repository at this point in the history
Empty Singleton
  • Loading branch information
xyzsd committed Dec 2, 2023
2 parents b080cff + 2454a6a commit 299054c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/main/java/net/xyzsd/dichotomy/Empty.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@
* as it cannot be instantiated, and extensive null-checking is in place.
* </p>
* <p>
* Instead, the {@link Empty} type can be used. All {@link Empty} types are considered equivalent.
* Instead, the {@link Empty} type can be used.
* </p>
*/
public record Empty() {}
public final class Empty {

private static Empty INSTANCE = new Empty();

private Empty() {
}

public static Empty getInstance() {
return INSTANCE;
}
}
// 'Empty' instead of None to differ from Maybe.None. 'Nil' could be a consideration. or maybe all caps to denote singleton ('NONE')
6 changes: 3 additions & 3 deletions src/main/java/net/xyzsd/dichotomy/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ static <V, E> Result<V, E> ofOK(@NotNull V value) {
*/
@NotNull
static <E> Result<Empty, E> ofOK() {
return new OK<>( new Empty() );
return new OK<>( Empty.getInstance() );
}

/**
Expand All @@ -123,7 +123,7 @@ static <E> Result<Empty, E> ofOK() {
*/
@NotNull
static <V> Result<V, Empty> ofNullable(@Nullable V value) {
return (value == null) ? ofErr( new Empty() ) : ofOK( value );
return (value == null) ? ofErr( Empty.getInstance() ) : ofOK( value );
}

/**
Expand Down Expand Up @@ -152,7 +152,7 @@ static <V> Result<V, Empty> ofNullable(@Nullable V value) {
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
@NotNull
static <V> Result<V, Empty> from(@NotNull Optional<V> opt) {
return opt.<Result<V, Empty>>map( Result::ofOK ).orElseGet( () -> Result.ofErr( new Empty() ) );
return opt.<Result<V, Empty>>map( Result::ofOK ).orElseGet( () -> Result.ofErr( Empty.getInstance() ) );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/xyzsd/dichotomy/trying/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static <V, E extends Exception> Try<V, E> ofOK(@NotNull V value) {
*/
@NotNull
static <E extends Exception> Try<Empty, E> ofOK() {
return new OK<>( new Empty() );
return new OK<>( Empty.getInstance() );
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/net/xyzsd/dichotomy/EmptyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.xyzsd.dichotomy;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class EmptyTest {

@Test
void uniqueness() {
assertSame(Empty.getInstance(), Empty.getInstance());
}

@Test
void equality() {
assertEquals(Empty.getInstance(), Empty.getInstance());
}

}
2 changes: 1 addition & 1 deletion src/test/java/net/xyzsd/dichotomy/ResultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void ofOK() {
void ofOK_NONE() {
final Result<Empty, Object> result = Result.ofOK();
assertEquals( OK.class, result.getClass() );
assertEquals( new Empty(), ((OK<Empty, Object>) result).get() );
assertEquals( Empty.getInstance(), ((OK<Empty, Object>) result).get() );
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/xyzsd/dichotomy/trying/TryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void ofOK() {
void ofOK_NONE() {
final Try<Empty, RuntimeException> result = Try.ofOK();
assertEquals( Try.OK.class, result.getClass() );
assertEquals( new Empty(), ((Try.OK<Empty, RuntimeException>) result).get() );
assertEquals( Empty.getInstance(), ((Try.OK<Empty, RuntimeException>) result).get() );
}


Expand Down

0 comments on commit 299054c

Please sign in to comment.