} else { try { return new Success<>(mapper.apply(get())); } catch (Throwable t) { return new Failure<>(t);
} else { try { return new Success<>(mapper.apply(get())); } catch (Throwable t) { return new Failure<>(t);
/** * Creates a Try of a CheckedFunction0. * * @param supplier A checked supplier * @param <T> Component type * @return {@code Success(supplier.apply())} if no exception occurs, otherwise {@code Failure(throwable)} if an * exception occurs calling {@code supplier.apply()}. */ static <T> Try<T> of(CheckedFunction0<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); try { return new Success<>(supplier.apply()); } catch (Throwable t) { return new Failure<>(t); } }
/** * Creates a Try of a CheckedRunnable. * * @param runnable A checked runnable * @return {@code Success(null)} if no exception occurs, otherwise {@code Failure(throwable)} if an exception occurs * calling {@code runnable.run()}. */ static Try<Void> run(CheckedRunnable runnable) { Objects.requireNonNull(runnable, "runnable is null"); try { runnable.run(); return new Success<>(null); // null represents the absence of an value, i.e. Void } catch (Throwable t) { return new Failure<>(t); } }
@Unapply static <T> Tuple1<T> Success(Try.Success<T> success) { return Tuple.of(success.get()); } @Unapply
/** * Returns {@code Success(throwable)} if this is a {@code Failure(throwable)}, otherwise * a {@code Failure(new NoSuchElementException("Success.failed()"))} if this is a Success. * * @return a new Try */ default Try<Throwable> failed() { if (isFailure()) { return new Success<>(getCause()); } else { return new Failure<>(new NoSuchElementException("Success.failed()")); } }
@Override public String toString() { return stringPrefix() + "(" + value + ")"; } }
/** * Creates a {@link Success} that contains the given {@code value}. Shortcut for {@code new Success<>(value)}. * * @param value A value. * @param <T> Type of the given {@code value}. * @return A new {@code Success}. */ static <T> Try<T> success(T value) { return new Success<>(value); }
/** * Creates a Try of a CheckedFunction0. * * @param supplier A checked supplier * @param <T> Component type * @return {@code Success(supplier.apply())} if no exception occurs, otherwise {@code Failure(throwable)} if an * exception occurs calling {@code supplier.apply()}. */ static <T> Try<T> of(CheckedFunction0<? extends T> supplier) { Objects.requireNonNull(supplier, "supplier is null"); try { return new Success<>(supplier.apply()); } catch (Throwable t) { return new Failure<>(t); } }
/** * Creates a Try of a CheckedRunnable. * * @param runnable A checked runnable * @return {@code Success(null)} if no exception occurs, otherwise {@code Failure(throwable)} if an exception occurs * calling {@code runnable.run()}. */ static Try<Void> run(CheckedRunnable runnable) { Objects.requireNonNull(runnable, "runnable is null"); try { runnable.run(); return new Success<>(null); // null represents the absence of an value, i.e. Void } catch (Throwable t) { return new Failure<>(t); } }
@Unapply static <T> Tuple1<T> Success(Try.Success<T> success) { return Tuple.of(success.get()); } @Unapply
/** * Returns {@code Success(throwable)} if this is a {@code Failure(throwable)}, otherwise * a {@code Failure(new NoSuchElementException("Success.failed()"))} if this is a Success. * * @return a new Try */ default Try<Throwable> failed() { if (isFailure()) { return new Success<>(getCause()); } else { return new Failure<>(new NoSuchElementException("Success.failed()")); } }
@Override public String toString() { return stringPrefix() + "(" + value + ")"; } }
/** * Creates a {@link Success} that contains the given {@code value}. Shortcut for {@code new Success<>(value)}. * * @param value A value. * @param <T> Type of the given {@code value}. * @return A new {@code Success}. */ static <T> Try<T> success(T value) { return new Success<>(value); }