/** * Lifts the given {@code partialFunction} into a total function that returns an {@code Option} result. * * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing) * @param <R> return type * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Some(result)} * if the function is defined for the given arguments, and {@code None} otherwise. */ @SuppressWarnings("RedundantTypeArguments") static <R> Function0<Option<R>> lift(CheckedFunction0<? extends R> partialFunction) { return () -> Try.<R>of(partialFunction::apply).toOption(); }
/** * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result. * * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing) * @param <R> return type * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)} * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise. */ static <R> Function0<Try<R>> liftTry(CheckedFunction0<? extends R> partialFunction) { return () -> Try.of(partialFunction::apply); }
/** * Folds either the {@code None} or the {@code Some} side of the Option value. * * @param ifNone maps the left value if this is a None * @param f maps the value if this is a Some * @param <U> type of the folded value * @return A value of type U */ default <U> U fold(Supplier<? extends U> ifNone, Function<? super T, ? extends U> f) { return this.<U>map(f).getOrElse(ifNone); }
@Override public T getNext() { final T next = nextOption.get(); nextOption = null; return next; } };
/** * Returns true, if this is {@code Some}, otherwise false, if this is {@code None}. * <p> * Please note that it is possible to create {@code new Some(null)}, which is defined. * * @return true, if this {@code Option} has a defined value, false otherwise */ default boolean isDefined() { return !isEmpty(); }
/** * Alias for {@link Try#failure(Throwable)} * * @param <T> Component type of the {@code Try}. * @param exception An exception. * @return A new {@link Try.Failure}. */ @SuppressWarnings("unchecked") public static <T> Try.Failure<T> Failure(Throwable exception) { return (Try.Failure<T>) Try.failure(exception); }
/** * Creates a {@code Try}-with-resources builder that operates on two {@link AutoCloseable} resources. * * @param t1Supplier The supplier of the 1st resource. * @param t2Supplier The supplier of the 2nd resource. * @param <T1> Type of the 1st resource. * @param <T2> Type of the 2nd resource. * @return a new {@link WithResources2} instance. */ static <T1 extends AutoCloseable, T2 extends AutoCloseable> WithResources2<T1, T2> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier) { return new WithResources2<>(t1Supplier, t2Supplier); }
@Override public String toString() { return stringPrefix() + "(" + value + ")"; } }
@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); }
/** * Combines two {@code Validation}s to form a {@link Builder}, which can then be used to perform further * combines, or apply a function to it in order to transform the {@link Builder} into a {@code Validation}. * * @param <U> type of the value contained in validation * @param validation the validation object to combine this with * @return an instance of Builder */ default <U> Builder<E, T, U> combine(Validation<E, U> validation) { return new Builder<>(this, validation); }
/** * Constructs a {@link Left} * * @param left The value. * @param <L> Type of left value. * @param <R> Type of right value. * @return A new {@code Left} instance. */ static <L, R> Either<L, R> left(L left) { return new Left<>(left); }
@Override public String toString() { return stringPrefix() + "(" + either + ")"; } }
/** * Lifts the given {@code partialFunction} into a total function that returns an {@code Option} result. * * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing) * @param <R> return type * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Some(result)} * if the function is defined for the given arguments, and {@code None} otherwise. */ @SuppressWarnings("RedundantTypeArguments") static <R> Function0<Option<R>> lift(Supplier<? extends R> partialFunction) { return () -> Try.<R>of(partialFunction::get).toOption(); }
/** * Alias for {@link Try#of(CheckedFunction0)} * * @param <T> Component type * @param supplier A checked supplier * @return {@link Try.Success} if no exception occurs, otherwise {@link Try.Failure} if an * exception occurs calling {@code supplier.get()}. */ public static <T> Try<T> Try(CheckedFunction0<? extends T> supplier) { return Try.of(supplier); }