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 it using the given function. 074 * 075 * @param value the value, if its a String it will be tested for text length as well 076 * @param function the function to be executed against value if not empty 077 */ 078 public static <I, R, T extends Throwable> Optional<R> applyIfNotEmpty(I value, ThrowingFunction<I, R, T> function) throws T { 079 if (ObjectHelper.isNotEmpty(value)) { 080 return Optional.ofNullable(function.apply(value)); 081 } 082 083 return Optional.empty(); 084 } 085 086 /** 087 * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map and transform it using the given function. 088 * 089 * @param value the value, if its a String it will be tested for text length as well 090 * @param consumer the function to be executed against value if not empty 091 * @param orElse the supplier to use to retrieve a result if the given value is empty 092 */ 093 public static <I, R, T extends Throwable> R applyIfNotEmpty(I value, ThrowingFunction<I, R, T> consumer, Supplier<R> orElse) throws T { 094 if (ObjectHelper.isNotEmpty(value)) { 095 return consumer.apply(value); 096 } 097 098 return orElse.get(); 099 } 100}