X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Forg%2Fmxchange%2Fjshopcore%2Fmodel%2Fbasket%2FBaseBasket.java;h=4d8e7c4480073f3463e808a0f1dd9ab2c9e8b053;hb=372d8438a9727498b709e318404fc1e6d1faa343;hp=b019f8300a7086ce1de34a24c7a016b527b4c598;hpb=6b9f17d0a9ac8377e24651fb9f48e2d3ce7de94e;p=jshop-core.git diff --git a/src/org/mxchange/jshopcore/model/basket/BaseBasket.java b/src/org/mxchange/jshopcore/model/basket/BaseBasket.java index b019f83..4d8e7c4 100644 --- a/src/org/mxchange/jshopcore/model/basket/BaseBasket.java +++ b/src/org/mxchange/jshopcore/model/basket/BaseBasket.java @@ -16,129 +16,125 @@ */ package org.mxchange.jshopcore.model.basket; -import java.io.IOException; -import java.io.Serializable; -import java.lang.reflect.InvocationTargetException; -import java.sql.SQLException; -import java.text.MessageFormat; -import java.util.Map; -import org.mxchange.jcore.BaseFrameworkSystem; -import org.mxchange.jshopcore.model.item.AddableBasketItem; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; +import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException; /** - * A general basket class + * A general basket class. This class does not store any properties, it only + * contains logic for handling the items (T). * - * @author Roland Haeder + * @author Roland Haeder * @param Any instance that implements AddableBasketItem */ -public abstract class BaseBasket extends BaseFrameworkSystem implements Basket, Serializable { +public abstract class BaseBasket implements Basket { + /** * Serial number */ - private static final long serialVersionUID = 784_396_762_230_845_717L; + private static final long serialVersionUID = 782_396_762_230_845_717L; /** - * Protected constructor with session instance - * - * @throws java.sql.SQLException If an SQL error occurs + * Ordered item list */ - protected BaseBasket () throws SQLException { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - } + private final Deque deque; - @Override - public void init () throws SQLException { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - - // Is the bundle initialized? - if (!BaseFrameworkSystem.isBundledInitialized()) { - // Temporary initialize default bundle - // TODO The enum Gender uses this - this.initBundle(); - } + /** + * Protected constructor with session instance + */ + protected BaseBasket () { + // Init queue + this.deque = new LinkedList<>(); } @Override - @SuppressWarnings ("unchecked") - public void addItem (final T item) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - // Trace call - this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N - + public void addItem (final T item) throws BasketItemAlreadyAddedException { // item must not be null if (null == item) { // Then abort here throw new NullPointerException("item is null"); //NOI18N } else if (this.isAdded(item)) { // Already been added - throw new IllegalArgumentException("item has already been added. Did you miss to call isAdded()?"); //NOI18N + throw new BasketItemAlreadyAddedException(item); //NOI18N } - // Add item to database - // TODO: ((BasketFrontend) this.getFrontend()).addItem(item, this.getSessionId()); - - // Trace call - this.getLogger().trace("EXIT!"); //NOI18N + // Add it here + this.deque.add(item); } @Override - public boolean isEmpty () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - // Deligate call to frontend - // TODO: return ((BasketFrontend) this.getFrontend()).isEmpty(); - throw new UnsupportedOperationException("Not yet implmeneted."); + public void clear () { + // Deligate to deque + this.deque.clear(); } @Override - @SuppressWarnings("unchecked") - public Map getAll () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - // Trace message - this.getLogger().trace("CALLED!"); //NOI18N - + public List getAll () { // Init map - // TODO: Map map = ((BasketFrontend) this.getFrontend()).getAll(); - Map map = null; - - // Trace message - this.getLogger().trace("map=" + map); //NOI18N + List list = new LinkedList<>(); + + // Iterate over full item list + for (final T item : this.deque) { + // item should not be null + if (null == item) { + // Abort here + throw new NullPointerException("item is null"); //NOI18N + } + + // Add to map, use the item's id as key + list.add(item); + } // Return it - return map; + return list; } @Override - public AddableBasketItem getLast () throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - // Deligate to frontend - // TODO: return ((BasketFrontend) this.getFrontend()).getLast(); - throw new UnsupportedOperationException("Not yet implmeneted."); + public T getLast () { + // Deligate to list + return this.deque.getLast(); } @Override public int getLastNumRows () { - // Deligate to frontend - // TODO: return this.getFrontend().getLastNumRows(); - throw new UnsupportedOperationException("Not yet implmeneted."); + // Is the list empty? + assert this.isEmpty() : "deque is empty"; //NOI18N + + // It is size-1 + return (this.deque.size() - 1); } @Override - public boolean isAdded (final T item) throws IOException, SQLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { - // Trace call - this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N - + public boolean isAdded (final T item) { // item must not be null if (null == item) { // Then abort here throw new NullPointerException("item is null"); //NOI18N } - // Call map's method - // TODO: boolean isAdded = ((BasketFrontend) this.getFrontend()).isAdded(item, this.getSessionId()); - boolean isAdded = true; + // Get all items + List list = this.getAll(); - // Trace message - this.getLogger().trace(MessageFormat.format("isAdded={0} - EXIT!", isAdded)); //NOI18N + // Default is not found + boolean isAdded = false; + // Loop through list + for (final T i : list) { + // Compare id + if (i.equals(item)) { + // Okay, found it + isAdded = true; + break; + } + } // Return it return isAdded; } + + @Override + public boolean isEmpty () { + // Deligate call to frontend + return this.deque.isEmpty(); + } }