diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedCallable.java b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedCallable.java new file mode 100644 index 0000000..12828c6 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedCallable.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.tool.function; + +import org.springframework.lang.Nullable; + +import java.io.Serializable; + +/** + * 受检的 Callable + * + * @author L.cm + */ +@FunctionalInterface +public interface CheckedCallable extends Serializable { + + /** + * Run this callable. + * + * @return result + * @throws Throwable CheckedException + */ + @Nullable + T call() throws Throwable; +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedComparator.java b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedComparator.java new file mode 100644 index 0000000..3baa3a1 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedComparator.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.tool.function; + +import java.io.Serializable; + +/** + * 受检的 Comparator + * + * @author L.cm + */ +@FunctionalInterface +public interface CheckedComparator extends Serializable { + + /** + * Compares its two arguments for order. + * + * @param o1 o1 + * @param o2 o2 + * @return int + * @throws Throwable CheckedException + */ + int compare(T o1, T o2) throws Throwable; + +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedConsumer.java b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedConsumer.java new file mode 100644 index 0000000..61f60df --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedConsumer.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.tool.function; + +import org.springframework.lang.Nullable; + +import java.io.Serializable; + +/** + * 受检的 Consumer + * + * @author L.cm + */ +@FunctionalInterface +public interface CheckedConsumer extends Serializable { + + /** + * Run the Consumer + * + * @param t T + * @throws Throwable UncheckedException + */ + void accept(@Nullable T t) throws Throwable; + +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedFunction.java b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedFunction.java new file mode 100644 index 0000000..ebaab0e --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedFunction.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.tool.function; + +import org.springframework.lang.Nullable; + +import java.io.Serializable; + +/** + * 受检的 function + * + * @author L.cm + */ +@FunctionalInterface +public interface CheckedFunction extends Serializable { + + /** + * Run the Function + * + * @param t T + * @return R R + * @throws Throwable CheckedException + */ + @Nullable + R apply(@Nullable T t) throws Throwable; + +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedPredicate.java b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedPredicate.java new file mode 100644 index 0000000..c19fec8 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedPredicate.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.tool.function; + +import java.io.Serializable; + +/** + * 受检的 Predicate + * + * @author L.cm + */ +@FunctionalInterface +public interface CheckedPredicate extends Serializable { + + /** + * Evaluates this predicate on the given argument. + * + * @param t the input argument + * @return {@code true} if the input argument matches the predicate, + * otherwise {@code false} + * @throws Throwable CheckedException + */ + boolean test(T t) throws Throwable; + +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedRunnable.java b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedRunnable.java new file mode 100644 index 0000000..d3dff73 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedRunnable.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.tool.function; + +import java.io.Serializable; + +/** + * 受检的 runnable + * + * @author L.cm + */ +@FunctionalInterface +public interface CheckedRunnable extends Serializable { + + /** + * Run this runnable. + * + * @throws Throwable CheckedException + */ + void run() throws Throwable; + +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedSupplier.java b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedSupplier.java new file mode 100644 index 0000000..dbdae1e --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/function/CheckedSupplier.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.tool.function; + +import org.springframework.lang.Nullable; + +import java.io.Serializable; + +/** + * 受检的 Supplier + * + * @author L.cm + */ +@FunctionalInterface +public interface CheckedSupplier extends Serializable { + + /** + * Run the Supplier + * + * @return T + * @throws Throwable CheckedException + */ + @Nullable + T get() throws Throwable; + +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/Unchecked.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/Unchecked.java new file mode 100644 index 0000000..1029ce0 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/Unchecked.java @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springblade.core.tool.utils; + + +import org.springblade.core.tool.function.*; + +import java.util.Comparator; +import java.util.Objects; +import java.util.concurrent.Callable; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + +/** + * Lambda 受检异常处理 + * + *

+ * https://segmentfault.com/a/1190000007832130 + * https://github.com/jOOQ/jOOL + *

+ * + * @author L.cm + */ +public class Unchecked { + + /** + * 构造受检的 function + * + * @param function CheckedFunction + * @param 泛型 + * @return Function + */ + public static Function function(CheckedFunction function) { + Objects.requireNonNull(function); + return t -> { + try { + return function.apply(t); + } catch (Throwable e) { + throw Exceptions.unchecked(e); + } + }; + } + + /** + * 构造受检的 supplier + * + * @param consumer CheckedConsumer + * @param 泛型 + * @return Consumer + */ + public static Consumer consumer(CheckedConsumer consumer) { + Objects.requireNonNull(consumer); + return t -> { + try { + consumer.accept(t); + } catch (Throwable e) { + throw Exceptions.unchecked(e); + } + }; + } + + /** + * 构造受检的 supplier + * + * @param supplier CheckedSupplier + * @param 泛型 + * @return Supplier + */ + public static Supplier supplier(CheckedSupplier supplier) { + Objects.requireNonNull(supplier); + return () -> { + try { + return supplier.get(); + } catch (Throwable e) { + throw Exceptions.unchecked(e); + } + }; + } + + /** + * 构造受检的 predicate + * + * @param predicate CheckedPredicate + * @param 泛型 + * @return Supplier + */ + public static Predicate predicate(CheckedPredicate predicate) { + Objects.requireNonNull(predicate); + return (t) -> { + try { + return predicate.test(t); + } catch (Throwable e) { + throw Exceptions.unchecked(e); + } + }; + } + + /** + * 构造受检的 runnable + * + * @param runnable CheckedRunnable + * @return Runnable + */ + public static Runnable runnable(CheckedRunnable runnable) { + Objects.requireNonNull(runnable); + return () -> { + try { + runnable.run(); + } catch (Throwable e) { + throw Exceptions.unchecked(e); + } + }; + } + + /** + * 构造受检的 callable + * + * @param callable CheckedCallable + * @param 泛型 + * @return Callable + */ + public static Callable callable(CheckedCallable callable) { + Objects.requireNonNull(callable); + return () -> { + try { + return callable.call(); + } catch (Throwable e) { + throw Exceptions.unchecked(e); + } + }; + } + + /** + * 构造受检的 comparator + * + * @param comparator CheckedComparator + * @param 泛型 + * @return Comparator + */ + public static Comparator comparator(CheckedComparator comparator) { + Objects.requireNonNull(comparator); + return (T o1, T o2) -> { + try { + return comparator.compare(o1, o2); + } catch (Throwable e) { + throw Exceptions.unchecked(e); + } + }; + } + +}