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

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

/*
 * Copyright (C) 2017 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.Collection;
import java.util.Map;
import java.util.function.Supplier;

import com.landawn.abacus.annotation.Beta;

/**
 * This class is mainly designed for functional programming.
 * Generally the traditional "{@code if-else}" or ternary operator: "{@code ? : }" is preferred over this class.
 *
 * @see N#ifOrEmpty(boolean, Throwables.Supplier)
 * @see N#ifOrElse(boolean, Throwables.Runnable, Throwables.Runnable)
 * @see N#ifNotNull(Object, Throwables.Consumer)
 * @see N#ifNotEmpty(CharSequence, Throwables.Consumer)
 * @see N#ifNotEmpty(Collection, Throwables.Consumer)
 * @see N#ifNotEmpty(Map, Throwables.Consumer)
 *
 */
@Beta
public final class If {

    private static final If TRUE = new If(true);

    private static final If FALSE = new If(false);

    private final boolean b;

    If(final boolean b) {
        this.b = b;
    }

    /**
     * Returns an instance of If based on the provided boolean value.
     *
     * @param b
     * @return
     */
    public static If is(final boolean b) {
        return b ? TRUE : FALSE;
    }

    /**
     * Returns an instance of If based on the provided boolean value.
     *
     * @param b
     * @return
     */
    public static If not(final boolean b) {
        return b ? FALSE : TRUE;
    }

    /**
     * {@code true} for {@code index >= 0}, {@code false} for {@code index < 0}.
     *
     * @param index
     * @return
     */
    public static If exists(final int index) {
        return index >= 0 ? TRUE : FALSE;
    }

    /**
     * Checks if the provided object is {@code null}.
     *
     * @param obj the object to check
     * @return an instance of If indicating whether the object is null
     */
    public static If isNull(final Object obj) {
        return is(obj == null);
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param s
     * @return
     */
    public static If isEmpty(final CharSequence s) {
        return is(Strings.isEmpty(s));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final boolean[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final char[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final byte[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final short[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final int[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final long[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final float[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final double[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If isEmpty(final Object[] a) {
        return is(N.isEmpty(a));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param c
     * @return
     */
    public static If isEmpty(final Collection c) {
        return is(N.isEmpty(c));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param m
     * @return
     */
    public static If isEmpty(final Map m) {
        return is(N.isEmpty(m));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param list
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static If isEmpty(final PrimitiveList list) {
        return is(N.isEmpty(list));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param s
     * @return
     */
    public static If isEmpty(final Multiset s) {
        return is(N.isEmpty(s));
    }

    /**
     * Checks if is {@code null} or empty.
     *
     * @param m
     * @return
     */
    public static If isEmpty(final Multimap m) {
        return is(N.isEmpty(m));
    }

    /**
     * Checks if is {@code null} or empty or blank.
     *
     * @param s
     * @return
     */
    // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu.
    public static If isBlank(final CharSequence s) {
        return is(Strings.isBlank(s));
    }

    /**
     * Checks if the provided object is not {@code null}.
     *
     * @param obj the object to check
     * @return an instance of If indicating whether the object is not null
     */
    public static If notNull(final Object obj) {
        return is(obj != null);
    }

    /**
     * Not {@code null} or empty.
     *
     * @param s
     * @return
     */
    public static If notEmpty(final CharSequence s) {
        return is(Strings.isNotEmpty(s));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final boolean[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final char[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final byte[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final short[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final int[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final long[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final float[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final double[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param a
     * @return
     */
    public static If notEmpty(final Object[] a) {
        return is(N.notEmpty(a));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param c
     * @return
     */
    public static If notEmpty(final Collection c) {
        return is(N.notEmpty(c));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param m
     * @return
     */
    public static If notEmpty(final Map m) {
        return is(N.notEmpty(m));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param list
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static If notEmpty(final PrimitiveList list) {
        return is(N.notEmpty(list));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param s
     * @return
     */
    public static If notEmpty(final Multiset s) {
        return is(N.notEmpty(s));
    }

    /**
     * Not {@code null} or empty.
     *
     * @param m
     * @return
     */
    public static If notEmpty(final Multimap m) {
        return is(N.notEmpty(m));
    }

    /**
     * Not {@code null} or empty or blank.
     *
     * @param s
     * @return
     */
    // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu.
    public static If notBlank(final CharSequence s) {
        return is(Strings.isNotBlank(s));
    }

    //    public  void thenRun(final Throwables.Runnable cmd) throws E {
    //        if (b) {
    //            cmd.run();
    //        }
    //    }
    //
    //    public  void thenRun(final T init, final Try.Consumer action) throws E {
    //        if (b) {
    //            action.accept(init);
    //        }
    //    }
    //
    //    public  Nullable thenCall(final Try.Callable callable) throws E {
    //        return b ? Nullable.of(callable.call()) : Nullable. empty();
    //    }
    //
    //    public  Nullable thenCall(final T init, final Try.Function func) throws E {
    //        return b ? Nullable.of(func.apply(init)) : Nullable. empty();
    //    }

    /**
     * Then do nothing if the condition is {@code true}.
     *
     * @return an instance of OrElse to allow further conditional operations
     */
    public OrElse thenDoNothing() {
        return OrElse.of(b);
    }

    /**
     * Executes the provided runnable if the condition is {@code true}.
     *
     * @param  the type of exception that the runnable may throw
     * @param cmd the runnable to execute if the condition is true
     * @return an instance of OrElse to allow further conditional operations
     * @throws IllegalArgumentException if the provided runnable is null
     * @throws E if the runnable throws an exception
     */
    public  OrElse then(final Throwables.Runnable cmd) throws IllegalArgumentException, E {
        N.checkArgNotNull(cmd);

        if (b) {
            cmd.run();
        }

        return OrElse.of(b);
    }

    /**
     * Executes the provided consumer action if the condition is {@code true}.
     *
     * @param  the type of the input to the consumer
     * @param  the type of exception that the consumer may throw
     * @param init the initial input to the consumer
     * @param action the consumer action to execute if the condition is true
     * @return an instance of OrElse to allow further conditional operations
     * @throws IllegalArgumentException if the provided action is null
     * @throws E if the consumer action throws an exception
     */
    @Beta
    public  OrElse then(final T init, final Throwables.Consumer action) throws IllegalArgumentException, E {
        N.checkArgNotNull(action);

        if (b) {
            action.accept(init);
        }

        return OrElse.of(b);
    }

    /**
     * Throws the exception provided by the supplier if the condition is {@code true}.
     *
     * @param  the type of exception that may be thrown
     * @param exceptionSupplier the supplier that provides the exception to be thrown
     * @return an instance of OrElse to allow further conditional operations
     * @throws IllegalArgumentException if the provided exception supplier is null
     * @throws E if the condition is {@code true} and the exception supplier provides an exception
     */
    public  OrElse thenThrow(final Supplier exceptionSupplier) throws IllegalArgumentException, E {
        N.checkArgNotNull(exceptionSupplier);

        if (b) {
            throw exceptionSupplier.get();
        }

        //noinspection ConstantValue
        return OrElse.of(b);
    }

    //    public  Nullable then(final Try.Callable callable) throws E {
    //        return b ? Nullable.of(callable.call()) : Nullable. empty();
    //    }
    //
    //    public  Nullable then(final T init, final Try.Function func) throws E {
    //        return b ? Nullable.of(func.apply(init)) : Nullable. empty();
    //    }

    /**
     * The Class OrElse.
     */
    public static final class OrElse {
        /**
         * For internal only
         */
        public static final OrElse TRUE = new OrElse(true);

        /**
         * For internal only
         */
        public static final OrElse FALSE = new OrElse(false);

        /** The b. */
        private final boolean isIfTrue;

        /**
         * Instantiates a new or.
         *
         * @param b
         */
        OrElse(final boolean b) {
            isIfTrue = b;
        }

        /**
         *
         * @param b
         * @return
         */
        static OrElse of(final boolean b) {
            return b ? TRUE : FALSE;
        }

        /**
         * Or else do nothing.
         */
        void orElseDoNothing() {
            // Do nothing.
        }

        /**
         * Executes the provided runnable if the condition is {@code false}.
         *
         * @param  the type of exception that the runnable may throw
         * @param cmd the runnable to execute if the condition is false
         * @throws IllegalArgumentException if the provided runnable is null
         * @throws E if the runnable throws an exception
         */
        public  void orElse(final Throwables.Runnable cmd) throws IllegalArgumentException, E {
            N.checkArgNotNull(cmd);

            if (!isIfTrue) {
                cmd.run();
            }
        }

        /**
         * Executes the provided consumer action if the condition is {@code false}.
         *
         * @param  the type of the input to the consumer
         * @param  the type of exception that the consumer may throw
         * @param init the initial input to the consumer
         * @param action the consumer action to execute if the condition is false
         * @throws IllegalArgumentException if the provided action is null
         * @throws E if the consumer action throws an exception
         */
        @Beta
        public  void orElse(final T init, final Throwables.Consumer action) throws IllegalArgumentException, E {
            N.checkArgNotNull(action);

            if (!isIfTrue) {
                action.accept(init);
            }
        }

        /**
         * Throws the exception provided by the supplier if the condition is {@code false}.
         *
         * @param  the type of exception that may be thrown
         * @param exceptionSupplier the supplier that provides the exception to be thrown
         * @throws IllegalArgumentException if the provided exception supplier is null
         * @throws E if the condition is {@code false} and the exception supplier provides an exception
         */
        public  void orElseThrow(final Supplier exceptionSupplier) throws IllegalArgumentException, E {
            N.checkArgNotNull(exceptionSupplier);

            if (!isIfTrue) {
                throw exceptionSupplier.get();
            }
        }
    }

    //    /**
    //     * This class is mainly designed for functional programming.
    //     * Generally the traditional "{@code if-else}" or ternary operator: "{@code ? : }" is preferred over this class.
    //     *
    //     *
    //     */
    //    @Beta
    //    @Deprecated
    //    public static final class IF {
    //        private static final IF TRUE = new IF(true);
    //        private static final IF FALSE = new IF(false);
    //
    //        @SuppressWarnings("rawtypes")
    //        private static final Or FALSE_OR = new FalseOr();
    //
    //        private final boolean b;
    //
    //        IF(boolean b) {
    //            this.b = b;
    //        }
    //
    //        public static IF is(final boolean b) {
    //            return b ? TRUE : FALSE;
    //        }
    //
    //        public static IF not(final boolean b) {
    //            return b ? FALSE : TRUE;
    //        }
    //
    //        /**
    //         * {@code true} for {@code index >= 0}, {@code false} for {@code index < 0}.
    //         *
    //         * @param index
    //         * @return
    //         */
    //        public static IF exists(final int index) {
    //            return index >= 0 ? TRUE : FALSE;
    //        }
    //
    //        public static IF isEmpty(final CharSequence s) {
    //            return is(N.isEmpty(s));
    //        }
    //
    //        public static IF isEmpty(final boolean[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final char[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final byte[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final short[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final int[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final long[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final float[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final double[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final Object[] a) {
    //            return is(N.isEmpty(a));
    //        }
    //
    //        public static IF isEmpty(final Collection c) {
    //            return is(N.isEmpty(c));
    //        }
    //
    //        public static IF isEmpty(final Map m) {
    //            return is(N.isEmpty(m));
    //        }
    //
    //        @SuppressWarnings("rawtypes")
    //        public static IF isEmpty(final PrimitiveList list) {
    //            return is(N.isEmpty(list));
    //        }
    //
    //        public static IF isEmpty(final Multiset s) {
    //            return is(N.isEmpty(s));
    //        }
    //
    //        public static IF isEmpty(final LongMultiset s) {
    //            return is(N.isEmpty(s));
    //        }
    //
    //        public static IF isEmpty(final Multimap m) {
    //            return is(N.isEmpty(m));
    //        }
    //
    //        // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu.
    //        public static IF isBlank(final CharSequence s) {
    //            return is(N.isBlank(s));
    //        }
    //
    //        public static IF notEmpty(final CharSequence s) {
    //            return is(N.notEmpty(s));
    //        }
    //
    //        public static IF notEmpty(final boolean[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final char[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final byte[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final short[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final int[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final long[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final float[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final double[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final Object[] a) {
    //            return is(N.notEmpty(a));
    //        }
    //
    //        public static IF notEmpty(final Collection c) {
    //            return is(N.notEmpty(c));
    //        }
    //
    //        public static IF notEmpty(final Map m) {
    //            return is(N.notEmpty(m));
    //        }
    //
    //        @SuppressWarnings("rawtypes")
    //        public static IF notEmpty(final PrimitiveList list) {
    //            return is(N.notEmpty(list));
    //        }
    //
    //        public static IF notEmpty(final Multiset s) {
    //            return is(N.notEmpty(s));
    //        }
    //
    //        public static IF notEmpty(final LongMultiset s) {
    //            return is(N.notEmpty(s));
    //        }
    //
    //        public static IF notEmpty(final Multimap m) {
    //            return is(N.notEmpty(m));
    //        }
    //
    //        // DON'T change 'OrEmptyOrBlank' to 'OrBlank' because of the occurring order in the auto-completed context menu.
    //        public static IF notBlank(final CharSequence s) {
    //            return is(N.notBlank(s));
    //        }
    //
    //        public  Nullable thenGet(Try.Supplier supplier) throws E {
    //            return b ? Nullable.of(supplier.get()) : Nullable. empty();
    //        }
    //
    //        public  Nullable thenApply(final U init, final Try.Function func) throws E {
    //            return b ? Nullable.of(func.apply(init)) : Nullable. empty();
    //        }
    //
    //        public  Or then(final Try.Callable callable) throws E {
    //            N.checkArgNotNull(callable);
    //
    //            return b ? new TrueOr<>(callable.call()) : FALSE_OR;
    //        }
    //
    //        public  Or then(final U init, final Try.Function func) throws E {
    //            N.checkArgNotNull(func);
    //
    //            return b ? new TrueOr<>(func.apply(init)) : FALSE_OR;
    //        }
    //
    //        public abstract static class Or {
    //            Or() {
    //            }
    //
    //            public abstract  T orElse(final Try.Callable callable) throws E;
    //
    //            public abstract  T orElse(final U init, final Try.Function func) throws E;
    //
    //            public abstract  T orElseThrow(final Supplier exceptionSupplier) throws E;
    //        }
    //
    //        static final class TrueOr extends Or {
    //            private final T result;
    //
    //            TrueOr(final T result) {
    //                this.result = result;
    //            }
    //
    //            @Override
    //            public  T orElse(final Try.Callable callable) throws E {
    //                N.checkArgNotNull(callable);
    //
    //                return result;
    //            }
    //
    //            @Override
    //            public  T orElse(final U init, final Try.Function func) throws E {
    //                N.checkArgNotNull(func);
    //
    //                return result;
    //            }
    //
    //            @Override
    //            public  T orElseThrow(final Supplier exceptionSupplier) throws E {
    //                N.checkArgNotNull(exceptionSupplier);
    //
    //                return result;
    //            }
    //        }
    //
    //        static final class FalseOr extends Or {
    //            FalseOr() {
    //            }
    //
    //            @Override
    //            public  T orElse(final Try.Callable callable) throws E {
    //                return callable.call();
    //            }
    //
    //            @Override
    //            public  T orElse(final U init, final Try.Function func) throws E {
    //                return func.apply(init);
    //            }
    //
    //            @Override
    //            public  T orElseThrow(final Supplier exceptionSupplier) throws E {
    //                throw exceptionSupplier.get();
    //            }
    //        }
    //    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy