简体   繁体   English

java:S1067 - 减少表达式中使用的条件运算符 (5) 的数量(最多允许 3 个)

[英]java:S1067 - Reduce the number of conditional operators (5) used in the expression (maximum allowed 3)

How can I reduce the number of conditional operators?如何减少条件运算符的数量? Sonar showing Major issue like Reduce the number of conditional operators (5) used in the expression (maximum allowed 3) but those all condition mandatory to keep in this block:声纳显示主要问题,例如减少表达式中使用的条件运算符 (5) 的数量(最多允许 3 个),但所有条件都必须保留在此块中:

private String processfromOrigin(Object value) {
    if ((value instanceof A) || (value instanceof B)
            || (value instanceof C) || (value instanceof D)
            || (value instanceof E)
            || (value instanceof F)) {
        return ((baseDto) processo).getProcess();
    } else if (value instanceof G) {
        return ((G) value ).getProcess();
    } else if (value instanceof H) {
        return ((H) value ).getProcess();
    } else {            
        return (String) value ;
    }
}

You can use code like below to simplify the first conditional in the if block:您可以使用如下代码来简化 if 块中的第一个条件:

import static java.util.Stream.of;
boolean checkABCDEF = of(A.class, B.class, C.class, D.class, E.class, F.class).
                      anyMatch(aClass -> aClass.isInstance(value))

of, you can encapsulate it in a local method: of,你可以把它封装在一个本地方法中:

    private boolean isABCDEF(Object x) {
        return Stream.of(A.class, B.class, C.class, D.class, E.class, F.class).
                      anyMatch(aClass -> aClass.isInstance(x));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 减少表达式中使用的条件运算符(4)的数量(允许的最大值3) - Reduce number of conditional operators(4) used in the expression (maximum allowed 3) 减少 SONAR 中表达式中使用的条件运算符 (4) 的数量(最多允许 3 个) - Reduce the number of conditional operators (4) used in the expression (maximum allowed 3) in SONAR 如何解决减少 java 中表达式中使用的条件运算符 (5) 的数量(最多允许 3 个)的声纳问题 - How to fix sonar issues for Reduce the number of conditional operators (5) used in the expression (maximum allowed 3) in java 减少条件运算符的数量 - Reduce number of conditional operators 减少条件运算符的数量 - Reduce the number of conditional operators java表达式太复杂,减少了conditionl运算符的数量 - java Expression too complex reduce the number of conditionl operators Java API中是否有用于条件表达式的CPLEX函数? - Is there a CPLEX function for conditional expression in it's Java API? 如何设置电话号码的正则表达式中允许的最小和最大位数 - How to set the minimum and maximum number of digit allowed in a regex expression for telephone number java中的相等和条件运算符的优先级 - Priority of equality and conditional operators in java Java:条件语句和关系运算符 - Java: Conditional Statement and relational Operators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM