mirror of
https://github.com/chillzhuang/blade-tool
synced 2024-11-15 06:59:29 +08:00
✨ 添加 Unchecked
This commit is contained in:
parent
ca85cf1077
commit
cc2d625d5d
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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<T> extends Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run this callable.
|
||||||
|
*
|
||||||
|
* @return result
|
||||||
|
* @throws Throwable CheckedException
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
T call() throws Throwable;
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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<T> 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;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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<T> extends Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the Consumer
|
||||||
|
*
|
||||||
|
* @param t T
|
||||||
|
* @throws Throwable UncheckedException
|
||||||
|
*/
|
||||||
|
void accept(@Nullable T t) throws Throwable;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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<T, R> extends Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the Function
|
||||||
|
*
|
||||||
|
* @param t T
|
||||||
|
* @return R R
|
||||||
|
* @throws Throwable CheckedException
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
R apply(@Nullable T t) throws Throwable;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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<T> 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;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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<T> extends Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the Supplier
|
||||||
|
*
|
||||||
|
* @return T
|
||||||
|
* @throws Throwable CheckedException
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
T get() throws Throwable;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.gnu.org/licenses/lgpl.html
|
||||||
|
* <p>
|
||||||
|
* 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 受检异常处理
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* https://segmentfault.com/a/1190000007832130
|
||||||
|
* https://github.com/jOOQ/jOOL
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @author L.cm
|
||||||
|
*/
|
||||||
|
public class Unchecked {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造受检的 function
|
||||||
|
*
|
||||||
|
* @param function CheckedFunction
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return Function
|
||||||
|
*/
|
||||||
|
public static <T, R> Function<T, R> function(CheckedFunction<T, R> function) {
|
||||||
|
Objects.requireNonNull(function);
|
||||||
|
return t -> {
|
||||||
|
try {
|
||||||
|
return function.apply(t);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw Exceptions.unchecked(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造受检的 supplier
|
||||||
|
*
|
||||||
|
* @param consumer CheckedConsumer
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return Consumer
|
||||||
|
*/
|
||||||
|
public static <T> Consumer<T> consumer(CheckedConsumer<T> consumer) {
|
||||||
|
Objects.requireNonNull(consumer);
|
||||||
|
return t -> {
|
||||||
|
try {
|
||||||
|
consumer.accept(t);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw Exceptions.unchecked(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造受检的 supplier
|
||||||
|
*
|
||||||
|
* @param supplier CheckedSupplier
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return Supplier
|
||||||
|
*/
|
||||||
|
public static <T> Supplier<T> supplier(CheckedSupplier<T> supplier) {
|
||||||
|
Objects.requireNonNull(supplier);
|
||||||
|
return () -> {
|
||||||
|
try {
|
||||||
|
return supplier.get();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw Exceptions.unchecked(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造受检的 predicate
|
||||||
|
*
|
||||||
|
* @param predicate CheckedPredicate
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return Supplier
|
||||||
|
*/
|
||||||
|
public static <T> Predicate<T> predicate(CheckedPredicate<T> 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 <T> 泛型
|
||||||
|
* @return Callable
|
||||||
|
*/
|
||||||
|
public static <T> Callable<T> callable(CheckedCallable<T> callable) {
|
||||||
|
Objects.requireNonNull(callable);
|
||||||
|
return () -> {
|
||||||
|
try {
|
||||||
|
return callable.call();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw Exceptions.unchecked(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 构造受检的 comparator
|
||||||
|
*
|
||||||
|
* @param comparator CheckedComparator
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @return Comparator
|
||||||
|
*/
|
||||||
|
public static <T> Comparator<T> comparator(CheckedComparator<T> comparator) {
|
||||||
|
Objects.requireNonNull(comparator);
|
||||||
|
return (T o1, T o2) -> {
|
||||||
|
try {
|
||||||
|
return comparator.compare(o1, o2);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw Exceptions.unchecked(e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user