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.

There is a newer version: 5.2.4
Show 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;

/**
 *
 * @author Haiyang Li
 * @param 
 * @param 
 */
@com.landawn.abacus.annotation.Immutable
public class Result implements Immutable {

    private final T value;

    private final E exception;

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

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

    /**
     * Checks if is failure.
     *
     * @return true, if is failure
     */
    public boolean isFailure() {
        return exception != null;
    }

    /**
     * Checks if is success.
     *
     * @return true, if is success
     */
    public boolean isSuccess() {
        return exception == null;
    }

    /**
     *
     * @param 
     * @param actionOnFailure
     * @throws E2 the e2
     */
    public  void ifFailure(final Throwables.Consumer actionOnFailure) throws E2 {
        ifFailureOrElse(actionOnFailure, Fn.doNothing());
    }

    /**
     * If failure or else.
     *
     * @param 
     * @param 
     * @param actionOnFailure
     * @param actionOnSuccess
     * @throws E2 the e2
     * @throws E3 the e3
     */
    public  void ifFailureOrElse(final Throwables.Consumer actionOnFailure,
            final Throwables.Consumer actionOnSuccess) throws E2, E3 {
        N.checkArgNotNull(actionOnFailure, "actionOnFailure");
        N.checkArgNotNull(actionOnSuccess, "actionOnSuccess");

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

    /**
     *
     * @param 
     * @param actionOnSuccess
     * @throws E2 the e2
     */
    public  void ifSuccess(final Throwables.Consumer actionOnSuccess) throws E2 {
        ifSuccessOrElse(actionOnSuccess, Fn.doNothing());
    }

    /**
     * If success or else.
     *
     * @param 
     * @param 
     * @param actionOnSuccess
     * @param actionOnFailure
     * @throws E2 the e2
     * @throws E3 the e3
     */
    public  void ifSuccessOrElse(final Throwables.Consumer actionOnSuccess,
            final Throwables.Consumer actionOnFailure) throws E2, E3 {
        N.checkArgNotNull(actionOnSuccess, "actionOnSuccess");
        N.checkArgNotNull(actionOnFailure, "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 
     * @param otherIfErrorOccurred
     * @return
     * @throws E2
     * @deprecated replaced by {@link #orElseGetIfFailure(Throwables.Supplier)}
     */
    @Deprecated
    public  T orElseGet(final Throwables.Supplier otherIfErrorOccurred) throws E2 {
        return orElseGetIfFailure(otherIfErrorOccurred);
    }

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

    /**
     *
     * @param 
     * @param otherIfErrorOccurred
     * @return
     * @throws E2
     */
    public  T orElseGetIfFailure(final Throwables.Supplier otherIfErrorOccurred) throws E2 {
        N.checkArgNotNull(otherIfErrorOccurred, "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 E2 the e2
     */
    public  T orElseThrow(final Function exceptionSupplierIfErrorOccurred) throws E2 {
        N.checkArgNotNull(exceptionSupplierIfErrorOccurred, "exceptionSupplierIfErrorOccurred");

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

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

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

    /**
     * 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();
    //    }

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

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

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

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

        if (obj instanceof Result other) {
            return N.equals(other.value, value) && N.equals(other.exception, exception);
        }

        return false;
    }

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

    @Beta
    public static class R extends Result {
        R(T value, 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 - 2024 Weber Informatics LLC | Privacy Policy