add.
, add OP-, if ... TRUE, FALSE.
:
keyAccntId.equalsIgnoreCase("-1") | F | T |
segmentId.equalsIgnoreCase("-1") | F | T | F | T |
regionId.equalsIgnoreCase("-1") | F | T | F | T | F | T | F | T |
----------------------------------+---+---+---+---+---+---+---+---+
templateOrder.add("1"); | y | - | - | - | - | - | - | - |
templateOrder.add("2"); | - | - | y | - | - | - | - | - |
templateOrder.add("3"); | - | - | - | y | - | - | - | - |
templateOrder.add("4"); | - | - | - | - | y | - | - | - |
templateOrder.add("5"); | - | - | - | - | - | - | y | - |
templateOrder.add("6"); | - | - | - | - | - | - | - | y |
----------------------------------+---+---+---+---+---+---+---+---+
If the resulting values ββwere βalignedβ with the conditions better, then you could reduce the number of times the comparisons were performed.
int n = 0;
if (keyAccntId.equalsIgnoreCase("-1") {
n += 4;
}
if (segmentId.equalsIgnoreCase("-1") {
n += 2;
}
if (regionId.equalsIgnoreCase("-1") {
n += 1;
}
templateOrder.add(String.valueOf(n));
NOTE. the above code is NOT equivalent to the OP code. To make it execute the equivalent, we need to add a conditional test, so that the method is addnot called when n is 1 or 5, and to convert the value nto the specified value of the string argument
n arg
--- ----
0 "1"
1 -
2 "2"
3 "3"
4 "4"
5 -
6 "5"
7 "6"
The logical table will look something like this:
keyAccntId.equalsIgnoreCase("-1") | F | T |
segmentId.equalsIgnoreCase("-1") | F | T | F | T |
regionId.equalsIgnoreCase("-1") | F | T | F | T | F | T | F | T |
----------------------------------+---+---+---+---+---+---+---+---+
n += 4; | - | - | - | - | y | y | y | y |
n += 2; | - | - | y | y | - | - | y | y |
n += 1; | - | y | - | y | - | y | - | y |
templateOrder.add(arg); | y | - | y | y | y | - | y | y |
----------------------------------+---+---+---+---+---+---+---+---+
source
share