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

com.jongsoft.utils.control.Try Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License
 *
 * Copyright 2016 Jong Soft.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.jongsoft.utils.control;

import com.jongsoft.utils.core.CheckedConsumer;
import com.jongsoft.utils.core.CheckedRunner;
import com.jongsoft.utils.core.CheckedSupplier;
import com.jongsoft.utils.core.Value;
import java.util.Objects;
import java.util.function.Consumer;

public interface Try extends Value {

    /**
     * Attempt to execute code that will return an entity, but may also result into an exception.
     *
     * @param       the type of entity that will be returned in case of success
     * @param supplier the supplier that will return the entity
     *
     * @return either an {@link Try} with success set to true and a get returning the entity, or a {@link Try} with
     *         failure set to true and the get throwing the exception.
     *
     * @throws NullPointerException in case the supplier is null
     */
    static  Try supply(CheckedSupplier supplier) {
        Objects.requireNonNull(supplier, "Supplier cannot be null");
        try {
            return new TrySuccess<>(supplier.get());
        } catch (Throwable exception) {
            return new TryFailure<>(exception);
        }
    }

    /**
     * Attempt to execute the code in the {@link CheckedRunner}. If the execution results into an exception then this
     * call will return a {@link Try} with a failure result. Otherwise it will return an empty success {@link Try}.
     *
     * @param runner the code to execute with a try..catch construction
     *
     * @return A {@link Try} with success set to true if the execution went without exceptions, otherwise it will return
     *         a {@link Try} with a failure set to true.
     *
     * @throws NullPointerException in case the runner is null
     */
    static Try run(CheckedRunner runner) {
        Objects.requireNonNull(runner, "Runner cannot be null");
        try {
            runner.run();
        } catch (Throwable exception) {
            return new TryFailure<>(exception);
        }

        return new TrySuccess<>(null);
    }

    /**
     * Indicates if the try operation resulted in an exception
     *
     * @return true in case of an exception of the try, otherwise false
     */
    boolean isFailure();

    /**
     * Indicates if the try operation was successful
     *
     * @return false in case of an exception of the try, otherwise true
     */
    boolean isSuccess();
    
    /**
     * Return the cause of the failure. If the {@link #isSuccess()} is true this call will cause an exception.
     * 
     * @return 
     */
    Throwable getCause();

    /**
     * Convenience method for checked consumer call.
     * 
     * @see #andTry(com.jongsoft.utils.core.CheckedConsumer)
     */
    default Try and(Consumer consumer) {
        Objects.requireNonNull(consumer, "Consumer cannot be null");
        return andTry(consumer::accept);
    }

    default Try and(Runnable runner) {
        Objects.requireNonNull(runner, "Runner cannot be null");
        return andTry(runner::run);
    }

    /**
     * Passes then entity contained within the {@link #get()} if the try has a success. Otherwise it will not call the
     * consumer and return the try containing the failure.
     * 

* This method exists for chaining checked functions, like: *

*

     *      Try.of( () -> "my success")
     *         .andTry( str -> System.out.println(str));
     * 
* * @param consumer the checked consumer to consume the value contained within * * @return a {@link Try} with {@link #isSuccess()} is true in case of no issues, otherwise a {@link Try} with * {@link #isFailure()} is true. * * @throws NullPointerException in case the {@code consumer} is null */ default Try andTry(CheckedConsumer consumer) { Objects.requireNonNull(consumer, "Consumer cannot be null"); if (!isFailure()) { try { consumer.accept(get()); } catch (Throwable th) { return new TryFailure<>(th); } } return this; } default Try andTry(CheckedRunner runner) { Objects.requireNonNull(runner, "Runner cannot be null"); if (!isFailure()) { try { runner.run(); } catch (Throwable th) { return new TryFailure<>(th); } } return this; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy