Skip to content

Commit

Permalink
Merge e788a82 into 551c8d0
Browse files Browse the repository at this point in the history
  • Loading branch information
whiskeysierra committed Apr 19, 2018
2 parents 551c8d0 + e788a82 commit 2569148
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 6 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
</properties>

<dependencies>
<dependency>
<groupId>org.apiguardian</groupId>
<artifactId>apiguardian-api</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/zalando/fauxpas/FauxPas.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package org.zalando.fauxpas;

import org.apiguardian.api.API;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.BiFunction;
import java.util.function.Function;

import static java.util.Objects.nonNull;
import static java.util.function.Function.identity;
import static org.apiguardian.api.API.Status.EXPERIMENTAL;
import static org.apiguardian.api.API.Status.MAINTAINED;
import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
public final class FauxPas {

FauxPas() {
Expand Down Expand Up @@ -64,6 +70,7 @@ public static <T, U, X extends Throwable> ThrowingBiPredicate<T, U, X> throwingB
return predicate;
}

@API(status = MAINTAINED)
public static <T extends Throwable, R> Function<Throwable, R> partially(final Class<T> type,
final ThrowingFunction<T, R, Throwable> function) {
return partially(e -> {
Expand All @@ -74,6 +81,7 @@ public static <T extends Throwable, R> Function<Throwable, R> partially(final Cl
});
}

@API(status = MAINTAINED)
public static <R> Function<Throwable, R> partially(final ThrowingFunction<Throwable, R, Throwable> function) {
return throwable -> {
try {
Expand All @@ -86,6 +94,7 @@ public static <R> Function<Throwable, R> partially(final ThrowingFunction<Throwa
};
}

@API(status = EXPERIMENTAL)
public static <R, T extends Throwable> ThrowingBiConsumer<R, Throwable, Throwable> failedWith(
final Class<T> type, final ThrowingConsumer<? super T, Throwable> action) {
return (result, throwable) -> {
Expand All @@ -103,13 +112,15 @@ private static Throwable unpack(final Throwable throwable) {
return throwable instanceof CompletionException && cause != null ? cause : throwable;
}

@API(status = EXPERIMENTAL)
public static <T> CompletableFuture<T> handleCompose(final CompletableFuture<T> future,
final BiFunction<T, Throwable, CompletableFuture<T>> function) {
return future
.handle(function)
.thenCompose(identity());
}

@API(status = EXPERIMENTAL)
public static <T> CompletableFuture<T> exceptionallyCompose(final CompletableFuture<T> future,
final Function<Throwable, CompletableFuture<T>> function) {
return future
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingBiConsumer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import java.util.function.BiConsumer;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingBiConsumer<T, U, X extends Throwable> extends BiConsumer<T, U> {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingBiFunction.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import java.util.function.BiFunction;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingBiFunction<T, U, R, X extends Throwable> extends BiFunction<T, U, R> {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingBiPredicate.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import java.util.function.BiPredicate;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingBiPredicate<T, U, X extends Throwable> extends BiPredicate<T, U> {

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingBinaryOperator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.zalando.fauxpas;

import org.apiguardian.api.API;

import java.util.function.BinaryOperator;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingBinaryOperator<T, X extends Throwable> extends ThrowingBiFunction<T, T, T, X>, BinaryOperator<T> {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingConsumer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import java.util.function.Consumer;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingConsumer<T, X extends Throwable> extends Consumer<T> {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingFunction.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import java.util.function.Function;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingFunction<T, R, X extends Throwable> extends Function<T, R> {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingPredicate.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import java.util.function.Predicate;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingPredicate<T, X extends Throwable> extends Predicate<T> {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingRunnable.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingRunnable<X extends Throwable> extends Runnable {

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingSupplier.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import java.util.function.Supplier;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingSupplier<T, X extends Throwable> extends Supplier<T> {

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/zalando/fauxpas/ThrowingUnaryOperator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.zalando.fauxpas;

import org.apiguardian.api.API;

import java.util.function.UnaryOperator;

import static org.apiguardian.api.API.Status.STABLE;

@API(status = STABLE)
@FunctionalInterface
public interface ThrowingUnaryOperator<T, X extends Throwable> extends ThrowingFunction<T, T, X>, UnaryOperator<T> {

Expand Down
20 changes: 14 additions & 6 deletions src/main/java/org/zalando/fauxpas/TryWith.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
package org.zalando.fauxpas;

import lombok.SneakyThrows;
import org.apiguardian.api.API;

import javax.annotation.Nullable;

import static org.apiguardian.api.API.Status.MAINTAINED;
import static org.apiguardian.api.API.Status.STABLE;

public final class TryWith {

TryWith() {
// package private so we can trick code coverage
}

@API(status = MAINTAINED)
public static <O extends AutoCloseable, I extends AutoCloseable, X extends Throwable> void tryWith(
@Nullable final O outer, @Nullable final I inner, final ThrowingBiConsumer<O, I, X> consumer) throws X {

tryWith(outer, (ಠ_ಠ) -> {
tryWith(inner, () -> {
consumer.tryAccept(outer, inner);
tryWith(outer, a -> {
tryWith(inner, b -> {
consumer.tryAccept(a, b);
});
});
}

@API(status = STABLE)
public static <R extends AutoCloseable, X extends Throwable> void tryWith(@Nullable final R resource,
final ThrowingConsumer<R, X> consumer) throws X {

Expand All @@ -32,16 +38,18 @@ public static <R extends AutoCloseable, X extends Throwable> void tryWith(@Nulla
tryClose(resource);
}

@API(status = MAINTAINED)
public static <O extends AutoCloseable, I extends AutoCloseable, T, X extends Throwable> T tryWith(
@Nullable final O outer, @Nullable final I inner, final ThrowingBiFunction<O, I, T, X> function) throws X {

// not exactly sure why those explicit type parameters are needed
return TryWith.<O, T, X>tryWith(outer, (ಠ_ಠ) ->
tryWith(inner, () -> {
return function.tryApply(outer, inner);
return TryWith.<O, T, X>tryWith(outer, a ->
tryWith(inner, b -> {
return function.tryApply(a, b);
}));
}

@API(status = STABLE)
public static <R extends AutoCloseable, T, X extends Throwable> T tryWith(@Nullable final R resource,
final ThrowingFunction<R, T, X> supplier) throws X {

Expand Down

0 comments on commit 2569148

Please sign in to comment.