简体   繁体   中英

Reduce the number of conditional operators

How can I reduce the number of operators to 3 properly, with multiple methods ? or loop ? the constant of Actions is 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;
    }

You could use a 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM