]> git.mxchange.org Git - jcustomer-core.git/blob - src/org/mxchange/jshopcore/model/basket/items/BaseItem.java
Continued:
[jcustomer-core.git] / src / org / mxchange / jshopcore / model / basket / items / BaseItem.java
1 /*
2  * Copyright (C) 2015 KLC
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jshopcore.model.basket.items;
18
19 import java.util.Objects;
20 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
21 import org.mxchange.jshopcore.model.product.Product;
22
23 /**
24  * An item (addedable to a basket) could respresent a product or a discount
25  * coupon. This depends on the type of the item.
26  *
27  * @author Roland Haeder<roland@mxchange.org>
28  */
29 public abstract class BaseItem implements AddableBasketItem, Comparable<AddableBasketItem> {
30
31         /**
32          * Serial number
33          */
34         private static final long serialVersionUID = 24_348_671_457_829_156L;
35
36         @Override
37         public int compareTo (final AddableBasketItem item) {
38                 // item should not be null
39                 if (null == item) {
40                         throw new NullPointerException("item is null"); //NOI18N
41                 }
42
43                 // Is the id the same?
44                 if (Objects.equals(this.getProduct(), item.getProduct())) {
45                         // Same id, means same item
46                         return 0;
47                 } else if (this.getProduct().getProductId() > item.getProduct().getProductId()) {
48                         // This id is larger than compared to
49                         return -1;
50                 }
51
52                 // The other id is larger
53                 return 1;
54         }
55
56         @Override
57         public boolean equals (final Object object) {
58                 // Is it same type?
59                 if (!(object instanceof BaseItem)) {
60                         // Not equal types
61                         return false;
62                 } else if (!(object instanceof AddableBasketItem)) {
63                         // Not correct interface
64                         return false;
65                 }
66
67                 // Securely cast to wanted interface
68                 AddableBasketItem item = (AddableBasketItem) object;
69
70                 // Item id and type must be the same
71                 return ((Objects.equals(item.getProduct().getProductId(), this.getProduct().getProductId()))
72                                 && (Objects.equals(item.getItemType(), this.getItemType())));
73         }
74
75         @Override
76         public int hashCode () {
77                 int hash = 5;
78                 hash = 29 * hash + Objects.hashCode(this.getProduct().getProductId());
79                 hash = 29 * hash + Objects.hashCode(this.getItemType());
80                 return hash;
81         }
82
83         @Override
84         public boolean isProductType () {
85                 // Is the instance set?
86                 return (this.getProduct() instanceof Product);
87         }
88 }