jeudi 19 novembre 2020

How can I reduce the Complexity of this code?

How can I reduce the Complexity of this code:

if (StringUtils.isNotBlank(productDTO.getName()))
            criteria = Criteria.where(NAME.getLabel()).regex(productDTO.getName(), insensitive);
        if (StringUtils.isNotBlank(productDTO.getDescription()))
            criteria = criteria != null ? criteria.and(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive) : Criteria.where(DESCRIPTION.getLabel()).regex(productDTO.getDescription(), insensitive);
        if (productDTO.getPrice() != 0)
            criteria = criteria != null ? criteria.and(PRICE.getLabel()).is(productDTO.getPrice()) : Criteria.where(PRICE.getLabel()).is(productDTO.getPrice());
        if (StringUtils.isNotBlank(productDTO.getBrand()))
            criteria = criteria != null ? criteria.and(BRAND.getLabel()).regex(productDTO.getBrand(), insensitive) : Criteria.where(BRAND.getLabel()).regex(productDTO.getBrand(), insensitive);
        if (productDTO.getProductSize() != null)
            criteria = criteria != null ? criteria.and(SIZE.getLabel()).is(productDTO.getProductSize()) : Criteria.where(SIZE.getLabel()).is(productDTO.getProductSize());
        if (productDTO.getStockCount() != 0)
            criteria = criteria != null ? criteria.and(STOCK_COUNT.getLabel()).is(productDTO.getStockCount()) : Criteria.where(STOCK_COUNT.getLabel()).is(productDTO.getStockCount());
        if (StringUtils.isNotBlank(productDTO.getType()))
            criteria = criteria != null ? criteria.and(TYPE.getLabel()).regex(productDTO.getType(), insensitive) : Criteria.where(TYPE.getLabel()).regex(productDTO.getType(), insensitive);
        if (StringUtils.isNotBlank(productDTO.getColor()))
            criteria = criteria != null ? criteria.and(COLOR.getLabel()).regex(productDTO.getColor(), insensitive) : Criteria.where(COLOR.getLabel()).regex(productDTO.getColor(), insensitive);
        if (productDTO.getGender() != null)
            criteria = criteria != null ? criteria.and(GENDER.getLabel()).is(productDTO.getGender()) : Criteria.where(GENDER.getLabel()).is(productDTO.getGender());

Aucun commentaire:

Enregistrer un commentaire