Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>

<build>
Expand All @@ -52,8 +52,10 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>15</release>
<compilerArgs>
--enable-preview
</compilerArgs>
</configuration>
</plugin>

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/io/github/mooninaut/result/AcceptedResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* limitations under the License.
*/

final class AcceptedResult<VAL> implements Result<VAL> {
final class AcceptedResult<VAL> extends Result<VAL> {
////// Fields //////
private final VAL value;

Expand Down Expand Up @@ -116,7 +116,6 @@ Result<OUT> map(F mapper) {
@Override
public Optional<VAL> toOptional() throws NullPointerException {
return Optional.of(get());

}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/mooninaut/result/EmptyResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* limitations under the License.
*/

final class EmptyResult<VAL> implements Result<VAL> {
public final class EmptyResult<VAL> extends Result<VAL> {

private enum Self {
INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* limitations under the License.
*/

final class RejectedResult<VAL> implements Result<VAL> {
public final class RejectedResult<VAL> extends Result<VAL> {
////// Fields //////
private final Throwable throwable;

Expand Down
54 changes: 27 additions & 27 deletions src/main/java/io/github/mooninaut/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* or if rejected, a Throwable.
* @param <VAL> The type of the included value, if present.
*/
public interface Result<VAL> {
public sealed abstract class Result<VAL> permits AcceptedResult, EmptyResult, RejectedResult {

/**
* Returns an empty Result. May or may not be a singleton.
Expand Down Expand Up @@ -132,31 +132,31 @@ static <VAL> Result<VAL> requireNonNull(VAL val) {
* Is this result Accepted?
* @return true for an accepted (possibly null) value, false if rejected.
*/
boolean isAccepted();
abstract boolean isAccepted();

/**
* Is this Result accepted and non-null?
* @return true for an accepted non-null value, false if null or rejected.
*/
boolean isPresent();
abstract boolean isPresent();

/**
* Is this Result accepted and null?
* @return true if this Result is accepted and null, false if non-null or rejected.
*/
boolean isEmpty();
abstract boolean isEmpty();

/**
* Is this result rejected?
* @return true if this Result is rejected, false if accepted.
*/
boolean isRejected();
abstract boolean isRejected();

/**
* Get the class of the contained value or Empty if this Result contains null or is rejected.
* @return the class of the contained value or Empty if this Result contains null or is rejected.
*/
Optional<Class<?>> getValueType();
abstract Optional<Class<?>> getValueType();

/**
* Perform a checked cast to {@code Result<OUT>} if this Result is accepted.
Expand All @@ -165,59 +165,59 @@ static <VAL> Result<VAL> requireNonNull(VAL val) {
* @param <OUT> The class to checkedCast to.
* @return {@code this} if {@code <IN>} can be checkedCast to {@code OUT}.
*/
<OUT> Result<OUT> checkedCast(Class<OUT> type) throws ClassCastException;
abstract <OUT> Result<OUT> checkedCast(Class<OUT> type) throws ClassCastException;

/**
* Performs an unchecked cast to {@code RESULT<OUT>}.
*/
<OUT> Result<OUT> uncheckedCast();
abstract <OUT> Result<OUT> uncheckedCast();

/**
* Get this Result's value if this Result is accepted, or throws IllegalStateException.
*/
VAL get() throws IllegalStateException;
abstract VAL get() throws IllegalStateException;

/**
* Get this Result's Throwable if this result is Rejected, or throws IllegalStateException.
*/
Throwable getException() throws IllegalStateException;
abstract Throwable getException() throws IllegalStateException;

/**
* Get this Result's value if this Result is accepted, or {@code other} if it is rejected.
*/
VAL orElse(VAL other);
abstract VAL orElse(VAL other);

/**
* Get this Result's value if this Result is accepted, or throws the included Throwable if it is rejected.
*/
VAL orElseThrow() throws Throwable;
abstract VAL orElseThrow() throws Throwable;

/**
* Get this Result's value if this Result is accepted, or throws the included Throwable
* wrapped in a {@link RuntimeException} if it is rejected.
*/
VAL orElseThrowRuntime() throws RuntimeException;
abstract VAL orElseThrowRuntime() throws RuntimeException;

/**
* Throws the included Throwable if it is rejected, otherwise does nothing.
*/
void throwIfRejected() throws Throwable;
abstract void throwIfRejected() throws Throwable;

/**
* Throws the included Throwable wrapped in a {@link RuntimeException} if it is rejected, otherwise does nothing.
*/
void throwRuntimeIfRejected() throws RuntimeException;
abstract void throwRuntimeIfRejected() throws RuntimeException;

/**
* Calls {@code mapper} on IN and returns {@code Result<OUT>}.
* @param <OUT> The return type of {@code mapper}.
* @param mapper A function mapping from type {@code <IN>} to type {@code <OUT>}, possibly throwing an exception.
* @return If this Result accepted, the result of executing {@code mapper} on this Result's value, otherwise {@code this}
*/
<OUT, EF extends ExceptionalFunction<? super VAL, ? extends OUT>>
abstract <OUT, EF extends ExceptionalFunction<? super VAL, ? extends OUT>>
Result<OUT> exMap(EF mapper);

<OUT, EF extends ExceptionalFunction<? super VAL, ? extends OUT>>
abstract <OUT, EF extends ExceptionalFunction<? super VAL, ? extends OUT>>
Result<OUT> exMapChecked(EF mapper, Class<VAL> inClass, Class<OUT> outClass);

/**
Expand All @@ -226,64 +226,64 @@ static <VAL> Result<VAL> requireNonNull(VAL val) {
* @param <OUT> The return type of {@code mapper}
* @return If this Result accepted, the Result of executing {@code mapper} on this Result's value, otherwise {@code this}
*/
<OUT, F extends Function<? super VAL, ? extends OUT>>
abstract <OUT, F extends Function<? super VAL, ? extends OUT>>
Result<OUT> map(F mapper);

/**
* Converts the Result to an {@link Optional}.
* @return the accepted value in an Optional, or, if rejected, an empty Optional.
* @throws NullPointerException if result is accepted, but value is null
*/
Optional<VAL> toOptional() throws NullPointerException;
abstract Optional<VAL> toOptional() throws NullPointerException;

/**
* Converts the Result to an {@link Optional}.
* Does not distinguish between a rejected result and an accepted, but null result. In either case, returns an empty Optional.
* @return the accepted value in an Optional, or, if rejected, an empty Optional.
*/
Optional<VAL> toNullableOptional();
abstract Optional<VAL> toNullableOptional();

/**
* If this Result is accepted, feed the included value to the supplied {@link Consumer}, otherwise, do nothing.
* Chainable.
*/
Result<VAL> ifAccepted(Consumer<? super VAL> consumer);
abstract Result<VAL> ifAccepted(Consumer<? super VAL> consumer);

/**
* If this Result is rejected, feed the included {@link Throwable} to the supplied {@link Consumer}, otherwise, do nothing.
* Chainable.
*/
Result<VAL> ifRejected(Consumer<? super Throwable> rejector);
abstract Result<VAL> ifRejected(Consumer<? super Throwable> rejector);

/**
* Feed this Result's value or {@link Throwable} to the appropriate {@link Consumer}.
* Chainable.
*/
Result<VAL> then(Consumer<? super VAL> consumer, Consumer<? super Throwable> rejector);
abstract Result<VAL> then(Consumer<? super VAL> consumer, Consumer<? super Throwable> rejector);

/**
* Feed this Result's value to the supplied {@link Consumer} if it is present, otherwise feed {@code other} to it.
* Chainable.
*/
Result<VAL> acceptOrElse(Consumer<? super VAL> consumer, VAL other);
abstract Result<VAL> acceptOrElse(Consumer<? super VAL> consumer, VAL other);

/**
* Feed this Result's value to the supplied {@link Consumer} if it is present, otherwise throw this Result's {@link Throwable}.
* Chainable.
*/
Result<VAL> acceptOrElseThrow(Consumer<? super VAL> consumer) throws Throwable;
abstract Result<VAL> acceptOrElseThrow(Consumer<? super VAL> consumer) throws Throwable;

/**
* Feed this Result's value to the supplied {@link Consumer} if it is present, otherwise
* throw this Result's {@link Throwable} wrapped in a {@link RuntimeException}.
* Chainable.
*/
Result<VAL> acceptOrElseThrowRuntime(Consumer<? super VAL> consumer) throws RuntimeException;
abstract Result<VAL> acceptOrElseThrowRuntime(Consumer<? super VAL> consumer) throws RuntimeException;

/**
* Feed this Result's value to the supplied {@link Consumer} if it is present, otherwise
* print the stack trace associated with this Result's {@link Throwable} to standard error.
* Chainable.
*/
Result<VAL> acceptOrPrintStacktrace(Consumer<? super VAL> consumer);
abstract Result<VAL> acceptOrPrintStacktrace(Consumer<? super VAL> consumer);
}