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 /** 032 * Wrap a {@link ThrowingSupplier} to a standard {@link Suppliers} by throwing a {@link RuntimeException} in case of 033 * an exception is thrown by the delegated supplier. 034 */ 035 public static <V, T extends Throwable> Supplier<V> wrapAsSupplier(ThrowingSupplier<V, T> supplier) { 036 return new Supplier<V>() { 037 @Override 038 public V get() { 039 try { 040 return supplier.get(); 041 } catch (Throwable t) { 042 throw new RuntimeException(t); 043 } 044 } 045 }; 046 } 047 048 /** 049 * Wrap a {@link ThrowingConsumer} to a standard {@link Consumer} by throwing a {@link RuntimeException} in case of 050 * an exception is thrown by the delegated consumer. 051 */ 052 public static <I, T extends Throwable> Consumer<I> wrapAsConsumer(ThrowingConsumer<I, T> consumer) { 053 return new Consumer<I>() { 054 @Override 055 public void accept(I in) { 056 try { 057 consumer.accept(in); 058 } catch (Throwable t) { 059 throw new RuntimeException(t); 060 } 061 } 062 }; 063 } 064 065 /** 066 * Wrap a {@link ThrowingBiConsumer} to a standard {@link BiConsumer} by throwing a {@link RuntimeException} in case 067 * of an exception is thrown by the delegated consumer. 068 */ 069 public static <I1, I2, T extends Throwable> BiConsumer<I1, I2> wrapAsBiConsumer(ThrowingBiConsumer<I1, I2, T> consumer) { 070 return new BiConsumer<I1, I2>() { 071 @Override 072 public void accept(I1 i1, I2 i2) { 073 try { 074 consumer.accept(i1, i2); 075 } catch (Throwable t) { 076 throw new RuntimeException(t); 077 } 078 } 079 }; 080 } 081 082 /** 083 * Wrap a {@link ThrowingFunction} to a standard {@link Function} by throwing a {@link RuntimeException} in case of 084 * an exception is thrown by the delegated function. 085 */ 086 public static <I, R, T extends Throwable> Function<I, R> wrapAsFunction(ThrowingFunction<I, R, T> function) { 087 return new Function<I, R>() { 088 @Override 089 public R apply(I in) { 090 try { 091 return function.apply(in); 092 093 } catch (Throwable t) { 094 throw new RuntimeException(t); 095 } 096 } 097 }; 098 } 099 100 /** 101 * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map and transform 102 * it using the given function. 103 * 104 * @param value the value, if its a String it will be tested for text length as well 105 * @param function the function to be executed against value if not empty 106 */ 107 public static <I, R, T extends Throwable> Optional<R> applyIfNotEmpty(I value, ThrowingFunction<I, R, T> function) 108 throws T { 109 if (ObjectHelper.isNotEmpty(value)) { 110 return Optional.ofNullable(function.apply(value)); 111 } 112 113 return Optional.empty(); 114 } 115 116 /** 117 * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map and transform 118 * it using the given function. 119 * 120 * @param value the value, if its a String it will be tested for text length as well 121 * @param consumer the function to be executed against value if not empty 122 * @param orElse the supplier to use to retrieve a result if the given value is empty 123 */ 124 public static <I, R, T extends Throwable> R applyIfNotEmpty(I value, ThrowingFunction<I, R, T> consumer, Supplier<R> orElse) 125 throws T { 126 if (ObjectHelper.isNotEmpty(value)) { 127 return consumer.apply(value); 128 } 129 130 return orElse.get(); 131 } 132}