减少条件运算符的数量

时间:2017-05-17 08:47:12

标签: java

如何正确地将操作员数量减少到3个, 用多种方法?还是循环? Actions的常量是Int

    String act = req.getParameter("ACTION");
    int actInt = -1;
    try {
        actInt = Integer.parseInt(act);
    } catch (NumberFormatException nfe) {
        //
    }
    boolean actionNulle = act == null;
    boolean actionDefaut = actionNulle || actInt == -1;

    if ( (actionReq.equals(ACTION_LIST_ENREG) && (actionDefaut || !(actInt == Actions.AJOUTER || actInt == Actions.VALIDER || actInt == Actions.NOUVEAU || actInt == Actions.NOUVEAU_PAR_COPIE)))
        || (actionReq.equals("PreAbattage") && (actionDefaut || (actInt == Actions.DEFAUT)))
        || (actionReq.equals(ACTION_FORMULAIRE_RECHERCHE) && (actionDefaut || !(actInt == Actions.NOUVEAU_PAR_COPIE || actInt == Actions.NOUVEAU)))
        || (actionReq.equals("ModificationMultiple") && (actionDefaut || !(actInt == Actions.OUI || actInt == Actions.SUBSTITUTION)))
        || (actionReq.equals(ACTION_VISU_RECORD) && (actionDefaut || !(actInt == Actions.COPIE_PRIVE || actInt == Actions.NOUVEAU_PRIVE || actInt == Actions.MODIFIER || actInt == Actions.RESULTAT_CREATION)))
        || (actionReq.equals("VisuLock") && (actionDefaut || !(actInt == Actions.DETRUIRE_VERROU)))
        || (actionReq.equals("CopiePublique") && (actionDefaut || !(actInt == Actions.CREER_DONNEES_PUBLIQUES)))
        || (actionReq.equals("VisuSessions") && (actionDefaut || !(actInt == Actions.DETRUIRE_SESSION)))
        || (actionReq.equals(ACTION_VISU_MESSAGE) && (actionDefaut))
        || (actionReq.equals("RecapModifPub") && (actionDefaut || actInt == Actions.VALIDER))
        ) {
        return true;
    }

1 个答案:

答案 0 :(得分:6)

您可以使用Map<String, Set<Integer>>

private final Map<String, Set<Integer>> validCombinations = initializeMap();
// initialize the map once

// later, in your code:

return validCombinations.containsKey(actionReq) && 
(actionDefault || validCombinations.get(actionReq).contains(actInt));