亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將循環轉換為lambda并引發異常

將循環轉換為lambda并引發異常

料青山看我應如是 2021-04-05 12:09:07
我如何在Java8中使用Lambda表達式編寫以下代碼。我是Java 8的新手。for (GlobalPricingRequest globalPricingRequest : globalPricingRequests) {    BigDecimal feePerTrans = globalPricingRequest.getFeePerTransact();    if (feePerTrans != null && feePerTrans.intValue() < 0) {        throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");    }    List<EventTypePricingMapping> eventTypePricingMappings = globalPricingRequest.getEventTypePricingList();    for (EventTypePricingMapping eventTypePricingMapping : eventTypePricingMappings) {        BigDecimal feePerRevenue = eventTypePricingMapping.getFeePerRevenue();        if (feePerRevenue != null && feePerRevenue.intValue() < 0) {            throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");        }        if (eventTypePricingMapping.getFeePerRevenue().intValue() < 0) {            throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");        }    }}我已經按照建議嘗試了以下代碼。我們還有什么可以改進的代碼,可以使用lambdas編寫更多代碼。globalPricingRequests.forEach((globalPricingRequest) -> {    if (checkIfValueLessThanZero(globalPricingRequest.getFeePerTransact())) {        throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");    }    List<EventTypePricingMapping> eventTypePricingMappings = globalPricingRequest.getEventTypePricingList();    eventTypePricingMappings.forEach((eventTypePricingMapping) -> {        if (checkIfValueLessThanZero(eventTypePricingMapping.getFeePerRevenue())) {            throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");        }        if (checkIfValueLessThanZero(eventTypePricingMapping.getFeePerReg())) {            throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");        }    });});private boolean checkIfValueLessThanZero(Object object) {    if (object instanceof BigDecimal) {       if (object != null && ((BigDecimal) object).intValue() < 0) {           return true;       }    }    return false;}
查看完整描述

3 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

通過命令式方法,您正在執行的這種類型的驗證會更好,但是我們可以在適當的地方使用lambda。


首先,我將signum在帖子中使用@ThomasKl?ger的建議,將重復條件與本地謂詞隔離開,因為在這種特定情況下,該條件比更為合適intValue。


Predicate<BigDecimal> criteria = b -> b != null && b.signum() < 0;

那么您的命令式方法將如下所示:


for (GlobalPricingRequest globalPricingRequest : globalPricingRequests) {

      isValidOrElseThrowBadRequestException(globalPricingRequest.getFeePerTransact(), criteria);

      for (EventTypePricingMapping eventTypePricingMapping : globalPricingRequest.getEventTypePricingList()) {

          isValidOrElseThrowBadRequestException(eventTypePricingMapping.getFeePerRevenue(), criteria);

      }

}

其中isValidOrElseThrow定義為:


public static void isValidOrElseThrowBadRequestException(BigDecimal data, Predicate<BigDecimal> criteria) throws Exception { // change the exception to the specific one you're using 

       if(criteria.test(data)) throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");

}

只需在此處和此處進行一些隔離,就可以使代碼更具可讀性。


查看完整回答
反對 回復 2021-04-14
?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

您可以使用流兩次,并提高代碼的可讀性,如下所示:


Predicate<BigDecimal> feeCheck =

        feePerTransactOrRevenue -> feePerTransactOrRevenue != null

                && feePerTransactOrRevenue.intValue() < 0;


boolean globalRequestCheck = globalPricingRequests.stream()

        .map(GlobalPricingRequest::getFeePerTransact)

        .anyMatch(feeCheck); 


boolean eventTypeCheck = globalPricingRequests.stream()

        .map(GlobalPricingRequest::getEventTypePricingList)

        .flatMap(List::stream)

        .map(EventTypePricingMapping::getFeePerRevenue)

        .anyMatch(feeCheck);


// if any of the element matches the condition, throw the exception

if (globalRequestCheck || eventTypeCheck) { 

    throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");

}


查看完整回答
反對 回復 2021-04-14
  • 3 回答
  • 0 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號