summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/functypes/ClosableFunction.java
blob: a559bfcde04ec51a42657224447fac2726bff8f9 (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
package bjc.functypes;

import java.util.function.Function;

/**
 * A function associated with a closable resource
 * 
 * @param <T> The input type
 * @param <R> The result type
 */
public interface ClosableFunction<T, R> extends AutoCloseable, Function<T, R> {
	/**
	 * 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> ClosableFunction<T, R> bindFunction(Function<T, R> f, AutoCloseable clos) {
		return new ClosableFunction<T, R>() {
			@Override
			public R apply(T t) {
				return f.apply(t);
			}
			
			@Override
			public void close() throws Exception {
				clos.close();
			}
		};
	}
}