arman
arman

Reputation: 181

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);
            }
        }
    }

Upvotes: 2

Views: 95

Answers (1)

lczapski
lczapski

Reputation: 4140

For first part you can use Optional.ofNullable. It would be helpful to introduce class that covers all parameters.

class DTO {
    String etimName;
    String busNo;
    String issuedTickets;
    String issuedRolls;
    String status;
    String fuel;

    public DTO() {
    }

    // getters and setters

Than:

DTO dto = new DTO();

Optional.ofNullable(waybillData.getEtimName()).ifPresent(dto::setEtimName);
Optional.ofNullable(waybillData.getVehicleNo()).ifPresent(dto::setBusNo);
Optional.ofNullable(waybillData.getIssuedTickets()).map(Objects::toString).ifPresent(dto::setIssuedTickets);
Optional.ofNullable(waybillData.getIssuedRolls()).map(Objects::toString).ifPresent(dto::setIssuedRolls);
Optional.ofNullable(waybillData.getWaybillStatus()).ifPresent(dto::setStatus);
Optional.ofNullable(waybillData.getFuel()).map(Objects::toString).ifPresent(dto::setFuel);

Or you can introduce map (assuming that toString will be always used):

Map<Supplier<Object>, Consumer<String>> mapThis = Map.of(
        waybillData::getEtimName, dto::setEtimName,
        waybillData::getVehicleNo, dto::setBusNo,
        waybillData::getIssuedTickets, dto::setIssuedTickets,
        waybillData::getIssuedRolls, dto::setIssuedRolls,
        waybillData::getWaybillStatus, dto::setStatus,
        waybillData::getFuel, dto::setFuel
        );
mapThis.entrySet().stream().forEach(e -> {
    Optional.ofNullable(e.getKey().get()).map(Objects::toString).ifPresent(e.getValue());
});

for the second part, I do not see good solution. You can try again with map and Predicate, example for first two statements:

Predicate<Object> testAgencyIdAndDepotId = (v) -> userInfo.getAgencyId() != null && userInfo.getDepotId() != null;
Predicate<Object> testSearch = (v) -> !search.isEmpty();
Predicate<Object> testFromdate= (v) -> fromdate != null;
Predicate<Object> testStatus = (v) -> status.isPresent();

Map<Predicate<Object>, Supplier<Object>> mapIf = Map.of(
        testAgencyIdAndDepotId.and(testSearch).and(testFromdate).and(testStatus),
        () -> wayBillRepository.findWaybillOnDateAndStatusSearchAgencyAndDepotPageable(
                fromDate, toDate, status.get(), search, userInfo.getAgencyId(), userInfo.getDepotId(),
                page),
        testAgencyIdAndDepotId.and(testSearch).and(testFromdate).and(Predicate.not(testStatus)),
        () -> wayBillRepository.findWaybillOnDateAndStatusSearchAgencyAndDepotPageable(
                fromDate, toDate, search, userInfo.getAgencyId(), userInfo.getDepotId()
                page)
        // other conditions
);

allwaybillCollection = mapIf.entrySet().stream().filter(e -> e.getKey().test(null)).findFirst()
        .map(Map.Entry::getValue).map(Supplier::get).orElse(null);

Upvotes: 1

Related Questions