All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landawn.abacus.util.Result Maven / Gradle / Ivy

Go to download

A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.

The newest version!
/*
 * Copyright (c) 2019, Haiyang Li.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.landawn.abacus.util;

import java.util.function.Function;
import java.util.function.Supplier;

import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.SuppressFBWarnings;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.u.Optional;

/**
 * This class encapsulates a result of an operation that can either be a value of type {@code T} or an exception of type {@code E}.
 * The class implements the Immutable interface, indicating that instances of this class are immutable.
 * The class is parameterized by two types: {@code T} which is the type of the result value, and {@code E} which is the type of the exception.
 * The exception type {@code E} is constrained to be a subtype of {@code Throwable}.
 *
 * @param  The type of the result value.
 * @param  The type of the exception. Must be a subtype of {@code Throwable}.
 *
 * @param 
 * @param 
 */
@com.landawn.abacus.annotation.Immutable
public class Result implements Immutable {

    private final T value;

    private final E exception;

    Result(final T value, final E exception) {
        this.value = value;
        this.exception = exception;
    }

    /**
     * Creates a new instance of the Result class with the provided value and exception.
     *
     * @param  The type of the result value.
     * @param  The type of the exception. Must be a subtype of {@code Throwable}.
     * @param value The value of the result.
     * @param exception The exception that occurred during the operation.
     * @return A new instance of the Result class.
     */
    public static  Result of(final T value, final E exception) {
        return new Result<>(value, exception);
    }

    /**
     * Checks if the operation resulted in a failure.
     *
     * @return {@code true} if the operation resulted in a failure (an exception was thrown), {@code false} otherwise.
     */
    public boolean isFailure() {
        return exception != null;
    }

    /**
     * Checks if the operation resulted in a success.
     *
     * @return {@code true} if the operation resulted in a success (no exception was thrown), {@code false} otherwise.
     */
    public boolean isSuccess() {
        return exception == null;
    }

    /**
     * Executes the provided action if the operation resulted in a failure (an exception was thrown).
     *
     * @param  The type of the exception that the action can throw. Must be a subtype of {@code Throwable}.
     * @param actionOnFailure The action to be executed if the operation resulted in a failure.
     * @throws E2 if the action throws an exception.
     */
    public  void ifFailure(final Throwables.Consumer actionOnFailure) throws E2 {
        ifFailureOrElse(actionOnFailure, Fn.doNothing());
    }

    /**
     * Executes the provided action if the operation resulted in a failure (an exception was thrown), otherwise executes the action on success.
     *
     * @param  The type of the exception that the actionOnFailure can throw. Must be a subtype of {@code Throwable}.
     * @param  The type of the exception that the actionOnSuccess can throw. Must be a subtype of {@code Throwable}.
     * @param actionOnFailure The action to be executed if the operation resulted in a failure.
     * @param actionOnSuccess The action to be executed if the operation resulted in a success.
     * @throws IllegalArgumentException if either actionOnFailure or actionOnSuccess is {@code null}.
     * @throws E2 if the actionOnFailure throws an exception.
     * @throws E3 if the actionOnSuccess throws an exception.
     */
    public  void ifFailureOrElse(final Throwables.Consumer actionOnFailure,
            final Throwables.Consumer actionOnSuccess) throws IllegalArgumentException, E2, E3 {
        N.checkArgNotNull(actionOnFailure, cs.actionOnFailure);
        N.checkArgNotNull(actionOnSuccess, cs.actionOnSuccess);

        if (exception != null) {
            actionOnFailure.accept(exception);
        } else {
            actionOnSuccess.accept(value);
        }
    }

    /**
     * Executes the provided action if the operation resulted in a success (no exception was thrown).
     *
     * @param  The type of the exception that the action can throw. Must be a subtype of {@code Throwable}.
     * @param actionOnSuccess The action to be executed if the operation resulted in a success.
     * @throws E2 if the action throws an exception.
     */
    public  void ifSuccess(final Throwables.Consumer actionOnSuccess) throws E2 {
        ifSuccessOrElse(actionOnSuccess, Fn.doNothing());
    }

    /**
     * Executes the provided action if the operation resulted in a success (no exception was thrown), otherwise executes the action on failure.
     *
     * @param  The type of the exception that the actionOnSuccess can throw. Must be a subtype of {@code Throwable}.
     * @param  The type of the exception that the actionOnFailure can throw. Must be a subtype of {@code Throwable}.
     * @param actionOnSuccess The action to be executed if the operation resulted in a success.
     * @param actionOnFailure The action to be executed if the operation resulted in a failure.
     * @throws IllegalArgumentException if either actionOnSuccess or actionOnFailure is {@code null}.
     * @throws E2 if the actionOnSuccess throws an exception.
     * @throws E3 if the actionOnFailure throws an exception.
     */
    public  void ifSuccessOrElse(final Throwables.Consumer actionOnSuccess,
            final Throwables.Consumer actionOnFailure) throws IllegalArgumentException, E2, E3 {
        N.checkArgNotNull(actionOnSuccess, cs.actionOnSuccess);
        N.checkArgNotNull(actionOnFailure, cs.actionOnFailure);

        if (exception == null) {
            actionOnSuccess.accept(value);
        } else {
            actionOnFailure.accept(exception);
        }
    }

    //    /**
    //     *
    //     * @param defaultValueIfErrorOccurred
    //     * @return
    //     * @deprecated replaced by {@link #orElseIfFailure(Object)}
    //     */
    //    @Deprecated
    //    public T orElse(final T defaultValueIfErrorOccurred) {
    //        return orElseIfFailure(defaultValueIfErrorOccurred);
    //    }
    //
    //    /**
    //     *
    //     * @param otherIfErrorOccurred
    //     * @return
    //     * @deprecated replaced by {@link #orElseGetIfFailure(Supplier)}
    //     */
    //    @Deprecated
    //    public T orElseGet(final Supplier otherIfErrorOccurred) {
    //        return orElseGetIfFailure(otherIfErrorOccurred);
    //    }

    /**
     *
     * @param defaultValueIfErrorOccurred
     * @return
     */
    public T orElseIfFailure(final T defaultValueIfErrorOccurred) {
        if (exception == null) {
            return value;
        } else {
            return defaultValueIfErrorOccurred;
        }
    }

    /**
     *
     * @param otherIfErrorOccurred
     * @return
     * @throws IllegalArgumentException
     */
    public T orElseGetIfFailure(final Supplier otherIfErrorOccurred) throws IllegalArgumentException {
        N.checkArgNotNull(otherIfErrorOccurred, cs.otherIfErrorOccurred);

        if (exception == null) {
            return value;
        } else {
            return otherIfErrorOccurred.get();
        }
    }

    /**
     * Or else throw.
     *
     * @return
     * @throws E the e
     */
    public T orElseThrow() throws E {
        if (exception == null) {
            return value;
        } else {
            throw exception;
        }
    }

    /**
     * Or else throw.
     *
     * @param 
     * @param exceptionSupplierIfErrorOccurred
     * @return
     * @throws IllegalArgumentException
     * @throws E2 the e2
     */
    public  T orElseThrow(final Function exceptionSupplierIfErrorOccurred) throws IllegalArgumentException, E2 {
        N.checkArgNotNull(exceptionSupplierIfErrorOccurred, cs.exceptionSupplierIfErrorOccurred);

        if (exception == null) {
            return value;
        } else {
            throw exceptionSupplierIfErrorOccurred.apply(exception);
        }
    }

    /**
     *
     * @param 
     * @param exceptionSupplier
     * @return
     * @throws E2
     */
    public  T orElseThrow(final Supplier exceptionSupplier) throws E2 {
        if (exception == null) {
            return value;
        } else {
            throw exceptionSupplier.get();
        }
    }

    /**
     *
     * @param 
     * @param exception
     * @return
     * @throws E2
     * @deprecated replaced by {@link #orElseThrow(Supplier)}
     */
    @Deprecated
    public  T orElseThrow(final E2 exception) throws E2 {
        if (exception == null) {
            return value;
        } else {
            throw exception;
        }
    }

    /**
     * Returns the {@code Exception} if occurred, otherwise {@code null} is returned.
     *
     * @return
     */
    @Beta
    public E getException() {
        return exception;
    }

    /**
     * Returns the {@code Exception} if occurred, otherwise an empty {@code Optional} is returned.
     *
     * @return
     * @deprecated replaced by {@code getException}
     */
    @Deprecated
    @Beta
    public Optional getExceptionIfPresent() {
        return Optional.ofNullable(exception);
    }

    //    /**
    //     *
    //     * @return
    //     * @deprecated
    //     */
    //    @Deprecated
    //    public Nullable toNullable() {
    //        return exception == null ? Nullable.of(value) : Nullable. empty();
    //    }

    public Pair toPair() {
        return Pair.of(value, exception);
    }

    public Tuple2 toTuple() {
        return Tuple.of(value, exception);
    }

    @Override
    public int hashCode() {
        return (exception == null) ? N.hashCode(value) : exception.hashCode();
    }

    /**
     *
     * @param obj
     * @return
     */
    @SuppressFBWarnings
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }

        //noinspection rawtypes
        if (obj instanceof final Result other) { // NOSONAR
            return N.equals(other.value, value) && N.equals(other.exception, exception);
        }

        return false;
    }

    @Override
    public String toString() {
        return "{value=" + N.toString(value) + ", exception=" + N.toString(exception) + "}";
    }

    @Beta
    public static class R extends Result {
        R(final T value, final RuntimeException exception) {
            super(value, exception);
        }

        /**
         *
         * @param 
         * @param value
         * @param exception
         * @return
         */
        public static  Result.R of(final T value, final RuntimeException exception) {
            return new R<>(value, exception);
        }
    }

    //    @Beta
    //    public static class X extends Result {
    //        X(T value, Exception exception) {
    //            super(value, exception);
    //        }
    //
    //        public static  Result.X of(final T value, final Exception exception) {
    //            return new X<>(value, exception);
    //        }
    //    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy