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

it.auties.whatsapp.util.Validate Maven / Gradle / Ivy

There is a newer version: 2.7.2
Show newest version
package it.auties.whatsapp.util;

import lombok.NonNull;
import lombok.experimental.UtilityClass;

import java.lang.reflect.Constructor;
import java.util.Arrays;

@UtilityClass
public class Validate {
    public void isTrue(boolean value, @NonNull String message, Object... args) {
        isTrue(value, message, IllegalArgumentException.class, args);
    }

    public  void isTrue(boolean value, @NonNull Class throwable) throws T {
        isTrue(value, null, throwable);
    }

    @SuppressWarnings("unchecked")
    public  void isTrue(boolean value, String message, @NonNull Class throwable, Object... args) throws T {
        if (value) {
            return;
        }
        throw (T) createThrowable(throwable, message == null ? null : message.formatted(args));
    }

    private Throwable createThrowable(Class throwable, String formattedMessage) {
        try {
            var result = formattedMessage == null ? throwable.getConstructor().newInstance() : throwable.getConstructor(String.class).newInstance(formattedMessage);
            var stackTrace = Arrays.stream(result.getStackTrace())
                    .filter(entry -> !entry.getClassName().equals(Validate.class.getName()) && !entry.getClassName().equals(Constructor.class.getName()))
                    .toArray(StackTraceElement[]::new);
            result.setStackTrace(stackTrace);
            return result;
        } catch (Throwable ignored) {
            return new RuntimeException(formattedMessage);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy