2 * Copyright (C) 2016 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> {
36 private static final long serialVersionUID = 782_396_762_230_845_717L;
41 private final Deque<T> deque;
44 * Protected constructor with session instance
46 protected BaseBasket () {
48 this.deque = new LinkedList<>();
52 public void addItem (final T item) throws BasketItemAlreadyAddedException {
53 // item must not be null
56 throw new NullPointerException("item is null"); //NOI18N
57 } else if (this.isAdded(item)) {
59 throw new BasketItemAlreadyAddedException(item); //NOI18N
67 public void clear () {
73 public List<T> getAll () {
75 List<T> list = new LinkedList<>();
77 // Iterate over full item list
78 for (final T item : this.deque) {
79 // item should not be null
82 throw new NullPointerException("item is null"); //NOI18N
85 // Add to map, use the item's id as key
96 return this.deque.getLast();
100 public boolean isAdded (final T item) {
101 // item must not be null
104 throw new NullPointerException("item is null"); //NOI18N
108 List<T> list = this.getAll();
110 // Default is not found
111 boolean isAdded = false;
114 for (final T i : list) {
116 if (i.equals(item)) {
127 public boolean isEmpty () {
128 // Deligate call to frontend
129 return this.deque.isEmpty();
134 // Is the list empty?
135 assert this.isEmpty() : "deque is empty"; //NOI18N
138 return this.deque.size();