|
@@ -0,0 +1,52 @@
|
|
|
+package thyyxxk.webserver.utils;
|
|
|
+
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.function.Supplier;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+public class TryUtil {
|
|
|
+
|
|
|
+ @FunctionalInterface
|
|
|
+ public interface TryRunnable {
|
|
|
+ void apply();
|
|
|
+ }
|
|
|
+
|
|
|
+ public TryUtil() {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private final List<Supplier<Object>> predicates = new LinkedList<>();
|
|
|
+
|
|
|
+ public static TryUtil create() {
|
|
|
+ return new TryUtil();
|
|
|
+ }
|
|
|
+
|
|
|
+ public TryUtil fun(Supplier<Object> predicate) {
|
|
|
+ this.predicates.add(predicate);
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object execute() {
|
|
|
+ for (Supplier<Object> predicate : predicates) {
|
|
|
+ try {
|
|
|
+ return predicate.get();
|
|
|
+ } catch (Exception ignore) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void ignoreErr(TryRunnable runnable) {
|
|
|
+ try {
|
|
|
+ runnable.apply();
|
|
|
+ } catch (Exception ignore) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|