Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Checked{Function, Consumer}1 and wrapper methods for Unchecked & Sneaky #272

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 12 additions & 1 deletion src/main/java/org/jooq/lambda/Sneaky.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package org.jooq.lambda;


import org.jooq.lambda.fi.util.function.*;
import org.jooq.lambda.fi.lang.CheckedRunnable;
import org.jooq.lambda.fi.util.CheckedComparator;
import org.jooq.lambda.fi.util.function.*;
import org.jooq.lambda.function.Consumer1;
import org.jooq.lambda.function.Function1;

import java.util.Comparator;
import java.util.function.*;
Expand Down Expand Up @@ -233,6 +235,11 @@ public static DoubleBinaryOperator doubleBinaryOperator(CheckedDoubleBinaryOpera
return Unchecked.doubleBinaryOperator(operator, Unchecked.RETHROW_ALL);
}

public static <T> Consumer1<T> consumer1(CheckedConsumer1<T> consumer) {
return Unchecked.consumer1(consumer, Unchecked.RETHROW_ALL);
}


// -----------------------------------------------------------------------------------------------------------------
// Wrappers for java.util.function.Consumers
// -----------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -318,6 +325,10 @@ public static <T, R> Function<T, R> function(CheckedFunction<T, R> function) {
return Unchecked.function(function, Unchecked.RETHROW_ALL);
}

public static <T, R> Function1<T, R> function1(CheckedFunction1<T, R> function) {
return Unchecked.function1(function, Unchecked.RETHROW_ALL);
}

/**
* Wrap a {@link CheckedToIntFunction} in a {@link ToIntFunction}.
* <p>
Expand Down
38 changes: 37 additions & 1 deletion src/main/java/org/jooq/lambda/Unchecked.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package org.jooq.lambda;


import org.jooq.lambda.fi.util.function.*;
import org.jooq.lambda.fi.lang.CheckedRunnable;
import org.jooq.lambda.fi.util.CheckedComparator;
import org.jooq.lambda.fi.util.function.*;
import org.jooq.lambda.function.Consumer1;
import org.jooq.lambda.function.Function1;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -653,6 +655,23 @@ public static <T> Consumer<T> consumer(CheckedConsumer<T> consumer, Consumer<Thr
};
}

public static <T> Consumer1<T> consumer1(CheckedConsumer1<T> consumer) {
return consumer1(consumer, THROWABLE_TO_RUNTIME_EXCEPTION);
}

public static <T> Consumer1<T> consumer1(CheckedConsumer1<T> consumer, Consumer<Throwable> handler) {
return t -> {
try {
consumer.accept(t);
}
catch (Throwable e) {
handler.accept(e);

throw new IllegalStateException("Exception handler must throw a RuntimeException", e);
}
};
}

/**
* Wrap a {@link CheckedIntConsumer} in a {@link IntConsumer}.
* <p>
Expand Down Expand Up @@ -806,6 +825,23 @@ public static <T, R> Function<T, R> function(CheckedFunction<T, R> function) {
return function(function, THROWABLE_TO_RUNTIME_EXCEPTION);
}

public static <T, R> Function1<T, R> function1(CheckedFunction1<T, R> function) {
return function1(function, THROWABLE_TO_RUNTIME_EXCEPTION);
}

public static <T, R> Function1<T, R> function1(CheckedFunction1<T, R> function, Consumer<Throwable> handler) {
return t -> {
try {
return function.apply(t);
}
catch (Throwable e) {
handler.accept(e);

throw new IllegalStateException("Exception handler must throw a RuntimeException", e);
}
};
}

/**
* Wrap a {@link CheckedFunction} in a {@link Function} with a custom handler for checked exceptions.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2014-2016, Data Geekery GmbH, [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jooq.lambda.fi.util.function;

import org.jooq.lambda.Sneaky;
import org.jooq.lambda.Unchecked;
import org.jooq.lambda.function.Consumer1;

import java.util.function.Consumer;

/**
* A {@link Consumer} that allows for checked exceptions.
*
* @author Lukas Eder
*/
@FunctionalInterface
public interface CheckedConsumer1<T> {

/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t) throws Throwable;

/**
* @see {@link Sneaky#consumer1(CheckedConsumer1)}
*/
static <T> Consumer1<T> sneaky(CheckedConsumer1<T> consumer) {
return Sneaky.consumer1(consumer);
}

/**
* @see {@link Unchecked#consumer1(CheckedConsumer1)}
*/
static <T> Consumer1<T> unchecked(CheckedConsumer1<T> consumer) {
return Unchecked.consumer1(consumer);
}

/**
* @see {@link Unchecked#consumer1(CheckedConsumer1, Consumer)}
*/
static <T> Consumer1<T> unchecked(CheckedConsumer1<T> consumer, Consumer<Throwable> handler) {
return Unchecked.consumer1(consumer, handler);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2014-2016, Data Geekery GmbH, [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jooq.lambda.fi.util.function;

import org.jooq.lambda.Sneaky;
import org.jooq.lambda.Unchecked;
import org.jooq.lambda.function.Function1;

import java.util.function.Consumer;
import java.util.function.Function;

/**
* A {@link Function} that allows for checked exceptions.
*
* @author Lukas Eder
*/
@FunctionalInterface
public interface CheckedFunction1<T, R> {

/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t) throws Throwable;

/**
* @see {@link Sneaky#function1(CheckedFunction1)}
*/
static <T, R> Function1<T, R> sneaky(CheckedFunction1<T, R> function) {
return Sneaky.function1(function);
}

/**
* @see {@link Unchecked#function1(CheckedFunction1)}
*/
static <T, R> Function1<T, R> unchecked(CheckedFunction1<T, R> function) {
return Unchecked.function1(function);
}

/**
* @see {@link Unchecked#function1(CheckedFunction1, Consumer)}
*/
static <T, R> Function1<T, R> unchecked(CheckedFunction1<T, R> function, Consumer<Throwable> handler) {
return Unchecked.function1(function, handler);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jooq.lambda.fi.util.function;

import org.jooq.lambda.Seq;
import org.jooq.lambda.Unchecked;
import org.jooq.lambda.UncheckedException;
import org.junit.Test;

import java.util.stream.Collectors;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class CheckedFunction1Test {

private String throwingFunction(boolean shouldThrow) throws Exception {
if (shouldThrow) {
throw new Exception("I fail on purpose");
} else {
return "OK";
}
}

@Test
public void uncheckedHappyPath() throws Exception {
Seq.of(false)
.map(Unchecked.function1(this::throwingFunction))
.collect(Collectors.toSet());
}

@Test
public void uncheckedUnhappyPath() throws Exception {
try {
Seq.of(true).map(Unchecked.function1(this::throwingFunction)).collect(Collectors.toSet());
fail("RuntimeException was expected to be thrown");
} catch (RuntimeException ex) {
assertTrue(ex instanceof UncheckedException);
assertTrue(ex.getMessage().equals("java.lang.Exception: I fail on purpose"));
}
}
}