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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The Try api is meant to be similar to the Optional type so has the same function
- filter((x) -> isTrue(x)) - If Success, returns the same Success if the predicate succeeds, otherwise, returns a Failure with type NoSuchElementException.
- onSuccess((x) -> f(x)) execute some code on success - takes Consumer (eg requires no return value).
- onFailure((x) -> f(x)) execute some code on failure - takes Consumer (eg requires no return value).
- raise(x) -> will throw an exception of type x when it happens
- orElse(x) will return the success value of the Try in success case or the value x in failure case.
- orElseTry(f) will return the success value of the Try in success case or a new Try(f) in the failure case.
- orElseThrow(() -> throw new T) gets result or on failure will throw checked exception of type T
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ organization := "com.jason-goodwin"

libraryDependencies ++= Seq(
"com.novocode" % "junit-interface" % "0.11" % "test",
"junit" % "junit" % "4.11" % "test"
"junit" % "junit" % "4.12" % "test"
)

crossPaths := false
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/jasongoodwin/monads/Try.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ public static <U> Try<U> ofFailable(TrySupplier<U> f) {
*/
public abstract <E extends Throwable> Try<T> onFailure(TryConsumer<Throwable, E> action) throws E;

/**
* When the clazz failure type happens, that exception is thrown
* @param clazz the expected exception class
* @return new composed Try
* @throws E if the failure type is the on provided
*/
public abstract <E extends Throwable> Try<T> raise(Class<E> clazz) throws E;

/**
* If a Try is a Success and the predicate holds true, the Success is passed further.
* Otherwise (Failure or predicate doesn't hold), pass Failure.
Expand Down Expand Up @@ -276,6 +284,11 @@ public Optional<T> toOptional() {
public <E extends Throwable> Try<T> onFailure(TryConsumer<Throwable, E> action) {
return this;
}

@Override
public <E extends Throwable> Try<T> raise(Class<E> clazz) throws E {
return this;
}
}


Expand Down Expand Up @@ -365,4 +378,14 @@ public <E extends Throwable> Try<T> onFailure(TryConsumer<Throwable, E> action)
action.accept(e);
return this;
}

@Override
public <E extends Throwable> Try<T> raise(Class<E> clazz) throws E {
return onFailure( t -> {
if (clazz.isAssignableFrom(t.getClass())) {
throw (E) t;
}
} );
}

}
7 changes: 7 additions & 0 deletions src/test/java/com/jasongoodwin/monads/RaiseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.jasongoodwin.monads;

public class RaiseException extends Exception {
public RaiseException(String message) {
super(message);
}
}
Loading