]> git.mxchange.org Git - jproduct-core.git/blob - src/org/mxchange/jshopcore/model/item/BaseItem.java
Continued:
[jproduct-core.git] / src / org / mxchange / jshopcore / model / item / BaseItem.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
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.item;
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         /**
37          * Entry id (from database backend)
38          */
39         private Long id;
40
41         /**
42          * Item amount
43          */
44         private Long amount;
45
46         /**
47          * Item id number
48          */
49         private Long itemId;
50
51         /**
52          * Item type
53          */
54         private String itemType;
55
56         /**
57          * Item instance
58          */
59         private Product product;
60
61         @Override
62         public int compareTo (final AddableBasketItem item) {
63                 // item should not be null
64                 if (null == item) {
65                         throw new NullPointerException("item is null"); //NOI18N
66                 }
67
68                 // Is the id the same?
69                 if (Objects.equals(this.getItemId(), item.getItemId())) {
70                         // Same id, means same item
71                         return 0;
72                 } else if (this.getItemId() > item.getItemId()) {
73                         // This id is larger than compared to
74                         return -1;
75                 }
76
77                 // The other id is larger
78                 return 1;
79         }
80
81         @Override
82         public Long getAmount () {
83                 return this.amount;
84         }
85
86         @Override
87         public void setAmount (final Long amount) {
88                 this.amount = amount;
89         }
90
91         @Override
92         public Long getId () {
93                 return this.id;
94         }
95
96         @Override
97         public void setId (final Long id) {
98                 this.id = id;
99         }
100
101         @Override
102         public Long getItemId () {
103                 return this.itemId;
104         }
105
106         @Override
107         public void setItemId (final Long itemId) {
108                 this.itemId = itemId;
109         }
110
111         @Override
112         public String getItemType () {
113                 return this.itemType;
114         }
115
116         @Override
117         public void setItemType (final String itemType) {
118                 this.itemType = itemType;
119         }
120
121         @Override
122         public Product getProduct () {
123                 return this.product;
124         }
125
126         @Override
127         public void setProduct (final Product product) {
128                 this.product = product;
129         }
130
131         @Override
132         public boolean equals (final Object object) {
133                 // Is it same type?
134                 if (!(object instanceof BaseItem)) {
135                         // Not equal types
136                         return false;
137                 } else if (!(object instanceof AddableBasketItem)) {
138                         // Not correct interface
139                         return false;
140                 }
141
142                 // Securely cast to wanted interface
143                 AddableBasketItem item = (AddableBasketItem) object;
144
145                 // Item id and type must be the same
146                 return ((Objects.equals(item.getItemId(), this.getItemId()))
147                                 && (Objects.equals(item.getItemType(), this.getItemType())));
148         }
149
150         @Override
151         public int hashCode () {
152                 int hash = 5;
153                 hash = 29 * hash + Objects.hashCode(this.getItemId());
154                 hash = 29 * hash + Objects.hashCode(this.getItemType());
155                 return hash;
156         }
157
158         @Override
159         public boolean isProductType () {
160                 // Is the instance set?
161                 return (this.getProduct() instanceof Product);
162         }
163 }