Java Reference
In-Depth Information
@Override
public String toString() {
if (isSuccess()) {
return "SUCCESS[" + success + "]";
} else {
return "FAILURE[" + failure + "]";
}
}
}
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 Function<Integer, Result<Integer, IOException>> generateMap() {
AtomicInteger counter = new AtomicInteger(0);
return i -> {
int count = counter.incrementAndGet();
try (InputStream in = Listing3.generateInputStream()) {
if (i == 100) {
return Result.ofFailure(
new IOException(
"And with a kiss, I die! (After " + count + " executions)")
);
}
return Result.ofSuccess(i);
} catch (IOException ioe) {
return Result.ofFailure(ioe);
}
};
}
public static void main(String[] args) {
// Perform the stream processing
generateParallelStream()
.map(generateMap())
.forEach(result -> {
if (result.isFailure()) {
result.getFailure().printStackTrace(System.err);
System.err.flush();
Search WWH ::




Custom Search