TryUtil.java 976 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package thyyxxk.webserver.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.function.Supplier;
  6. @Slf4j
  7. public class TryUtil {
  8. @FunctionalInterface
  9. public interface TryRunnable {
  10. void apply();
  11. }
  12. public TryUtil() {
  13. }
  14. private final List<Supplier<Object>> predicates = new LinkedList<>();
  15. public static TryUtil create() {
  16. return new TryUtil();
  17. }
  18. public TryUtil fun(Supplier<Object> predicate) {
  19. this.predicates.add(predicate);
  20. return this;
  21. }
  22. public Object execute() {
  23. for (Supplier<Object> predicate : predicates) {
  24. try {
  25. return predicate.get();
  26. } catch (Exception ignore) {
  27. }
  28. }
  29. return null;
  30. }
  31. public static void ignoreErr(TryRunnable runnable) {
  32. try {
  33. runnable.apply();
  34. } catch (Exception ignore) {
  35. }
  36. }
  37. }