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