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.function.BiConsumer; 020import java.util.function.Consumer; 021import java.util.function.Function; 022import java.util.function.Supplier; 023 024public final class ThrowingHelper { 025 private ThrowingHelper() { 026 } 027 028 public static <V, T extends Throwable> Supplier<V> wrapAsSupplier(ThrowingSupplier<V, T> supplier) { 029 return () -> { 030 try { 031 return supplier.get(); 032 } catch (Throwable t) { 033 throw new RuntimeException(t); 034 } 035 }; 036 } 037 038 public static <I, T extends Throwable> Consumer<I> wrapAsConsumer(ThrowingConsumer<I, T> consumer) { 039 return in -> { 040 try { 041 consumer.accept(in); 042 } catch (Throwable t) { 043 throw new RuntimeException(t); 044 } 045 }; 046 } 047 048 public static <I1, I2, T extends Throwable> BiConsumer<I1, I2> wrapAsBiConsumer(ThrowingBiConsumer<I1, I2, T> consumer) { 049 return (i1, i2) -> { 050 try { 051 consumer.accept(i1, i2); 052 } catch (Throwable t) { 053 throw new RuntimeException(t); 054 } 055 }; 056 } 057 058 public static <I, R, T extends Throwable> Function<I, R> wrapAsFunction(ThrowingFunction<I, R, T> function) { 059 return in -> { 060 try { 061 return function.apply(in); 062 063 } catch (Throwable t) { 064 throw new RuntimeException(t); 065 } 066 }; 067 } 068}