How can i write below code using lambda expression in java8. I am new to 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");
}
}
}
I have tried the below code as yet as per the suggestion . Is there any other thing which we can improve in this code to write it using lambdas more.
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;
}