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

com.watchrabbit.commons.exception.Throwables Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
/*
 * Copyright 2014 Mariusz.
 *
 * 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.watchrabbit.commons.exception;

import com.watchrabbit.commons.callback.CheckedConsumer;
import com.watchrabbit.commons.callback.CheckedPredicate;
import java.util.function.Consumer;
import java.util.function.Predicate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Mariusz
 */
public class Throwables {

    private static final Logger LOGGER = LoggerFactory.getLogger(Throwables.class);

    @FunctionalInterface
    public static interface ExceptionWrapper {

        E wrap(Exception e);
    }

    public static  Consumer propagateFromConsumer(CheckedConsumer consumer) throws SystemException {
        return propagate(consumer, SystemException::new);
    }

    public static  Consumer propagate(CheckedConsumer consumer, ExceptionWrapper wrapper) throws E {
        return (T t) -> {
            try {
                consumer.accept(t);
            } catch (Exception e) {
                throw wrapper.wrap(e);
            }
        };

    }

    public static  Predicate propagateFromPredicate(CheckedPredicate predicate) throws SystemException {
        return propagate(predicate, SystemException::new);
    }

    public static  Predicate propagate(CheckedPredicate predicate, ExceptionWrapper wrapper) throws E {
        return (T t) -> {
            try {
                return predicate.test(t);
            } catch (Exception e) {
                throw wrapper.wrap(e);
            }
        };

    }

    public static  Predicate suppress(boolean defaultValue, CheckedPredicate predicate) {
        return (T t) -> {
            try {
                return predicate.test(t);
            } catch (Exception ex) {
                LOGGER.info("Supressed exception, returning default value", ex);
                return defaultValue;
            }
        };
    }

    public static  Consumer suppress(CheckedConsumer consumer) {
        return (T t) -> {
            try {
                consumer.accept(t);
            } catch (Exception ex) {
                LOGGER.info("Supressed exception", ex);
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy