Java Reference
In-Depth Information
private final ResourceMaker<RESOURCE_T> init;
private final ConcurrentMap<Object, Exception> exceptions
= new ConcurrentSkipListMap<>();
public ResourceExceptionHandler(final ResourceMaker<RESOURCE_T> init) {
Objects.requireNonNull(init,
"ResourceMaker (initialization code for resource)");
this.init = init;
}
public Map<Object, Exception> getExceptions() {
return exceptions;
}
public Function<Integer, Stream<Integer>> map(
FunctionWithResource<RESOURCE_T, Integer, Integer> f
) {
return i -> {
try (RESOURCE_T resource = init.create()) {
return Stream.of(f.apply(resource, i));
} catch (Exception e) {
exceptions.put(i, e);
return Stream.empty();
}
};
}
}
public static InputStream generateInputStream() {
return new ByteArrayInputStream("foobar".getBytes());
}
public static Stream<Integer> generateParallelStream() {
final int elements = 1000;
List<Integer> toReturn = new ArrayList(elements);
for (int i = 0; i < elements; i++) {
toReturn.add(i);
}
return toReturn.parallelStream();
}
public static ResourceExceptionHandler.FunctionWithResource
<InputStream, Integer, Integer> generateMap()
{
AtomicInteger counter = new AtomicInteger(0);
return (in, i) -> {
int count = counter.incrementAndGet();
Search WWH ::




Custom Search