mercredi 13 novembre 2019

Some ways or existing design Pattern in Java to prevent lot of if/else conditions occuring cos of parameter checks

I am working on a spring-boot application. It has api's which fetch based on input parameters, these parameters for some api are large in numbers which has led to lot of if/else, nested if/else in the codebase. Can someone please suggest way to prevent this or there exist some design patter in java to specifically avoid this.

The code looks like this-

        if (waybillData.getEtimName() != null) {
            etimName = waybillData.getEtimName();
        }
        if (waybillData.getVehicleNo() != null) {
            busNo = waybillData.getVehicleNo();
        }
        if (waybillData.getIssuedTickets() != null) {
            issuedTickets = waybillData.getIssuedTickets().toString();
        }
        if (waybillData.getIssuedRolls() != null) {
            issuedRolls = waybillData.getIssuedRolls().toString();
        }

        if (waybillData.getWaybillStatus() != null) {
            status = waybillData.getWaybillStatus();
        }
        if (waybillData.getFuel() != null) {
            fuel = waybillData.getFuel().toString();
        }

also another example -

if (userInfo.getAgencyId() != null && userInfo.getDepotId() != null) {
    if (!search.isEmpty()) {
        if (fromdate != null) {
            if (status.isPresent()) {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusSearchAgencyAndDepotPageable(
                    fromDate, toDate, status.get(), search, userInfo.getAgencyId(), userInfo.getDepotId(),
                    page);
            } else {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusSearchAgencyAndDepotPageable(
                    fromDate, toDate, search, userInfo.getAgencyId(), userInfo.getDepotId(), page);
            }
        } else {
            if (status.isPresent()) {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusSearchAgencyAndDepotPageable(
                    status.get(), search, userInfo.getAgencyId(), userInfo.getDepotId(), page);
            } else {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusSearchAgencyAndDepotPageable(
                    search, userInfo.getAgencyId(), userInfo.getDepotId(), page);
            }
        }
    } else {
        if (fromdate != null) {
            if (status.isPresent()) {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusAgencyAndDepotPageable(
                    fromDate, toDate, status.get(), userInfo.getAgencyId(), userInfo.getDepotId(), page);
            } else {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusAgencyAndDepotPageable(
                    fromDate, toDate, userInfo.getAgencyId(), userInfo.getDepotId(), page);
            }
        } else {
            if (status.isPresent()) {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusAgencyAndDepotPageable(
                    status.get(), userInfo.getAgencyId(), userInfo.getDepotId(), page);
            } else {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusAgencyAndDepotPageable(
                    userInfo.getAgencyId(), userInfo.getDepotId(), page);
            }
        }
    }
} else if (userInfo.getAgencyId() != null) {
    if (!search.isEmpty()) {
        if (fromdate != null) {
            if (status.isPresent()) {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusSearchAgencyPageable(
                    fromDate, toDate, status.get(), search, userInfo.getAgencyId(), page);
            } else {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusSearchAgencyPageable(
                    fromDate, toDate, search, userInfo.getAgencyId(), page);
            }
        } else {
            if (status.isPresent()) {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusSearchAgencyPageable(
                    status.get(), search, userInfo.getAgencyId(), page);
            } else {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusSearchAgencyPageable(search,
                    userInfo.getAgencyId(), page);
            }
        }
    } else {
        if (fromdate != null) {
            if (status.isPresent()) {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusAgencyPageable(fromDate,
                    toDate, status.get(), userInfo.getAgencyId(), page);
            } else {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusAgencyPageable(fromDate,
                    toDate, userInfo.getAgencyId(), page);
            }
        } else {
            if (status.isPresent()) {
                allwaybillCollection = wayBillRepository.findWaybillOnDateAndStatusAgencyPageable(status.get(),
                    userInfo.getAgencyId(), page);
            } else {
                allwaybillCollection = wayBillRepository
                    .findWaybillOnDateAndStatusAgencyPageable(userInfo.getAgencyId(), page);
            }
        }
    }

Aucun commentaire:

Enregistrer un commentaire