-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maxim Butov
committed
Feb 3, 2020
1 parent
47f5782
commit 8e7a886
Showing
9 changed files
with
158 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
sugar-cubes-sandbox/src/main/java/org/sugarcubes/concurrent/XCallable.java
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
sugar-cubes-sandbox/src/main/java/org/sugarcubes/concurrent/XRunnable.java
This file was deleted.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
sugar-cubes-sandbox/src/main/java/org/sugarcubes/executable/XCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
sugar-cubes-sandbox/src/main/java/org/sugarcubes/executable/XExecutable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
sugar-cubes-sandbox/src/main/java/org/sugarcubes/executable/XRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters