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.LinkedHashMap;
21 import java.util.LinkedList;
23 import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
26 * A general basket class. This class does not store any properties, it only
27 * contains logic for handling the items (T).
29 * @author Roland Haeder<roland@mxchange.org>
30 * @param <T> Any instance that implements AddableBasketItem
32 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 boolean isEmpty () {
68 // Deligate call to frontend
69 return this.deque.isEmpty();
73 public Map<Long, T> getAll () {
75 Map<Long, T> map = new LinkedHashMap<>(this.deque.size());
77 // Iterate over full item list
78 for (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
86 map.put(item.getItemId(), item);
96 return this.deque.getLast();
100 public int getLastNumRows () {
101 // Is the list empty?
102 assert this.isEmpty() : "deque is empty"; //NOI18N
105 return (this.deque.size() - 1);
109 public boolean isAdded (final T item) {
110 // item must not be null
113 throw new NullPointerException("item is null"); //NOI18N
117 Map<Long, T> map = this.getAll();
119 // Default is not found
120 boolean isAdded = false;
122 for (Map.Entry<Long, T> entrySet : map.entrySet()) {
124 T i = entrySet.getValue();
127 if (i.equals(item)) {