Search code examples
javaspring-bootsonarqubejava-11

How to reduce the cognitive complexity of sonar for the below code?


Below is my code and Sonar is complaining about it. Asking to make 15 and now it is 19. Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed. I tried with enum which is not working. And it having the transactionId as RequestParam. Is there any eclipse tool to extract this or we need to do it manually.

@GetMapping(value = "display",produces = APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<RspNotification> display(@RequestParam(name = Constants.TRANSACTION_ID) final String transactionId)
    {
        List<RspNotification> list = new ArrayList<>();
        if (manageProductOrderCISStore.get(transactionId) != null)
        {
            list.add(manageProductOrderCISStore.get(transactionId));
        }
        else if (manageAppointmentWWMStore.get(transactionId) != null)
        {
            list.add(manageAppointmentWWMStore.get(transactionId));
        }
        else if (manageAppointmentHWMStore.get(transactionId) != null)
        {
            list.add(manageAppointmentHWMStore.get(transactionId));
        }
        else if (manageAppointmentUnifyStore.get(transactionId) != null)
        {
            list.add(manageAppointmentUnifyStore.get(transactionId));
        }
        else if (manageAppointmentUnifyXmlStore.get(transactionId) != null)
        {
            list.add(manageAppointmentUnifyXmlStore.get(transactionId));
        }
        else if (manageProductOrderFLSStore.get(transactionId) != null)
        {
            list.add(manageProductOrderFLSStore.get(transactionId));
        }
        else if (manageProductOrderHFSStore.get(transactionId) != null)
        {
            list.add(manageProductOrderHFSStore.get(transactionId));
        }
        else if (fulfilmentQuoteEEStore.get(transactionId) != null)
        {
            list.add(fulfilmentQuoteEEStore.get(transactionId));
        }
        else if (manageInventoryHFSStore.get(transactionId) != null)
        {
            list.add(manageInventoryHFSStore.get(transactionId));
        }
        else if (manageInventoryFLSStore.get(transactionId) != null)
        {
            list.add(manageInventoryFLSStore.get(transactionId));
        }
        else if (manageBillingDocumentStore.get(transactionId) != null)
        {
            list.add(manageBillingDocumentStore.get(transactionId));
        }
        else if (serviceHealthStore.get(transactionId) != null)
        {
            list.add(serviceHealthStore.get(transactionId));
        }
        else if (fulfilmentOrderEEStore.get(transactionId) != null)
        {
            list.add(fulfilmentOrderEEStore.get(transactionId));
        }
        else if (manageWorkOrderUnifyStore.get(transactionId) != null)
        {
            list.add(manageWorkOrderUnifyStore.get(transactionId));
        }
        else if (manageDiagnosticAssuranceStore.get(transactionId) != null)
        {
            list.add(manageDiagnosticAssuranceStore.get(transactionId));
        }
        else if (materialsSupplyUnifyStore.get(transactionId) != null)
        {
            list.add(materialsSupplyUnifyStore.get(transactionId));
        }
        ///////// more if statement

        return list;
    }

Any help is much appreciated and enum will not work.


Solution

  • Make a list of the maps and loop over them

    List<Map<String, RspNotification>> maps = Arrays.asList(
        manageProductOrderCISStore,
        manageAppointmentWWMStore,
        manageAppointmentHWMStore,
        manageAppointmentUnifyStore
        //the rest
    );
    
    List<RspNotification> list = new ArrayList<>();
    for (Map<String, RspNotification> map : maps) {
        RspNotification notification = map.get(transactionId);
        if (notification != null) {
            list.add(notification);
            return;
        }
    }
    return list;
    

    Which you can simplify further to

    return Stream.of(
            manageProductOrderCISStore,
            manageAppointmentWWMStore,
            manageAppointmentHWMStore,
            manageAppointmentUnifyStore
            //the rest
        )
        .map(map -> map.get(transactionId))
        .filter(Objects::nonNull)
        .findFirst()
        .map(Collections::singletonList)
        .orElse(Collections.emptyList());