package com.onthegomap.planetiler.util; /** * A container for the result of an operation that may succeed or fail. * * @param Type of the result value, if success */ public interface Try { /** * Calls {@code supplier} and wraps the result in {@link Success} if successful, or {@link Failure} if it throws an * exception. */ static Try apply(SupplierThatThrows supplier) { try { return success(supplier.get()); } catch (Exception e) { return failure(e); } } static Success success(T item) { return new Success<>(item); } static Failure failure(Exception throwable) { return new Failure<>(throwable); } /** * Returns the result if success, or throws an exception if failure. * * @throws IllegalStateException wrapping the exception on failure */ T get(); default boolean isSuccess() { return !isFailure(); } default boolean isFailure() { return exception() != null; } default Exception exception() { return null; } record Success (T get) implements Try {} record Failure (@Override Exception exception) implements Try { @Override public T get() { throw new IllegalStateException(exception); } } @FunctionalInterface interface SupplierThatThrows { @SuppressWarnings("java:S112") T get() throws Exception; } }