summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/functypes/ClosableThrowFunction.java
blob: 856f15d08cec3346753cb9d2e318f01feceec22f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package bjc.functypes;

import java.util.function.Function;

/**
 * A (possibly throwing) function associated with a closable resource
 * 
 * @param <T> The input type
 * @param <R> The result type
 * @param <E> The exception type
 */
public interface ClosableThrowFunction<T, R, E extends Throwable> extends AutoCloseable, ThrowFunction<T, R, E> {
	/**
	 * Create a ClosableFunction from a {@link AutoCloseable} and a {@link Function}
	 * 
	 * @param <T> The input type
	 * @param <R> The result type
	 * @param f The function
	 * @param clos The AutoClosable
	 * @return The closable function
	 */
	public static <T, R, E extends Throwable> ClosableThrowFunction<T, R, E> bindFunction(ThrowFunction<T, R, E> f, AutoCloseable clos) {
		return new ClosableThrowFunction<T, R, E>() {
			@Override
			public R apply(T t) throws E {
				return f.apply(t);
			}
			
			@Override
			public void close() throws Exception {
				clos.close();
			}
		};
	}
}