@Override public Result<T> build() { return new Result<>( value, failure); }
/** * Creates a failed result containing a failure. * <p> * This is useful for converting an existing {@code Failure} instance to a result. * * @param <R> the expected type of the result * @param failure details of the failure * @return a failed result containing the specified failure */ public static <R> Result<R> failure(Failure failure) { return new Result<>(failure); }
/** * Creates a failed result caused by an exception. * <p> * The failure will have a reason of {@code ERROR}. * * @param <R> the expected type of the result * @param exception the cause of the failure * @return a failure result */ public static <R> Result<R> failure(Exception exception) { return new Result<>(Failure.of(FailureReason.ERROR, exception)); }
/** * Creates a failed result caused by an exception with a specified reason. * * @param <R> the expected type of the result * @param reason the result reason * @param exception the cause of the failure * @return a failure result */ public static <R> Result<R> failure(FailureReason reason, Exception exception) { return new Result<>(Failure.of(reason, exception)); }
/** * Creates a successful result wrapping a value. * <p> * This returns a successful result object for the non-null value. * <p> * Note that passing an instance of {@code Failure} to this method would * be a programming error. * * @param <R> the type of the value * @param value the result value * @return a successful result wrapping the value */ public static <R> Result<R> success(R value) { return new Result<>(ArgChecker.notNull(value, "value")); }
/** * Creates a failed result caused by an exception. * <p> * The failure will have a reason of {@code ERROR}. * <p> * The message is produced using a template that contains zero to many "{}" placeholders. * Each placeholder is replaced by the next available argument. * If there are too few arguments, then the message will be left with placeholders. * If there are too many arguments, then the excess arguments are appended to the * end of the message. No attempt is made to format the arguments. * See {@link Messages#format(String, Object...)} for more details. * * @param <R> the expected type of the result * @param exception the cause of the failure * @param message a message explaining the failure, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return a failure result */ public static <R> Result<R> failure(Exception exception, String message, Object... messageArgs) { return new Result<>(Failure.of(FailureReason.ERROR, exception, message, messageArgs)); }
/** * Creates a failed result caused by an exception with a specified reason and message. * <p> * The message is produced using a template that contains zero to many "{}" placeholders. * Each placeholder is replaced by the next available argument. * If there are too few arguments, then the message will be left with placeholders. * If there are too many arguments, then the excess arguments are appended to the * end of the message. No attempt is made to format the arguments. * See {@link Messages#format(String, Object...)} for more details. * * @param <R> the expected type of the result * @param reason the result reason * @param exception the cause of the failure * @param message a message explaining the failure, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return a failure result */ public static <R> Result<R> failure( FailureReason reason, Exception exception, String message, Object... messageArgs) { return new Result<>(Failure.of(reason, exception, message, messageArgs)); }
/** * Creates a failed result specifying the failure reason. * <p> * The message is produced using a template that contains zero to many "{}" placeholders. * Each placeholder is replaced by the next available argument. * If there are too few arguments, then the message will be left with placeholders. * If there are too many arguments, then the excess arguments are appended to the * end of the message. No attempt is made to format the arguments. * See {@link Messages#format(String, Object...)} for more details. * * @param <R> the expected type of the result * @param reason the result reason * @param message a message explaining the failure, uses "{}" for inserting {@code messageArgs} * @param messageArgs the arguments for the message * @return a failure result */ public static <R> Result<R> failure(FailureReason reason, String message, Object... messageArgs) { String msg = Messages.format(message, messageArgs); return new Result<>(Failure.of(FailureItem.of(reason, msg, 1))); }
/** * Creates a failed result combining multiple failed results. * <p> * The input results can be successes or failures, only the failures will be included in the created result. * Intended to be used with {@link #anyFailures(Iterable)}. * <blockquote><pre> * if (Result.anyFailures(results) { * return Result.failure(results); * } * </pre></blockquote> * * @param <R> the expected type of the result * @param results multiple results, of which at least one must be a failure, not empty * @return a failed result wrapping multiple other failed results * @throws IllegalArgumentException if results is empty or contains nothing but successes */ public static <R> Result<R> failure(Iterable<? extends Result<?>> results) { ArgChecker.notEmpty(results, "results"); ImmutableSet<FailureItem> items = Guavate.stream(results) .filter(Result::isFailure) .map(Result::getFailure) .flatMap(f -> f.getItems().stream()) .collect(Guavate.toImmutableSet()); if (items.isEmpty()) { throw new IllegalArgumentException("All results were successes"); } return new Result<>(Failure.of(items)); }