]> git.mxchange.org Git - jproduct-core.git/blob - src/org/mxchange/jshopcore/model/basket/BaseBasket.java
updated jar + isAdded() cannot call contains() because the item instance is always...
[jproduct-core.git] / src / org / mxchange / jshopcore / model / basket / BaseBasket.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.basket;
18
19 import java.util.Deque;
20 import java.util.LinkedHashMap;
21 import java.util.LinkedList;
22 import java.util.Map;
23 import java.util.Objects;
24 import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
25
26 /**
27  * A general basket class. This class does not store any properties, it only
28  * contains logic for handling the items (T).
29  *
30  * @author Roland Haeder<roland@mxchange.org>
31  * @param <T> Any instance that implements AddableBasketItem
32  */
33 public abstract class BaseBasket<T extends AddableBasketItem> implements Basket<T> {
34         /**
35          * Serial number
36          */
37         private static final long serialVersionUID = 782_396_762_230_845_717L;
38
39         /**
40          * Ordered item list
41          */
42         private final Deque<T> deque;
43
44         /**
45          * Protected constructor with session instance
46          */
47         protected BaseBasket () {
48                 // Init queue
49                 this.deque = new LinkedList<>();
50         }
51
52         @Override
53         public void addItem (final T item) throws BasketItemAlreadyAddedException {
54                 // item must not be null
55                 if (null == item) {
56                         // Then abort here
57                         throw new NullPointerException("item is null"); //NOI18N
58                 } else if (this.isAdded(item)) {
59                         // Already been added
60                         throw new BasketItemAlreadyAddedException(item); //NOI18N
61                 }
62
63                 // Add it here
64                 this.deque.add(item);
65         }
66
67         @Override
68         public boolean isEmpty () {
69                 // Deligate call to frontend
70                 return this.deque.isEmpty();
71         }
72
73         @Override
74         public Map<Long, T> getAll () {
75                 // Init map
76                 Map<Long, T> map = new LinkedHashMap<>(this.deque.size());
77
78                 // Iterate over full item list
79                 for (T item : this.deque) {
80                         // item should not be null
81                         if (null == item) {
82                                 // Abort here
83                                 throw new NullPointerException("item is null"); //NOI18N
84                         }
85
86                         // Add to map, use the item's id as key
87                         map.put(item.getItemId(), item);
88                 }
89
90                 // Return it
91                 return map;
92         }
93
94         @Override
95         public T getLast () {
96                 // Deligate to list
97                 return this.deque.getLast();
98         }
99
100         @Override
101         public int getLastNumRows () {
102                 // Is the list empty?
103                 assert this.isEmpty() : "deque is empty"; //NOI18N
104
105                 // It is size-1
106                 return (this.deque.size() - 1);
107         }
108
109         @Override
110         public boolean isAdded (final T item) {
111                 // item must not be null
112                 if (null == item) {
113                         // Then abort here
114                         throw new NullPointerException("item is null"); //NOI18N
115                 }
116
117                 // Get all items
118                 Map<Long, T> map = this.getAll();
119
120                 // Default is not found
121                 boolean isAdded = false;
122
123                 for (Map.Entry<Long, T> entrySet : map.entrySet()) {
124                         // Get item id
125                         Long itemId = entrySet.getKey();
126
127                         // Compare id
128                         if (Objects.equals(itemId, item.getItemId())) {
129                                 // Okay, found it
130                                 isAdded = true;
131                                 break;
132                         }
133                 }
134                 // Return it
135                 return isAdded;
136         }
137 }