R
- the underlying resource typepublic class Res<R> extends java.lang.Object implements CloseableResource
AutoCloseable
so that its instances can be closed by try blocks.
Usage:
try (Res<SomeResource> someResource = Res.res(SomeResource::release, new SomeResource)) {
someResource.unwrap().foo();
}
Modifier and Type | Class and Description |
---|---|
static interface |
Res.CloseFunction<R>
Functional interface for closing a resource.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Throws any exception thrown by
Res.CloseFunction.close(Object)
including checked exceptions. |
<T extends java.lang.Throwable> |
expected()
Fluent method for allowing an instance to declare it throws any
Throwable . |
static <R> Res<R> |
res(Res.CloseFunction<R> closeFunction,
R resource)
Creates a new
Res instance. |
R |
unwrap() |
public R unwrap()
public void close()
Res.CloseFunction.close(Object)
including checked exceptions.
If the Res.CloseFunction
throws a checked exception not declared by any other call use expected()
to tell the compiler to expect it.close
in interface java.lang.AutoCloseable
close
in interface CloseableResource
public static <R> Res<R> res(Res.CloseFunction<R> closeFunction, R resource)
Res
instance.R
- the resource typecloseFunction
- the function for releasing the resourceresource
- the resource instancepublic <T extends java.lang.Throwable> Res<R> expected() throws T extends java.lang.Throwable
CloseableResource
Throwable
.expected
in interface CloseableResource
T
- the type of exception to expectT extends java.lang.Throwable
Exceptions.expected()