Skip to content

Commit

Permalink
Additional methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Grzegorz Piwowarek committed Nov 27, 2015
1 parent 1474313 commit 8151ca6
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/touk/pl/function/ThrowingBiFunction.java
Expand Up @@ -2,10 +2,11 @@

import java.util.Objects;

@FunctionalInterface
public interface ThrowingBiFunction<T1, T2, R, E extends Exception> {
R apply(T1 arg1, T2 arg2) throws E;

default <V> ThrowingBiFunction<T1, T2, V, E> andThen(ThrowingFunction<? super R, ? extends V, E> after) {
default <V> ThrowingBiFunction<T1, T2, V, E> andThen(final ThrowingFunction<? super R, ? extends V, E> after) {
Objects.requireNonNull(after);

return (arg1, arg2) -> after.apply(apply(arg1, arg2));
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/touk/pl/function/ThrowingConsumer.java
@@ -0,0 +1,17 @@
package touk.pl.function;

import java.util.Objects;

@FunctionalInterface
public interface ThrowingConsumer<T, E extends Exception> {
void accept(T t) throws E;

default ThrowingConsumer<T, E> andThenConsume(final ThrowingConsumer<? super T, E> after) {
Objects.requireNonNull(after);

return t -> {
accept(t);
after.accept(t);
};
}
}
7 changes: 6 additions & 1 deletion src/main/java/touk/pl/function/ThrowingFunction.java
Expand Up @@ -4,16 +4,21 @@
import java.util.Optional;
import java.util.function.Function;

@FunctionalInterface
public interface ThrowingFunction<T,R,E extends Exception> {
R apply(T arg) throws E;

static <T, E extends Exception> ThrowingFunction<T, T, E> identity() {
return t -> t;
}

default <V> ThrowingFunction<V, R, E> compose(final ThrowingFunction<? super V, ? extends T, E> before) {
Objects.requireNonNull(before);

return (V v) -> apply(before.apply(v));
}

default <V> ThrowingFunction<T, V, E> andThen(ThrowingFunction<? super R, ? extends V, E> after) {
default <V> ThrowingFunction<T, V, E> andThen(final ThrowingFunction<? super R, ? extends V, E> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/touk/pl/function/ThrowingPredicate.java
Expand Up @@ -3,22 +3,23 @@
import java.util.Objects;
import java.util.Optional;

@FunctionalInterface
public interface ThrowingPredicate<T, E extends Exception> {
boolean test(T t) throws E;

default ThrowingPredicate<T, E> and(ThrowingPredicate<? super T, E> other) {
default ThrowingPredicate<T, E> and(final ThrowingPredicate<? super T, E> other) {
Objects.requireNonNull(other);

return t -> test(t) && other.test(t);
}

default ThrowingPredicate<T, E> or(ThrowingPredicate<? super T, E> other) {
default ThrowingPredicate<T, E> or(final ThrowingPredicate<? super T, E> other) {
Objects.requireNonNull(other);

return t -> test(t) || other.test(t);
}

default ThrowingPredicate<T, E> xor(ThrowingPredicate<? super T, E> other) {
default ThrowingPredicate<T, E> xor(final ThrowingPredicate<? super T, E> other) {
Objects.requireNonNull(other);

return t -> test(t) ^ other.test(t);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/touk/pl/function/ThrowingRunnable.java
@@ -0,0 +1,6 @@
package touk.pl.function;

@FunctionalInterface
public interface ThrowingRunnable<E extends Exception> {
void run() throws E;
}
10 changes: 10 additions & 0 deletions src/main/java/touk/pl/function/ThrowingSupplier.java
@@ -0,0 +1,10 @@
package touk.pl.function;

@FunctionalInterface
public interface ThrowingSupplier<T, E extends Exception> {
T get() throws E;

static <T, E extends Exception> ThrowingSupplier<T, E> empty() {
return () -> null;
}
}
17 changes: 17 additions & 0 deletions src/main/java/touk/pl/function/ThrowingUnaryOperator.java
@@ -0,0 +1,17 @@
package touk.pl.function;

@FunctionalInterface
public interface ThrowingUnaryOperator<T, E extends Exception> extends ThrowingFunction<T, T, E> {

static <T, E extends Exception> ThrowingUnaryOperator<T, E> identity() {
return t -> t;
}

default ThrowingUnaryOperator<T, E> applyBefore(final ThrowingUnaryOperator<T, E> operator) {
return t -> operator.apply(apply(t));
}

default ThrowingUnaryOperator<T, E> applyAfter(final ThrowingUnaryOperator<T, E> operator) {
return t -> apply(operator.apply(t));
}
}

0 comments on commit 8151ca6

Please sign in to comment.