import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        // Using tryGet with a supplier that returns a string
        String result1 = SafeExecutor.tryGet(() -> "Hello, World!");
        System.out.println("tryGet: " + result1);

        // Using tryGet with a supplier that throws an exception
        String result2 = SafeExecutor.tryGet(() -> {
            throw new RuntimeException("Error in Supplier");
        });
        System.out.println("tryGet with exception: " + result2);

        // Using tryRun with a runnable that prints a message
        System.out.print("tryRun: ");
        SafeExecutor.tryRun(() -> System.out.println("Runnable executed"));

        // Using tryRun with a runnable that throws an exception
        System.out.println("tryRun with exception:");
        SafeExecutor.tryRun(() -> {
            throw new IllegalStateException("Runnable error");
        });

        // Using tryGetOptional with a supplier that returns an integer
        Optional<Integer> opt1 = SafeExecutor.tryGetOptional(() -> 42);
        System.out.println("tryGetOptional: " + opt1);

        // Using tryGetOptional with a supplier that throws an exception
        Optional<Integer> opt2 = SafeExecutor.tryGetOptional(() -> {
            throw new ArithmeticException("Division by zero");
        });
        System.out.println("tryGetOptional with exception: " + opt2);

        // Using tryGetAndCollectError with a supplier that throws and stores the exception
        Map<String, Throwable> errorMap = new HashMap<>();
        String result3 = SafeExecutor.tryGetAndCollectError("key1", errorMap, () -> {
            throw new NullPointerException("Simulated NPE");
        });
        System.out.println("tryGetAndCollectError with exception: " + result3);
        System.out.println("Collected error: " + errorMap.get("key1"));

        // Using tryGetAndCollectError with a supplier that returns a value
        errorMap.clear();
        String result4 = SafeExecutor.tryGetAndCollectError("key2", errorMap, () -> "OK");
        System.out.println("tryGetAndCollectError: " + result4);
        System.out.println("Error map size: " + errorMap.size());
    }
}

final class SafeExecutor {

    public static <T> T tryGet(Supplier<T> action) {
        try {
            return action.get();
        } catch (Throwable e) {
            return null;
        }
    }

    public static void tryRun(Runnable action) {
        tryGet(() -> {
            action.run();
            return null;
        });
    }

    public static <T> Optional<T> tryGetOptional(Supplier<T> action) {
        return Optional.ofNullable(tryGet(action));
    }

    public static <T> T tryGetAndCollectError(
        String key,
        Map<String, Throwable> errorMap,
        Supplier<T> action
    ) {
        try {
            return action.get();
        } catch (Throwable e) {
            errorMap.put(key, e);
            return null;
        }
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: