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

net.binis.codegen.tools.Tools Maven / Gradle / Ivy

There is a newer version: 1.2.22
Show newest version
package net.binis.codegen.tools;

/*-
 * #%L
 * code-generator
 * %%
 * Copyright (C) 2021 - 2024 Binis Belev
 * %%
 * 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.
 * #L%
 */

import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import static java.util.Objects.nonNull;

@Slf4j
public class Tools {

    private Tools() {
        //Do nothing
    }

    public static  T nullCheck(R object, Function func) {
        return nonNull(object) ? func.apply(object) : null;
    }

    public static  T nullCheck(T object, T defaultObject) {
        return nonNull(object) ? object : defaultObject;
    }

    public static  T nullCheck(T object, Supplier defaultObjectSupplier) {
        return nonNull(object) ? object : defaultObjectSupplier.get();
    }

    public static  T nullCheck(R object, Class iface, Function func) {
        return nonNull(object) ? func.apply(iface.cast(object)) : null;
    }

    public static  T nullCheck(R object, Function func, T defaultObject) {
        return nonNull(object) ? func.apply(object) : defaultObject;
    }

    public static  T nullCheck(R object, Function func, Supplier defaultObjectSupplier) {
        return nonNull(object) ? func.apply(object) : defaultObjectSupplier.get();
    }

    public static  void with(R object, Consumer consumer) {
        if (nonNull(object)) {
            consumer.accept(object);
        }
    }

    public static  void with(R object, Consumer consumer, Runnable defaultRunnable) {
        if (nonNull(object)) {
            consumer.accept(object);
        } else {
            defaultRunnable.run();
        }
    }

    public static  R ifNull(R object, Supplier supplier) {
        if (object == null) {
            return supplier.get();
        }
        return object;
    }

    public static void ifNull(Object object, Runnable runnable) {
        if (object == null) {
            runnable.run();
        }
    }

    public static  void conditional(R object, Predicate condition, Consumer consumer) {
        if (object != null && condition.test(object)) {
            consumer.accept(object);
        }
    }

    public static  T condition(boolean condition, Supplier supplier) {
        return condition ? supplier.get() : null;
    }

    public static  T condition(boolean condition, T constant) {
        return condition ? constant : null;
    }

    public static void condition(boolean condition, Runnable task) {
        if (condition) {
            task.run();
        }
    }

    public static void tryCatch(Runnable task, Consumer onException) {
        try {
            task.run();
        } catch (Throwable e) {
            onException.accept(e);
        }
    }

    public static void tryCatch(Runnable task) {
        try {
            task.run();
        } catch (Throwable e) {
            //Do nothing
        }
    }

    public static void tryCatchLog(Runnable task) {
        try {
            task.run();
        } catch (Throwable e) {
            log.error("Error executing task!", e);
        }
    }

    public static  T emptyCheck(Optional optional, Function func, T defaultObject) {
        return optional.isPresent() ? func.apply(optional.get()) : defaultObject;
    }

    public static  T condition(boolean condition, R object, Class iface, Function func) {
        return condition && nonNull(object) ? func.apply(iface.cast(object)) : null;
    }

    public static  T safeCall(Supplier func, Function onException) {
        try {
            return func.get();
        } catch (Exception e) {
            return onException.apply(e);
        }
    }

    public static  R withRes(T object, Function wither) {
        if (nonNull(object)) {
            return wither.apply(object);
        }
        return null;
    }

    public static  R withRes(T object, Function wither, R defaultValue) {
        if (nonNull(object)) {
            return wither.apply(object);
        }
        return defaultValue;
    }

    @SafeVarargs
    public static  boolean in(T object, T... list) {
        for (var o : list) {
            if (o.equals(object)) {
                return true;
            }
        }
        return false;
    }

    public static boolean allNotNull(Object... values) {
        if (values == null) {
            return false;
        } else {
            for (var value : values) {
                if (value == null) {
                    return false;
                }
            }
            return true;
        }
    }

    public static  void as(Object obj, Class intf, Consumer consumer) {
        if (nonNull(obj) && intf.isAssignableFrom(obj.getClass())) {
            consumer.accept(intf.cast(obj));
        }
    }

    public static  T as(Object obj, Class intf) {
        if (nonNull(obj) && intf.isAssignableFrom(obj.getClass())) {
            return intf.cast(obj);
        }

        return null;
    }

    public static  R asRes(Object obj, Class intf, Function func) {
        if (nonNull(obj) && intf.isAssignableFrom(obj.getClass())) {
            return func.apply(intf.cast(obj));
        }

        return null;
    }

    public static boolean anyNull(Object... values) {
        return !allNotNull(values);
    }

    public static long measure(Runnable run) {
        var start = System.currentTimeMillis();
        run.run();
        return System.currentTimeMillis() - start;
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy