]> git.mxchange.org Git - jcustomer-core.git/commitdiff
Added equals() and hashCode() for easy item comparison.
authorRoland Haeder <roland@mxchange.org>
Fri, 11 Sep 2015 09:53:09 +0000 (11:53 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 11 Sep 2015 09:53:09 +0000 (11:53 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/jshopcore/model/basket/BaseBasket.java
src/org/mxchange/jshopcore/model/item/BaseItem.java

index 2a4de120f7b119a20fb9af9bf303ceefc10244cf..aac402052b9fa438db40707c612e4fe1e60864bd 100644 (file)
@@ -20,7 +20,6 @@ import java.util.Deque;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
-import java.util.Objects;
 import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
 
 /**
@@ -121,11 +120,11 @@ public abstract class BaseBasket<T extends AddableBasketItem> implements Basket<
                boolean isAdded = false;
 
                for (Map.Entry<Long, T> entrySet : map.entrySet()) {
-                       // Get item id
-                       Long itemId = entrySet.getKey();
+                       // Get whole item
+                       T i = entrySet.getValue();
 
                        // Compare id
-                       if (Objects.equals(itemId, item.getItemId())) {
+                       if (i.equals(item)) {
                                // Okay, found it
                                isAdded = true;
                                break;
index 7a8d50c756699c8e4bc91a5e69988c107516ed6a..2015d2dd1c09f925f0e70af6634ba7564e4af945 100644 (file)
@@ -127,4 +127,31 @@ public abstract class BaseItem implements AddableBasketItem, Comparable<AddableB
        public void setProduct (final Product product) {
                this.product = product;
        }
+
+       @Override
+       public boolean equals (final Object object) {
+               // Is it same type?
+               if (!(object instanceof BaseItem)) {
+                       // Not equal types
+                       return false;
+               } else if (!(object instanceof AddableBasketItem)) {
+                       // Not correct interface
+                       return false;
+               }
+
+               // Securely cast to wanted interface
+               AddableBasketItem item = (AddableBasketItem) object;
+
+               // Item id and type must be the same
+               return ((Objects.equals(item.getItemId(), this.getItemId()))
+                               && (Objects.equals(item.getItemType(), this.getItemType())));
+       }
+
+       @Override
+       public int hashCode () {
+               int hash = 5;
+               hash = 29 * hash + Objects.hashCode(this.getItemId());
+               hash = 29 * hash + Objects.hashCode(this.getItemType());
+               return hash;
+       }
 }