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

org.apache.bval.util.Exceptions Maven / Gradle / Ivy

There is a newer version: 10.0.0-M3
Show newest version
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 org.apache.bval.util;

import java.lang.reflect.InvocationTargetException;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Utility class for sundry {@link Exception}-related tasks.
 */
public class Exceptions {
    /**
     * Callback interface that collects format arguments in conditional raise* method variants.
     * @see Exceptions#raiseIf(boolean, Function, String, Consumer)
     * @see Exceptions#raiseIf(boolean, BiFunction, Throwable, String, Consumer)
     * @see Exceptions#raiseUnless(boolean, Function, String, Consumer)
     * @see Exceptions#raiseUnless(boolean, BiFunction, Throwable, String, Consumer)
     */
    @FunctionalInterface
    public interface FormatArgs {
        void args(Object... args);
    }

    public static  E create(Function fn, String format,
        Object... args) {
        return create(fn, () -> String.format(format, args));
    }

    public static  E create(
        BiFunction fn, C cause, String format, Object... args) {
        return create(fn, cause, () -> String.format(format, args));
    }

    public static  E create(Function fn, Supplier message) {
        return elideStackTrace(fn.apply(message.get()));
    }

    public static  E create(
        BiFunction fn, C cause, Supplier message) {
        return elideStackTrace(fn.apply(message.get(), cause));
    }

    public static  R raise(Function fn, String format,
        Object... args) throws E {
        throw create(fn, format, args);
    }

    public static  void raiseIf(boolean condition, Function fn,
        String format, Object... args) throws E {
        if (condition) {
            raise(fn, format, args);
        }
    }

    public static  void raiseUnless(boolean condition, Function fn,
        String format, Object... args) throws E {
        raiseIf(!condition, fn, format, args);
    }

    public static  R raise(Function fn, Supplier message)
        throws E {
        throw create(fn, message);
    }

    public static  void raiseIf(boolean condition, Function fn,
        String format, Consumer argsProvider) throws E {
        if (condition) {
            raise(fn, message(format, argsProvider));
        }
    }

    public static  void raiseIf(boolean condition, Function fn,
        Supplier message) throws E {
        if (condition) {
            raise(fn, message);
        }
    }

    public static  void raiseUnless(boolean condition, Function fn,
        String format, Consumer argsProvider) throws E {
        raiseIf(!condition, fn, format, argsProvider);
    }

    public static  void raiseUnless(boolean condition, Function fn,
        Supplier message) throws E {
        raiseIf(!condition, fn, message);
    }

    public static  R raise(
        BiFunction fn, C cause, String format, Object... args) throws E {
        throw create(fn, cause, format, args);
    }

    public static  void raiseIf(boolean condition,
        BiFunction fn, C cause, String format, Object... args) throws E {
        if (condition) {
            raise(fn, cause, format, args);
        }
    }

    public static  void raiseUnless(boolean condition,
        BiFunction fn, C cause, String format, Object... args) throws E {
        raiseIf(!condition, fn, cause, format, args);
    }

    public static  R raise(
        BiFunction fn, C cause, Supplier message) throws E {
        throw create(fn, cause, message);
    }

    public static  void raiseIf(boolean condition,
        BiFunction fn, C cause, String format,
        Consumer argsProvider) throws E {
        if (condition) {
            raise(fn, cause, message(format, argsProvider));
        }
    }

    public static  void raiseIf(boolean condition,
        BiFunction fn, C cause, Supplier message) throws E {
        if (condition) {
            raise(fn, cause, message);
        }
    }

    public static  void raiseUnless(boolean condition,
        BiFunction fn, C cause, String format,
        Consumer argsProvider) throws E {
        raiseIf(!condition, fn, cause, message(format, argsProvider));
    }

    public static  void raiseUnless(boolean condition,
        BiFunction fn, C cause, Supplier message) throws E {
        raiseIf(!condition, fn, cause, message);
    }

    /**
     * Extract cause from {@link InvocationTargetException}s.
     * @param t to unwrap
     * @return first of t, cause hierarchy not instanceof {@link InvocationTargetException}
     */
    public static Throwable causeOf(Throwable t) {
        while (t instanceof InvocationTargetException) {
            t = t.getCause();
        }
        return t;
    }

    private static  T elideStackTrace(T t) {
        final StackTraceElement[] stackTrace = t.fillInStackTrace().getStackTrace();
        t.setStackTrace(Stream.of(stackTrace).filter(e -> !Exceptions.class.getName().equals(e.getClassName()))
            .toArray(StackTraceElement[]::new));
        return t;
    }

    private static Supplier message(String format, Consumer argsProvider) {
        final ObjectWrapper args = new ObjectWrapper<>();
        argsProvider.accept(args::accept);
        return () -> String.format(format, args.get());
    }

    private Exceptions() {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy