2 * Copyright (C) 2016 KLC
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.
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.
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/>.
17 package org.mxchange.jshopcore.model.basket.items;
19 import java.util.Objects;
20 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
21 import org.mxchange.jshopcore.model.product.Product;
24 * An item (addedable to a basket) could respresent a product or a discount
25 * coupon. This depends on the type of the item.
27 * @author Roland Haeder<roland@mxchange.org>
29 public abstract class BaseItem implements AddableBasketItem, Comparable<AddableBasketItem> {
34 private static final long serialVersionUID = 24_348_671_457_829_156L;
37 public int compareTo (final AddableBasketItem item) {
38 // item should not be null
40 throw new NullPointerException("item is null"); //NOI18N
43 // Is the id the same?
44 if (Objects.equals(this.getItemProduct(), item.getItemProduct())) {
45 // Same id, means same item
47 } else if (this.getItemProduct().getProductId() > item.getItemProduct().getProductId()) {
48 // This id is larger than compared to
52 // The other id is larger
57 public boolean equals (final Object object) {
59 if (!(object instanceof BaseItem)) {
62 } else if (!(object instanceof AddableBasketItem)) {
63 // Not correct interface
67 // Securely cast to wanted interface
68 AddableBasketItem item = (AddableBasketItem) object;
70 // Item id and type must be the same
71 return ((Objects.equals(item.getItemProduct().getProductId(), this.getItemProduct().getProductId())) &&
72 (Objects.equals(item.getItemType(), this.getItemType())));
76 public int hashCode () {
78 hash = 29 * hash + Objects.hashCode(this.getItemProduct().getProductId());
79 hash = 29 * hash + Objects.hashCode(this.getItemType());
84 public boolean isProductType () {
85 // Is the instance set?
86 return (this.getItemProduct() instanceof Product);