davidmoten / davidmoten/rxjava-extras
Resource Manager
- Dominant language
- Java
- Stars
- 271
- Forks
- 28
- Avg merge
- 2m
- Merged PRs (30d)
- 1
Description
I have this bit of code that I've used a couple of times and I figured it could use a good home. Its a class for currying the resource creation and clean of the `rx.Observable#using` function. Would this be a good fit for this project?
``` java
import rx.Observable;
import rx.functions.Func1;
public class ResourceManager {
public static interface CheckedFunc0 {
public R call() throws Exception;
}
public static interface CheckedAction1 {
public void call(T t) throws Exception;
}
private CheckedFunc0 resourceFactory;
private CheckedAction1 disposeAction;
public ResourceManager(final CheckedFunc0 resourceFactory, final CheckedAction1 disposeAction) {
this.resourceFactory = resourceFactory;
this.disposeAction = disposeAction;
}
/**
* The resource T is available for use by the function passed in until the {@link Observable} returned is unsubscribed from.
*
* @param func
* @return
*/
public final Observable checkout(final Func1> func) {
return Observable.using(() -> {
try {
return resourceFactory.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
} , func, (rsrc) -> {
try {
disposeAction.call(rsrc);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.