001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util.function;
018
019import java.util.Optional;
020import java.util.function.BiConsumer;
021import java.util.function.Consumer;
022import java.util.function.Function;
023import java.util.function.Supplier;
024
025import org.apache.camel.util.ObjectHelper;
026
027public final class ThrowingHelper {
028    private ThrowingHelper() {
029    }
030
031    public static <V, T extends Throwable> Supplier<V> wrapAsSupplier(ThrowingSupplier<V, T> supplier) {
032        return () -> {
033            try {
034                return supplier.get();
035            } catch (Throwable t) {
036                throw new RuntimeException(t);
037            }
038        };
039    }
040
041    public static <I, T extends Throwable> Consumer<I> wrapAsConsumer(ThrowingConsumer<I, T> consumer) {
042        return in -> {
043            try {
044                consumer.accept(in);
045            } catch (Throwable t) {
046                throw new RuntimeException(t);
047            }
048        };
049    }
050
051    public static <I1, I2, T extends Throwable> BiConsumer<I1, I2> wrapAsBiConsumer(ThrowingBiConsumer<I1, I2, T> consumer) {
052        return (i1, i2) -> {
053            try {
054                consumer.accept(i1, i2);
055            } catch (Throwable t) {
056                throw new RuntimeException(t);
057            }
058        };
059    }
060
061    public static <I, R, T extends Throwable> Function<I, R> wrapAsFunction(ThrowingFunction<I, R, T> function) {
062        return in -> {
063            try {
064                return function.apply(in);
065
066            } catch (Throwable t) {
067                throw new RuntimeException(t);
068            }
069        };
070    }
071
072    /**
073     * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map and transform
074     * it using the given function.
075     *
076     * @param value    the value, if its a String it will be tested for text length as well
077     * @param function the function to be executed against value if not empty
078     */
079    public static <I, R, T extends Throwable> Optional<R> applyIfNotEmpty(I value, ThrowingFunction<I, R, T> function)
080            throws T {
081        if (ObjectHelper.isNotEmpty(value)) {
082            return Optional.ofNullable(function.apply(value));
083        }
084
085        return Optional.empty();
086    }
087
088    /**
089     * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map and transform
090     * it using the given function.
091     *
092     * @param value    the value, if its a String it will be tested for text length as well
093     * @param consumer the function to be executed against value if not empty
094     * @param orElse   the supplier to use to retrieve a result if the given value is empty
095     */
096    public static <I, R, T extends Throwable> R applyIfNotEmpty(I value, ThrowingFunction<I, R, T> consumer, Supplier<R> orElse)
097            throws T {
098        if (ObjectHelper.isNotEmpty(value)) {
099            return consumer.apply(value);
100        }
101
102        return orElse.get();
103    }
104}