From 461f759cc8b7bb5286f579175f7321afd9bad50d Mon Sep 17 00:00:00 2001 From: lukaseder Date: Sun, 2 Mar 2014 15:25:26 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 + pom.xml | 39 + .../org/jooq/unchecked/CheckedBiConsumer.java | 51 ++ .../unchecked/CheckedBooleanSupplier.java | 50 ++ .../org/jooq/unchecked/CheckedConsumer.java | 50 ++ .../jooq/unchecked/CheckedDoubleConsumer.java | 50 ++ .../jooq/unchecked/CheckedDoubleFunction.java | 51 ++ .../jooq/unchecked/CheckedDoubleSupplier.java | 50 ++ .../org/jooq/unchecked/CheckedFunction.java | 51 ++ .../jooq/unchecked/CheckedIntConsumer.java | 50 ++ .../jooq/unchecked/CheckedIntFunction.java | 51 ++ .../jooq/unchecked/CheckedIntSupplier.java | 50 ++ .../jooq/unchecked/CheckedLongConsumer.java | 50 ++ .../jooq/unchecked/CheckedLongFunction.java | 51 ++ .../jooq/unchecked/CheckedLongSupplier.java | 50 ++ .../org/jooq/unchecked/CheckedSupplier.java | 50 ++ .../java/org/jooq/unchecked/Unchecked.java | 689 ++++++++++++++++++ .../jooq/unchecked/CheckedBiConsumerTest.java | 105 +++ .../jooq/unchecked/CheckedConsumerTest.java | 232 ++++++ .../jooq/unchecked/CheckedFunctionTest.java | 237 ++++++ .../jooq/unchecked/CheckedSupplierTest.java | 268 +++++++ 21 files changed, 2278 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/org/jooq/unchecked/CheckedBiConsumer.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedBooleanSupplier.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedConsumer.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedDoubleConsumer.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedDoubleFunction.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedDoubleSupplier.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedFunction.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedIntConsumer.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedIntFunction.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedIntSupplier.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedLongConsumer.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedLongFunction.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedLongSupplier.java create mode 100644 src/main/java/org/jooq/unchecked/CheckedSupplier.java create mode 100644 src/main/java/org/jooq/unchecked/Unchecked.java create mode 100644 src/test/java/org/jooq/unchecked/CheckedBiConsumerTest.java create mode 100644 src/test/java/org/jooq/unchecked/CheckedConsumerTest.java create mode 100644 src/test/java/org/jooq/unchecked/CheckedFunctionTest.java create mode 100644 src/test/java/org/jooq/unchecked/CheckedSupplierTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..556e6be3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +target +*.iml diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..ce325536 --- /dev/null +++ b/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + org.jooq + unchecked + 0.9.0 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + true + 512m + 256m + UTF-8 + 1.8 + 1.8 + true + lines,vars,source + + + + + + + + junit + junit + 4.11 + test + + + \ No newline at end of file diff --git a/src/main/java/org/jooq/unchecked/CheckedBiConsumer.java b/src/main/java/org/jooq/unchecked/CheckedBiConsumer.java new file mode 100644 index 00000000..661f8ca1 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedBiConsumer.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedBiConsumer { + + /** + * Performs this operation on the given arguments. + * + * @param t the first input argument + * @param u the second input argument + */ + void accept(T t, U u) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedBooleanSupplier.java b/src/main/java/org/jooq/unchecked/CheckedBooleanSupplier.java new file mode 100644 index 00000000..e34b0164 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedBooleanSupplier.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedBooleanSupplier { + + /** + * Gets a result. + * + * @return a result + */ + boolean getAsBoolean() throws Throwable; +} \ No newline at end of file diff --git a/src/main/java/org/jooq/unchecked/CheckedConsumer.java b/src/main/java/org/jooq/unchecked/CheckedConsumer.java new file mode 100644 index 00000000..05602db4 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedConsumer.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedConsumer { + + /** + * Performs this operation on the given argument. + * + * @param t the input argument + */ + void accept(T t) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedDoubleConsumer.java b/src/main/java/org/jooq/unchecked/CheckedDoubleConsumer.java new file mode 100644 index 00000000..4ee62ede --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedDoubleConsumer.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedDoubleConsumer { + + /** + * Performs this operation on the given argument. + * + * @param value the input argument + */ + void accept(double value) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedDoubleFunction.java b/src/main/java/org/jooq/unchecked/CheckedDoubleFunction.java new file mode 100644 index 00000000..edbefc3e --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedDoubleFunction.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedDoubleFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + */ + R apply(double value) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedDoubleSupplier.java b/src/main/java/org/jooq/unchecked/CheckedDoubleSupplier.java new file mode 100644 index 00000000..733ae95b --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedDoubleSupplier.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedDoubleSupplier { + + /** + * Gets a result. + * + * @return a result + */ + double getAsDouble() throws Throwable; +} \ No newline at end of file diff --git a/src/main/java/org/jooq/unchecked/CheckedFunction.java b/src/main/java/org/jooq/unchecked/CheckedFunction.java new file mode 100644 index 00000000..4ab66344 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedFunction.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedFunction { + + /** + * Applies this function to the given argument. + * + * @param t the function argument + * @return the function result + */ + R apply(T t) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedIntConsumer.java b/src/main/java/org/jooq/unchecked/CheckedIntConsumer.java new file mode 100644 index 00000000..4b0f129a --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedIntConsumer.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedIntConsumer { + + /** + * Performs this operation on the given argument. + * + * @param value the input argument + */ + void accept(int value) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedIntFunction.java b/src/main/java/org/jooq/unchecked/CheckedIntFunction.java new file mode 100644 index 00000000..9a2215be --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedIntFunction.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedIntFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + */ + R apply(int value) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedIntSupplier.java b/src/main/java/org/jooq/unchecked/CheckedIntSupplier.java new file mode 100644 index 00000000..251c1e59 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedIntSupplier.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedIntSupplier { + + /** + * Gets a result. + * + * @return a result + */ + int getAsInt() throws Throwable; +} \ No newline at end of file diff --git a/src/main/java/org/jooq/unchecked/CheckedLongConsumer.java b/src/main/java/org/jooq/unchecked/CheckedLongConsumer.java new file mode 100644 index 00000000..79450d67 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedLongConsumer.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedLongConsumer { + + /** + * Performs this operation on the given argument. + * + * @param value the input argument + */ + void accept(long value) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedLongFunction.java b/src/main/java/org/jooq/unchecked/CheckedLongFunction.java new file mode 100644 index 00000000..1dd65db9 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedLongFunction.java @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedLongFunction { + + /** + * Applies this function to the given argument. + * + * @param value the function argument + * @return the function result + */ + R apply(long value) throws Throwable; +} diff --git a/src/main/java/org/jooq/unchecked/CheckedLongSupplier.java b/src/main/java/org/jooq/unchecked/CheckedLongSupplier.java new file mode 100644 index 00000000..1e6cc097 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedLongSupplier.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedLongSupplier { + + /** + * Gets a result. + * + * @return a result + */ + long getAsLong() throws Throwable; +} \ No newline at end of file diff --git a/src/main/java/org/jooq/unchecked/CheckedSupplier.java b/src/main/java/org/jooq/unchecked/CheckedSupplier.java new file mode 100644 index 00000000..320b5050 --- /dev/null +++ b/src/main/java/org/jooq/unchecked/CheckedSupplier.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +/** + * @author Lukas Eder + */ +@FunctionalInterface +public interface CheckedSupplier { + + /** + * Gets a result. + * + * @return a result + */ + T get() throws Throwable; +} \ No newline at end of file diff --git a/src/main/java/org/jooq/unchecked/Unchecked.java b/src/main/java/org/jooq/unchecked/Unchecked.java new file mode 100644 index 00000000..edf3d86d --- /dev/null +++ b/src/main/java/org/jooq/unchecked/Unchecked.java @@ -0,0 +1,689 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + + +import java.util.function.*; + +/** + * Improved interoperability between checked exceptions and Java 8. + *

+ * Checked exceptions are one of Java's biggest flaws. Due to backwards-compatibility, we're inheriting all the checked + * exception trouble back from JDK 1.0. This becomes even more obvious when using lambda expressions, most of which are + * not allowed to throw checked exceptions. + *

+ * This library tries to ease some pain and wraps / unwraps a variety of API elements from the JDK 8 to improve + * interoperability with checked exceptions. + * + * @author Lukas Eder + */ +public final class Unchecked { + + /** + * A {@link Consumer} that wraps any {@link Throwable} in a {@link RuntimeException}. + */ + public static final Consumer CHECKED_CONSUMER = t -> { + throw new RuntimeException(t); + }; + + // ----------------------------------------------------------------------------------------------------------------- + // Wrappers for BiConsumers + // ----------------------------------------------------------------------------------------------------------------- + + /** + * Wrap a {@link CheckedBiConsumer} in a {@link BiConsumer}. + *

+ * Example: + *

+     * map.forEach(Unchecked.biConsumer((k, v) -> {
+     *     if (k == null || v == null)
+     *         throw new Exception("No nulls allowed in map");
+     * }));
+     * 
+ */ + public static BiConsumer biConsumer(CheckedBiConsumer consumer) { + return biConsumer(consumer, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedConsumer} in a {@link Consumer} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * map.forEach(Unchecked.biConsumer(
+     *     (k, v) -> {
+     *         if (k == null || v == null)
+     *             throw new Exception("No nulls allowed in map");
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static BiConsumer biConsumer(CheckedBiConsumer consumer, Consumer handler) { + return (t, u) -> { + try { + consumer.accept(t, u); + } + catch (Throwable e) { + handler.accept(e); + } + }; + } + + // ----------------------------------------------------------------------------------------------------------------- + // Wrappers for Consumers + // ----------------------------------------------------------------------------------------------------------------- + + /** + * Wrap a {@link CheckedConsumer} in a {@link Consumer}. + *

+ * Example: + *

+     * Arrays.asList("a", "b").stream().forEach(Unchecked.consumer(s -> {
+     *     if (s.length() > 10)
+     *         throw new Exception("Only short strings allowed");
+     * }));
+     * 
+ */ + public static Consumer consumer(CheckedConsumer consumer) { + return consumer(consumer, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedConsumer} in a {@link Consumer} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * Arrays.asList("a", "b").stream().forEach(Unchecked.consumer(
+     *     s -> {
+     *         if (s.length() > 10)
+     *             throw new Exception("Only short strings allowed");
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static Consumer consumer(CheckedConsumer consumer, Consumer handler) { + return t -> { + try { + consumer.accept(t); + } + catch (Throwable e) { + handler.accept(e); + } + }; + } + + /** + * Wrap a {@link CheckedIntConsumer} in a {@link IntConsumer}. + *

+ * Example: + *

+     * Arrays.stream(new int[] { 1, 2 }).forEach(Unchecked.intConsumer(i -> {
+     *     if (i < 0)
+     *         throw new Exception("Only positive numbers allowed");
+     * }));
+     * 
+ */ + public static IntConsumer intConsumer(CheckedIntConsumer consumer) { + return intConsumer(consumer, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedIntConsumer} in a {@link IntConsumer} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * Arrays.stream(new int[] { 1, 2 }).forEach(Unchecked.intConsumer(
+     *     i -> {
+     *         if (i < 0)
+     *             throw new Exception("Only positive numbers allowed");
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static IntConsumer intConsumer(CheckedIntConsumer consumer, Consumer handler) { + return i -> { + try { + consumer.accept(i); + } + catch (Throwable e) { + handler.accept(e); + } + }; + } + + /** + * Wrap a {@link CheckedLongConsumer} in a {@link LongConsumer}. + *

+ * Example: + *

+     * Arrays.stream(new long[] { 1L, 2L }).forEach(Unchecked.longConsumer(l -> {
+     *     if (l < 0)
+     *         throw new Exception("Only positive numbers allowed");
+     * }));
+     * 
+ */ + public static LongConsumer longConsumer(CheckedLongConsumer consumer) { + return longConsumer(consumer, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedLongConsumer} in a {@link LongConsumer} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * Arrays.stream(new long[] { 1L, 2L }).forEach(Unchecked.longConsumer(
+     *     l -> {
+     *         if (l < 0)
+     *             throw new Exception("Only positive numbers allowed");
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static LongConsumer longConsumer(CheckedLongConsumer consumer, Consumer handler) { + return l -> { + try { + consumer.accept(l); + } + catch (Throwable e) { + handler.accept(e); + } + }; + } + + /** + * Wrap a {@link CheckedDoubleConsumer} in a {@link DoubleConsumer}. + *

+ * Example: + *

+     * Arrays.stream(new double[] { 1.0, 2.0 }).forEach(Unchecked.doubleConsumer(d -> {
+     *     if (d < 0.0)
+     *         throw new Exception("Only positive numbers allowed");
+     * }));
+     * 
+ */ + public static DoubleConsumer doubleConsumer(CheckedDoubleConsumer consumer) { + return doubleConsumer(consumer, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedDoubleConsumer} in a {@link DoubleConsumer} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * Arrays.stream(new double[] { 1.0, 2.0 }).forEach(Unchecked.doubleConsumer(
+     *     d -> {
+     *         if (d < 0.0)
+     *             throw new Exception("Only positive numbers allowed");
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static DoubleConsumer doubleConsumer(CheckedDoubleConsumer consumer, Consumer handler) { + return d -> { + try { + consumer.accept(d); + } + catch (Throwable e) { + handler.accept(e); + } + }; + } + + // ----------------------------------------------------------------------------------------------------------------- + // Wrappers for Functions + // ----------------------------------------------------------------------------------------------------------------- + + /** + * Wrap a {@link CheckedFunction} in a {@link Function}. + *

+ * Example: + *

+     * map.computeIfAbsent("key", Unchecked.function(k -> {
+     *     if (k.length() > 10)
+     *         throw new Exception("Only short strings allowed");
+     *
+     *     return 42;
+     * }));
+     * 
+ */ + public static Function function(CheckedFunction function) { + return function(function, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedFunction} in a {@link Function} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * map.forEach(Unchecked.function(
+     *     k -> {
+     *         if (k.length() > 10)
+     *             throw new Exception("Only short strings allowed");
+     *
+     *         return 42;
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static Function function(CheckedFunction function, Consumer 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 CheckedIntFunction} in a {@link IntFunction}. + *

+ * Example: + *

+     * IntStream.of(1, 2, 3).mapToObj(Unchecked.intFunction(i -> {
+     *     if (i < 0)
+     *         throw new Exception("Only positive numbers allowed");
+     *
+     *     return "" + i;
+     * });
+     * 
+ */ + public static IntFunction intFunction(CheckedIntFunction function) { + return intFunction(function, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedIntFunction} in a {@link IntFunction} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * IntStream.of(1, 2, 3).mapToObj(Unchecked.intFunction(
+     *     i -> {
+     *         if (i < 0)
+     *             throw new Exception("Only positive numbers allowed");
+     *
+     *         return "" + i;
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static IntFunction intFunction(CheckedIntFunction function, Consumer 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 CheckedLongFunction} in a {@link LongFunction}. + *

+ * Example: + *

+     * LongStream.of(1L, 2L, 3L).mapToObj(Unchecked.longFunction(l -> {
+     *     if (l < 0L)
+     *         throw new Exception("Only positive numbers allowed");
+     *
+     *     return "" + l;
+     * });
+     * 
+ */ + public static LongFunction longFunction(CheckedLongFunction function) { + return longFunction(function, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedLongFunction} in a {@link LongFunction} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * LongStream.of(1L, 2L, 3L).mapToObj(Unchecked.longFunction(
+     *     l -> {
+     *         if (l < 0L)
+     *             throw new Exception("Only positive numbers allowed");
+     *
+     *         return "" + l;
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static LongFunction longFunction(CheckedLongFunction function, Consumer 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 CheckedDoubleFunction} in a {@link DoubleFunction}. + *

+ * Example: + *

+     * DoubleStream.of(1.0, 2.0, 3.0).mapToObj(Unchecked.doubleFunction(d -> {
+     *     if (d < 0.0)
+     *         throw new Exception("Only positive numbers allowed");
+     *
+     *     return "" + d;
+     * });
+     * 
+ */ + public static DoubleFunction doubleFunction(CheckedDoubleFunction function) { + return doubleFunction(function, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedDoubleFunction} in a {@link DoubleFunction} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * DoubleStream.of(1.0, 2.0, 3.0).mapToObj(Unchecked.doubleFunction(
+     *     d -> {
+     *         if (d < 0.0)
+     *             throw new Exception("Only positive numbers allowed");
+     *
+     *         return "" + d;
+     *     },
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static DoubleFunction doubleFunction(CheckedDoubleFunction function, Consumer handler) { + return t -> { + try { + return function.apply(t); + } + catch (Throwable e) { + handler.accept(e); + + throw new IllegalStateException("Exception handler must throw a RuntimeException", e); + } + }; + } + + // ----------------------------------------------------------------------------------------------------------------- + // Wrappers for Suppliers + // ----------------------------------------------------------------------------------------------------------------- + + /** + * Wrap a {@link CheckedSupplier} in a {@link Supplier}. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     * Stream.generate(Unchecked.supplier(() -> rs.getObject(1)));
+     * 
+ */ + public static Supplier supplier(CheckedSupplier supplier) { + return supplier(supplier, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedSupplier} in a {@link Supplier} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     *
+     * Stream.generate(Unchecked.supplier(
+     *     () -> rs.getObject(1),
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static Supplier supplier(CheckedSupplier supplier, Consumer handler) { + return () -> { + try { + return supplier.get(); + } + catch (Throwable e) { + handler.accept(e); + + throw new IllegalStateException("Exception handler must throw a RuntimeException", e); + } + }; + } + + /** + * Wrap a {@link CheckedIntSupplier} in a {@link IntSupplier}. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     * Stream.generate(Unchecked.intSupplier(() -> rs.getInt(1)));
+     * 
+ */ + public static IntSupplier intSupplier(CheckedIntSupplier supplier) { + return intSupplier(supplier, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedIntSupplier} in a {@link IntSupplier} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     *
+     * Stream.generate(Unchecked.intSupplier(
+     *     () -> rs.getInt(1),
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static IntSupplier intSupplier(CheckedIntSupplier supplier, Consumer handler) { + return () -> { + try { + return supplier.getAsInt(); + } + catch (Throwable e) { + handler.accept(e); + + throw new IllegalStateException("Exception handler must throw a RuntimeException", e); + } + }; + } + + /** + * Wrap a {@link CheckedLongSupplier} in a {@link LongSupplier}. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     * Stream.generate(Unchecked.longSupplier(() -> rs.getLong(1)));
+     * 
+ */ + public static LongSupplier longSupplier(CheckedLongSupplier supplier) { + return longSupplier(supplier, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedLongSupplier} in a {@link LongSupplier} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     *
+     * Stream.generate(Unchecked.longSupplier(
+     *     () -> rs.getLong(1),
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static LongSupplier longSupplier(CheckedLongSupplier supplier, Consumer handler) { + return () -> { + try { + return supplier.getAsLong(); + } + catch (Throwable e) { + handler.accept(e); + + throw new IllegalStateException("Exception handler must throw a RuntimeException", e); + } + }; + } + + /** + * Wrap a {@link CheckedDoubleSupplier} in a {@link DoubleSupplier}. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     * Stream.generate(Unchecked.doubleSupplier(() -> rs.getDouble(1)));
+     * 
+ */ + public static DoubleSupplier doubleSupplier(CheckedDoubleSupplier supplier) { + return doubleSupplier(supplier, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedDoubleSupplier} in a {@link DoubleSupplier} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     *
+     * Stream.generate(Unchecked.doubleSupplier(
+     *     () -> rs.getDouble(1),
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static DoubleSupplier doubleSupplier(CheckedDoubleSupplier supplier, Consumer handler) { + return () -> { + try { + return supplier.getAsDouble(); + } + catch (Throwable e) { + handler.accept(e); + + throw new IllegalStateException("Exception handler must throw a RuntimeException", e); + } + }; + } + + /** + * Wrap a {@link CheckedBooleanSupplier} in a {@link BooleanSupplier}. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     * Stream.generate(Unchecked.booleanSupplier(() -> rs.getBoolean(1)));
+     * 
+ */ + public static BooleanSupplier booleanSupplier(CheckedBooleanSupplier supplier) { + return booleanSupplier(supplier, CHECKED_CONSUMER); + } + + /** + * Wrap a {@link CheckedBooleanSupplier} in a {@link BooleanSupplier} with a custom handler for checked exceptions. + *

+ * Example: + *

+     * ResultSet rs = statement.executeQuery();
+     *
+     * Stream.generate(Unchecked.booleanSupplier(
+     *     () -> rs.getBoolean(1),
+     *     e -> {
+     *         throw new IllegalStateException(e);
+     *     }
+     * ));
+     * 
+ */ + public static BooleanSupplier booleanSupplier(CheckedBooleanSupplier supplier, Consumer handler) { + return () -> { + try { + return supplier.getAsBoolean(); + } + catch (Throwable e) { + handler.accept(e); + + throw new IllegalStateException("Exception handler must throw a RuntimeException", e); + } + }; + } + + /** + * No instances + */ + private Unchecked() {} +} diff --git a/src/test/java/org/jooq/unchecked/CheckedBiConsumerTest.java b/src/test/java/org/jooq/unchecked/CheckedBiConsumerTest.java new file mode 100644 index 00000000..16161e76 --- /dev/null +++ b/src/test/java/org/jooq/unchecked/CheckedBiConsumerTest.java @@ -0,0 +1,105 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +import org.junit.Test; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.*; + +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.fail; +import static org.junit.Assert.assertEquals; + +/** + * @author Lukas Eder + */ +public class CheckedBiConsumerTest { + + @Test + public void testCheckedBiConsumer() { + BiConsumer test = Unchecked.biConsumer( + (o1, o2) -> { + throw new Exception(o1 + ":" + o2); + } + ); + + assertBiConsumer(test, RuntimeException.class); + } + + @Test + public void testCheckedBiConsumerWithCustomHandler() { + BiConsumer test = Unchecked.biConsumer( + (o1, o2) -> { + throw new Exception(o1 + ":" + o2); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertBiConsumer(test, IllegalStateException.class); + } + + private void assertBiConsumer(BiConsumer test, Class type) { + assertNotNull(test); + try { + test.accept(null, null); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "null:null"); + } + + try { + Map map = new LinkedHashMap<>(); + map.put("a", 1); + map.put("b", 2); + map.put("c", 3); + map.forEach(test); + } + catch (RuntimeException e) { + assertException(type, e, "a:1"); + } + } + + private void assertException(Class type, RuntimeException e, String message) { + assertEquals(type, e.getClass()); + assertEquals(Exception.class, e.getCause().getClass()); + assertEquals(message, e.getCause().getMessage()); + } +} diff --git a/src/test/java/org/jooq/unchecked/CheckedConsumerTest.java b/src/test/java/org/jooq/unchecked/CheckedConsumerTest.java new file mode 100644 index 00000000..4d4bbe44 --- /dev/null +++ b/src/test/java/org/jooq/unchecked/CheckedConsumerTest.java @@ -0,0 +1,232 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +import org.junit.Test; + +import java.util.Arrays; +import java.util.function.Consumer; +import java.util.function.DoubleConsumer; +import java.util.function.IntConsumer; +import java.util.function.LongConsumer; + +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.fail; +import static org.junit.Assert.assertEquals; + +/** + * @author Lukas Eder + */ +public class CheckedConsumerTest { + + @Test + public void testCheckedConsumer() { + Consumer test = Unchecked.consumer( + o -> { + throw new Exception("" + o); + } + ); + + assertConsumer(test, RuntimeException.class); + } + + @Test + public void testCheckedConsumerWithCustomHandler() { + Consumer test = Unchecked.consumer( + o -> { + throw new Exception("" + o); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertConsumer(test, IllegalStateException.class); + } + + @Test + public void testCheckedIntConsumer() { + IntConsumer test = Unchecked.intConsumer( + i -> { + throw new Exception("" + i); + } + ); + + assertIntConsumer(test, RuntimeException.class); + } + + @Test + public void testCheckedIntConsumerWithCustomHandler() { + IntConsumer test = Unchecked.intConsumer( + l -> { + throw new Exception("" + l); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertIntConsumer(test, IllegalStateException.class); + } + + @Test + public void testCheckedLongConsumer() { + LongConsumer test = Unchecked.longConsumer( + l -> { + throw new Exception("" + l); + } + ); + + assertLongConsumer(test, RuntimeException.class); + } + + @Test + public void testCheckedLongConsumerWithCustomHandler() { + LongConsumer test = Unchecked.longConsumer( + l -> { + throw new Exception("" + l); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertLongConsumer(test, IllegalStateException.class); + } + + @Test + public void testCheckedDoubleConsumer() { + DoubleConsumer test = Unchecked.doubleConsumer( + d -> { + throw new Exception("" + d); + } + ); + + assertDoubleConsumer(test, RuntimeException.class); + } + + @Test + public void testCheckedDoubleConsumerWithCustomHandler() { + DoubleConsumer test = Unchecked.doubleConsumer( + d -> { + throw new Exception("" + d); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertDoubleConsumer(test, IllegalStateException.class); + } + + private void assertConsumer(Consumer test, Class type) { + assertNotNull(test); + try { + test.accept(null); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "null"); + } + + try { + Arrays.asList("a", "b", "c").stream().forEach(test); + } + catch (RuntimeException e) { + assertException(type, e, "a"); + } + } + + private void assertIntConsumer(IntConsumer test, Class type) { + assertNotNull(test); + try { + test.accept(0); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "0"); + } + + try { + Arrays.stream(new int[]{1, 2, 3}).forEach(test); + } + catch (RuntimeException e) { + assertException(type, e, "1"); + } + } + + private void assertLongConsumer(LongConsumer test, Class type) { + assertNotNull(test); + try { + test.accept(0L); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "0"); + } + + try { + Arrays.stream(new long[] { 1L, 2L, 3L }).forEach(test); + } + catch (RuntimeException e) { + assertException(type, e, "1"); + } + } + + private void assertDoubleConsumer(DoubleConsumer test, Class type) { + assertNotNull(test); + try { + test.accept(0.0); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "0.0"); + } + + try { + Arrays.stream(new double[] { 1.0, 2.0, 3.0 }).forEach(test); + } + catch (RuntimeException e) { + assertException(type, e, "1.0"); + } + } + + private void assertException(Class type, RuntimeException e, String message) { + assertEquals(type, e.getClass()); + assertEquals(Exception.class, e.getCause().getClass()); + assertEquals(message, e.getCause().getMessage()); + } +} diff --git a/src/test/java/org/jooq/unchecked/CheckedFunctionTest.java b/src/test/java/org/jooq/unchecked/CheckedFunctionTest.java new file mode 100644 index 00000000..0c4ab743 --- /dev/null +++ b/src/test/java/org/jooq/unchecked/CheckedFunctionTest.java @@ -0,0 +1,237 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +import org.junit.Test; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.DoubleFunction; +import java.util.function.Function; +import java.util.function.IntFunction; +import java.util.function.LongFunction; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; + +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.fail; +import static org.junit.Assert.assertEquals; + +/** + * @author Lukas Eder + */ +public class CheckedFunctionTest { + + @Test + public void testCheckedFunction() { + Function test = Unchecked.function( + t -> { + throw new Exception("" + t); + } + ); + + assertFunction(test, RuntimeException.class); + } + + @Test + public void testCheckedFunctionWithCustomHandler() { + Function test = Unchecked.function( + t -> { + throw new Exception("" + t); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertFunction(test, IllegalStateException.class); + } + + @Test + public void testCheckedIntFunction() { + IntFunction test = Unchecked.intFunction( + i -> { + throw new Exception("" + i); + } + ); + + assertIntFunction(test, RuntimeException.class); + } + + @Test + public void testCheckedIntFunctionWithCustomHandler() { + IntFunction test = Unchecked.intFunction( + i -> { + throw new Exception("" + i); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertIntFunction(test, IllegalStateException.class); + } + + @Test + public void testCheckedLongFunction() { + LongFunction test = Unchecked.longFunction( + l -> { + throw new Exception("" + l); + } + ); + + assertLongFunction(test, RuntimeException.class); + } + + @Test + public void testCheckedLongFunctionWithCustomHandler() { + LongFunction test = Unchecked.longFunction( + l -> { + throw new Exception("" + l); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertLongFunction(test, IllegalStateException.class); + } + + @Test + public void testCheckedDoubleFunction() { + DoubleFunction test = Unchecked.doubleFunction( + d -> { + throw new Exception("" + d); + } + ); + + assertDoubleFunction(test, RuntimeException.class); + } + + @Test + public void testCheckedDoubleFunctionWithCustomHandler() { + DoubleFunction test = Unchecked.doubleFunction( + d -> { + throw new Exception("" + d); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertDoubleFunction(test, IllegalStateException.class); + } + + private void assertFunction(Function test, Class type) { + assertNotNull(test); + try { + test.apply(null); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "null"); + } + + try { + Map map = new LinkedHashMap<>(); + map.computeIfAbsent("a", test); + } + catch (RuntimeException e) { + assertException(type, e, "a"); + } + } + + private void assertIntFunction(IntFunction test, Class type) { + assertNotNull(test); + try { + test.apply(0); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "0"); + } + + try { + IntStream.of(1, 2, 3).mapToObj(test); + } + catch (RuntimeException e) { + assertException(type, e, "1"); + } + } + + private void assertLongFunction(LongFunction test, Class type) { + assertNotNull(test); + try { + test.apply(0L); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "0"); + } + + try { + LongStream.of(1L, 2L, 3L).mapToObj(test); + } + catch (RuntimeException e) { + assertException(type, e, "1"); + } + } + + private void assertDoubleFunction(DoubleFunction test, Class type) { + assertNotNull(test); + try { + test.apply(0.0); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "0.0"); + } + + try { + DoubleStream.of(1.0, 2.0, 3.0).mapToObj(test); + } + catch (RuntimeException e) { + assertException(type, e, "1.0"); + } + } + + private void assertException(Class type, RuntimeException e, String message) { + assertEquals(type, e.getClass()); + assertEquals(Exception.class, e.getCause().getClass()); + assertEquals(message, e.getCause().getMessage()); + } +} diff --git a/src/test/java/org/jooq/unchecked/CheckedSupplierTest.java b/src/test/java/org/jooq/unchecked/CheckedSupplierTest.java new file mode 100644 index 00000000..42500f34 --- /dev/null +++ b/src/test/java/org/jooq/unchecked/CheckedSupplierTest.java @@ -0,0 +1,268 @@ +/** + * Copyright (c) 2014, Data Geekery GmbH, contact@datageekery.com + * All rights reserved. + * + * This software is licensed to you under the Apache License, Version 2.0 + * (the "License"); You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * . Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * . Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * . Neither the name "jOOQ" nor the names of its contributors may be + * used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +package org.jooq.unchecked; + +import org.junit.Test; + +import java.util.function.*; +import java.util.stream.DoubleStream; +import java.util.stream.IntStream; +import java.util.stream.LongStream; +import java.util.stream.Stream; + +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.fail; +import static org.junit.Assert.assertEquals; + +/** + * @author Lukas Eder + */ +public class CheckedSupplierTest { + + @Test + public void testCheckedSupplier() { + Supplier test = Unchecked.supplier( + () -> { + throw new Exception("object"); + } + ); + + assertSupplier(test, RuntimeException.class); + } + + @Test + public void testCheckedSupplierWithCustomHandler() { + Supplier test = Unchecked.supplier( + () -> { + throw new Exception("object"); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertSupplier(test, IllegalStateException.class); + } + + @Test + public void testCheckedIntSupplier() { + IntSupplier test = Unchecked.intSupplier( + () -> { + throw new Exception("int"); + } + ); + + assertIntSupplier(test, RuntimeException.class); + } + + @Test + public void testCheckedIntSupplierWithCustomHandler() { + IntSupplier test = Unchecked.intSupplier( + () -> { + throw new Exception("int"); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertIntSupplier(test, IllegalStateException.class); + } + + @Test + public void testCheckedLongSupplier() { + LongSupplier test = Unchecked.longSupplier( + () -> { + throw new Exception("long"); + } + ); + + assertLongSupplier(test, RuntimeException.class); + } + + @Test + public void testCheckedLongSupplierWithCustomHandler() { + LongSupplier test = Unchecked.longSupplier( + () -> { + throw new Exception("long"); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertLongSupplier(test, IllegalStateException.class); + } + + @Test + public void testCheckedDoubleSupplier() { + DoubleSupplier test = Unchecked.doubleSupplier( + () -> { + throw new Exception("double"); + } + ); + + assertDoubleSupplier(test, RuntimeException.class); + } + + @Test + public void testCheckedDoubleSupplierWithCustomHandler() { + DoubleSupplier test = Unchecked.doubleSupplier( + () -> { + throw new Exception("double"); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertDoubleSupplier(test, IllegalStateException.class); + } + + @Test + public void testCheckedBooleanSupplier() { + BooleanSupplier test = Unchecked.booleanSupplier( + () -> { + throw new Exception("boolean"); + } + ); + + assertBooleanSupplier(test, RuntimeException.class); + } + + @Test + public void testCheckedBooleanSupplierWithCustomHandler() { + BooleanSupplier test = Unchecked.booleanSupplier( + () -> { + throw new Exception("boolean"); + }, + e -> { + throw new IllegalStateException(e); + } + ); + + assertBooleanSupplier(test, IllegalStateException.class); + } + + private void assertSupplier(Supplier test, Class type) { + assertNotNull(test); + try { + test.get(); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "object"); + } + + try { + Stream.generate(test).findFirst(); + } + catch (RuntimeException e) { + assertException(type, e, "object"); + } + } + + private void assertIntSupplier(IntSupplier test, Class type) { + assertNotNull(test); + try { + test.getAsInt(); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "int"); + } + + try { + IntStream.generate(test).findFirst(); + } + catch (RuntimeException e) { + assertException(type, e, "int"); + } + } + + private void assertLongSupplier(LongSupplier test, Class type) { + assertNotNull(test); + try { + test.getAsLong(); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "long"); + } + + try { + LongStream.generate(test).findFirst(); + } + catch (RuntimeException e) { + assertException(type, e, "long"); + } + } + + private void assertDoubleSupplier(DoubleSupplier test, Class type) { + assertNotNull(test); + try { + test.getAsDouble(); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "double"); + } + + try { + DoubleStream.generate(test).findFirst(); + } + catch (RuntimeException e) { + assertException(type, e, "double"); + } + } + + private void assertBooleanSupplier(BooleanSupplier test, Class type) { + assertNotNull(test); + try { + test.getAsBoolean(); + fail(); + } + catch (RuntimeException e) { + assertException(type, e, "boolean"); + } + } + + private void assertException(Class type, RuntimeException e, String message) { + assertEquals(type, e.getClass()); + assertEquals(Exception.class, e.getCause().getClass()); + assertEquals(message, e.getCause().getMessage()); + } +}