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

com.landawn.abacus.util.Try 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.3.16
Show newest version
package com.landawn.abacus.util;

import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;

/*
 * 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.
 */

public final class Try {
    private static final AutoCloseable EMPTY = () -> {
        // Do nothing.
    };

    private final T targetResource;
    private final Throwables.Supplier targetResourceSupplier;
    private final Runnable finalAction;

    Try(final T targetResource, final Throwables.Supplier targetResourceSupplier, final Runnable finalAction) {
        this.targetResource = targetResource;
        this.targetResourceSupplier = targetResourceSupplier;
        this.finalAction = finalAction;
    }

    public static  Try with(final T targetResource) {
        N.checkArgNotNull(targetResource, "targetResourceSupplier");

        return new Try<>(targetResource, null, null);
    }

    public static  Try with(final T targetResource, final Runnable finalAction) {
        N.checkArgNotNull(targetResource, "targetResourceSupplier");
        N.checkArgNotNull(finalAction, "finalAction");

        return new Try<>(targetResource, null, finalAction);
    }

    public static  Try with(final Throwables.Supplier targetResourceSupplier) {
        N.checkArgNotNull(targetResourceSupplier, "targetResourceSupplier");
        return new Try<>(null, targetResourceSupplier, null);
    }

    public static  Try with(final Throwables.Supplier targetResourceSupplier, final Runnable finalAction) {
        N.checkArgNotNull(targetResourceSupplier, "targetResourceSupplier");
        N.checkArgNotNull(finalAction, "finalAction");

        return new Try<>(null, targetResourceSupplier, finalAction);
    }

    /**
     *
     * @param 
     * @param targetResource
     * @return
     */
    public static  Try with(final Runnable finalAction) {
        N.checkArgNotNull(finalAction);

        return new Try<>(null, null, finalAction);
    }

    /**
     *
     * @param cmd
     * @throws RuntimeException if some error happens
     */
    public static void run(final Throwables.Runnable cmd) {
        try {
            cmd.run();
        } catch (Exception e) {
            throw ExceptionUtil.toRuntimeException(e);
        }
    }

    /**
     *
     * @param cmd
     * @param actionOnError
     */
    public static void run(final Throwables.Runnable cmd, final Consumer actionOnError) {
        try {
            cmd.run();
        } catch (Exception e) {
            actionOnError.accept(e);
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @return
     * @throws RuntimeException if some error happens
     */
    public static  R call(final java.util.concurrent.Callable cmd) {
        try {
            return cmd.call();
        } catch (Exception e) {
            throw ExceptionUtil.toRuntimeException(e);
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param actionOnError
     * @return
     */
    public static  R call(final java.util.concurrent.Callable cmd, final Function actionOnError) {
        try {
            return cmd.call();
        } catch (Exception e) {
            return actionOnError.apply(e);
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param supplier
     * @return
     */
    public static  R call(final java.util.concurrent.Callable cmd, final Supplier supplier) {
        try {
            return cmd.call();
        } catch (Exception e) {
            return supplier.get();
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param defaultValue
     * @return
     */
    public static  R call(final java.util.concurrent.Callable cmd, final R defaultValue) {
        try {
            return cmd.call();
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param predicate
     * @param supplier
     * @return
     * @throws RuntimeException if some error happens and predicate return false.
     */
    public static  R call(final java.util.concurrent.Callable cmd, final Predicate predicate, final Supplier supplier) {
        try {
            return cmd.call();
        } catch (Exception e) {
            if (predicate.test(e)) {
                return supplier.get();
            } else {
                throw ExceptionUtil.toRuntimeException(e);
            }
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param predicate
     * @param defaultValue
     * @return
     * @throws RuntimeException if some error happens and predicate return false.
     */
    public static  R call(final java.util.concurrent.Callable cmd, final Predicate predicate, final R defaultValue) {
        try {
            return cmd.call();
        } catch (Exception e) {
            if (predicate.test(e)) {
                return defaultValue;
            } else {
                throw ExceptionUtil.toRuntimeException(e);
            }
        }
    }

    //    /**
    //     *
    //     * @return
    //     */
    //    public T val() {
    //        return t;
    //    }

    /**
     *
     * @param cmd
     */
    public void run(final Throwables.Consumer cmd) {
        try (final AutoCloseable c = targetResource == null ? (targetResourceSupplier == null ? EMPTY : targetResourceSupplier.get()) : targetResource) {
            cmd.accept(targetResource);
        } catch (Exception e) {
            throw ExceptionUtil.toRuntimeException(e);
        } finally {
            if (finalAction != null) {
                finalAction.run();
            }
        }
    }

    /**
     *
     * @param cmd
     * @param actionOnError
     */
    public void run(final Throwables.Consumer cmd, final Consumer actionOnError) {
        try (final AutoCloseable c = targetResource == null ? (targetResourceSupplier == null ? EMPTY : targetResourceSupplier.get()) : targetResource) {
            cmd.accept(targetResource);
        } catch (Exception e) {
            actionOnError.accept(e);
        } finally {
            if (finalAction != null) {
                finalAction.run();
            }
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @return
     */
    public  R call(final Throwables.Function cmd) {
        try (final AutoCloseable c = targetResource == null ? (targetResourceSupplier == null ? EMPTY : targetResourceSupplier.get()) : targetResource) {
            return cmd.apply(targetResource);
        } catch (Exception e) {
            throw ExceptionUtil.toRuntimeException(e);
        } finally {
            if (finalAction != null) {
                finalAction.run();
            }
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param actionOnError
     * @return
     */
    public  R call(final Throwables.Function cmd, final Function actionOnError) {
        try (final AutoCloseable c = targetResource == null ? (targetResourceSupplier == null ? EMPTY : targetResourceSupplier.get()) : targetResource) {
            return cmd.apply(targetResource);
        } catch (Exception e) {
            return actionOnError.apply(e);
        } finally {
            if (finalAction != null) {
                finalAction.run();
            }
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param supplier
     * @return
     */
    public  R call(final Throwables.Function cmd, final Supplier supplier) {
        try (final AutoCloseable c = targetResource == null ? (targetResourceSupplier == null ? EMPTY : targetResourceSupplier.get()) : targetResource) {
            return cmd.apply(targetResource);
        } catch (Exception e) {
            return supplier.get();
        } finally {
            if (finalAction != null) {
                finalAction.run();
            }
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param defaultValue
     * @return
     */
    public  R call(final Throwables.Function cmd, final R defaultValue) {
        try (final AutoCloseable c = targetResource == null ? (targetResourceSupplier == null ? EMPTY : targetResourceSupplier.get()) : targetResource) {
            return cmd.apply(targetResource);
        } catch (Exception e) {
            return defaultValue;
        } finally {
            if (finalAction != null) {
                finalAction.run();
            }
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param predicate
     * @param supplier
     * @return
     */
    public  R call(final Throwables.Function cmd, final Predicate predicate,
            final Supplier supplier) {
        try (final AutoCloseable c = targetResource == null ? (targetResourceSupplier == null ? EMPTY : targetResourceSupplier.get()) : targetResource) {
            return cmd.apply(targetResource);
        } catch (Exception e) {
            if (predicate.test(e)) {
                return supplier.get();
            } else {
                throw ExceptionUtil.toRuntimeException(e);
            }
        } finally {
            if (finalAction != null) {
                finalAction.run();
            }
        }
    }

    /**
     *
     * @param 
     * @param cmd
     * @param predicate
     * @param defaultValue
     * @return
     */
    public  R call(final Throwables.Function cmd, final Predicate predicate, final R defaultValue) {
        try (final AutoCloseable c = targetResource == null ? (targetResourceSupplier == null ? EMPTY : targetResourceSupplier.get()) : targetResource) {
            return cmd.apply(targetResource);
        } catch (Exception e) {
            if (predicate.test(e)) {
                return defaultValue;
            } else {
                throw ExceptionUtil.toRuntimeException(e);
            }
        } finally {
            if (finalAction != null) {
                finalAction.run();
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy