簡體   English   中英

減少條件運算符的數量

[英]Reduce the number of conditional operators

如何使用多種方法將運算符的數量減少到3個? 還是循環? 動作的常量是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;
    }

您可以使用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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM