I have the below piece of code in Java. I need to execute all of the if statements.Is there a better approach to code this.In each of the statement, I would then make a database call.
我在Java中有下面的代码。我需要执行所有的if语句。有一个更好的方法来编写这个。在每个语句中,我都会进行数据库调用。
if (!keyAccntId.equalsIgnoreCase("-1") && !(segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("1");
}
if (!keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("2");
}
if (!keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && (regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("3");
}
if (keyAccntId.equalsIgnoreCase("-1") && !(segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("4");
}
if (keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && !(regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("5");
}
if (keyAccntId.equalsIgnoreCase("-1") && (segmentId.equalsIgnoreCase("-1")) && (regionId.equalsIgnoreCase("-1"))) {
templateOrder.add("6");
}
5 个解决方案
#1
16
You could create a bitmask and switch on it:
您可以创建一个位掩码并切换它:
public static final int KEY = 1;
public static final int SEGMENT = 2;
public static final int REGION = 4;
int value = 0;
if (keyAccntId.equalsIgnoreCase("-1")) {
value += KEY;
}
if (segmentId.equalsIgnoreCase("-1")) {
value += SEGMENT;
}
if (regionId.equalsIgnoreCase("-1")) {
value += REGION;
}
That gives you 8 possible values, which you can switch on:
这为您提供了8个可能的值,您可以打开它们:
switch(value) {
case 0:
// All false
break;
case KEY:
// only keyAccntId is true
break;
case REGION:
// only segmentId is true
break;
case KEY + REGION:
// segmentId and keyAccntId are true
break;
// So on
}
edit
Added constants to improve readability
添加常量以提高可读性
#2
7
A shorter version of @njzk2's answer using Java 7.
@ njzk2使用Java 7的答案的缩短版本。
public static final int KEY = 0b001, SEGMENT = 0b010, REGION = 0b100;
int comb = (keyAccntId.equals("-1") ? KEY : 0) +
(segmentId.equals("-1") ? SEGMENT : 0) +
(regionId.equals("-1") ? REGION : 0);
switch(comb) {
case 0: /* none are true. */ break;
case REGION: /* only regionId is -1 */ break;
// more combinations.
case KEY + SEGMENT + REGION: /* all are -1 */ break;
}
#3
2
No "If", No "Switch". This 3 lines would cover all your cases:
没有“如果”,没有“切换”。这3行将涵盖您的所有情况:
int val = keyAccntId.equals("-1")? 3:0;
val += ((segmentId.equals("-1")? 1:0) + (regionId.equals("-1")? 1:0);
templateOrder.add(val+1+"");
.
。
You may want to encapsulated the code, so it would end up being something like this:
您可能希望封装代码,因此它最终会像这样:
public int getTemplateOrder(int keyAccntId, int segmentId, int regionId){
int val = keyAccntId.equals("-1")? 3:0;
return val + (segmentId.equals("-1")? 1:0) + (regionId.equals("-1")? 1:0) + 1;
}
//Usage
templateOrder.add(getTemplateOrder(keyAccntId, segmentId, regionId)+"");
.
。
For readability these are those 3 lines:
为了便于阅读,这些是3行:
int acc = keyAccntId.equals("-1")? 3:0;
int seg = segmentId.equals("-1")? 1:0;
int reg = regionId.equals("-1")? 1:0;
int val = acc;
//Construct the value currently being used on each If statement.
val += seg + reg;
//You've got the value, so instead of If/switch, just set it.
templateOrder.add(val+1+"");
I hope it's not too obscure.
我希望它不是太模糊。
Regards.
问候。
#4
0
To make things easier visually you could do something like this...
为了使视觉上更容易,你可以做这样的事......
boolean keyTF = keyAccntId.equalsIgnoreCase("-1"),
segmentTF = segmentId.equalsIgnoreCase("-1"),
regionTF = regionId.equalsIgnoreCase("-1");
if (!keyTF && !segmentTF && !regionTF ) {
templateOrder.add("1");
}
if (!keyTF && segmentTF && !regionTF ) {
templateOrder.add("2");
}
if (!keyTF && segmentTF && regionTF ) {
templateOrder.add("3");
}
if (keyTF && !segmentTF && !regionTF ) {
templateOrder.add("4");
}
if (keyTF && segmentTF && !regionTF ) {
templateOrder.add("5");
}
if (keyTF && segmentTF && regionTF ) {
templateOrder.add("6");
}
The problem though is that you're dealing with a logical truth table, and you simply have to step through all of the possibilities. Since you're working with 3 separate variables there is no way to compress the code any further
但问题是你正在处理逻辑真值表,而你只需要逐步完成所有可能性。由于您使用3个单独的变量,因此无法再进一步压缩代码
FFF
FTF
FTT
TFF
TTF
TTT
you're actually missing the TFT case :)
你真的错过了TFT的情况:)
#5
0
Only six of the possible combinations result in a call to add
method.
只有六种可能的组合导致调用add方法。
We note that the add
method only gets called once in the OP code, because the conditions in the if statements are mutually exclusive... if one of those evaluates as TRUE, the others will return FALSE.
我们注意到add方法只在OP代码中被调用一次,因为if语句中的条件是互斥的...如果其中一个计算为TRUE,则其他方法将返回FALSE。
To go old school with a logic truth table:
用逻辑真值表去老派:
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 "lined up" with the conditions better, then you could reduce the number of times the comparisons were done.
如果结果值与条件“排列”更好,那么您可以减少比较完成的次数。
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 code above is NOT equivalent to the OP code. To make it perform the equivalent, we'd need to add a conditional test, so that the add
method is not called when when n is 1 or 5, and to convert the value of n
to the specified string argument value:
注意:上面的代码不等同于OP代码。为了使它执行等效,我们需要添加条件测试,以便在n为1或5时不调用add方法,并将n的值转换为指定的字符串参数值:
n arg
--- ----
0 "1"
1 -
2 "2"
3 "3"
4 "4"
5 -
6 "5"
7 "6"
The logic table would 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 |
----------------------------------+---+---+---+---+---+---+---+---+
#1
16
You could create a bitmask and switch on it:
您可以创建一个位掩码并切换它:
public static final int KEY = 1;
public static final int SEGMENT = 2;
public static final int REGION = 4;
int value = 0;
if (keyAccntId.equalsIgnoreCase("-1")) {
value += KEY;
}
if (segmentId.equalsIgnoreCase("-1")) {
value += SEGMENT;
}
if (regionId.equalsIgnoreCase("-1")) {
value += REGION;
}
That gives you 8 possible values, which you can switch on:
这为您提供了8个可能的值,您可以打开它们:
switch(value) {
case 0:
// All false
break;
case KEY:
// only keyAccntId is true
break;
case REGION:
// only segmentId is true
break;
case KEY + REGION:
// segmentId and keyAccntId are true
break;
// So on
}
edit
Added constants to improve readability
添加常量以提高可读性
#2
7
A shorter version of @njzk2's answer using Java 7.
@ njzk2使用Java 7的答案的缩短版本。
public static final int KEY = 0b001, SEGMENT = 0b010, REGION = 0b100;
int comb = (keyAccntId.equals("-1") ? KEY : 0) +
(segmentId.equals("-1") ? SEGMENT : 0) +
(regionId.equals("-1") ? REGION : 0);
switch(comb) {
case 0: /* none are true. */ break;
case REGION: /* only regionId is -1 */ break;
// more combinations.
case KEY + SEGMENT + REGION: /* all are -1 */ break;
}
#3
2
No "If", No "Switch". This 3 lines would cover all your cases:
没有“如果”,没有“切换”。这3行将涵盖您的所有情况:
int val = keyAccntId.equals("-1")? 3:0;
val += ((segmentId.equals("-1")? 1:0) + (regionId.equals("-1")? 1:0);
templateOrder.add(val+1+"");
.
。
You may want to encapsulated the code, so it would end up being something like this:
您可能希望封装代码,因此它最终会像这样:
public int getTemplateOrder(int keyAccntId, int segmentId, int regionId){
int val = keyAccntId.equals("-1")? 3:0;
return val + (segmentId.equals("-1")? 1:0) + (regionId.equals("-1")? 1:0) + 1;
}
//Usage
templateOrder.add(getTemplateOrder(keyAccntId, segmentId, regionId)+"");
.
。
For readability these are those 3 lines:
为了便于阅读,这些是3行:
int acc = keyAccntId.equals("-1")? 3:0;
int seg = segmentId.equals("-1")? 1:0;
int reg = regionId.equals("-1")? 1:0;
int val = acc;
//Construct the value currently being used on each If statement.
val += seg + reg;
//You've got the value, so instead of If/switch, just set it.
templateOrder.add(val+1+"");
I hope it's not too obscure.
我希望它不是太模糊。
Regards.
问候。
#4
0
To make things easier visually you could do something like this...
为了使视觉上更容易,你可以做这样的事......
boolean keyTF = keyAccntId.equalsIgnoreCase("-1"),
segmentTF = segmentId.equalsIgnoreCase("-1"),
regionTF = regionId.equalsIgnoreCase("-1");
if (!keyTF && !segmentTF && !regionTF ) {
templateOrder.add("1");
}
if (!keyTF && segmentTF && !regionTF ) {
templateOrder.add("2");
}
if (!keyTF && segmentTF && regionTF ) {
templateOrder.add("3");
}
if (keyTF && !segmentTF && !regionTF ) {
templateOrder.add("4");
}
if (keyTF && segmentTF && !regionTF ) {
templateOrder.add("5");
}
if (keyTF && segmentTF && regionTF ) {
templateOrder.add("6");
}
The problem though is that you're dealing with a logical truth table, and you simply have to step through all of the possibilities. Since you're working with 3 separate variables there is no way to compress the code any further
但问题是你正在处理逻辑真值表,而你只需要逐步完成所有可能性。由于您使用3个单独的变量,因此无法再进一步压缩代码
FFF
FTF
FTT
TFF
TTF
TTT
you're actually missing the TFT case :)
你真的错过了TFT的情况:)
#5
0
Only six of the possible combinations result in a call to add
method.
只有六种可能的组合导致调用add方法。
We note that the add
method only gets called once in the OP code, because the conditions in the if statements are mutually exclusive... if one of those evaluates as TRUE, the others will return FALSE.
我们注意到add方法只在OP代码中被调用一次,因为if语句中的条件是互斥的...如果其中一个计算为TRUE,则其他方法将返回FALSE。
To go old school with a logic truth table:
用逻辑真值表去老派:
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 "lined up" with the conditions better, then you could reduce the number of times the comparisons were done.
如果结果值与条件“排列”更好,那么您可以减少比较完成的次数。
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 code above is NOT equivalent to the OP code. To make it perform the equivalent, we'd need to add a conditional test, so that the add
method is not called when when n is 1 or 5, and to convert the value of n
to the specified string argument value:
注意:上面的代码不等同于OP代码。为了使它执行等效,我们需要添加条件测试,以便在n为1或5时不调用add方法,并将n的值转换为指定的字符串参数值:
n arg
--- ----
0 "1"
1 -
2 "2"
3 "3"
4 "4"
5 -
6 "5"
7 "6"
The logic table would 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 |
----------------------------------+---+---+---+---+---+---+---+---+