Skip to content
This repository has been archived by the owner on Apr 13, 2019. It is now read-only.

Commit

Permalink
asm: replaced from(f,ef) with more composable catching
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Sobolev committed Nov 6, 2017
1 parent 20c0500 commit 2eb9699
Showing 1 changed file with 5 additions and 16 deletions.
21 changes: 5 additions & 16 deletions java/core/src/main/java/ws/epigraph/assembly/Asm.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,14 @@ public interface Asm<D, P, R> {
return (dto, projection, ctx) -> assemble(f.apply(dto), projection, ctx);
}

/**
* Composes a function {@code f} with an assembler by applying {@code f} to the data object
* before {@code assemble} call. Ideologically it would be similar to {@code f.andThen(this)}
*
* @param f function to run on the data object
* @param ef function to be called if {@code f} application produces an error
* @param <T> new data object type
*
* @return composed assembler
*/
default <T> @NotNull Asm<T, P, R> from(@NotNull Function<T, D> f, @NotNull Function<Exception, R> ef) {
// parameterize on exception class?
default @NotNull Asm<D, P, R> catching(@NotNull Function<Exception, R> handler) {
return (dto, projection, ctx) -> {
D d;
try {
d = f.apply(dto);
} catch (Exception ex) {
return ef.apply(ex);
return assemble(dto, projection, ctx);
} catch (Exception e) {
return handler.apply(e);
}
return assemble(d, projection, ctx);
};
}

Expand Down

0 comments on commit 2eb9699

Please sign in to comment.