72 | *
73 | * @author Rainer Hahnekamp {@literal }
74 | */
75 | public class Sneaky {
76 | /**
77 | * returns a value from a lambda (Supplier) that can potentially throw an exception.
78 | *
79 | * @param supplier Supplier that can throw an exception
80 | * @param type of supplier's return value
81 | * @return a Supplier as defined in java.util.function
82 | */
83 | public static T sneak(SneakySupplier supplier) {
84 | return sneaked(supplier).get();
85 | }
86 |
87 | /**
88 | * Sneaky throws a BiConsumer lambda.
89 | *
90 | * @param biConsumer BiConsumer that can throw an exception
91 | * @param type of first argument
92 | * @param type of the second argument
93 | * @return a BiConsumer as defined in java.util.function
94 | */
95 | public static BiConsumer sneaked(
96 | SneakyBiConsumer biConsumer) {
97 | return (t, u) -> {
98 | @SuppressWarnings("unchecked")
99 | SneakyBiConsumer castedBiConsumer =
100 | (SneakyBiConsumer) biConsumer;
101 | castedBiConsumer.accept(t, u);
102 | };
103 | }
104 |
105 | /**
106 | * Sneaky throws a BiFunction lambda.
107 | *
108 | * @param biFunction BiFunction that can throw an exception
109 | * @param type of first argument
110 | * @param type of second argument
111 | * @param return type of biFunction
112 | * @return a BiFunction as defined in java.util.function
113 | */
114 | public static BiFunction sneaked(
115 | SneakyBiFunction biFunction) {
116 | return (t, u) -> {
117 | @SuppressWarnings("unchecked")
118 | SneakyBiFunction castedBiFunction =
119 | (SneakyBiFunction) biFunction;
120 | return castedBiFunction.apply(t, u);
121 | };
122 | }
123 |
124 | /**
125 | * Sneaky throws a BinaryOperator lambda.
126 | *
127 | * @param binaryOperator BinaryOperator that can throw an exception
128 | * @param type of the two arguments and the return type of the binaryOperator
129 | * @return a BinaryOperator as defined in java.util.function
130 | */
131 | public static BinaryOperator sneaked(
132 | SneakyBinaryOperator binaryOperator) {
133 | return (t1, t2) -> {
134 | @SuppressWarnings("unchecked")
135 | SneakyBinaryOperator castedBinaryOperator =
136 | (SneakyBinaryOperator) binaryOperator;
137 | return castedBinaryOperator.apply(t1, t2);
138 | };
139 | }
140 |
141 | /**
142 | * Sneaky throws a BiPredicate lambda.
143 | *
144 | * @param biPredicate BiPredicate that can throw an exception
145 | * @param type of first argument
146 | * @param type of second argument
147 | * @return a BiPredicate as defined in java.util.function
148 | */
149 | public static BiPredicate sneaked(
150 | SneakyBiPredicate biPredicate) {
151 | return (t, u) -> {
152 | @SuppressWarnings("unchecked")
153 | SneakyBiPredicate castedBiPredicate =
154 | (SneakyBiPredicate) biPredicate;
155 | return castedBiPredicate.test(t, u);
156 | };
157 | }
158 |
159 | /**
160 | * Sneaky throws a Consumer lambda.
161 | *
162 | * @param consumer Consumer that can throw an exception
163 | * @param type of first argument
164 | * @return a Consumer as defined in java.util.function
165 | */
166 | public static Consumer sneaked(SneakyConsumer consumer) {
167 | return t -> {
168 | @SuppressWarnings("unchecked")
169 | SneakyConsumer casedConsumer =
170 | (SneakyConsumer) consumer;
171 | casedConsumer.accept(t);
172 | };
173 | }
174 |
175 | /**
176 | * Sneaky throws a Function lambda.
177 | *
178 | * @param function Function that can throw an exception
179 | * @param type of first argument
180 | * @param type of the second argument
181 | * @return a Function as defined in java.util.function
182 | */
183 | public static Function sneaked(
184 | SneakyFunction function) {
185 | return t -> {
186 | @SuppressWarnings("unchecked")
187 | SneakyFunction f1 = (SneakyFunction) function;
188 | return f1.apply(t);
189 | };
190 | }
191 |
192 | /**
193 | * Sneaky throws a Predicate lambda.
194 | *
195 | * @param predicate Predicate that can throw an exception
196 | * @param type of first argument
197 | * @return a Predicate as defined in java.util.function
198 | */
199 | public static Predicate sneaked(SneakyPredicate predicate) {
200 | return t -> {
201 | @SuppressWarnings("unchecked")
202 | SneakyPredicate castedSneakyPredicate =
203 | (SneakyPredicate) predicate;
204 | return castedSneakyPredicate.test(t);
205 | };
206 | }
207 |
208 | /**
209 | * Sneaky throws a Runnable lambda.
210 | *
211 | * @param runnable Runnable that can throw an exception
212 | * @return a Runnable as defined in java.util.function
213 | */
214 | public static Runnable sneaked(SneakyRunnable runnable) {
215 | return () -> {
216 | @SuppressWarnings("unchecked")
217 | SneakyRunnable castedRunnable = (SneakyRunnable) runnable;
218 | castedRunnable.run();
219 | };
220 | }
221 |
222 | /**
223 | * Sneaky throws a Supplier lambda.
224 | *
225 | * @param supplier Supplier that can throw an exception
226 | * @param type of supplier's return value
227 | * @return a Supplier as defined in java.util.function
228 | */
229 | public static Supplier sneaked(SneakySupplier supplier) {
230 | return () -> {
231 | @SuppressWarnings("unchecked")
232 | SneakySupplier castedSupplier =
233 | (SneakySupplier) supplier;
234 | return castedSupplier.get();
235 | };
236 | }
237 |
238 | /**
239 | * Sneaky throws a UnaryOperator lambda.
240 | *
241 | * @param unaryOperator UnaryOperator that can throw an exception
242 | * @param type of unaryOperator's argument and returned value
243 | * @return a UnaryOperator as defined in java.util.function
244 | */
245 | public static UnaryOperator sneaked(
246 | SneakyUnaryOperator unaryOperator) {
247 | return t -> {
248 | @SuppressWarnings("unchecked")
249 | SneakyUnaryOperator castedUnaryOperator =
250 | (SneakyUnaryOperator) unaryOperator;
251 | return castedUnaryOperator.apply(t);
252 | };
253 | }
254 | }
255 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyBiConsumer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyBiConsumer {
29 | void accept(T t, U u) throws E;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyBiFunction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyBiFunction {
29 | R apply(T t, U u) throws E;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyBiPredicate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyBiPredicate {
29 | boolean test(T t, U u) throws E;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyBinaryOperator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyBinaryOperator
29 | extends SneakyBiFunction {}
30 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyConsumer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyConsumer {
29 | void accept(T t) throws E;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyFunction.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyFunction {
29 | R apply(T t) throws E;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyPredicate.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyPredicate {
29 | boolean test(T t) throws E;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyRunnable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyRunnable {
29 | void run() throws E;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakySupplier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakySupplier {
29 | T get() throws E;
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/rainerhahnekamp/sneakythrow/functional/SneakyUnaryOperator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow.functional;
26 |
27 | @FunctionalInterface
28 | public interface SneakyUnaryOperator extends SneakyFunction {}
29 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/BiPredicateTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static java.lang.Integer.parseInt;
29 | import static org.junit.jupiter.api.Assertions.assertThrows;
30 | import static org.junit.jupiter.api.Assertions.assertTrue;
31 |
32 | import java.util.function.BiPredicate;
33 |
34 | import org.junit.jupiter.api.Test;
35 |
36 | public class BiPredicateTest {
37 | @Test
38 | public void withoutException() {
39 | assertTrue(execute(sneaked((Integer a, String b) -> a == parseInt(b))));
40 | }
41 |
42 | @Test
43 | public void withException() {
44 | assertThrows(
45 | ArithmeticException.class,
46 | () -> execute(sneaked((Integer a, String b) -> (a / 0) == parseInt(b))));
47 | }
48 |
49 | private boolean execute(BiPredicate biPredicate) {
50 | return biPredicate.test(2, "2");
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/BinaryOperatorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static org.junit.jupiter.api.Assertions.assertEquals;
29 | import static org.junit.jupiter.api.Assertions.assertThrows;
30 |
31 | import org.junit.jupiter.api.Test;
32 |
33 | import java.util.function.BinaryOperator;
34 |
35 | class BinaryOperatorTest {
36 | @Test
37 | public void withoutException() {
38 | assertEquals(5, execute(sneaked((Integer a, Integer b) -> a / b), 25, 5));
39 | }
40 |
41 | @Test
42 | public void withException() {
43 | assertThrows(
44 | ArithmeticException.class, () -> execute(sneaked((Integer a, Integer b) -> a / b), 25, 0));
45 | }
46 |
47 | private int execute(BinaryOperator binaryOperator, Integer a, Integer b) {
48 | return binaryOperator.apply(a, b);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/ConstructorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static org.junit.jupiter.api.Assertions.assertTrue;
28 |
29 | import org.junit.jupiter.api.Test;
30 |
31 | public class ConstructorTest {
32 | @Test
33 | public void newInstance() {
34 | assertTrue(new Sneaky() instanceof Sneaky);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/ConsumerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static org.junit.jupiter.api.Assertions.assertEquals;
29 | import static org.junit.jupiter.api.Assertions.assertThrows;
30 |
31 | import java.util.ArrayList;
32 | import java.util.Collections;
33 | import java.util.List;
34 | import java.util.function.Consumer;
35 |
36 | import org.junit.jupiter.api.Test;
37 |
38 | public class ConsumerTest {
39 | @Test
40 | public void withoutException() {
41 | Consumer> consumer =
42 | sneaked(
43 | (List list) -> {
44 | list.add(5);
45 | });
46 |
47 | List list = new ArrayList<>();
48 | consumer.accept(list);
49 |
50 | assertEquals(1, list.size());
51 | }
52 |
53 | @Test
54 | public void withException() {
55 | Consumer> consumer =
56 | sneaked(
57 | (List list) -> {
58 | list.add(5);
59 | });
60 |
61 | assertThrows(
62 | UnsupportedOperationException.class, () -> consumer.accept(Collections.emptyList()));
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/FunctionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static java.lang.Integer.parseInt;
29 | import static org.junit.jupiter.api.Assertions.assertEquals;
30 | import static org.junit.jupiter.api.Assertions.assertThrows;
31 |
32 | import org.junit.jupiter.api.Test;
33 |
34 | import java.util.function.Function;
35 |
36 | class FunctionTest {
37 | @Test
38 | void withoutException() {
39 | Function function = sneaked((String b) -> parseInt(b) * 2);
40 | assertEquals(10, (int) function.apply("5"));
41 | }
42 |
43 | @Test
44 | void withRuntimeException() {
45 | Function function = sneaked((String b) -> parseInt(b) * 2);
46 |
47 | assertThrows(NumberFormatException.class, () -> function.apply("foo"));
48 | }
49 |
50 | @Test
51 | void withException() {
52 | Function function = sneaked(this::exceptionOn1);
53 | assertThrows(Exception.class, () -> function.apply(1));
54 | }
55 |
56 | private int exceptionOn1(int a) throws Exception {
57 | if (a == 1) {
58 | throw new Exception("not working");
59 | } else {
60 | return a;
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/PredicateTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static java.lang.Integer.parseInt;
29 | import static org.junit.jupiter.api.Assertions.assertThrows;
30 | import static org.junit.jupiter.api.Assertions.assertTrue;
31 |
32 | import java.util.function.Predicate;
33 |
34 | import org.junit.jupiter.api.Test;
35 |
36 | public class PredicateTest {
37 | @Test
38 | public void withoutException() {
39 | Predicate predicate = sneaked((String a) -> 2 == parseInt(a));
40 |
41 | assertTrue(predicate.test("2"));
42 | }
43 |
44 | @Test
45 | public void withException() {
46 | Predicate predicate = sneaked((String a) -> 2 == parseInt(a));
47 |
48 | assertThrows(NumberFormatException.class, () -> predicate.test("foo"));
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/RunnableTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static org.junit.jupiter.api.Assertions.assertEquals;
29 | import static org.junit.jupiter.api.Assertions.assertThrows;
30 |
31 | import java.util.ArrayList;
32 | import java.util.Collections;
33 | import java.util.List;
34 |
35 | import org.junit.jupiter.api.Test;
36 |
37 | public class RunnableTest {
38 | @Test
39 | public void withoutException() {
40 | List list = new ArrayList();
41 | Runnable runnable =
42 | sneaked(
43 | () -> {
44 | list.add(5);
45 | });
46 | runnable.run();
47 |
48 | assertEquals(1, list.size());
49 | }
50 |
51 | @Test
52 | public void withException() {
53 | List list = Collections.emptyList();
54 | Runnable runnable =
55 | sneaked(
56 | () -> {
57 | list.add(5);
58 | });
59 |
60 | assertThrows(UnsupportedOperationException.class, () -> runnable.run());
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/SneakyBiConsumerTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static org.junit.jupiter.api.Assertions.assertEquals;
29 | import static org.junit.jupiter.api.Assertions.assertThrows;
30 |
31 | import com.rainerhahnekamp.sneakythrow.functional.SneakyBiConsumer;
32 | import org.junit.jupiter.api.Test;
33 |
34 | import java.util.ArrayList;
35 | import java.util.Collections;
36 | import java.util.List;
37 | import java.util.function.BiConsumer;
38 |
39 | public class SneakyBiConsumerTest {
40 | @Test
41 | public void withoutException() {
42 | executeAndAssert(
43 | sneaked((SneakyBiConsumer, Integer, RuntimeException>) List::add));
44 | }
45 |
46 | @Test
47 | public void withException() {
48 | List list = Collections.emptyList();
49 | assertThrows(
50 | ArithmeticException.class,
51 | () ->
52 | executeAndAssert(
53 | sneaked(
54 | (List l, Integer e) -> {
55 | list.add(e / 0);
56 | })));
57 | }
58 |
59 | private void executeAndAssert(BiConsumer, Integer> consumer) {
60 | List list = new ArrayList<>();
61 | consumer.accept(list, 5);
62 | assertEquals(1, list.size());
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/SneakyBiFunctionTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static java.lang.Integer.parseInt;
29 | import static org.junit.jupiter.api.Assertions.assertEquals;
30 | import static org.junit.jupiter.api.Assertions.assertThrows;
31 |
32 | import java.util.function.BiFunction;
33 |
34 | import org.junit.jupiter.api.Test;
35 |
36 | public class SneakyBiFunctionTest {
37 | @Test
38 | public void withoutException() {
39 | assertEquals(500, execute(sneaked((Integer a, String b) -> parseInt(b) * a), 5, "100"));
40 | }
41 |
42 | @Test
43 | public void withException() {
44 | assertThrows(
45 | NumberFormatException.class,
46 | () -> execute(sneaked((Integer a, String b) -> parseInt(b) * a), 5, "foo"));
47 | }
48 |
49 | private int execute(BiFunction biFunction, Integer a, String b) {
50 | return biFunction.apply(a, b);
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/SneakyTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneak;
28 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
29 | import static org.junit.jupiter.api.Assertions.assertEquals;
30 | import static org.junit.jupiter.api.Assertions.assertThrows;
31 |
32 | import java.net.MalformedURLException;
33 | import java.net.URL;
34 | import java.util.List;
35 | import java.util.stream.Collectors;
36 | import java.util.stream.Stream;
37 |
38 | import org.junit.jupiter.api.Test;
39 |
40 | class SneakyTest {
41 | @Test
42 | public void withoutException() {
43 | assertEquals(String.class, sneak(() -> Class.forName("java.lang.String")));
44 |
45 | List urls =
46 | Stream.of("www.hahnekamp.com", "www.orf.at")
47 | .map(sneaked(this::createUrl))
48 | .collect(Collectors.toList());
49 | }
50 |
51 | private URL createUrl(String url) throws MalformedURLException {
52 | return new URL("https://www.hahnekamp.com");
53 | }
54 |
55 | @Test
56 | public void withException() {
57 | assertThrows(ClassNotFoundException.class, () -> sneak(() -> Class.forName("java.string")));
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/SupplierTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static org.junit.jupiter.api.Assertions.assertEquals;
29 | import static org.junit.jupiter.api.Assertions.assertThrows;
30 |
31 | import org.junit.jupiter.api.Test;
32 |
33 | import java.util.function.Supplier;
34 |
35 | public class SupplierTest {
36 | @Test
37 | public void withoutException() {
38 | Supplier supplier = sneaked(() -> 5);
39 | assertEquals(5, (int) supplier.get());
40 | }
41 |
42 | @Test
43 | public void withException() {
44 | Supplier supplier = sneaked(() -> 5 / 0);
45 | assertThrows(ArithmeticException.class, supplier::get);
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/TutorialTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneak;
28 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
29 | import static org.junit.jupiter.api.Assertions.assertEquals;
30 |
31 | import java.net.MalformedURLException;
32 | import java.net.URL;
33 | import java.util.List;
34 | import java.util.stream.Collectors;
35 | import java.util.stream.Stream;
36 |
37 | import org.junit.jupiter.api.Test;
38 |
39 | public class TutorialTest {
40 | @Test
41 | public void defaultUsage() {
42 | URL url = sneak(() -> new URL("https://www.hahnekamp.com"));
43 | assertEquals("https", url.getProtocol());
44 | }
45 |
46 | @Test
47 | public void streamUsage() {
48 | List urls =
49 | Stream.of("https://www.hahnekamp.com", "https://www.austria.info")
50 | .map(sneaked(this::createUrl))
51 | .collect(Collectors.toList());
52 |
53 | assertEquals("www.hahnekamp.com", urls.get(0).getHost());
54 | assertEquals("www.austria.info", urls.get(1).getHost());
55 | }
56 |
57 | private URL createUrl(String url) throws MalformedURLException {
58 | return new URL(url);
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/test/java/com/rainerhahnekamp/sneakythrow/UnaryOperatorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2018-present, Rainer Hahnekamp
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 |
25 | package com.rainerhahnekamp.sneakythrow;
26 |
27 | import static com.rainerhahnekamp.sneakythrow.Sneaky.sneaked;
28 | import static org.junit.jupiter.api.Assertions.assertEquals;
29 | import static org.junit.jupiter.api.Assertions.assertThrows;
30 |
31 | import java.util.function.UnaryOperator;
32 |
33 | import org.junit.jupiter.api.Test;
34 |
35 | public class UnaryOperatorTest {
36 | @Test
37 | public void withoutException() {
38 | UnaryOperator unaryOperator = sneaked((String name) -> "Hello " + name);
39 | assertEquals("Hello Sneaky", unaryOperator.apply("Sneaky"));
40 | }
41 |
42 | @Test
43 | public void withException() {
44 | UnaryOperator unaryOperator =
45 | sneaked(
46 | (String name) -> {
47 | if (name == null) {
48 | throw new NullPointerException();
49 | }
50 |
51 | return "Hello " + name;
52 | });
53 |
54 | assertThrows(NullPointerException.class, () -> unaryOperator.apply(null));
55 | }
56 | }
57 |
--------------------------------------------------------------------------------