gpt4 book ai didi

java - 将循环转换为 lambda 并抛出异常

转载 作者:搜寻专家 更新时间:2023-10-31 19:36:35 27 4
gpt4 key购买 nike

我如何在 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");
}
}
}

我已经按照建议尝试了以下代码。我们是否可以在此代码中改进其他任何东西,以便更多地使用 lambda 来编写它。

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

最佳答案

问题

您的问题不在于 lambda,而在于代码组织。你有一个数据,即 List<GlobalPricingRequest>和一组验证规则。您需要做的就是将这些验证规则应用于给定数据。

这种方法使您可以灵活地轻松添加或删除验证规则。并分别测试或检查每条规则。

解决方案

最佳解决方案是将每个验证拆分为单独的类。

首先,为验证规则创建管理器和接口(interface):

public final class GlobalPricingRequestValidationManager {

private final List<ValidationRule> validationRules =
Arrays.asList(
new TransactionFeeEqualOrGreaterThanZeroValidationRule(),
new RevenueFeeEqualOrGreaterThanZeroValidationRule());

public void validate(List<GlobalPricingRequest> globalPricingRequests) {
validationRules.forEach(validationRule -> validationRule.validate(globalPricingRequests));
}

public interface ValidationRule {

void validate(List<GlobalPricingRequest> globalPricingRequests);
}

}

其次,在单独的类中实现每个验证规则(已添加到管理器):

public final class TransactionFeeEqualOrGreaterThanZeroValidationRule implements GlobalPricingRequestValidationManager.ValidationRule {

@Override
public void validate(List<GlobalPricingRequest> globalPricingRequests) {
if (globalPricingRequests.stream()
.map(GlobalPricingRequest::getFeePerTransact)
.filter(Objects::nonNull)
.anyMatch(val -> val.signum() == -1)))
throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");
}
}

public final class RevenueFeeEqualOrGreaterThanZeroValidationRule implements GlobalPricingRequestValidationManager.ValidationRule {

@Override
public void validate(List<GlobalPricingRequest> globalPricingRequests) {
if (globalPricingRequests.stream()
.map(GlobalPricingRequest::getEventTypePricingList)
.flatMap(List::stream)
.map(EventTypePricingMapping::getFeePerRevenue)
.filter(Objects::nonNull)
.anyMatch(val -> val.signum() == -1)))
throw ExceptionHelper.badRequest("Fee Per Transaction can't be less than zero");

}
}

客户端代码:

GlobalPricingRequestValidationManager validationManager = new GlobalPricingRequestValidationManager();
List<GlobalPricingRequest> globalPricingRequests = Collections.emptyList();
validationManager.validate(globalPricingRequests);

关于java - 将循环转换为 lambda 并抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54051601/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com