You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by pa...@apache.org on 2020/06/27 06:07:35 UTC

[ofbiz-framework] branch trunk updated: Improved: Replace Iterator.remove() with Collection.removeIf().(OFBIZ-11831)

This is an automated email from the ASF dual-hosted git repository.

pawan pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 7f67a49  Improved: Replace Iterator.remove() with Collection.removeIf().(OFBIZ-11831)
7f67a49 is described below

commit 7f67a493f0407038623826bd63d14d1a6b47621b
Author: Pawan Verma <pa...@hotwaxsystems.com>
AuthorDate: Sat Jun 27 11:36:36 2020 +0530

    Improved: Replace Iterator.remove() with Collection.removeIf().(OFBIZ-11831)
    
    From Java8 we have Collection.removeIf() method works by applying the condition provided in the Predicate instance to all the elements in the Collection on which it is invoked. The elements which satisfy the condition are retained while the remaining are removed from the Collection.
---
 .../ofbiz/order/shoppingcart/ShoppingCart.java     | 49 ++++++----------------
 .../ofbiz/order/shoppingcart/ShoppingCartItem.java |  9 +---
 2 files changed, 13 insertions(+), 45 deletions(-)

diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java
index 366f171..a9bcb48 100644
--- a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java
+++ b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java
@@ -1774,12 +1774,7 @@ public class ShoppingCart implements Iterable<ShoppingCartItem>, Serializable {
         if (UtilValidate.isEmpty(paymentMethodIdsToRemove)) {
             return;
         }
-        for (Iterator<CartPaymentInfo> iter = paymentInfo.iterator(); iter.hasNext();) {
-            CartPaymentInfo info = iter.next();
-            if (paymentMethodIdsToRemove.contains(info.paymentMethodId)) {
-                iter.remove();
-            }
-        }
+        paymentInfo.removeIf(info -> paymentMethodIdsToRemove.contains(info.paymentMethodId));
     }
 
     /** remove declined payment methods for an order from cart.  The idea is to call this after an attempted order is rejected */
@@ -2234,13 +2229,7 @@ public class ShoppingCart implements Iterable<ShoppingCartItem>, Serializable {
         Iterator<CartShipInfo> csi = this.shipInfo.iterator();
         while (csi.hasNext()) {
             CartShipInfo info = csi.next();
-            Iterator<ShoppingCartItem> si = info.shipItemInfo.keySet().iterator();
-            while (si.hasNext()) {
-                ShoppingCartItem item = si.next();
-                if (item.getQuantity().compareTo(BigDecimal.ZERO) == 0) {
-                    si.remove();
-                }
-            }
+            info.shipItemInfo.keySet().removeIf(item -> item.getQuantity().compareTo(BigDecimal.ZERO) == 0);
             if (info.shipItemInfo.size() == 0) {
                 csi.remove();
             }
@@ -3224,12 +3213,7 @@ public class ShoppingCart implements Iterable<ShoppingCartItem>, Serializable {
             return;
         }
 
-        Iterator<GenericValue> fsppas = this.freeShippingProductPromoActions.iterator();
-        while (fsppas.hasNext()) {
-            if (productPromoActionPK.equals((fsppas.next()).getPrimaryKey())) {
-                fsppas.remove();
-            }
-        }
+        this.freeShippingProductPromoActions.removeIf(genericValue -> productPromoActionPK.equals((genericValue).getPrimaryKey()));
     }
     /** Adds a ProductPromoAction to be used for free shipping (must be of type free shipping, or nothing will be done). */
     public void addFreeShippingProductPromoAction(GenericValue productPromoAction) {
@@ -3389,34 +3373,25 @@ public class ShoppingCart implements Iterable<ShoppingCartItem>, Serializable {
         // remove cart adjustments from promo actions
         List<GenericValue> cartAdjustments = this.getAdjustments();
         if (cartAdjustments != null) {
-            Iterator<GenericValue> cartAdjustmentIter = cartAdjustments.iterator();
-            while (cartAdjustmentIter.hasNext()) {
-                GenericValue checkOrderAdjustment = cartAdjustmentIter.next();
-                if (UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoId")) &&
-                        UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoRuleId")) &&
-                        UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoActionSeqId"))) {
-                    cartAdjustmentIter.remove();
-                }
-            }
+            cartAdjustments.removeIf(checkOrderAdjustment -> UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoId")) &&
+                    UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoRuleId")) &&
+                    UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoActionSeqId")));
         }
 
         // remove cart lines that are promos (ie GWPs) and cart line adjustments from promo actions
         Iterator<ShoppingCartItem> cartItemIter = this.iterator();
         while (cartItemIter.hasNext()) {
-            ShoppingCartItem checkItem = cartItemIter.next();
+            org.apache.ofbiz.order.shoppingcart.ShoppingCartItem checkItem = cartItemIter.next();
             if (checkItem.getIsPromo()) {
                 this.clearItemShipInfo(checkItem);
                 cartItemIter.remove();
             } else {
                 // found a promo item with the productId, see if it has a matching adjustment on it
-                Iterator<GenericValue> checkOrderAdjustments = UtilMisc.toIterator(checkItem.getAdjustments());
-                while (checkOrderAdjustments != null && checkOrderAdjustments.hasNext()) {
-                    GenericValue checkOrderAdjustment = checkOrderAdjustments.next();
-                    if (UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoId")) &&
-                            UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoRuleId")) &&
-                            UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoActionSeqId"))) {
-                        checkOrderAdjustments.remove();
-                    }
+                List<GenericValue> checkOrderAdjustments = checkItem.getAdjustments();
+                if (!checkOrderAdjustments.isEmpty()) {
+                    checkOrderAdjustments.removeIf(checkOrderAdjustment -> UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoId"))
+                            && UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoRuleId")) &&
+                            UtilValidate.isNotEmpty(checkOrderAdjustment.getString("productPromoActionSeqId")));
                 }
             }
         }
diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartItem.java b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartItem.java
index 340eca0..62618a9 100644
--- a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartItem.java
+++ b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCartItem.java
@@ -2310,15 +2310,8 @@ public class ShoppingCartItem implements java.io.Serializable {
         if (productFeatureId == null) {
             return;
         }
-        Iterator<GenericValue> itemAdjustmentsIter = itemAdjustments.iterator();
 
-        while (itemAdjustmentsIter.hasNext()) {
-            GenericValue itemAdjustment = itemAdjustmentsIter.next();
-
-            if (productFeatureId.equals(itemAdjustment.getString("productFeatureId"))) {
-                itemAdjustmentsIter.remove();
-            }
-        }
+        itemAdjustments.removeIf(itemAdjustment -> productFeatureId.equals(itemAdjustment.getString("productFeatureId")));
     }
 
     public List<GenericValue> getOrderItemPriceInfos() {