Java Reference
In-Depth Information
public Function<Integer, Optional<Integer>> map(
FunctionWithResource<RESOURCE_T, Integer, Integer> f
) {
return i -> {
try (RESOURCE_T resource = init.create()) {
return Optional.of(f.apply(resource, i));
} catch (Exception e) {
exceptions.put(i, e);
return Optional.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();
if (i == 100) {
throw new IOException(
"And with a kiss, I die! (After " + count + " executions)");
}
return i;
};
}
public static void main(String[] args) {
// Create the handler for exceptions
ResourceExceptionHandler handler =
new ResourceExceptionHandler(Listing4::generateInputStream);
Search WWH ::




Custom Search