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