Okay, working with Java 8, need to know if my code below does what I need it to do.
- I have three methods. Each do an operation and return success or failure as a boolean.
- I need to have all three operate at the same time.
- If any return a
false
, the first one to do so will cause the entire process to short-circuit and the master method to return afalse
. - If none of them return a
false
, all three need to run to completion and return atrue
for the master method to return atrue
.
My code:
private boolean checkProcess1(){
//does stuff, mostly DB schema checking
}
private boolean checkProcess2(){
//does stuff, mostly queries on a different DB
}
private boolean checkProcess3(){
//does stuff, checking files and versioning
}
public boolean checkProcessesForSuccess(){
final List<Supplier<Boolean>> checkList = Arrays.asList(
this::checkProcess1,
this::checkProcess2,
this::checkProcess3
);
return checkList.parallelStream().allMatch( Supplier :: get );
}
My main sticking point concerns the .allMatch()
. It is supposed to take a predicate that determines how each item in the list is evaluated. By passing in the Supplier :: get
, does it not return a true
on any full set of matches, such as all false
values, or does it require all values to be true
in order to return a true
?
Additionally, if the first method to return a result is false
, does this short-circuit the entire master method, or does it continue processing until all three are complete? My worry is that if a short-running method returns a false
, the entire system will be bound by the longest-running method.
I had previously tried to utilize CompletableFuture
for this problem, and I did come up with a solution, but it was quite a bit more complex and - honestly - quite inelegant and ugly. I turned to Streams in an attempt to see if the problem could be resolved in a simpler and more elegant manner.
Suggestions?