2 * Copyright (C) 2015 Roland Haeder
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;
19 import java.util.Deque;
20 import java.util.LinkedList;
21 import java.util.List;
22 import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
25 * A general basket class. This class does not store any properties, it only
26 * contains logic for handling the items (T).
28 * @author Roland Haeder<roland@mxchange.org>
29 * @param <T> Any instance that implements AddableBasketItem
31 public abstract class BaseBasket<T extends AddableBasketItem> implements Basket<T> {
35 private static final long serialVersionUID = 782_396_762_230_845_717L;
40 private final Deque<T> deque;
43 * Protected constructor with session instance
45 protected BaseBasket () {
47 this.deque = new LinkedList<>();
51 public void addItem (final T item) throws BasketItemAlreadyAddedException {
52 // item must not be null
55 throw new NullPointerException("item is null"); //NOI18N
56 } else if (this.isAdded(item)) {
58 throw new BasketItemAlreadyAddedException(item); //NOI18N
66 public boolean isEmpty () {
67 // Deligate call to frontend
68 return this.deque.isEmpty();
72 public List<T> getAll () {
74 List<T> list = new LinkedList<>();
76 // Iterate over full item list
77 for (final T item : this.deque) {
78 // item should not be null
81 throw new NullPointerException("item is null"); //NOI18N
84 // Add to map, use the item's id as key
95 return this.deque.getLast();
99 public int getLastNumRows () {
100 // Is the list empty?
101 assert this.isEmpty() : "deque is empty"; //NOI18N
104 return (this.deque.size() - 1);
108 public boolean isAdded (final T item) {
109 // item must not be null
112 throw new NullPointerException("item is null"); //NOI18N
116 List<T> list = this.getAll();
118 // Default is not found
119 boolean isAdded = false;
122 for (final T i : list) {
124 if (i.equals(item)) {
135 public void clear () {