So I have an if statement that looks like the following:
List<Integer> portsInUse = new ArrayList<Integer>();
. . .
if(portsInUse.contains((Integer) newPort1)
|| portsInUse.contains((Integer) newPort2){
fail(_____, "This port is already in use.");
}
newPort1 and newPort2 are assigned by digging them out of an HttpServletRequest. fail(String parameter, String message) is an internal method to alert the user that something went wrong.
What I’d like to do is be able to tell which condition caused the if statement to trigger, and then set the failure message accordingly. Can I do that, or would I have to do something like this:
List<Integer> portsInUse = new ArrayList<Integer>();
. . .
boolean port1 = portsInUse.contains((Integer) newPort1);
boolean port2 = portsInUse.contains((Integer) newPort2);
if(port1 || port2){
if(port1)
fail("newPort1", "This port is already in use.");
else
fail("newPort2", "This port is already in use.");
}
Or, third option, should I break up the conditions like this:
List<Integer> portsInUse = new ArrayList<Integer>();
. . .
if(portsInUse.contains((Integer) newPort1) {
fail("newPort1", "This port is already in use.");
}
if(portsInUse.contains((Integer) newPort2) {
fail("newPort2", "This port is already in use.");
}
I’m sure I’m overcomplicating things, but thank you in advance.
Since you need to determine which specific port is in use, the third option would be more ideal.