Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Butov committed Feb 3, 2020
1 parent 47f5782 commit 8e7a886
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.function.Consumer;
import java.util.function.Function;

import org.sugarcubes.check.Checks;

/**
* Rex means "runtime exceptions".
*
Expand Down Expand Up @@ -53,7 +55,7 @@ public static <T extends Throwable> Rex<T> of(T error) {
private final T error;

public Rex(T error) {
this.error = error;
this.error = Checks.arg().notNull(error, "Error must be not null.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.function.Function;
import java.util.function.Supplier;

import org.sugarcubes.executable.XCallable;
import org.sugarcubes.executable.XRunnable;
import org.sugarcubes.rex.Rex;

/**
Expand Down Expand Up @@ -97,19 +99,7 @@ static <T> Executable<T, Exception> of(Callable<T> callable) {
* {@link Runnable} -> {@link Executable}
*/
static <T> Executable<T, RuntimeException> of(Runnable runnable) {
return ofx(XRunnable.of(runnable));
}

/**
* {@link Supplier} -> {@link Executable}
*/
static <T> Executable<T, RuntimeException> of(Supplier<T> supplier) {
return new Executable<T, RuntimeException>() {
@Override
public T call() throws RuntimeException {
return supplier.get();
}
};
return ofx(runnable::run);
}

/**
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.sugarcubes.executable;

import java.util.concurrent.Callable;

import org.sugarcubes.rex.Rex;

/**
* The variant of {@link Callable} which throws specific exception instead of {@link Exception}.
* To be used as method reference.
*
* @author Maxim Butov
*/
@FunctionalInterface
public interface XCallable<T, E extends Throwable> extends XExecutable<T> {

@Override
default T execute() {
try {
return call();
}
catch (Throwable e) {
throw Rex.rethrowAsRuntime(e);
}
}

/**
* Computes a result, or throws an exception if unable to do so.
*
* @return computed result
*
* @throws E if unable to compute a result
*/
T call() throws E;

@Override
default Callable<T> asCallable() {
return () -> {
try {
return call();
}
catch (Error | Exception e) {
throw e;
}
catch (Throwable e) {
throw Rex.rethrowAsRuntime(e);
}
};
}

/**
* {@link XCallable} of method reference
*/
static <T, E extends Throwable> XCallable<T, E> of(XCallable<T, E> callable) {
return callable;
}

/**
* {@link XRunnable} -> {@link XCallable}
*/
static <T, E extends Throwable> XCallable<T, E> of(XRunnable<E> runnable) {
return of(runnable, null);
}

/**
* {@link XRunnable} -> {@link XCallable}
*/
static <T, E extends Throwable> XCallable<T, E> of(XRunnable<E> runnable, T result) {
return () -> {
runnable.run();
return result;
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.sugarcubes.executable;

import java.util.concurrent.Callable;
import java.util.function.Supplier;

/**
* @author Maxim Butov
*/
@FunctionalInterface
public interface XExecutable<T> {

T execute();

default Runnable asRunnable() {
return this::execute;
}

default Callable<T> asCallable() {
return this::execute;
}

default Supplier<T> asSupplier() {
return this::execute;
}

default <E extends Throwable> XCallable<T, E> asXCallable() {
return this::execute;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.sugarcubes.executable;

import org.sugarcubes.rex.Rex;

/**
* A modification of {@link Runnable} which throws exception.
* To be used as method reference.
*
* @author Maxim Butov
*/
@FunctionalInterface
public interface XRunnable<E extends Throwable> extends XExecutable<Void> {

/**
* Executes some code without result, or throws an exception if unable to do so.
*
* @throws E
*/
void run() throws E;

@Override
default Void execute() {
try {
run();
}
catch (Throwable e) {
throw Rex.of(e).rethrowAsRuntime();
}
return null;
}

/**
* {@link Runnable} -> {@link XRunnable}
*/
static <E extends Throwable> XRunnable<E> of(XRunnable<E> runnable) {
return runnable;
}

/**
* {@link Runnable} -> {@link XRunnable}
*/
static <X, E extends Throwable> XRunnable<E> of(XCallable<X, E> callable) {
return callable::call;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;

import org.junit.Test;
import org.sugarcubes.executable.XCallable;

/**
* @author Maxim Butov
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;

import org.junit.Test;
import org.sugarcubes.executable.XRunnable;

/**
* @author Maxim Butov
Expand Down

0 comments on commit 8e7a886

Please sign in to comment.