1 /* 2 * The contents of this file are subject to the terms of the Common Development and 3 * Distribution License (the License). You may not use this file except in compliance with the 4 * License. 5 * 6 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 7 * specific language governing permission and limitations under the License. 8 * 9 * When distributing Covered Software, include this CDDL Header Notice in each file and include 10 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 11 * Header, with the fields enclosed by brackets [] replaced by your own identifying 12 * information: "Portions copyright [year] [name of copyright owner]". 13 * 14 * Copyright 2016 ForgeRock AS. 15 */ 16 17 package org.forgerock.util; 18 19 import static org.forgerock.util.Reject.checkNotNull; 20 21 import java.io.Closeable; 22 23 /** 24 * {@link Function} that silently closes an input-parameter after a delegate-function's {@link Function#apply(Object)} 25 * is invoked. The static {@link #closeSilently(Function)} method is provided for convenience. 26 * 27 * @param <VIN> 28 * The type of the function input-parameter, which implements {@code Closeable}. 29 * @param <VOUT> 30 * The type of the function result, or {@link Void} if the function 31 * does not return anything (i.e. it only has side-effects). 32 * @param <E> 33 * The type of the exception thrown by the function, or 34 * {@link org.forgerock.util.promise.NeverThrowsException} if no exception is thrown by the 35 * function. 36 */ 37 public class CloseSilentlyFunction<VIN extends Closeable, VOUT, E extends Exception> implements Function<VIN, VOUT, E> { 38 39 private final Function<VIN, VOUT, E> delegate; 40 41 /** 42 * Creates a new {@code CloseSilentlyFunction} instance. 43 * 44 * @param delegate Delegate function. 45 */ 46 public CloseSilentlyFunction(final Function<VIN, VOUT, E> delegate) { 47 this.delegate = checkNotNull(delegate); 48 } 49 50 /** 51 * Invokes the delegate function's {@link Function#apply(Object)} with the input parameter {@code value}, closes it, 52 * and returns the result. 53 * 54 * @param value {@code Closeable} input parameter. 55 * @return The result of applying delegate function to {@code value}. 56 * @throws E Propagates {@code Exception} thrown by delegate {@link Function}. 57 */ 58 @Override 59 public VOUT apply(final VIN value) throws E { 60 try { 61 return delegate.apply(value); 62 } finally { 63 Utils.closeSilently(value); 64 } 65 } 66 67 /** 68 * Wraps a delegate function in a {@code CloseSilentlyFunction}. 69 * 70 * @param delegate Delegate function. 71 * @param <IN> 72 * The type of the function input-parameter, which implements {@code Closeable}. 73 * @param <OUT> 74 * The type of the function result, or {@link Void} if the function does not return anything 75 * (i.e. it only has side-effects). 76 * @param <EX> The type of the exception thrown by the function, or 77 * {@link org.forgerock.util.promise.NeverThrowsException} if no exception is thrown by the function. 78 * @return New {@code CloseSilentlyFunction} instance. 79 */ 80 public static <IN extends Closeable, OUT, EX extends Exception> Function<IN, OUT, EX> closeSilently( 81 final Function<IN, OUT, EX> delegate) { 82 return new CloseSilentlyFunction<>(delegate); 83 } 84 85 }